Typos.
[selector.git] / bash-selector.sh
1 #!/bin/bash
2
3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify  #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation.                         #
7 #                                                                       #
8 # This program is distributed in the hope that it will be useful, but   #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of            #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
11 # General Public License for more details.                              #
12 #                                                                       #
13 # You should have received a copy of the GNU General Public License     #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.  #
15 #                                                                       #
16 # Written by and Copyright (C) Francois Fleuret                         #
17 # Contact <francois@fleuret.org> for comments & bug reports             #
18 #########################################################################
19
20 # This script installs two key-bindings:
21 #
22 # Alt-r for selector-based command history
23 #
24 # Alt-c for selector-based directoy history
25 #
26 # Note that you have to call it with "source bash-selector.sh"
27 # otherwise the key-bindings will not be effective in your current
28 # bash
29
30 if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
31     echo "This script must be called with 'source $(basename $0)'" >&2
32     exit 1
33 fi
34
35 ######################################################################
36 # Selector-based command history
37 ######################################################################
38
39 function selector-history () {
40     selector --bash -u -c 7,4,0,3 -q <(history)
41 }
42
43 ######################################################################
44 # Selector-based directory history
45 ######################################################################
46
47 # The file where we will keep track of the directories
48
49 export SELECTOR_CD_HISTORY
50
51 [[ "${SELECTOR_CD_HISTORY}" ]] || SELECTOR_CD_HISTORY=${HOME}/.selector-cd-history
52
53 # The function to use in place of the standard "cd"
54
55 function selector-cd () {
56     if [[ -z "$1" ]]; then
57         cd
58     else
59         cd "$@"
60     fi
61
62     if [[ -f ${SELECTOR_CD_HISTORY} ]]; then
63         TMP=$(mktemp /tmp/selector-cd.XXXXXX)
64         tail -999 < ${SELECTOR_CD_HISTORY} > ${TMP}
65         cat ${TMP} > ${SELECTOR_CD_HISTORY}
66         rm -f ${TMP}
67     fi
68
69     echo $PWD | sed -e "s!^${HOME}!~!" >> ${SELECTOR_CD_HISTORY}
70 }
71
72 function selector-cd-search () {
73     PATH_TEMP=$(mktemp /tmp/selector-cd-path.XXXXXX)
74     selector -u -t "cd" -l 1000 -d -i -c 7,2,0,3 -o ${PATH_TEMP} -q ${SELECTOR_CD_HISTORY}
75     NEW_PATH="$(cat ${PATH_TEMP} | sed -e 's!~!'${HOME}'!')"
76     if [[ -s "${NEW_PATH}" ]]; then
77         selector-cd "$(cat ${PATH_TEMP} | sed -e 's!^~!'${HOME}'!')"
78     fi
79     \rm ${PATH_TEMP}
80 }
81
82 alias cd=selector-cd
83
84 ######################################################################
85 # The key-bindings themselves
86 ######################################################################
87
88     # M-t appends the selected history line and the end of the current
89     # one bind '"\C-[t":"\C-a\C-kselector-history\C-m\C-a\C-y\C-e"'
90
91 if [[ "$1" ]]; then
92
93     while [[ "$1" ]]; do
94
95         case "$1" in
96
97             --hist)
98
99                 # M-r puts the selected history line in place of the current one
100
101                 bind '"\C-[r":"\C-a\C-kselector-history\C-m"'
102
103                 ;;
104
105             --cd)
106
107                 # M-c provides a dynamic list of directories to cd into
108
109                 bind '"\C-[c":"\C-a\C-kselector-cd-search\C-m"'
110                 ;;
111
112
113             *)
114                 echo "Unknown argument $1" >&2
115                 ;;
116         esac
117
118         shift
119
120     done
121
122 else
123
124     echo "source bash-selector.sh <--hist|--cd> [...]"
125     echo
126     echo "Defines bash functions, and installs key-bindings and aliases to use selector"
127     echo "for history search with M-r and/or intelligent cd history with M-c."
128
129 fi