""" smashlib.patches.edit
"""
#from smashlib.patches.base import PatchMagic
from IPython.core.magics.code import CodeMagics


class PatchEdit(object):
    """ patches the builtin ipython cd magic so that a post-dir-change
        event can be sent to anyone who wants to subscribe, and so that
        the "cd" command is quiet by default.
    """
    original = staticmethod(CodeMagics._find_edit_target)
    def __init__(self, *args, **kargs):
        pass

    def install(self):
        CodeMagics._find_edit_target = my_find_edit_target

def my_find_edit_target(himself,shell,args,opts,last_call):
    """Utility method used by magic_edit to find what to edit."""
    if args.endswith(':'):
        args=args[:-1]
    result = PatchEdit.original(shell,args,opts,last_call)
    if result==(None,None,None):
        filename, lineno, use_temp=(args, None, False)
        return filename, lineno, use_temp
    return result

@@ -27,53 +27,17 @@
 %22%22%22%0A
-#from smashlib.patches.base import PatchMagic
+import re
 %0Afro
@@ -81,16 +81,99 @@
 eMagics%0A
+from .base import PatchMagic%0A%0Ar_file_and_line = re.compile('.+%5B:%5D(%5Cd+)%5B:%5D?(%5Cd*)$')%0A
 %0A%0Aclass 
@@ -186,14 +186,18 @@
 dit(
-object
+PatchMagic
 ):%0A 
@@ -388,24 +388,42 @@
 lt.%0A    %22%22%22%0A
+    name = 'edit'%0A
     original
@@ -422,16 +422,17 @@
 original
+2
  = stati
@@ -469,24 +469,26 @@
 target)%0A
+%0A%0A
     def 
 __init__
@@ -483,52 +483,62 @@
 def 
-__init__(self, *args, **kargs):%0A        pass
+install(self):%0A        super(PatchEdit,self).install()
 %0A%0A  
@@ -547,233 +547,215 @@
 def 
-inst
+__c
 all
+__
 (self
-):%0A        CodeMagics._find_edit_target = my_find_edit_target%0A%0Adef my_find_edit_target(h
+, parameter_s='',last_call=%5B'',''%5D):%0A        opts, args = self.original.
 im
+_
 self
-,shell,args,opts,last_call):%0A    %22%22%22Utility method used by magic_edit to find what to edit.%22%22%22%0A
+.parse_options(parameter_s,'prxn:')%0A        fname = args%0A        lineno = ''%0A   
     
+ 
 if 
-args.endswith(':'
+r_file_and_line.match(fname
 ):%0A 
@@ -765,114 +765,159 @@
     
-args=args%5B:-1%5D%0A    result = PatchEdit.original(shell,args,opts,last_call)%0A    if result==(None,None,Non
+    lineno = '-n %7B0%7D'.format(fname.split(':')%5B1%5D)%0A            fname = fname.split(':')%5B0%5D%0A        parameter_s = '%7B0%7D %7B1%7D %7B2%7D'.format('-x',lineno,fnam
 e)
-:
 %0A   
@@ -925,111 +925,150 @@
     
-filename, lineno, use_temp=(args, None, False)%0A        return filename, lineno, use_temp%0A    return result
+self.component.report('rewrote arguments going to edit: '+parameter_s)%0A        return self.original(parameter_s=parameter_s, last_call=last_call)
 %0A

patch edit magic to support linenos