From c4317eef4c3a26581212f22ebf9a231823031305 Mon Sep 17 00:00:00 2001 From: Mehmet Tekman Date: Tue, 29 Aug 2017 03:06:13 +0100 Subject: [PATCH] added move functionality --- plugins/org-mode-manager/orgnotes.sh | 82 ++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 23 deletions(-) diff --git a/plugins/org-mode-manager/orgnotes.sh b/plugins/org-mode-manager/orgnotes.sh index 705c229c..d71c6949 100644 --- a/plugins/org-mode-manager/orgnotes.sh +++ b/plugins/org-mode-manager/orgnotes.sh @@ -1,5 +1,23 @@ #!/bin/bash +__cleanup_org(){ + local file=$1 + local dir=$2 + + [ "$file" = "" ] || [ "$dir" = "" ] && echo "nothing to clean up." && return 0 + + rm `dirname ${file}`/\#`basename ${file}`\# ${file}\~ 2>/dev/null # remove temp files + + # cleanup + # - Remove unused directories + local stack_count=6 # can't expect more than 6 nested dirs, surely... + while [ $stack_count -gt 0 ]; do + stack_count=$(( $stack_count - 1 )) + find ${dir} -type d -exec rmdir {} \; 2>/dev/null + done +} + + org(){ local org_loc=~/.config/org_notes.location @@ -13,7 +31,7 @@ or run the install script." local org_dir=$(cat $org_loc) local file=$1 - local delete=$2 + local command=$2 case $file in "") @@ -26,7 +44,7 @@ or run the install script." -h);& --help) echo " - `basename $0` [--delete] + `basename $0` [--delete] [--move=] Generates an org-notes file in a folder location set in the $org_loc file "; @@ -34,39 +52,57 @@ Generates an org-notes file in a folder location set in the $org_loc file ;; esac - if [[ "$delete" =~ "--" ]]; then - [ "$delete" != "--delete" ] && echo "Unable to parse: $delete" && return -1 + if [ "$command" = "--delete" ] || [[ "$command" =~ "--move=" ]]; then + else + echo "Unable to parse: $command" + return -1 fi - local ext=$( echo $file | awk -F'.' '{print $NF}' ) - + local ext=$( echo $file | awk -F'.' '{print $NF}' ) [ "$ext" != ".org" ] && file=${file}.org - - if [ "$delete" = "--delete" ]; then - local eloc=$org_dir/$file - - if [ -e $eloc ]; then - echo -n "Remove $file? [y/n] "; read ans; - if [ "$ans" = "y" ]; then - rm `dirname ${eloc}`/\#`basename ${eloc}`\# ${eloc}\~ 2>/dev/null # remove temp files - rm ${eloc} && echo "Deleted." - # cleanup - # - Remove unused directories - local stack_count=6 # can't expect more than 6 nested dirs, surely... - while [ $stack_count -gt 0 ]; do - stack_count=$(( $stack_count - 1 )) - find ${org_dir} -type d -exec rmdir {} \; 2>/dev/null - done - fi + local eloc=$org_dir/$file + + if [ "$command" != "" ]; then + if [ -e $eloc ]; then + + case $command in + "--delete") + echo -n "Remove $file? [y/n] "; read ans; + if [ "$ans" = "y" ]; then + rm ${eloc} && echo "Deleted." + __cleanup_org $eloc $org_dir + return 0 + fi + ;; + + "--move="*) + local dest=`echo $command | sed 's|--move=||'` + echo -n "Move $file to $dest ? [y/n] "; read ans; + if [ "$ans" = "y" ]; then + local ext1=$( echo $dest | awk -F'.' '{print $NF}' ) + [ "$ext1" != ".org" ] && dest=${dest}.org + + local edest=$org_dir/$dest + mkdir -p $org_dir/`dirname $dest` + mv $eloc $edest && echo "Moved: $file --> $dest" + __cleanup_org $eloc $org_dir + return 0 + fi + ;; + esac else echo "Cannot find $file. Aborting." + return -1 fi else + # no command, just create + local dirn=`dirname $file` mkdir -p $org_dir/$dirn emacs $org_dir/$file + return 0 fi }