forked from ericfreese/zsh-prioritize-cwd-history
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathzsh-prioritize-cwd-history.zsh
More file actions
157 lines (126 loc) · 5.46 KB
/
zsh-prioritize-cwd-history.zsh
File metadata and controls
157 lines (126 loc) · 5.46 KB
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
# Scope history to the current working directory.
# Idea from https://github.com/jimhester/per-directory-history
# https://github.com/ericfreese/zsh-cwd-history
# v0.2.1
# Copyright (c) 2016 Eric Freese
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#--------------------------------------------------------------------#
# Required History Options #
#--------------------------------------------------------------------#
# We use timestamps as unique keys for commands in the history file
setopt EXTENDED_HISTORY
# We reload global history from $HISTFILE on directory change
# Have to write to the file incrementally to avoid losing history on
# directory change.
setopt INC_APPEND_HISTORY
#--------------------------------------------------------------------#
# Global Config Variables #
#--------------------------------------------------------------------#
ZSH_PRIORITIZE_CWD_HISTORY_DIR="$HOME/.zsh_prioritize_cwd_history"
#--------------------------------------------------------------------#
# Histrefs files store timestamp references to entries in HISTFILE #
#--------------------------------------------------------------------#
# Use an appropriate md5 command (md5 is BSD, md5sum is GNU),
# whichever is actually available
zmodload zsh/parameter
_zsh_prioritize_cwd_history_md5() {
if (( $+commands[md5] )); then
md5 -q
elif (( $+commands[md5sum] )); then
md5sum | cut -d ' ' -f 1
else
# TODO: Fall back on something or error
fi
}
# Prints to STDOUT the name of the histrefs file to use for current
# working directory
_zsh_prioritize_cwd_history_histrefs_for_cwd() {
local md5=$(echo "${PWD:A}" | _zsh_prioritize_cwd_history_md5)
echo "$ZSH_PRIORITIZE_CWD_HISTORY_DIR/.histrefs-$md5"
}
# Prints to STDOUT up to 1000 of the last history entries that were
# executed in the current working directory
_zsh_prioritize_cwd_history_cwd_hist_entries() {
local histrefs=$(_zsh_prioritize_cwd_history_histrefs_for_cwd)
[ -s "$histrefs" ] || return
# Build a grep pattern that will pick out from HISTFILE all
# history entries (that were saved with EXTENDED_HISTORY)
# referenced by the histrefs file
# Get last 1000 histrefs
# Prepend "^: " to timestamps
# Join lines with a pipe delimiter
# Format into grep `OR` with backslash pipe
local pattern="$(
tail -1000 "$histrefs" |
sed -e 's/^/^: /' |
paste -s -d '|' - |
sed -e 's/[|]/\\|/g'
)"
grep "$pattern" "$HISTFILE"
}
# Reads history entries executed in the current working directory into
# the history list
_zsh_prioritize_cwd_history_load_cwd_history() {
local histrefs=$(_zsh_prioritize_cwd_history_histrefs_for_cwd)
[ -s "$histrefs" ] || return
# TODO: Validate format of histrefs file
# [ (valid_histrefs) ] || return
# Create a tmp file for use with `fc -R`
local template="$ZSH_PRIORITIZE_CWD_HISTORY_DIR/.tmphistXXX"
local tmp_histfile=$(mktemp "$template")
# Copy history entries executed in this directory to tmp file
_zsh_prioritize_cwd_history_cwd_hist_entries > "$tmp_histfile"
# Read the history entries from the tmp file
fc -R "$tmp_histfile"
# All done, remove the tmp file
rm "$tmp_histfile"
}
#--------------------------------------------------------------------#
# Zsh Hooks #
#--------------------------------------------------------------------#
# Initialize on first prompt, then remove the precmd hook
_zsh_prioritize_cwd_history_initialize() {
# Remove the precmd hook so this is only called once
add-zsh-hook -d precmd _zsh_prioritize_cwd_history_initialize
# Simulate initial chpwd
_zsh_prioritize_cwd_history_cwd_changed
# Add a hook to save histrefs
add-zsh-hook zshaddhistory _zsh_prioritize_cwd_history_addhistory
# Add a hook to reload history on chpwd
add-zsh-hook chpwd _zsh_prioritize_cwd_history_cwd_changed
}
# Read current working directory history when changing dirs
_zsh_prioritize_cwd_history_cwd_changed() {
# Reset history list from $HISTFILE
fc -P; fc -p "$HISTFILE"
# Read in history for this dir after global history
_zsh_prioritize_cwd_history_load_cwd_history
}
# Save a histref every time we save something to history
_zsh_prioritize_cwd_history_addhistory() {
local histrefs=$(_zsh_prioritize_cwd_history_histrefs_for_cwd)
[ -d "$ZSH_PRIORITIZE_CWD_HISTORY_DIR" ] || mkdir -p "$ZSH_PRIORITIZE_CWD_HISTORY_DIR"
echo "$(date +%s)" >> "$histrefs"
}
autoload -Uz add-zsh-hook
add-zsh-hook precmd _zsh_prioritize_cwd_history_initialize