Cosmetics
[scripts.git] / bashrc
1 # -*-Shell-script-*-
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 # The site-specific and confidential settings are in another file
21
22 PRIVATE_BASHRC="${HOME}/private/bashrc.perso"
23
24 # If the MANPATH is not set, set it
25
26 [ "${MANPATH}" ] || MANPATH=$(manpath)
27
28 # If the private bashrc exists, execute it
29
30 [ -f "${PRIVATE_BASHRC}" ] && source "${PRIVATE_BASHRC}"
31
32 # !!! THIS HAS TO BE HERE EVEN IN THE NON-INTERACTIVE PART OR YOU WILL
33 # LOSE YOU PREVIOUS HISTORY !!!
34
35 export HISTFILESIZE=20000
36 export HISTSIZE=${HISTFILESIZE}
37
38 export HISTIGNORE="${HISTIGNORE}:&:[ ]*"
39
40 # I want to save the command time, but I do not want to see it in
41 # history
42
43 export HISTTIMEFORMAT=""
44
45 shopt -s histappend
46
47 # I realized that most of my settings are meaningful only in
48 # interactive mode. This should maybe be done more properly through
49 # using different .bash_profile and .bash_login
50
51 [ ${TERM} == "dumb" ] || [ ! -t 0 ] && return
52
53 # Remove the annoying beeps in console
54
55 setterm -blength 0
56
57 ######################################################################
58 ## The interactive part
59
60 export VT_RESET=$'\e[0m'
61 export VT_BOLD=$'\e[1m'
62 export VT_UNDERLINE=$'\e[4m'
63 export VT_BLINK=$'\e[5m'
64
65 export VT_SET_TITLE=$'\e]0;'
66 export VT_END_TITLE=$'\007'
67
68 export VT_BLACK_FG=$'\e[30m'
69 export VT_RED_FG=$'\e[31m'
70 export VT_GREEN_FG=$'\e[32m'
71 export VT_YELLOW_FG=$'\e[33m'
72 export VT_BLUE_FG=$'\e[34m'
73 export VT_MAGENTA_FG=$'\e[35m'
74 export VT_CYAN_FG=$'\e[36m'
75 export VT_WHITE_FG=$'\e[37m'
76
77 export VT_BLACK_BG=$'\e[40m'
78 export VT_RED_BG=$'\e[41m'
79 export VT_GREEN_BG=$'\e[42m'
80 export VT_YELLOW_BG=$'\e[43m'
81 export VT_BLUE_BG=$'\e[44m'
82 export VT_MAGENTA_BG=$'\e[45m'
83 export VT_CYAN_BG=$'\e[46m'
84 export VT_WHITE_BG=$'\e[47m'
85
86 # Colorize man pages!
87
88 export LESS_TERMCAP_us=${VT_GREEN_FG}
89 export LESS_TERMCAP_ue=${VT_RESET}
90 export LESS_TERMCAP_md=${VT_BLUE_FG}${VT_BOLD}
91 export LESS_TERMCAP_me=${VT_RESET}
92
93 # export LESS_TERMCAP_md=$'\e[1;34;40m'
94
95 # This prevents ^S from freezing the shell
96
97 # stty -ixon
98
99 ulimit -c unlimited
100
101 alias ..='cd ..'
102 alias -- -='cd -'
103 alias rm='rm -i'
104 alias mv='mv -i'
105 # alias chmod='chmod -v'
106 alias chmod='chmod -c'
107 alias cp='cp -i -v'
108 alias rd=rmdir
109 alias md='mkdir -pv'
110 alias ps='ps uxaf'
111 alias df='df -hT'
112 # alias df='df -hT --sync'
113 alias grep='grep -i -E --color=auto'
114 alias find='ionice -c3 find'
115 alias pd=pushd
116 alias val='valgrind --leak-check=full --show-reachable=yes --db-attach=yes '
117
118 alias s='screen -d -R -U && clear'
119
120 function nh () {
121     export HISTFILE=/dev/null
122     unalias cd
123 }
124
125 function ding () {
126     if [ $1 ] && which winshepherd.sh; then
127         play -q ~/local/sounds/deskbell.wav &
128         unset E_APP_WINDOW
129         winshepherd.sh message green "$1"
130     else
131         play -q ~/local/sounds/deskbell.wav
132     fi
133 }
134
135 alias impressive="impressive -s -D 1000 -t Crossfade -T 100"
136
137 if [ -e "${HOME}/.dircolors" ]; then
138     eval $(dircolors "${HOME}/.dircolors")
139     alias ls='ls -p --group-directories-first --color'
140     alias lt='ls -p --color -gohtr --time-style="+%Y %b %d %H:%M"'
141     alias ll='ls -p --color -goh --time-style="+%Y %b %d %H:%M"'
142     alias lll='ls -p --color -lth'
143     alias l='ls -p --color -I "*~" -I "*.o"'
144     alias less='less -R'
145 else
146     alias ls='ls -p --group-directories-first'
147     alias lt='ls -p -gohtr --time-style="+%Y %b %d %H:%M"'
148     alias ll='ls -p -goh --time-style="+%Y %b %d %H:%M"'
149     alias lll='ls -p -lth'
150     alias l='ls -p -I "*~" -I "*.o"'
151 fi
152
153 export EDITOR=emacsclient
154 export GIT_EDITOR=${EDITOR}
155
156 ######################################################################
157 # Ignored extensions when completing
158
159 # export FIGNORE="CVS"
160
161 function latexdiff () {
162     wdiff -n \
163         -w $'\033[30;41m' -x $'\033[0m' \
164         -y $'\033[30;42m' -z $'\033[0m' \
165         $*
166 }
167
168 # Looks for the most recent .log and pdflatex + bibtex the
169 # corresponding tex file
170
171 function rl () {
172     RECENT_LOG=$(ls -t $(find -maxdepth 1 -name "*.log" -type f) | head -1)
173
174     if [ ${RECENT_LOG} ]; then
175         FILEBASE="${RECENT_LOG/.log/}"
176
177         if [ -f "${FILEBASE}.tex" ]; then
178
179             pdflatex "${FILEBASE}"
180             bibtex "${FILEBASE}"
181             pdflatex "${FILEBASE}"
182             pdflatex "${FILEBASE}"
183
184             if [ "$1" ]; then
185                 if [ "$1" == "-v" ] || [ "$1" == "--view" ]; then
186                     xpdf "${FILEBASE}.pdf"
187                 elif [ "$1" == "-p" ] || [ "$1" == "--print" ]; then
188                     lpr "${FILEBASE}.pdf"
189                 else
190                     echo "Usage: rl [-v|--view] [-p|--print]" >&2
191                     echo "Unknown option $1" >&2
192                     return 1
193                 fi
194             fi
195
196         else
197
198             echo "Can not find a tex file corresponding to the most recent log (${RECENT_LOG/.log/})." >&2
199             return 1
200
201         fi
202
203     else
204         echo "Can not find a recent log." >&2
205         return 1
206     fi
207 }
208
209
210 ######################################################################
211 # Functions
212
213 # http://www.reddit.com/r/linux/comments/2cgu5k/a_handy_little_script_for_interacting_with_your/
214 function clip () {
215     if [ -t 0 ] && [ -z "$1" ]; then
216         # output contents of clipboard
217         xclip -out -selection clipboard || exit 1
218     elif [ "$1" ]; then
219         # copy file contents to clipboard
220         xclip -in -selection clipboard < "$1" || exit 1
221     else
222         # copy stdin to clipboard
223         xclip -in -selection clipboard <&0 || exit 1
224     fi
225 }
226
227 # Find a file whose name contains a substring
228
229 function fn () {
230     name=$1
231     shift
232     find "$@" -name "*${name}*";
233 }
234
235 function bak () {
236     while [ "$1" ]; do
237         cp "$1" "$1".bak
238         shift
239     done
240 }
241
242 function ua () {
243
244     [ "$1" ] || ( echo "Universal unarchive: ua <file> [<file> ...]" >&2 && return 1)
245
246     while [ "$1" ]; do
247
248         case "$1" in
249             *.tgz|*.tar.gz|*.tbz|*.tar.bz2)
250                 tar xvf "$1"
251                 ;;
252
253             *.rar)
254                 unrar -kb x "$1"
255                 ;;
256
257             *.zip)
258                 unzip "$1"
259                 ;;
260
261             *)
262                 echo "Unknown file extension $1"
263                 ;;
264         esac
265
266         shift
267
268     done
269 }
270
271 # Create a dir and cd there
272
273 function mcd () {
274     mkdir -vp "$1"
275     cd "$1"
276 }
277
278 # Capture the screen in a dated png
279
280 function cap () {
281     if [ $2 ]; then
282         name=$2
283     else
284         name="capture-$(date +%s).png"
285     fi
286     echo "Waiting $1 s and saving to ${name}."
287     [ "$1" ] && sleep "$1"
288     echo "Please click on the window to capture."
289     xwd  | convert - ${name}
290     \ls -l ${name}
291 }
292
293 # Create and CD in a /tmp/tmp.XXXXXX directory. With the '-'
294 # arguments, do not create one and CD in the most recent instead
295
296 function cdt () {
297     if [ "$1" ]; then
298         if [ "$1" == "-" ]; then
299             cd $(\ls -td /tmp/tmp.?????? | head -1)
300         else
301             echo "USAGE: cdt [-]" >&2
302             return 1
303         fi
304     else
305         dir=$(mktemp -d /tmp/tmp.XXXXXX)
306         link=/tmp/tmp
307         if [ -h ${link} ]; then
308             \rm ${link}
309         fi
310         if [ ! -a ${link} ]; then
311             ln -s ${dir} ${link}
312         fi
313         cd ${dir}
314     fi
315 }
316
317 alias t='cd /tmp'
318
319 function trash () {
320     TRASH=$(date +/tmp/trash-%Y-%b-%d-%Hh)
321     LINK=/tmp/trash
322
323     mkdir -p ${TRASH}
324
325     [ -h ${LINK} ] && \rm ${LINK}
326
327     [ ! -f ${LINK} ] && ln -s ${TRASH} ${LINK}
328
329     mv "$@" ${TRASH}
330     echo "Trashed $@"
331 }
332
333 ######################################################################
334 ## A version of pho which stores the image numbers in environment
335 ## variables
336
337 function pho () {
338     TEMP=$(mktemp /tmp/pho.XXXXXXX)
339     $(which pho) "$@" | tee ${TEMP}
340     PHO_NOTE_1=$(grep ^"Note 1: " ${TEMP} | sed -e "s/^[^:]*: //")
341     PHO_NOTE_2=$(grep ^"Note 2: " ${TEMP} | sed -e "s/^[^:]*: //")
342     PHO_NOTE_3=$(grep ^"Note 3: " ${TEMP} | sed -e "s/^[^:]*: //")
343     PHO_NOTE_R90=$(grep ^"Rotate 90 \(CW\): " ${TEMP} | sed -e "s/^[^:]*: //")
344     PHO_NOTE_R180=$(grep ^"Rotate 180: " ${TEMP} | sed -e "s/^[^:]*: //")
345     PHO_NOTE_R270=$(grep ^"Rotate -90 \(CCW\): " ${TEMP} | sed -e "s/^[^:]*: //")
346     [ "${PHO_NOTE_1}" ] || unset PHO_NOTE_1
347     [ "${PHO_NOTE_2}" ] || unset PHO_NOTE_2
348     [ "${PHO_NOTE_3}" ] || unset PHO_NOTE_3
349     [ "${PHO_NOTE_R90}" ] || unset PHO_NOTE_R90
350     [ "${PHO_NOTE_R180}" ] || unset PHO_NOTE_R180
351     [ "${PHO_NOTE_R270}" ] || unset PHO_NOTE_R270
352     \rm ${TEMP}
353 }
354
355 ######################################################################
356 ## A version of date that shows the time at home if TZ is set
357
358 function dt () {
359     echo "Local: $(date)"
360     if [ ${TZ} ]; then
361         unset TZ
362         echo "Home:  $(date)"
363     fi
364 }
365
366 ######################################################################
367 ## ifup / ifdown with sudo and memorization of the network
368
369 ## When invoked without an argument netup uses the same argument as
370 ## the previous time
371
372 ## When invoked without an argument netdown removes the last interface
373 ## which was netuped
374
375 [ ${NETUP_HISTORY} ] || NETUP_HISTORY="${HOME}/.netup_history"
376
377 function netup () {
378     local upped_wifi
379
380     [ "${WIFI_INTERFACE}" ] || WIFI_INTERFACE=wlan0
381
382     if [ "$1" == "--scan" ]; then
383         if ifconfig -s | grep -v -q ${WIFI_INTERFACE}; then
384             sudo ifconfig ${WIFI_INTERFACE} up
385             upped_wifi=1
386         fi
387
388         sudo iwlist ${WIFI_INTERFACE} scan | \grep -E 'ESS|Quali|Encry' | sed -e 's/^[ \t]*//'
389
390         if [ ${upped_wifi} ]; then
391             sudo ifconfig ${WIFI_INTERFACE} down
392             unset upped_wifi
393         fi
394
395         return 0
396     fi
397
398     if \ifconfig -s | grep -q -v ^'(Iface|lo) '; then
399         echo "There is/are already interface(s) up." >&2
400         return 1
401     fi
402
403     if \ps h -C dhclient | grep -q .; then
404         echo "There is already a dhcp client running." >&2
405         return 1
406     fi
407
408     if \ps h -C wpa_supplicant | grep -q .; then
409         echo "There is already a wpa_supplicant running." >&2
410         return 1
411     fi
412
413     if [ ! "$@" ] && [ -s ${NETUP_HISTORY} ]; then
414         echo "netup <interface>" >&2
415         return 1
416         # # If we have no argument and there is a .netup_history, use it
417         # ARGS=$(cat ${NETUP_HISTORY})
418     else
419         # Otherwise uses the given arguments, and store them
420         ARGS="$@"
421         echo ${ARGS} > ${NETUP_HISTORY}
422     fi
423
424     if [ "${PRIVATE_INTERFACE_DEFINITION}" ]; then
425         ARGS="-i ${PRIVATE_INTERFACE_DEFINITION} ${ARGS}"
426     fi
427
428     echo "Executing ${VT_GREEN_FG}[sudo ifup ${ARGS}]${VT_RESET}"
429     sudo ifup ${ARGS}
430
431     # Ugly hack to remove the dsl modem dns server when we add
432     # explicitely a dns in the /etc/network/interfaces
433
434     REMOVE_LOCAL_DNS=/usr/local/bin/remove-local-dns.sh
435     # REMOVE_LOCAL_DNS=${HOME}/sources/scripts/remove-local-dns.sh
436
437     if [ -x ${REMOVE_LOCAL_DNS} ]; then
438         echo "Executing ${VT_GREEN_FG}[sudo ${REMOVE_LOCAL_DNS} 192.168]${VT_RESET}"
439         sudo ${REMOVE_LOCAL_DNS} 192.168
440     fi
441 }
442
443 function netdown () {
444     if [ ! "$@" ] && [ -s ${NETUP_HISTORY} ]; then
445         # If there are no arguments and there is a .netup_history, get the
446         # interface from it
447         ARGS=$(tail -1 ${NETUP_HISTORY} | sed -e "s/=.*$//")
448     else
449         # Otherwise, use the standard ifdown
450         ARGS="$@"
451     fi
452
453     [ "${PRIVATE_INTERFACE_DEFINITION}" ] && ARGS="-i ${PRIVATE_INTERFACE_DEFINITION} ${ARGS}"
454
455     echo "Executing sudo ${VT_GREEN_FG}[ifdown ${ARGS}]${VT_RESET}"
456     sudo ifdown ${ARGS}
457
458     # if [[ $(\ps -C dhclient | tail -n +2) ]]; then
459     # echo "There is still a dhcp client running." >&2
460     # return 1
461     # fi
462
463     # if [[ $(\ps -C wpa_supplicant | tail -n +2) ]]; then
464     # echo "There is still a wpa_supplicant running." >&2
465     # return 1
466     # fi
467 }
468
469 function checkgw () {
470     GW=$(route -n | grep ^0.0.0.0 | awk '{print $2}')
471     if [ "${GW}" ]; then
472         ping ${GW}
473     else
474         echo "Can not find a getaway." >&2
475         return 1
476     fi
477 }
478
479 ######################################################################
480 # Show the most recent files, no scroll
481
482 function lr () {
483     HEIGHT=$(stty size | awk '{print $1}')
484     WIDTH=$(stty size | awk '{print $2}')
485     \ls -goth --time-style="+%Y %b %d %H:%M" "$@" | \
486         head -$((HEIGHT-2)) | \
487         cut -b1-${WIDTH}
488 }
489
490 ######################################################################
491 # You can change the xterm background color on the fly!
492
493 function setxtermbg () {
494     echo -n $'\e]11;'$1$'\007'
495 }
496
497 ######################################################################
498 # Shuffle the lines from the stdin
499
500 function shuffle () {
501     SEED=$1
502     [ $SEED ] || SEED=0
503     awk 'BEGIN{srand('${SEED}')} { print rand()" "$0 }' | sort -g | sed -e "s/^[0-9\.e\-]* //"
504 }
505
506 ######################################################################
507 # Stores the last entered command into a file
508
509 KEPT_COMMANDS=${HOME}/.kept_bash_commands
510
511 function keep () {
512     if [ ${KEPT_COMMANDS} ]; then
513         TOKEEP=$(mktemp /tmp/keep.XXXXXX)
514         if [ "$*" ]; then
515             echo "$*" > ${TOKEEP}
516         else
517             selector -b -i -d -l ${HISTSIZE} -o "${TOKEEP}" <(history)
518         fi
519
520         if [ -s "${TOKEEP}" ]; then
521             echo $(date)": "$(cat ${TOKEEP}) >> ${KEPT_COMMANDS}
522             cat "${TOKEEP}"
523         else
524             echo "No command stored!"
525         fi
526
527         \rm ${TOKEEP}
528     else
529         echo "You have to set \$KEPT_COMMANDS"
530     fi
531 }
532
533 ######################################################################
534 # I sometime burn CDs and DVDs
535
536 function burn () {
537     DEVICE="/dev/cdrw1"
538     if [ ! "$1" ]; then
539         echo "burn <iso name | dirname>" >&2
540     elif [ -f "$1" ]; then
541         if [[ $(file "$1" | \grep -E 'ISO 9660|UDF filesystem data') ]]; then
542             wodim -eject -v dev=${DEVICE} "$1"
543         else
544             echo "Do not know what to do with $1" >&2
545         fi
546     elif [ -d "$1" ]; then
547         [ "${TMP_ROOT}" ] || TMP_ROOT=/tmp/
548         echo "Using ${TMP_ROOT} as temporary directory."
549         TMP=$(mktemp ${TMP_ROOT}/cdimage.XXXXXX) && \
550             genisoimage -input-charset iso8859-1 -r -o ${TMP} "$1" && \
551             wodim -eject -v dev=${DEVICE} ${TMP}
552         rm -f ${TMP}
553     else
554         echo "Can not find $1" >&2
555     fi
556 }
557
558 ######################################################################
559 # And watch DVDs too!
560
561 function dvd () {
562
563     echo
564     echo " ! @   Seek to the beginning of the previous/next chapter"
565     echo " j     Cycle through the available subtitles"
566     echo " o     Show/hide the timing"
567     echo " x z   Subtitle delay"
568     echo " / *   Volume"
569     echo
570
571     if [ "$1" ]; then
572         dvd_device="$1"
573         shift
574     else
575         dvd_device="/dev/dvd"
576     fi
577
578     title="1"
579
580     if [ "$1" ]; then
581         title=$1
582         shift
583     fi
584
585     # -vc ffmpeg12 -vf yadif
586
587     mplayer > /dev/null \
588         -stop-xscreensaver \
589         -quiet \
590         -alang en -slang en \
591         -softvol -softvol-max 1000 \
592         -dvd-device ${dvd_device} ${MPLAYER_OPTIONS} dvd://${title}
593 }
594
595 function ripdvd () {
596     if [ -e "/dev/dvd" ]; then
597         DVD_DEVICE="/dev/dvd"
598     elif [ -e "/dev/dvd3" ]; then
599         DVD_DEVICE="/dev/dvd3"
600     else
601         echo "Can not find the dvd device." >&2
602         return 1
603     fi
604
605     echo "Attemptin to rip from ${DVD_DEVICE}."
606
607     mkdir -p ${HOME}/dvds
608
609     cd ${HOME}/dvds
610     time dvdbackup -i ${DVD_DEVICE} -v -M $* && eject
611 }
612
613 alias ripcd=abcde
614
615 ######################################################################
616 # Create small images from images
617
618 function mksmall () {
619
620     PARAMS="-geometry 800x600"
621
622     # Auto-orient does not seem to work at all, hence the ugly hack
623     # with exif below
624
625     # PARAMS="-auto-orient -geometry 800x600"
626
627     echo "Using ${PARAMS}"
628
629     DEST_DIR=$1
630
631     [ ${DEST_DIR} ] || DEST_DIR=./small
632
633     mkdir -p ${DEST_DIR}
634
635     if [ ! -d ${DEST_DIR} ]; then
636         echo "Can not create ${DEST_DIR}" >&2
637         return
638     fi
639
640     NB_TOTAL=$(find -maxdepth 1 -type f | wc -l)
641     NB=0
642
643     for i in $(find -maxdepth 1 -type f); do
644         if [[ $(file $i | grep image) ]]; then
645             if [ -e ${DEST_DIR}/$i ]; then
646                 echo "The file ${DEST_DIR}/$i already exists."
647             else
648                 if [[ $(file ${i/%.*/}.* | grep -E movie) ]] ; then
649                     CAPTION_PARAMS="-font FreeSans-Bold -pointsize 32 -fill green -annotate +10+32 Video"
650                 else
651                     CAPTION_PARAMS=""
652                 fi
653
654                 convert ${rotation_cmd} $i ${PARAMS} ${CAPTION_PARAMS} ${DEST_DIR}/$i
655             fi
656
657             \ls -lt ${DEST_DIR}/$i
658         fi
659
660         NB=$((NB+1))
661
662         echo "$((NB*100/NB_TOTAL))% (${NB}/${NB_TOTAL})"
663     done
664 }
665
666 ######################################################################
667 # Move a file to the ~/sources/config directory and replace it where
668 # it was by a symbolic link
669
670 function mvtoconfig () {
671     CONFIGDIR=${HOME}/sources/config
672     if [[ -d ${CONFIGDIR} ]]; then
673         NEWNAME=${CONFIGDIR}/$(basename "$1" | sed -e "s/^\.//")
674         mv "$1" $NEWNAME
675         ln -s $NEWNAME $1
676     else
677         echo "Can not find ${CONFIGDIR}"
678     fi
679 }
680
681 ######################################################################
682 # The complex prompt policy
683
684 export PS1
685
686 if [ "${CONSOLE}" == "yes" ]; then
687     PS1=""
688 else
689
690     # If the login is a standard one (as specified in
691     # IGNORED_PROMPT_LOGIN, which is set in the private bash file), do not
692     # show it. I have IGNORED_PROMPT_LOGIN="^fleuret$".
693
694     if [ ! ${IGNORED_PROMPT_LOGIN} ] || [[ ! ${USER} =~ ${IGNORED_PROMPT_LOGIN} ]]; then
695         IDENT="${USER}"
696     fi
697
698     # If the display is not the main one, make the assumption that the
699     # shell is not running on the localhost, and show the hostname
700
701     [ "${DISPLAY}" != ":0.0" ] && IDENT="${IDENT}@\h"
702
703     # If there is the login or the hostname, add a ":" to the prompt
704
705     [ "${IDENT}" ] && IDENT="${IDENT}:"
706
707     # If we are root, show that in red
708
709     if [[ ${USER} == "root" ]]; then
710         PS1="\[${VT_RED_BG}${VT_WHITE_FG}\]${IDENT}\w\[${VT_RESET}\] "
711     else
712         PS1="\[${VT_WHITE_BG}${VT_BLACK_FG}\]${IDENT}\w\[${VT_RESET}\] "
713     fi
714
715     # In an xterm, show the hostname and path in the title bar, highlight
716     # the prompt
717
718     # [ "${TERMS_WITH_BAR}" ] || TERMS_WITH_BAR="^xterm|screen$"
719
720     # if [[ "${TERM}" =~ "${TERMS_WITH_BAR}" ]]; then
721     # PS1="\[${VT_SET_TITLE}shell@\h (\w)${VT_END_TITLE}${VT_WHITE_BG}\]${IDENT}\w\[${VT_RESET}\] "
722     # else
723     # PS1="\[${VT_WHITE_BG}\]${IDENT}\w\[${VT_RESET}\] "
724     # fi
725
726 fi
727
728 ######################################################################
729 # This implements a local history. If we are in a directory containing
730 # a writable local history file, we add the last line of the global
731 # history to it.
732
733 LOCAL_HISTORY_FILE=".local_bash_history"
734
735 function keep_local_history () {
736     if [[ -w "${LOCAL_HISTORY_FILE}" ]]; then
737         history 1 | sed -e 's/^ *[0-9]* *//' >> ${LOCAL_HISTORY_FILE}
738         TMP=$(mktemp /tmp/lh.XXXXXX)
739         \chmod 600 ${TMP}
740         uniq < ${LOCAL_HISTORY_FILE} | tail -${HISTSIZE} > ${TMP}
741         # mv would replace a symbolic link, while cp keeps it
742         \cp ${TMP} ${LOCAL_HISTORY_FILE}
743         \rm ${TMP}
744         LOCAL_HISTORY_HINT="* "
745     else
746         LOCAL_HISTORY_HINT=""
747     fi
748 }
749
750 PS1="\[${VT_WHITE_BG}\]\${LOCAL_HISTORY_HINT}\[${VT_RESET}\]${PS1}"
751
752 ######################################################################
753 # Switch off the history
754
755 function histfile_cue () {
756     if [[ ! "${HISTFILE}" == "${HOME}/.bash_history" ]]; then
757         HISTORY_CUE="[${HISTFILE}]"
758     else
759         HISTORY_CUE=""
760     fi
761 }
762
763 PS1="\[${VT_YELLOW_BG}\]\${HISTORY_CUE}\[${VT_RESET}\]${PS1}"
764
765 ######################################################################
766 # The dus command is available on my web site
767 #
768 # git clone http://fleuret.org/git/dus/
769
770 alias dus='dus -f -i'
771
772 ######################################################################
773 # The finddup command is available on my web site
774 #
775 # git clone http://fleuret.org/git/finddup/
776
777 alias finddup='finddup -p'
778
779 ######################################################################
780 # The selector command is available on my web site
781 #
782 # git clone http://fleuret.org/git/selector/
783
784 source bash-selector.sh --hist --cd
785
786 ######################################################################
787 # And we avoid to put in the history the use of the selector, which we
788 # do too often
789
790 HISTIGNORE="${HISTIGNORE}:selector-history"
791
792 ######################################################################
793
794 function selector-printer () {
795     TMP=$(mktemp /tmp/selector-printer.XXXXXX)
796     selector -o ${TMP} <(lpstat -a | awk '{print $1}')
797     export PRINTER=$(cat ${TMP})
798     echo "PRINTER=${PRINTER}"
799     rm -f ${TMP}
800     lpq
801 }
802
803 ######################################################################
804 # A ls with memory to notice what files have been added/removed from
805 # the current directory
806
807 function lsn () {
808     LSN_MEMORY=".lsn-state"
809
810     if  [[ $1 == "--mem" ]] || [[ "$1" == "-m" ]]; then
811
812         \ls -pa | sort > ${LSN_MEMORY}
813         echo "State updated." >&2
814
815     elif [[ $1 == "+" ]]; then
816
817         shift
818
819         TMP=$(mktemp /tmp/lsn.XXXXXX)
820         \ls -d $* > ${TMP}
821         cat ${LSN_MEMORY} >> ${TMP}
822         sort -u ${TMP} > ${LSN_MEMORY}
823         \rm ${TMP}
824
825     elif [ "$1" ]; then
826
827         echo "lsn [--mem|-m] [+ <file> ...]" >&2
828         return 1
829
830     else
831
832         if [[ -f ${LSN_MEMORY} ]]; then
833             TMP=$(mktemp /tmp/lsn.XXXXXX)
834             \ls -pa | sort > ${TMP}
835             if diff > /dev/null ${TMP} ${LSN_MEMORY}; then
836                 echo "${VT_GREEN_FG}${VT_BOLD}No change (since $(date +"%b %d, %Y" -r ${LSN_MEMORY}))${VT_RESET}"
837             else
838                 \comm -1 -3 ${LSN_MEMORY} ${TMP}
839                 \comm -2 -3 ${LSN_MEMORY} ${TMP} | while read line; do
840                     echo "${VT_RED_FG}${VT_BOLD}${line}${VT_RESET} (missing)"
841                 done
842             fi
843             \rm ${TMP}
844         else
845             echo "No lsn state here." >&2
846             return 1
847         fi
848
849     fi
850 }
851
852 ######################################################################
853
854 function prompt_command () {
855     # save the history after every command to avoid loosing some when
856     # multiple shells are open
857     history -a
858     # load the saved history
859     history -n
860     # and the local histories system defined above
861     keep_local_history
862     # and the history cue
863     histfile_cue
864 }
865
866 PROMPT_COMMAND="prompt_command"
867
868 ######################################################################
869
870 # Displaying the timezone if it is set
871
872 if [[ ${TZ} ]]; then
873     echo "${VT_BOLD}${VT_GREEN_FG}Time zone is ${TZ}.${VT_RESET}"
874 fi
875
876 ######################################################################