summaryrefslogtreecommitdiff
path: root/dot-oh-my-zsh/custom/plugins/zsh-cwd-history/zsh-cwd-history.plugin.zsh
blob: d5d717f6478830c63e7a66d96d1b3000bccc7258 (plain)
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
# 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.

#--------------------------------------------------------------------#
# Global Config Variables                                            #
#--------------------------------------------------------------------#

ZSH_CWD_HISTORY_DIR="$HOME/.zsh_cwd_history"
ZSH_CWD_HISTORY_SHOW_TOGGLE_MSG=true
ZSH_CWD_HISTORY_SHOW_CHPWD_MSG=false

#--------------------------------------------------------------------#
# Histfile Helper Functions                                          #
#--------------------------------------------------------------------#

# Echo the name of the history file to use for current working dir
# Uses the md5 hash of the absolute path
_zsh_cwd_history_histfile_for_cwd() {
	echo "$ZSH_CWD_HISTORY_DIR/.hist-$(echo "${PWD:A}" | md5 -q)"
}

# Switch to a new history file, writing to the old one
_zsh_cwd_history_switch_histfile() {
	local new_histfile=$1

	# Write old histfile
	fc -P

	# Read new histfile
	fc -p "$new_histfile"
}

#--------------------------------------------------------------------#
# Display a Message                                                  #
#--------------------------------------------------------------------#

_zsh_cwd_history_message() {
	local message=$1

	if zle; then
		zle -Rc "" $message
	else
		echo $message
	fi
}

#--------------------------------------------------------------------#
# Toggle Widget                                                      #
#--------------------------------------------------------------------#

# Implementation for toggle widget
_zsh_cwd_history_toggle() {
	if [ "$HISTFILE" = "$_ZSH_CWD_HISTORY_ORIG_HISTFILE" ]; then
		# Switch to the cwd histfile
		_zsh_cwd_history_switch_histfile $(_zsh_cwd_history_histfile_for_cwd)

		# Add back the cwd hook
		_zsh_cwd_history_add_cwd_hook

		# Show message if necessary
		if $ZSH_CWD_HISTORY_SHOW_TOGGLE_MSG; then
			_zsh_cwd_history_message "Using cwd histfile: $HISTFILE"
		fi
	else
		# Switch to the original histfile
		_zsh_cwd_history_switch_histfile $_ZSH_CWD_HISTORY_ORIG_HISTFILE

		# Temporarily remove the cwd hook
		_zsh_cwd_history_remove_cwd_hook

		# Show message if necessary
		if $ZSH_CWD_HISTORY_SHOW_TOGGLE_MSG; then
			_zsh_cwd_history_message "Using original histfile: $HISTFILE"
		fi
	fi
}

# Create the toggle widget
zle -N cwd-history-toggle _zsh_cwd_history_toggle

#--------------------------------------------------------------------#
# Zsh Hooks                                                          #
#--------------------------------------------------------------------#

# Initialize on first prompt, then remove the precmd hook
_zsh_cwd_history_initialize() {
	# Simulate initial chpwd
	_zsh_cwd_history_cwd_changed

	# Add a hook to switch histfiles on chpwd
	_zsh_cwd_history_add_cwd_hook

	# Remove the precmd hook
	add-zsh-hook -d precmd _zsh_cwd_history_initialize
}

# Load history for current working directory when changing dirs
_zsh_cwd_history_cwd_changed() {
	local cwd_histfile=$(_zsh_cwd_history_histfile_for_cwd)

	# Save a reference to any original histfile from outside our dir
	if [ "${HISTFILE:A:h}" != "$ZSH_CWD_HISTORY_DIR" ]; then
		_ZSH_CWD_HISTORY_ORIG_HISTFILE="$HISTFILE"
	fi

	# Switch the histfile
	_zsh_cwd_history_switch_histfile $cwd_histfile

	# Show message if necessary
	if $ZSH_CWD_HISTORY_SHOW_CHPWD_MSG; then
		_zsh_cwd_history_message "Using cwd histfile: $HISTFILE"
	fi
}

# Add the chpwd hook
_zsh_cwd_history_add_cwd_hook() {
	add-zsh-hook chpwd _zsh_cwd_history_cwd_changed
}

# Remove the chpwd hook
_zsh_cwd_history_remove_cwd_hook() {
	add-zsh-hook -d chpwd _zsh_cwd_history_cwd_changed
}

autoload -Uz add-zsh-hook
add-zsh-hook precmd _zsh_cwd_history_initialize