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