# -*- coding: utf-8 -*-
""" IPython extension: add %clear magic """

import IPython.ipapi
import gc
ip = IPython.ipapi.get()


def clear_f(self,arg):
    """ Clear various data (e.g. stored history data)
    
    %clear out - clear output history
    %clear in  - clear input history
    %clear shadow_compress - Compresses shadow history (to speed up ipython)
    %clear shadow_nuke - permanently erase all entries in shadow history
    %clear dhist - clear dir history
    """
    
    api = self.getapi()
    for target in arg.split():
        if target == 'out':
            print "Flushing output cache (%d entries)" % len(api.user_ns['_oh'])
            self.outputcache.flush()
        elif target == 'in':
            print "Flushing input history"
            from IPython import iplib
            pc = self.outputcache.prompt_count + 1
            for n in range(1, pc):
                key = '_i'+`n`
                try:
                    del self.user_ns[key]
                except: pass
            # must be done in-place
            self.input_hist[:] = ['\n'] * pc 
            self.input_hist_raw[:] = ['\n'] * pc
        elif target == 'array':
            try:
                pylab=ip.IP.pylab
                for x in self.user_ns.keys():
                    if isinstance(self.user_ns[x],pylab.arraytype):
                        del self.user_ns[x]
            except AttributeError:
                print "Clear array only available in -pylab mode"
            gc.collect()                

        elif target == 'shadow_compress':
            print "Compressing shadow history"
            api.db.hcompress('shadowhist')
            
        elif target == 'shadow_nuke':
            print "Erased all keys from shadow history "
            for k in ip.db.keys('shadowhist/*'):
                del ip.db[k]
        elif target == 'dhist':
            print "Clearing directory history"
            del ip.user_ns['_dh'][:]

            
ip.expose_magic("clear",clear_f)
import ipy_completers
ipy_completers.quick_completer(
 '%clear','in out shadow_nuke shadow_compress dhist')
    




@@ -119,17 +119,16 @@
 .get()%0A%0A
-%0A
 def clea
@@ -212,19 +212,19 @@
  %25clear 
-out
+in 
  - clear
@@ -224,19 +224,18 @@
 - clear 
-out
+in
 put hist
@@ -241,35 +241,35 @@
 tory%0A    %25clear 
-in 
+out
  - clear input h
@@ -253,34 +253,35 @@
 ear out - clear 
-in
+out
 put history%0A    
@@ -471,40 +471,434 @@
     
-%22%22%22%0A    %0A    api = self.getapi()
+%25clear array - clear only variables that are NumPy arrays%0A    %0A    Examples:%0A%0A    In %5B1%5D: clear in%0A    Flushing input history%0A%0A    In %5B2%5D: clear shadow_compress%0A    Compressing shadow history%0A%0A    In %5B3%5D: clear shadow_nuke%0A    Erased all keys from shadow history%0A%0A    In %5B4%5D: clear dhist%0A    Clearing directory history%0A    %22%22%22%0A    %0A    api = self.getapi()%0A    user_ns = self.user_ns  # local lookup, heavily used%0A        %0A    
 %0A   
@@ -921,24 +921,25 @@
 rg.split():%0A
+%0A
         if t
@@ -1015,20 +1015,16 @@
 %22 %25 len(
-api.
 user_ns%5B
@@ -1068,16 +1068,17 @@
 flush()%0A
+%0A
         
@@ -1145,46 +1145,8 @@
 ry%22%0A
-            from IPython import iplib%0A
     
@@ -1258,16 +1258,54 @@
 _i'+%60n%60%0A
+                user_ns.pop(key,None)%0A
         
@@ -1333,37 +1333,32 @@
             del 
-self.
 user_ns%5Bkey%5D%0A   
@@ -1514,16 +1514,17 @@
 '%5D * pc%0A
+%0A
         
@@ -1563,46 +1563,233 @@
     
-try:%0A                pylab=ip.IP.pylab
+# Support cleaning up numpy arrays%0A            try:%0A                from numpy import ndarray%0A                # This must be done with items and not iteritems because we're%0A                # going to modify the dict in-place.
 %0A   
@@ -1810,25 +1810,24 @@
 or x
+,val
  in 
-self.
 user_ns.
 keys
@@ -1822,19 +1822,20 @@
 user_ns.
-key
+item
 s():%0A   
@@ -1869,39 +1869,19 @@
 nce(
-self.user_ns%5Bx%5D,pylab.
+val,nd
 array
-type
 ):%0A 
@@ -1907,21 +1907,16 @@
     del 
-self.
 user_ns%5B
@@ -1997,74 +1997,37 @@
 nly 
-available in -pylab mode%22%0A            gc.collect()                
+works if Numpy is available.%22
 %0A%0A  
@@ -2342,16 +2342,17 @@
 p.db%5Bk%5D%0A
+%0A
         
@@ -2438,19 +2438,16 @@
     del 
-ip.
 user_ns%5B
@@ -2461,24 +2461,62 @@
 :%5D%0A%0A    
-        
+gc.collect()        %0A%0A# Activate the extension
 %0Aip.expo
@@ -2595,16 +2595,19 @@
 pleter(%0A
+   
  '%25clear
@@ -2656,12 +2656,4 @@
 t')%0A
-    %0A%0A%0A%0A

Add tests and fixes to clearcmd extension, when using numpy.