summaryrefslogtreecommitdiffstats
path: root/config/sh/cdhist.sh
blob: d67d2e93602f10eabb46bb0d1a266ffa4fb607de (plain) (blame)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
###  cdhist.sh
###
###  Copyright (c) 2001 Yusuke Shinyama <yusuke at cs . nyu . edu>
###
###  Permission to use, copy, modify, distribute this software and
###  its documentation for any purpose is hereby granted, provided
###  that existing copyright notices are retained in all copies and
###  that this notice is included verbatim in any distributions. 
###  This software is provided ``AS IS'' without any express or implied
###  warranty.
###

###  WARNING: THIS SCRIPT IS FOR GNU BASH ONLY!

###  What is this?
###
###  Cdhist adds 'web-browser like history' to your bash shell.
###  Every time you change the current directory it records the directory
###  you can go back by simply typing a short command such as '-' or '+',
###  just like clicking web-browsers's 'back' button.
###  It's more convenient than using directory stacks when
###  you walk around two or three directories.
###

###  Usage
###
###  Just call this file from your .bashrc script.
###  The following commands are added.
###
###  cd [pathname]
###	Go to the given directory, or your home directory if 
###	pathname is omitted. This overrides the original command.
###	You can use it by typing '\cd'.
###
###  + [n]
###	'Forward' button. Go to the n'th posterior directory in the history.
###	Go to the next directory if the number is omitted.
###
###  - [n]
###	'Back' button. Go to the n'th prior directory in the history.
###	Go to the previous directory if the number is omitted.
###
###  = [n]
###	Show histories with directory numbers.
###
###	A directory number shows the index to the current directory 
###	in the history. The current directory always has directory number 0.
###	For prior directories, a negative number is given.
###	For posterior directories, a positive number is given.
###
###  cdhist_reset
###	Clear the cd history.
###

###  Example
###
###	/home/yusuke:$ . cdhist.sh
###	/home/yusuke:$ cd /tmp
###	/tmp:$ cd /usr/bin
###	/usr/bin:$ cd /etc
###	/etc:$ -
###	/usr/bin:$ -
###	/tmp:$ +
###	/usr/bin:$ =
###	-2 ~
###	-1 /tmp
###	 0:/usr/bin
###	 1 /etc
###	/usr/bin:$ - 2
###     /home/yusuke:$
###


CDHIST_CDQMAX=10
declare -a CDHIST_CDQ

function cdhist_reset {
  CDHIST_CDQ=("$PWD")
}

function cdhist_disp {
  echo "$*" | sed "s $HOME ~ g"
}

function cdhist_add {
  CDHIST_CDQ=("$1" "${CDHIST_CDQ[@]}")
}

function cdhist_del {
  [[ -n $ZSH_VERSION ]] && setopt localoptions && setopt ksharrays
  local i=${1-0}
  if [ ${#CDHIST_CDQ[@]} -le 1 ]; then return; fi
  for ((; i<${#CDHIST_CDQ[@]}-1; i++)); do
    CDHIST_CDQ[$i]="${CDHIST_CDQ[$((i+1))]}"
  done
  unset CDHIST_CDQ[$i]
}

function cdhist_rot {
  [[ -n $ZSH_VERSION ]] && setopt localoptions && setopt ksharrays
  local i q
  declare -a q
  for ((i=0; i<$1; i++)); do
    q[$i]="${CDHIST_CDQ[$(((i+$1+$2)%$1))]}"
  done
  for ((i=0; i<$1; i++)); do
    CDHIST_CDQ[$i]="${q[$i]}"
  done
}

function cdhist_cd {
  [[ -n $ZSH_VERSION ]] && setopt localoptions && setopt ksharrays
  local i f=0
  builtin cd "$@" || return 1
  for ((i=0; i<${#CDHIST_CDQ[@]}; i++)); do
    if [ "${CDHIST_CDQ[$i]}" = "$PWD" ]; then f=1; break; fi
  done
  if [ $f -eq 1 ]; then
    cdhist_rot $((i+1)) -1
  elif [ ${#CDHIST_CDQ[@]} -lt $CDHIST_CDQMAX ]; then 
    cdhist_add "$PWD"
  else
    cdhist_rot ${#CDHIST_CDQ[@]} -1
    CDHIST_CDQ[0]="$PWD"
  fi
}

function cdhist_history {
  [[ -n $ZSH_VERSION ]] && setopt localoptions && setopt ksharrays
  local i d
  if [ $# -eq 0 ]; then
    for ((i=${#CDHIST_CDQ[@]}-1; 0<=i; i--)); do
      cdhist_disp " $i ${CDHIST_CDQ[$i]}"
    done
  elif [ "$1" -lt ${#CDHIST_CDQ[@]} ]; then
    d=${CDHIST_CDQ[$1]}
    if builtin cd "$d"; then
      cdhist_rot $(($1+1)) -1
    else
      cdhist_del $1
    fi
    cdhist_disp "${CDHIST_CDQ[@]}"
  fi
}

function cdhist_forward {
  [[ -n $ZSH_VERSION ]] && setopt localoptions && setopt ksharrays
  cdhist_rot ${#CDHIST_CDQ[@]} -${1-1}
  if ! builtin cd "${CDHIST_CDQ[0]}"; then
    cdhist_del 0
  fi
  cdhist_disp "${CDHIST_CDQ[@]}"
}

function cdhist_back {
  [[ -n $ZSH_VERSION ]] && setopt localoptions && setopt ksharrays
  cdhist_rot ${#CDHIST_CDQ[@]} ${1-1}
  if ! builtin cd "${CDHIST_CDQ[0]}"; then
    cdhist_del 0
  fi
  cdhist_disp "${CDHIST_CDQ[@]}"
}


if [ ${#CDHIST_CDQ[@]} = 0 ]; then cdhist_reset; fi


###  Aliases
###

function cd { cdhist_cd "$@"; }
function + { cdhist_forward "$@"; }
function - { cdhist_back "$@"; }
function = { cdhist_history "$@"; }