[IPython-user] changed %edit magic command to re-edit previous user cmd if no arg specified

Tom Popovich tpop.news at gmail.com
Sun May 22 20:56:08 CDT 2005


Proposed changing %edit magic command to re-edit previous user cmd if
no arg specified

Details:
I'm proposing that   ed    (note not arguments) would be equivalent
below  to  ed 18     (which would work).   However   ed 18 is  hard to
type since you need to know "18" as the command now.  Then later we
need to change 18 to say 20 ... as we use the tool.  I'm pitching that
  ed  alone does this by default ...


e.g.  consider the following ipython session:

assume previously did   ed   and just put  99  as the only contents...
....
Out[16]: '99'

In [17]: ed -p
Editing... done. Executing edited code...            <<== ok redid   99
Out[17]: '99\n'

In [18]: 22                                    ##  try something new.
Out[18]: 22

In [19]: ed -p    ## using -p  just to test...   but wanted   22 
(command in preceeding REPL iteration #18) in the buffer not  99 which
-p will populate
Editing... done. Executing edited code...
Out[19]: '99\n'

In [20]: ed                   ## here I just exited to show that the
buffer  is empty.
Editing... done. Executing edited code...
WARNING: File not found. Did you forget to save?

In [21]:


I'd like to %edit  if used like in In#20  to edit the previous
command's text (e.g. here we take it from ln [18]  or maybe the Out of
Ln#19).  Above in  Ln 19  (the editor shows 99) as text output from
edit session (which is correct in that 99 is what I edited in the last
%edit session)     and  in  Ln#20   we get a blank edit window due to
the currently defined %edit behavior.

================
>From the official manual:
%edit  Arguments:
If arguments are given, the following possibilites exist:
- The arguments are numbers or pairs of colon-separated numbers (like
1 4:8 9). These are interpreted
as lines of previous input to be loaded into the editor. The syntax is
the same of the %macro command.
================


Alg: ... could we define   %edit  to   do    %edit  (assume the user
entered 18  here, since if I did   ed  19 the editor opens with :
ipmagic("ed -p")  ... so maybe we could implement... if ed  ==> try  
ed 19 ==> if that has a command starting w/  ipmag...   try next
command ==> ed 18  which has real user text  so edit that.

-Thanks, Tom / tpop

===============================================
Here are the changes:

*** ../Magic.py Sun May 22 18:40:27 2005
--- /tmp/Magic.py Tue May 17 17:23:25 2005
***************
*** 1716,1756 ****
          # by default this is done with temp files, except when the given
          # arg is a filename
          use_temp = 1
-         if args == '':
-             current_history_index =  len(self.shell.input_hist) - 1
-             ranges = [ str(current_history_index) ]
  
!             data = ''.join(self.extract_input_slices(ranges))
!             ## assume that the args is '22', if that maps to a
ipmagic command, look
!             ## at previous command, keep repeating till we find one
not starting w/ ipmagic
!             while re.match(r'^ipmagic', data):
!                 try:
!                     interger_number = int(ranges[0])
!                     interger_number -= 1
!                     ranges = [ str(interger_number) ]
!                     data = ''.join(self.extract_input_slices(ranges))
!                 except:
!                     break
!             
!         elif re.match(r'\d',args):
              # Mode where user specifies ranges of lines, like in %macro.
              # This means that you can't edit files whose names begin with
              # numbers this way. Tough.
              ranges = args.split()
              data = ''.join(self.extract_input_slices(ranges))
-             if re.match(r'^\d+$',args):
-                 ## assume that the args is '22', if that maps to a
ipmagic command, look
-                 ## at previous command, keep repeating till we find
one not starting w/ ipmagic
-                 while re.match(r'^ipmagic', data):
-                     try:
-                         interger_number = int(ranges[0])
-                         interger_number -= 1
-                         ranges = [ str(interger_number) ]
-                         data = ''.join(self.extract_input_slices(ranges))
-                     except:
-                         break
          elif args.endswith('.py'):
              filename = make_filename(args)
              data = ''
--- 1716,1728 ----
          # by default this is done with temp files, except when the given
          # arg is a filename
          use_temp = 1
  
!         if re.match(r'\d',args):
              # Mode where user specifies ranges of lines, like in %macro.
              # This means that you can't edit files whose names begin with
              # numbers this way. Tough.
              ranges = args.split()
              data = ''.join(self.extract_input_slices(ranges))
          elif args.endswith('.py'):
              filename = make_filename(args)
              data = ''
***************
*** 2507,2511 ****
                  bkms[args[0]] = args[1]
          self.persist['bookmarks'] = bkms
  # end Magic
- 
-     
--- 2479,2481 ----




More information about the IPython-user mailing list