From d08bc641c0d62773d5662a003fb9ef2e6d331907 Mon Sep 17 00:00:00 2001 From: Keegi Date: Thu, 7 Jul 2011 00:19:45 +0300 Subject: [PATCH 1/3] simple small GUI editor for macros in pronterface --- pronsole.py | 20 +++++++++++++------- pronterface.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/pronsole.py b/pronsole.py index 0b0a00b..e5c434a 100644 --- a/pronsole.py +++ b/pronsole.py @@ -182,6 +182,15 @@ class pronsole(cmd.Cmd): print "Empty macro - cancelled" del self.cur_macro,self.cur_macro_name,self.cur_macro_def + def start_macro(self,macro_name,prev_definition=""): + if not self.processing_rc: + print "Enter macro using indented lines, end with empty line" + self.cur_macro_name = macro_name + self.cur_macro_def = "" + self.cur_macro = "def macro(self,*arg):\n" + self.onecmd = self.hook_macro # override onecmd temporarily + self.prompt="..>" + def do_macro(self,args): if args.strip()=="": self.print_topics("User-defined macros",self.macros.keys(),15,80) @@ -214,13 +223,10 @@ class pronsole(cmd.Cmd): self.cur_macro = "def macro(self,*arg):\n self.onecmd('"+macro_def+"'.format(*arg))\n" self.end_macro() return - if not self.processing_rc: - print "Enter macro using indented lines, end with empty line" - self.cur_macro_name = macro_name - self.cur_macro_def = "" - self.cur_macro = "def macro(self,*arg):\n" - self.onecmd = self.hook_macro # override onecmd temporarily - self.prompt="..>" + if self.macros.has_key(macro_name): + self.start_macro(macro_name,self.macros[macro_name]) + else: + self.start_macro(macro_name) def help_macro(self): print "Define single-line macro: macro " diff --git a/pronterface.py b/pronterface.py index 8421a8d..7e8bbee 100644 --- a/pronterface.py +++ b/pronterface.py @@ -172,6 +172,19 @@ class PronterWindow(wx.Frame,pronsole.pronsole): except: print "You must enter a temperature." + def start_macro(self,macro_name,old_macro_definition=""): + if not self.processing_rc: + import macroed + def cb(definition): + pronsole.pronsole.start_macro(self,macro_name) + for line in definition.split("\n"): + if hasattr(self,"cur_macro_def"): + self.hook_macro(line) + if hasattr(self,"cur_macro_def"): + self.end_macro() + macroed.macroed(macro_name,old_macro_definition,cb) + else: + pronsole.pronsole.start_macro(self,macro_name,old_macro_definition) def catchprint(self,l): wx.CallAfter(self.logbox.AppendText,l) @@ -786,6 +799,38 @@ class PronterWindow(wx.Frame,pronsole.pronsole): self.printbtn.SetLabel("Print") self.paused=0 +class macroed(wx.Frame): + """Really simple editor to edit macro definitions""" + def __init__(self,macro_name,definition,callback): + # TBD: remove top-level indent from the definition (if any) + wx.Frame.__init__(self,None,title="macro %s" % macro_name) + self.callback = callback + self.panel=wx.Panel(self,-1) + titlesizer=wx.BoxSizer(wx.HORIZONTAL) + title = wx.StaticText(self.panel,-1," macro %s: "%macro_name) + title.SetFont(wx.Font(11,wx.NORMAL,wx.NORMAL,wx.BOLD)) + titlesizer.Add(title,1) + self.okb = wx.Button(self.panel,-1,"Save") + self.okb.Bind(wx.EVT_BUTTON,self.save) + titlesizer.Add(self.okb) + self.cancelb = wx.Button(self.panel,-1,"Cancel") + self.cancelb.Bind(wx.EVT_BUTTON,self.close) + titlesizer.Add(self.cancelb) + topsizer=wx.BoxSizer(wx.VERTICAL) + topsizer.Add(titlesizer,0,wx.EXPAND) + self.e=wx.TextCtrl(self.panel,style=wx.TE_MULTILINE+wx.HSCROLL,size=(200,200)) + self.e.SetValue(definition) + topsizer.Add(self.e,1,wx.ALL+wx.EXPAND) + self.panel.SetSizer(topsizer) + topsizer.Layout() + topsizer.Fit(self) + self.Show() + def save(self,ev): + self.Close() + # TBD: add back top-level indent unless single line. + self.callback(self.e.GetValue()) + def close(self,ev): + self.Close() if __name__ == '__main__': From 049400b66bfcc322e88baf5a8dce3e9673b94c0d Mon Sep 17 00:00:00 2001 From: Keegi Date: Fri, 15 Jul 2011 10:19:28 +0300 Subject: [PATCH 2/3] indent/unindent multiline macros automatically for macro editor --- pronterface.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/pronterface.py b/pronterface.py index 55e89d0..e26a1fc 100644 --- a/pronterface.py +++ b/pronterface.py @@ -843,7 +843,7 @@ class PronterWindow(wx.Frame,pronsole.pronsole): class macroed(wx.Frame): """Really simple editor to edit macro definitions""" def __init__(self,macro_name,definition,callback): - # TBD: remove top-level indent from the definition (if any) + self.indent_chars = " " wx.Frame.__init__(self,None,title="macro %s" % macro_name) self.callback = callback self.panel=wx.Panel(self,-1) @@ -860,7 +860,7 @@ class macroed(wx.Frame): topsizer=wx.BoxSizer(wx.VERTICAL) topsizer.Add(titlesizer,0,wx.EXPAND) self.e=wx.TextCtrl(self.panel,style=wx.TE_MULTILINE+wx.HSCROLL,size=(200,200)) - self.e.SetValue(definition) + self.e.SetValue(self.unindent(definition)) topsizer.Add(self.e,1,wx.ALL+wx.EXPAND) self.panel.SetSizer(topsizer) topsizer.Layout() @@ -868,10 +868,30 @@ class macroed(wx.Frame): self.Show() def save(self,ev): self.Close() - # TBD: add back top-level indent unless single line. - self.callback(self.e.GetValue()) + self.callback(self.reindent(self.e.GetValue())) def close(self,ev): self.Close() + def unindent(self,text): + import re + self.indent_chars = text[:len(text)-len(text.lstrip())] + unindented = "" + lines = re.split(r"(\r\n?|\n)",text) + if len(lines) <= 1: + return text + for line in lines: + if line.startswith(self.indent_chars): + unindented += line[len(self.indent_chars):] + "\n" + else: + unindented += line + "\n" + return unindented + def reindent(self,text): + lines = re.split(r"(\r\n?|\n)",text) + if len(lines) <= 1: + return text + reindented = "" + for line in lines: + reindented += self.indent_chars + line + "\n" + return reindented if __name__ == '__main__': From 1d5ce77a36cabb9bead454ff8c4bfa0f4ef4bd0e Mon Sep 17 00:00:00 2001 From: Keegi Date: Fri, 15 Jul 2011 11:12:21 +0300 Subject: [PATCH 3/3] some fixes to macro editor routines --- pronsole.py | 4 ++-- pronterface.py | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pronsole.py b/pronsole.py index ac9e618..7e15d4f 100755 --- a/pronsole.py +++ b/pronsole.py @@ -182,8 +182,8 @@ class pronsole(cmd.Cmd): print "Empty macro - cancelled" del self.cur_macro,self.cur_macro_name,self.cur_macro_def - def start_macro(self,macro_name,prev_definition=""): - if not self.processing_rc: + def start_macro(self,macro_name,prev_definition="",suppress_instructions=False): + if not self.processing_rc and not suppress_instructions: print "Enter macro using indented lines, end with empty line" self.cur_macro_name = macro_name self.cur_macro_def = "" diff --git a/pronterface.py b/pronterface.py index 764baa2..964de65 100755 --- a/pronterface.py +++ b/pronterface.py @@ -180,15 +180,24 @@ class PronterWindow(wx.Frame,pronsole.pronsole): def start_macro(self,macro_name,old_macro_definition=""): if not self.processing_rc: - import macroed def cb(definition): - pronsole.pronsole.start_macro(self,macro_name) + if "\n" not in definition and len(definition.strip())>0: + macro_def = definition.strip() + self.cur_macro_def = macro_def + self.cur_macro_name = macro_name + if macro_def.startswith("!"): + self.cur_macro = "def macro(self,*arg):\n "+macro_def[1:]+"\n" + else: + self.cur_macro = "def macro(self,*arg):\n self.onecmd('"+macro_def+"'.format(*arg))\n" + self.end_macro() + return + pronsole.pronsole.start_macro(self,macro_name,True) for line in definition.split("\n"): if hasattr(self,"cur_macro_def"): self.hook_macro(line) if hasattr(self,"cur_macro_def"): self.end_macro() - macroed.macroed(macro_name,old_macro_definition,cb) + macroed(macro_name,old_macro_definition,cb) else: pronsole.pronsole.start_macro(self,macro_name,old_macro_definition) @@ -875,7 +884,8 @@ class macroed(wx.Frame): import re self.indent_chars = text[:len(text)-len(text.lstrip())] unindented = "" - lines = re.split(r"(\r\n?|\n)",text) + lines = re.split(r"(?:\r\n?|\n)",text) + #print lines if len(lines) <= 1: return text for line in lines: @@ -885,7 +895,8 @@ class macroed(wx.Frame): unindented += line + "\n" return unindented def reindent(self,text): - lines = re.split(r"(\r\n?|\n)",text) + import re + lines = re.split(r"(?:\r\n?|\n)",text) if len(lines) <= 1: return text reindented = ""