baa187e4b903f39422a84b580e6e617ec3738e09

Author
Jim Hester <james.f.hester@gmail.com>
Committer
Jim Hester <james.f.hester@gmail.com>
Date

Message

Per directory history plugin

   This is a implementation of per directory history for zsh, some
   implementations of which exist in bash[1,2].  It also implements
   a per-directory-history-toggle-history function to change from using the
   directory history to using the global history.  In both cases the history is
   always saved to both the global history and the directory history, so the
   toggle state will not effect the saved histories.  Being able to switch
   between global and directory histories on the fly is a novel feature as far
   as I am aware.

   [1]: http://www.compbiome.com/2010/07/bash-per-directory-bash-history.html
   [2]: http://dieter.plaetinck.be/per_directory_bash

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..ce36db6af621c33d09a44bd63ab6c40c7ad400dd
  4--- /dev/null
  5+++ b/plugins/per-directory-history/per-directory-history.plugin.zsh
  6@@ -0,0 +1,149 @@
  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_global=$HISTFILE
 91+_per_directory_history_directory="$HISTORY_BASE${PWD:A}/history"
 92+
 93+function _per-directory-history-change-directory() {
 94+  _per_directory_history_directory="$HISTORY_BASE${PWD:A}/history"
 95+  mkdir -p ${_per_directory_history_directory:h}
 96+  if [[ $_per_directory_history_is_global == false ]]; then
 97+    #save to the global history
 98+    fc -AI $_per_directory_history_global
 99+    #save history to previous file
100+    local prev="$HISTORY_BASE${OLDPWD:A}/history"
101+    mkdir -p ${prev:h}
102+    fc -AI $prev
103+
104+    #discard previous directory's history
105+    local original_histsize=$HISTSIZE