zshrc: improve purge() function.
authorBernhard Tittelbach <bernhard@tittelbach.org>
Fri, 8 Jul 2011 10:34:42 +0000 (12:34 +0200)
committerMichael Prokop <mika@grml.org>
Thu, 21 Jul 2011 15:26:50 +0000 (17:26 +0200)
While at it only delete FILES, not e.g. *~ directories.

doc/grmlzshrc.t2t
etc/zsh/zshrc

index 13bccb2..271cedb 100644 (file)
@@ -790,9 +790,16 @@ Runs a command in $SHELL with profiling enabled (See startup variable
 ZSH_PROFILE_RC above).
 
 : **purge()**
-Removes typical temporary files (i. e. files like "*~", ".*~", "#*#", "*.o",
-"a.out", "*.core", "*.cmo", "*.cmi" and ".*.swp") from current directory.
-Asks for confirmation.
+Removes temporary files from current directory. Asks for confirmation. Uses sudo if necessary.
+In detail it purges
+ - common temp files like "*~", ".*~", "#*#", "*.o", "a.out", "*.orig", "*.rej", "*.cmo", "*.cmi" and ".*.swp"
+ - core dumps
+ - debconf backup files: "*.dpkg-old", "*.dkpg-new", "*.dpkg-dist"
+ - gentoo dispatch-conf backups: ".cfg0000_*", ".mrg0000_*"
+ - precompiled python code ("*.pyc", "*.pyo") as long as matching "*.py" source is also present
+ - LaTeX temp files i.e. "*.(log|toc|aux|nav|snm|out|tex.backup|bbl|blg|bib.backup|vrb|lof|lot|hd|idx)" for any present "*.tex"
+ - ghc temp files, as long as matching "*.hs" or "*.lhs" is also present
+ - "*.mood(D)" Files which are missing their corresponding audio file
 
 : **readme()**
 Opens all README-like files in current working directory with the program
index 041bae3..399ac4f 100644 (file)
@@ -3611,15 +3611,24 @@ lcheck() {
 
 #f5# Clean up directory - remove well known tempfiles
 purge() {
-    FILES=(*~(N) .*~(N) \#*\#(N) *.o(N) a.out(N) *.core(N) *.cmo(N) *.cmi(N) .*.swp(N))
-    NBFILES=${#FILES}
+    emulate -L zsh
+    setopt HIST_SUBST_PATTERN
+    local -a TEXTEMPFILES GHCTEMPFILES PYTEMPFILES FILES
+    TEXTEMPFILES=(*.tex(N:s/%tex/'(log|toc|aux|nav|snm|out|tex.backup|bbl|blg|bib.backup|vrb|lof|lot|hd|idx)(N)'/))
+    GHCTEMPFILES=(*.(hs|lhs)(N:r:s/%/'.(hi|hc|(p|u|s)_(o|hi))(N)'/))
+    PYTEMPFILES=(*.py(N:s/%py/'(pyc|pyo)(N)'/))
+    LONELY_MOOD_FILES=((*.mood)(NDe:'local -a AF;AF=( ${${REPLY#.}%mood}(mp3|flac|ogg|asf|wmv|aac)(N) ); [[ -z "$AF" ]]':))
+    FILES=(*~(.N) \#*\#(.N) *.o(.N) a.out(.N) (*.|)core(.N) *.cmo(.N) *.cmi(.N) .*.swp(.N) *.(orig|rej)(.DN) *.dpkg-(old|dist|new)(DN) ._(cfg|mrg)[0-9][0-9][0-9][0-9]_*(N) ${~TEXTEMPFILES} ${~GHCTEMPFILES} ${~PYTEMPFILES} ${LONELY_MOOD_FILES})
+    local NBFILES=${#FILES}
+    local CURDIRSUDO=""
+    [[ ! -w ./ ]] && CURDIRSUDO=$SUDO
     if [[ $NBFILES > 0 ]] ; then
-        print $FILES
+        print -l $FILES
         local ans
         echo -n "Remove these files? [y/n] "
         read -q ans
         if [[ $ans == "y" ]] ; then
-            rm ${FILES}
+            $CURDIRSUDO rm ${FILES}
             echo ">> $PWD purged, $NBFILES files removed"
         else
             echo "Ok. .. then not.."