initial
[zsh.git] / .zsh / 50_Trash.zsh
1 # -*- mode: sh;-*-
2
3 TRASH=$ZDOTDIR/.trash
4
5 move_to_trash ()
6 {
7 FOLDER=$TRASH/$PWD
8
9 for element in $@; do
10 if [ -e $element ]; then
11 echo "Deleting $element..."
12 mkdir -p $FOLDER/${element:h}
13 mv -f $element $FOLDER/${element:h}/.
14 fi
15 done
16 }
17
18 list_deleted_elements ()
19 {
20 FOLDER=$TRASH/$PWD
21
22 if [ -d $FOLDER ]; then
23 ls -lad $(find $FOLDER -maxdepth 1 ! -wholename $FOLDER) | sed "s:$FOLDER/::"
24 else
25 echo "Nothing found in trash."
26 fi
27 }
28
29 undelete_from_trash ()
30 {
31 FOLDER=$TRASH/$PWD
32
33 for element in $@; do
34 if [ -e $FOLDER/$element ]; then
35 echo "Getting back $element..."
36 mkdir -p ${element:h}
37 mv $FOLDER/$element .
38 rmdir --ignore-fail-on-non-empty -p $FOLDER
39 else
40 echo "Not found in trash: $element"
41 fi
42 done
43 }
44
45 alias delete='move_to_trash'
46 alias undelete='undelete_from_trash'
47 alias lsdeleted='list_deleted_elements'
48
49 alias cdtrash='cd $TRASH/$PWD'
50 alias sotrash='cd ${PWD/$TRASH/}'
51
52 hash -d trash=$TRASH