523b26c6efb1a140aa786c63367d80832a6187fd

Author
Robby Russell <robby@planetargon.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

Merge pull request #1321 from jimhester/per-directory-history

Per directory history plugin

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/per-directory-history/per-directory-history.plugin.zsh b/plugins/per-directory-history/per-directory-history.plugin.zsh
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..22383b8b96cf7ff31738fe4ebf3d9a8f28223e13
  4--- /dev/null
  5+++ b/plugins/per-directory-history/per-directory-history.plugin.zsh
  6@@ -0,0 +1,148 @@
  7+#!/usr/bin/env zsh
  8+#
  9+# This is a implementation of per directory history for zsh, some 
 10+# implementations of which exist in bash[1,2].  It also implements 
 11+# a per-directory-history-toggle-history function to change from using the 
 12+# directory history to using the global history.  In both cases the history is 
 13+# always saved to both the global history and the directory history, so the 
 14+# toggle state will not effect the saved histories.  Being able to switch 
 15+# between global and directory histories on the fly is a novel feature as far 
 16+# as I am aware.
 17+#
 18+#-------------------------------------------------------------------------------
 19+# Configuration
 20+#-------------------------------------------------------------------------------
 21+#
 22+# HISTORY_BASE a global variable that defines the base directory in which the 
 23+# directory histories are stored
 24+#
 25+#-------------------------------------------------------------------------------
 26+# History
 27+#-------------------------------------------------------------------------------
 28+#
 29+# The idea/inspiration for a per directory history is from Stewart MacArthur[1] 
 30+# and Dieter[2], the implementation idea is from Bart Schaefer on the the zsh 
 31+# mailing list[3].  The implementation is by Jim Hester in September 2012.
 32+#
 33+# [1]: http://www.compbiome.com/2010/07/bash-per-directory-bash-history.html
 34+# [2]: http://dieter.plaetinck.be/per_directory_bash
 35+# [3]: http://www.zsh.org/mla/users/1997/msg00226.html
 36+#
 37+################################################################################
 38+#
 39+# Copyright (c) 2012 Jim Hester
 40+#
 41+# This software is provided 'as-is', without any express or implied warranty.  
 42+# In no event will the authors be held liable for any damages arising from the 
 43+# use of this software.
 44+#
 45+# Permission is granted to anyone to use this software for any purpose, 
 46+# including commercial applications, and to alter it and redistribute it 
 47+# freely, subject to the following restrictions:
 48+#
 49+# 1. The origin of this software must not be misrepresented; you must not claim 
 50+# that you wrote the original software. If you use this software in a product, 
 51+# an acknowledgment in the product documentation would be appreciated but is 
 52+# not required.
 53+#
 54+# 2. Altered source versions must be plainly marked as such, and must not be 
 55+# misrepresented as being the original software.
 56+#
 57+# 3. This notice may not be removed or altered from any source distribution..
 58+#
 59+################################################################################
 60+
 61+#-------------------------------------------------------------------------------
 62+# configuration, the base under which the directory histories are stored
 63+#-------------------------------------------------------------------------------
 64+
 65+[[ -z $HISTORY_BASE ]] && HISTORY_BASE="$HOME/.directory_history"
 66+
 67+#-------------------------------------------------------------------------------
 68+# toggle global/directory history used for searching - ctrl-G by default
 69+#-------------------------------------------------------------------------------
 70+
 71+function per-directory-history-toggle-history() {
 72+  if [[ $_per_directory_history_is_global == true ]]; then
 73+    _per-directory-history-set-directory-history
 74+    echo "using local history\n"
 75+  else
 76+    _per-directory-history-set-global-history
 77+    echo "using global history\n"
 78+  fi
 79+  zle reset-prompt
 80+}
 81+
 82+autoload per-directory-history-toggle-history
 83+zle -N per-directory-history-toggle-history
 84+bindkey '^G' per-directory-history-toggle-history
 85+
 86+#-------------------------------------------------------------------------------
 87+# implementation details
 88+#-------------------------------------------------------------------------------
 89+
 90+_per_directory_history_directory="$HISTORY_BASE${PWD:A}/history"
 91+
 92+function _per-directory-history-change-directory() {
 93+  _per_directory_history_directory="$HISTORY_BASE${PWD:A}/history"
 94+  mkdir -p ${_per_directory_history_directory:h}
 95+  if [[ $_per_directory_history_is_global == false ]]; then
 96+    #save to the global history
 97+    fc -AI $HISTFILE
 98+    #save history to previous file
 99+    local prev="$HISTORY_BASE${OLDPWD:A}/history"
100+    mkdir -p ${prev:h}
101+    fc -AI $prev
102+
103+    #discard previous directory's history
104+    local original_histsize=$HISTSIZE
105+    HISTSIZE=0