root/dotfiles/bash/kana/dot.bash.d/cdhist.sh

Revision 132, 3.9 kB (checked in by kana, 16 months ago)

dotfiles/bash/kana-bash.d, dotfiles/bash/kana-bash_profile, dotfiles/bash/kana-bashrc:
* Import from my repository.
dotfiles/screen/kana-screenrc:
* Import from my repository.
dotfiles/vim/kana-vim, dotfiles/vim/kana-vimrc:
* Import from my repository.

Line 
1###  cdhist.sh
2###
3###  Copyright (c) 2001 Yusuke Shinyama <yusuke at cs . nyu . edu>
4###
5###  Permission to use, copy, modify, distribute this software and
6###  its documentation for any purpose is hereby granted, provided
7###  that existing copyright notices are retained in all copies and
8###  that this notice is included verbatim in any distributions.
9###  This software is provided ``AS IS'' without any express or implied
10###  warranty.
11###
12
13###  WARNING: THIS SCRIPT IS FOR GNU BASH ONLY!
14
15###  What is this?
16###
17###  Cdhist adds 'web-browser like history' to your bash shell.
18###  Every time you change the current directory it records the directory
19###  you can go back by simply typing a short command such as '-' or '+',
20###  just like clicking web-browsers's 'back' button.
21###  It's more convenient than using directory stacks when
22###  you walk around two or three directories.
23###
24
25###  Usage
26###
27###  Just call this file from your .bashrc script.
28###  The following commands are added.
29###
30###  cd [pathname]
31###     Go to the given directory, or your home directory if
32###     pathname is omitted. This overrides the original command.
33###     You can use it by typing '\cd'.
34###
35###  + [n]
36###     'Forward' button. Go to the n'th posterior directory in the history.
37###     Go to the next directory if the number is omitted.
38###
39###  - [n]
40###     'Back' button. Go to the n'th prior directory in the history.
41###     Go to the previous directory if the number is omitted.
42###
43###  = [n]
44###     Show histories with directory numbers.
45###
46###     A directory number shows the index to the current directory
47###     in the history. The current directory always has directory number 0.
48###     For prior directories, a negative number is given.
49###     For posterior directories, a positive number is given.
50###
51###  cdhist_reset
52###     Clear the cd history.
53###
54
55###  Example
56###
57###     /home/yusuke:$ . cdhist.sh
58###     /home/yusuke:$ cd /tmp
59###     /tmp:$ cd /usr/bin
60###     /usr/bin:$ cd /etc
61###     /etc:$ -
62###     /usr/bin:$ -
63###     /tmp:$ +
64###     /usr/bin:$ =
65###     -2 ~
66###     -1 /tmp
67###      0:/usr/bin
68###      1 /etc
69###     /usr/bin:$ - 2
70###     /home/yusuke:$
71###
72
73
74CDHIST_CDQMAX=10
75declare -a CDHIST_CDQ
76
77function cdhist_reset {
78  CDHIST_CDQ=("$PWD")
79}
80
81function cdhist_disp {
82  echo "$*" | sed "s $HOME ~ g"
83}
84
85function cdhist_add {
86  CDHIST_CDQ=("$1" "${CDHIST_CDQ[@]}")
87}
88
89function cdhist_del {
90  local i=${1-0}
91  if [ ${#CDHIST_CDQ[@]} -le 1 ]; then return; fi
92  for ((; i<${#CDHIST_CDQ[@]}-1; i++)); do
93    CDHIST_CDQ[$i]="${CDHIST_CDQ[$((i+1))]}"
94  done
95  unset CDHIST_CDQ[$i]
96}
97
98function cdhist_rot {
99  local i q
100  for ((i=0; i<$1; i++)); do
101    q[$i]="${CDHIST_CDQ[$(((i+$1+$2)%$1))]}"
102  done
103  for ((i=0; i<$1; i++)); do
104    CDHIST_CDQ[$i]="${q[$i]}"
105  done
106}
107
108function cdhist_cd {
109  local i f=0
110  builtin cd "$@" || return 1
111  for ((i=0; i<${#CDHIST_CDQ[@]}; i++)); do
112    if [ "${CDHIST_CDQ[$i]}" = "$PWD" ]; then f=1; break; fi
113  done
114  if [ $f -eq 1 ]; then
115    cdhist_rot $((i+1)) -1
116  elif [ ${#CDHIST_CDQ[@]} -lt $CDHIST_CDQMAX ]; then
117    cdhist_add "$PWD"
118  else
119    cdhist_rot ${#CDHIST_CDQ[@]} -1
120    CDHIST_CDQ[0]="$PWD"
121  fi
122}
123
124function cdhist_history {
125  local i d
126  if [ $# -eq 0 ]; then
127    for ((i=${#CDHIST_CDQ[@]}-1; 0<=i; i--)); do
128      cdhist_disp " $i ${CDHIST_CDQ[$i]}"
129    done
130  elif [ "$1" -lt ${#CDHIST_CDQ[@]} ]; then
131    d=${CDHIST_CDQ[$1]}
132    if builtin cd "$d"; then
133      cdhist_rot $(($1+1)) -1
134    else
135      cdhist_del $1
136    fi
137    cdhist_disp "${CDHIST_CDQ[@]}"
138  fi
139}
140
141function cdhist_forward {
142  cdhist_rot ${#CDHIST_CDQ[@]} -${1-1}
143  if ! builtin cd "${CDHIST_CDQ[0]}"; then
144    cdhist_del 0
145  fi
146  cdhist_disp "${CDHIST_CDQ[@]}"
147}
148
149function cdhist_back {
150  cdhist_rot ${#CDHIST_CDQ[@]} ${1-1}
151  if ! builtin cd "${CDHIST_CDQ[0]}"; then
152    cdhist_del 0
153  fi
154  cdhist_disp "${CDHIST_CDQ[@]}"
155}
156
157
158if [ ${#CDHIST_CDQ[@]} = 0 ]; then cdhist_reset; fi
159
160
161###  Aliases
162###
163
164function cd { cdhist_cd "$@"; }
165function + { cdhist_forward "$@"; }
166function - { cdhist_back "$@"; }
167function = { cdhist_history "$@"; }
Note: See TracBrowser for help on using the browser.