per-directory-history.plugin.zsh 5.6 KB

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