def get_line_offset(): """Return number of characters cursor is offset from margin. """ user_pos = lisp.point() lisp.beginning_of_line() line_start = lisp.point() lisp.goto_char(user_pos) return user_pos - line_start
def fixer_add_blank_line (unused_error, errtext): # E302 expected 2 blank lines, found 1 m = re.search(r"expected (\d+) blank lines, found (\d+)", errtext) if m: linecount = int(m.group(1)) - int(m.group(2)) lisp.beginning_of_line() lisp.newline(linecount)
def occurrences_goto(): if lisp.line_number_at_pos() < 1: lisp.forward_line(1 - lisp.line_number_at_pos()) lisp.end_of_line() end = lisp.point() lisp.beginning_of_line() line = lisp.buffer_substring_no_properties(lisp.point(), end) tokens = line.split() semicolon_tokens = line.split(":") project_root = lisp.rope_get_project_root() if tokens and semicolon_tokens: # Mark this line with an arrow lisp(''' (remove-overlays (point-min) (point-max)) (overlay-put (make-overlay (line-beginning-position) (line-end-position)) 'before-string (propertize "A" 'display '(left-fringe right-triangle))) ''') filename = project_root + "/" + semicolon_tokens[0] offset = int(tokens[-1]) resource = _interface._get_resource(filename) LispUtils().find_file(resource.real_path, other=True) lisp.goto_char(offset + 1)
def run_py_code(): remember_where = lisp.point() # check if the line contains \inputminted lisp.beginning_of_line() l1 = lisp.point() lisp.end_of_line() l2 = lisp.point() line = lisp.buffer_substring(l1,l2) # if code comes from file # get code content from latex block_begin,block_end,content = get_block_content("```python","```\n") # we have code content at this point # scan content to find plt.plot(). if there is, scan buffer # previous to *here* to determine order of _this_ plt.plot(), and # give it an appropiate index that will be appended to the end of # the .png image file, i.e. [buffer name]_[index].png. plt.plot() # commands will be replaced by the corresponding plt.savefig # command. # generate savefig for execution code (no output in emacs yet) bc = lisp.buffer_string() plt_count_before = len(re.findall('plt\.savefig\(',bc)) base = os.path.splitext(lisp.buffer_name())[0] f = '%s_%s.png' % (base, two_digit(plt_count_before+1)) rpl = "plt.savefig('%s')" % f show_replaced = True if "plt.show()" in content else False content=content.replace("plt.show()",rpl) content="plt.figure();\n"+content include_graphics_command = "![](%s)" % f # we have code content at this point start = time.time() with capture_output() as io: res_code = ip.run_cell(content) res = io.stdout elapsed = (time.time() - start) if len(res) > 0: display_results(block_end, res) # display it if show_replaced: lisp.goto_char(block_end) lisp.forward_line(2) # skip over end verbatim, leave one line emtpy lisp.insert('\n' + include_graphics_command + '\n') lisp.scroll_up(1) # skip over end verbatim, leave one line emtpy lisp.goto_char(remember_where) lisp.replace_string("plt.show()",rpl,None,block_begin,block_end) lisp.goto_char(remember_where) lisp.message("Ran in " + str(elapsed) + " seconds")
def verb_exists(): remem = lisp.point() lisp.forward_line(1) lisp.beginning_of_line() verb_line_b = lisp.point() lisp.end_of_line() verb_line_e = lisp.point() verb_line = lisp.buffer_substring(verb_line_b, verb_line_e) lisp.goto_char(remem) if "```text" in verb_line: return True else: return False
def run_py_code(): remember_where = lisp.point() # check if the line contains \inputminted lisp.beginning_of_line() l1 = lisp.point() lisp.end_of_line() l2 = lisp.point() line = lisp.buffer_substring(l1, l2) # if code comes from file # get code content from latex block_begin, block_end, content = get_block_content("```python", "```\n") # we have code content at this point # scan content to find plt.plot(). if there is, scan buffer # previous to *here* to determine order of _this_ plt.plot(), and # give it an appropiate index that will be appended to the end of # the .png image file, i.e. [buffer name]_[index].png. plt.plot() # commands will be replaced by the corresponding plt.savefig # command. # generate savefig for execution code (no output in emacs yet) bc = lisp.buffer_string() plt_count_before = len(re.findall('plt\.savefig\(', bc)) base = os.path.splitext(lisp.buffer_name())[0] f = '%s_%s.png' % (base, two_digit(plt_count_before + 1)) rpl = "plt.savefig('%s')" % f show_replaced = True if "plt.show()" in content else False content = content.replace("plt.show()", rpl) content = "plt.figure();\n" + content include_graphics_command = "![](%s)" % f # we have code content at this point start = time.time() with capture_output() as io: res_code = get_ip().run_cell(content) res = io.stdout elapsed = (time.time() - start) if len(res) > 0: display_results(block_end, res) # display it if show_replaced: lisp.goto_char(block_end) lisp.forward_line(2) # skip over end verbatim, leave one line emtpy lisp.insert('\n' + include_graphics_command + '\n') lisp.scroll_up(1) # skip over end verbatim, leave one line emtpy lisp.goto_char(remember_where) lisp.replace_string("plt.show()", rpl, None, block_begin, block_end) lisp.goto_char(remember_where) lisp.message("Ran in " + str(elapsed) + " seconds")
def verb_exists(): remem = lisp.point() lisp.forward_line(2) lisp.beginning_of_line() verb_line_b = lisp.point() lisp.end_of_line() verb_line_e = lisp.point() verb_line = lisp.buffer_substring(verb_line_b, verb_line_e) lisp.goto_char(remem) if "\\begin{verbatim}" in verb_line: return True else: return False
def occurrences_goto(): if lisp.line_number_at_pos() < 3: lisp.forward_line(3 - lisp.line_number_at_pos()) lisp.end_of_line() end = lisp.point() lisp.beginning_of_line() line = lisp.buffer_substring_no_properties(lisp.point(), end) tokens = line.split() if tokens: filename = tokens[0] offset = int(tokens[-1]) resource = _interface._get_resource(filename) LispUtils().find_file(resource.real_path, other=True) lisp.goto_char(offset + 1)
def occurrences_goto_occurrence(): lisp.end_of_line() end = lisp.point() lisp.beginning_of_line() start = lisp.point() line = lisp.buffer_substring_no_properties(start, end) tokens = line.split() if tokens: filename = tokens[0] offset = int(tokens[-1]) resource = _interface._get_resource(filename) LispUtils().find_file(resource.real_path, other=True) lisp.goto_char(offset + 1) lisp.switch_to_buffer_other_window('*rope-occurrences*')
def insert_indented_text(text,offset): """Insert indented text in buffer and move cursor to a certain offset.""" # save undo state so we can roll everything into a single operation for undo checkpoint = lisp.buffer_undo_list.value() # figure out if we are indented or not, and adapt text accordingly indent_level = get_line_offset() if indent_level > 0: text = indent(text,indent_level) # perform actual insertion with proper cursor positioning offset += indent_level lisp.beginning_of_line() user_pos = lisp.point() lisp.insert(text) lisp.goto_char(user_pos+offset) # Collapse all operations into a single one, for Undo. clean_undo_after(checkpoint)
def insert_indented_text(text, offset): """Insert indented text in buffer and move cursor to a certain offset.""" # save undo state so we can roll everything into a single operation for undo checkpoint = lisp.buffer_undo_list.value() # figure out if we are indented or not, and adapt text accordingly indent_level = get_line_offset() if indent_level > 0: text = indent(text, indent_level) # perform actual insertion with proper cursor positioning offset += indent_level lisp.beginning_of_line() user_pos = lisp.point() lisp.insert(text) lisp.goto_char(user_pos + offset) # Collapse all operations into a single one, for Undo. clean_undo_after(checkpoint)
def fixer_remove_blank_lines (unused_error, errtext): # E303 too many blank lines (7) linecount = 1 m = re.search(r"too many blank lines \((\d+)\)", errtext) if m: linecount = int(m.group(1)) # There's a bug in flake8 it's returning 2 lines too many if linecount > 2: linecount -= 2 # XXX try this with a comment right about the warning, deletes the comment lisp.beginning_of_line() beg = lisp.line_beginning_position() lisp.beginning_of_line(-(linecount - 1)) lisp.forward_line(1) lisp.forward_char(-1) lisp.delete_region(beg, lisp.point()) return
def display_results(end_block, res): lisp.goto_char(end_block) lisp.forward_line(2) lisp.beginning_of_line() verb_line_b = lisp.point() lisp.end_of_line() verb_line_e = lisp.point() verb_line = lisp.buffer_substring(verb_line_b, verb_line_e) if "\\begin{verbatim}" in verb_line: verb_begin,verb_end,content = get_block_content("\\begin{verbatim}","\\end{verbatim}") lisp.delete_region(verb_begin, verb_end) lisp.goto_char(verb_begin) else: lisp.backward_line_nomark(1) lisp.insert("\n") res=res.replace("\r","") lisp.insert("\\begin{verbatim}\n") lisp.insert(res) lisp.insert("\\end{verbatim}")
def show(): b,e,res = get_block_content("$$","$$") d = hash(res) foutsimple = "_preview/f-%d.png" % d if os.path.isfile(foutsimple): return # do nothing latex(res) curr_dir = os.path.dirname(lisp.buffer_file_name()) fout = "%s/_preview/f-%d.png" % (curr_dir,d) shutil.copy(dir + "\standalone.png",fout) remem = lisp.point() lisp.goto_char(e) lisp.forward_line(1) lisp.beginning_of_line(); bb = lisp.point() lisp.end_of_line(); ee = lisp.point() lisp.delete_region(bb,ee) lisp.insert("%{{" + foutsimple + "}}") lisp.goto_char(remem) lisp.iimage_mode(1)
def show(): b, e, res = get_block_content("$$", "$$") d = hash(res) foutsimple = "_preview/f-%d.png" % d if os.path.isfile(foutsimple): return # do nothing latex(res) curr_dir = os.path.dirname(lisp.buffer_file_name()) fout = "%s/_preview/f-%d.png" % (curr_dir, d) shutil.copy(dir + "\standalone.png", fout) remem = lisp.point() lisp.goto_char(e) lisp.forward_line(1) lisp.beginning_of_line() bb = lisp.point() lisp.end_of_line() ee = lisp.point() lisp.delete_region(bb, ee) lisp.insert("%{{" + foutsimple + "}}") lisp.goto_char(remem) lisp.iimage_mode(1)
def find_comment(self): """\ Find and return the limits of the block of comments following or enclosing the cursor, or return an error if the cursor is not within such a block of comments. Extend it as far as possible in both directions. """ let = Let().push_excursion() try: # Find the start of the current or immediately following comment. lisp.beginning_of_line() lisp.skip_chars_forward(' \t\n') lisp.beginning_of_line() if not language_matcher[0](self.remainder_of_line()): temp = lisp.point() if not lisp.re_search_forward('\\*/', None, lisp.t): lisp.error("outside any comment block") lisp.re_search_backward('/\\*') if lisp.point() > temp: lisp.error("outside any comment block") temp = lisp.point() lisp.beginning_of_line() lisp.skip_chars_forward(' \t') if lisp.point() != temp: lisp.error("text before start of comment") lisp.beginning_of_line() start = lisp.point() language = guess_language(self.remainder_of_line()) # Find the end of this comment. if language == 2: lisp.search_forward('*/') if not lisp.looking_at('[ \t]*$'): lisp.error("text after end of comment") lisp.end_of_line() if lisp.eobp(): lisp.insert('\n') else: lisp.forward_char(1) end = lisp.point() # Try to extend the comment block backwards. lisp.goto_char(start) while not lisp.bobp(): if language == 2: lisp.skip_chars_backward(' \t\n') if not lisp.looking_at('[ \t]*\n[ \t]*/\\*'): break if lisp.point() < 2: break lisp.backward_char(2) if not lisp.looking_at('\\*/'): break lisp.re_search_backward('/\\*') temp = lisp.point() lisp.beginning_of_line() lisp.skip_chars_forward(' \t') if lisp.point() != temp: break lisp.beginning_of_line() else: lisp.previous_line(1) if not language_matcher[language]( self.remainder_of_line()): break start = lisp.point() # Try to extend the comment block forward. lisp.goto_char(end) while language_matcher[language](self.remainder_of_line()): if language == 2: lisp.re_search_forward('[ \t]*/\\*') lisp.re_search_forward('\\*/') if lisp.looking_at('[ \t]*$'): lisp.beginning_of_line() lisp.forward_line(1) end = lisp.point() else: lisp.forward_line(1) end = lisp.point() return start, end finally: let.pops()
def find_comment(self): """\ Find and return the limits of the block of comments following or enclosing the cursor, or return an error if the cursor is not within such a block of comments. Extend it as far as possible in both directions. """ let = Let().push_excursion() try: # Find the start of the current or immediately following comment. lisp.beginning_of_line() lisp.skip_chars_forward(" \t\n") lisp.beginning_of_line() if not language_matcher[0](self.remainder_of_line()): temp = lisp.point() if not lisp.re_search_forward("\\*/", None, lisp.t): lisp.error("outside any comment block") lisp.re_search_backward("/\\*") if lisp.point() > temp: lisp.error("outside any comment block") temp = lisp.point() lisp.beginning_of_line() lisp.skip_chars_forward(" \t") if lisp.point() != temp: lisp.error("text before start of comment") lisp.beginning_of_line() start = lisp.point() language = guess_language(self.remainder_of_line()) # Find the end of this comment. if language == 2: lisp.search_forward("*/") if not lisp.looking_at("[ \t]*$"): lisp.error("text after end of comment") lisp.end_of_line() if lisp.eobp(): lisp.insert("\n") else: lisp.forward_char(1) end = lisp.point() # Try to extend the comment block backwards. lisp.goto_char(start) while not lisp.bobp(): if language == 2: lisp.skip_chars_backward(" \t\n") if not lisp.looking_at("[ \t]*\n[ \t]*/\\*"): break if lisp.point() < 2: break lisp.backward_char(2) if not lisp.looking_at("\\*/"): break lisp.re_search_backward("/\\*") temp = lisp.point() lisp.beginning_of_line() lisp.skip_chars_forward(" \t") if lisp.point() != temp: break lisp.beginning_of_line() else: lisp.previous_line(1) if not language_matcher[language](self.remainder_of_line()): break start = lisp.point() # Try to extend the comment block forward. lisp.goto_char(end) while language_matcher[language](self.remainder_of_line()): if language == 2: lisp.re_search_forward("[ \t]*/\\*") lisp.re_search_forward("\\*/") if lisp.looking_at("[ \t]*$"): lisp.beginning_of_line() lisp.forward_line(1) end = lisp.point() else: lisp.forward_line(1) end = lisp.point() return start, end finally: let.pops()
def run_py_code(): remember_where = lisp.point() # check if the line contains \inputminted lisp.beginning_of_line() l1 = lisp.point() lisp.end_of_line() l2 = lisp.point() line = lisp.buffer_substring(l1,l2) block_begin,block_end,content = get_block_content("```python","```") #lisp.message(content) # we have code content at this point # scan content to find plt.plot(). if there is, scan buffer # previous to *here* to determine order of _this_ plt.plot(), and # give it an appropiate index that will be appended to the end of # the .png image file, i.e. [buffer name]_[index].png. plt.plot() # commands will be replaced by the corresponding plt.savefig # command. # generate savefig for execution code (no output in emacs yet) bc = get_buffer_content_prev(block_begin) plt_count_before = len(re.findall('plt\.savefig\(',bc)) base = os.path.splitext(lisp.buffer_name())[0] f = '%s_%s.png' % (base, two_digit(plt_count_before+1)) rpl = "plt.savefig('%s')" % f show_replaced = True if "plt.show()" in content else False content=content.replace("plt.show()",rpl) include_graphics_command = "![](%s)" % f (kc,kernel,ip) = get_kernel_pointer(lisp.buffer_name()) start = time.time() res = '' with capture_output() as io: ip.run_cell(content) res = io.stdout if kernel.shell.last_known_outflag: etype, value, tb = kernel.shell._get_exc_info() res = str(etype) + " " + str(value) + "\n" elapsed = (time.time() - start) # replace this unnecessary message so output becomes blank if res and len(res) > 0: # if result not empty res = res.replace("Populating the interactive namespace from numpy and matplotlib\n","") display_results(block_end, res) # display it else: display_results(block_end, "") lisp.goto_char(block_end) # generate includegraphics command if show_replaced: lisp.forward_line(2) # skip over end verbatim, leave one line emtpy lisp.insert(include_graphics_command + '\n') #lisp.backward_line_nomark(1) # skip over end verbatim, leave one line emtpy lisp.scroll_up(1) # skip over end verbatim, leave one line emtpy lisp.goto_char(remember_where) lisp.replace_string("plt.show()",rpl,None,block_begin,block_end) lisp.goto_char(remember_where) if "plt.savefig" in content: lisp.preview_buffer() lisp.message("Ran in " + str(elapsed) + " seconds")
def run_py_code(): remember_where = lisp.point() # check if the line contains \inputminted lisp.beginning_of_line() l1 = lisp.point() lisp.end_of_line() l2 = lisp.point() line = lisp.buffer_substring(l1, l2) # if code comes from file if "\\inputminted" in line: block_begin = lisp.point() lisp.message(line) py_file = re.search("\{python\}\{(.*?)\}", line).groups(1)[0] # get code content from file curr_dir = os.path.dirname(lisp.buffer_file_name()) content = open(curr_dir + "/" + py_file).read() block_end = l2 # end of block happens to be end of include file line lisp.goto_char(remember_where) else: # get code content from latex block_begin, block_end, content = get_block_content( "\\begin{minted}", "\\end{minted}") # we have code content at this point # scan content to find plt.plot(). if there is, scan buffer # previous to *here* to determine order of _this_ plt.plot(), and # give it an appropiate index that will be appended to the end of # the .png image file, i.e. [buffer name]_[index].png. plt.plot() # commands will be replaced by the corresponding plt.savefig # command. # generate savefig for execution code (no output in emacs yet) bc = lisp.buffer_string() plt_count_before = len(re.findall('plt\.savefig\(', bc)) base = os.path.splitext(lisp.buffer_name())[0] f = '%s_%s.png' % (base, two_digit(plt_count_before + 1)) rpl = "plt.savefig('%s')" % f show_replaced = True if "plt.show()" in content else False content = content.replace("plt.show()", rpl) content = "plt.figure()\n" + content include_graphics_command = "\\includegraphics[height=6cm]{%s}" % f #(ip) = get_kernel_pointer(lisp.buffer_name()) start = time.time() with capture_output() as io: res_code = get_ip().run_cell(content) res = io.stdout elapsed = (time.time() - start) display_results(block_end, res) # display it # generate includegraphics command if show_replaced: lisp.forward_line(2) # skip over end verbatim, leave one line emtpy lisp.insert(include_graphics_command + '\n') lisp.scroll_up(1) # skip over end verbatim, leave one line emtpy lisp.goto_char(remember_where) lisp.replace_string("plt.show()", rpl, None, block_begin, block_end) lisp.goto_char(remember_where) if "plt.savefig" in content: lisp.preview_buffer() lisp.message("Ran in " + str(elapsed) + " seconds")
def run_py_code(): remember_where = lisp.point() # check if the line contains \inputminted lisp.beginning_of_line() l1 = lisp.point() lisp.end_of_line() l2 = lisp.point() line = lisp.buffer_substring(l1, l2) # if code comes from file if "\\inputminted" in line: lisp.message(line) py_file = re.search("\{python\}\{(.*?)\}", line).groups(1)[0] # get code content from file curr_dir = os.path.dirname(lisp.buffer_file_name()) content = open(curr_dir + "/" + py_file).read() block_end = l2 # end of block happens to be end of include file line lisp.goto_char(remember_where) else: # get code content from latex block_begin, block_end, content = get_block_content( "\\begin{minted}", "\\end{minted}") #lisp.message(content) # we have code content at this point # scan content to find plt.plot(). if there is, scan buffer # previous to *here* to determine order of _this_ plt.plot(), and # give it an appropiate index that will be appended to the end of # the .png image file, i.e. [buffer name]_[index].png. plt.plot() # commands will be replaced by the corresponding plt.savefig # command. # generate savefig for execution code (no output in emacs yet) bc = get_buffer_content_prev(block_begin) plt_count_before = len(re.findall('plt\.savefig\(', bc)) base = os.path.splitext(lisp.buffer_name())[0] f = '%s_%s.png' % (base, two_digit(plt_count_before + 1)) rpl = "plt.savefig('%s')" % f show_replaced = True if "plt.show()" in content else False content = content.replace("plt.show()", rpl) include_graphics_command = "\\includegraphics[height=6cm]{%s}" % f (kc, kernel, ip) = get_kernel_pointer(lisp.buffer_name()) start = time.time() res = '' with capture_output() as io: ip.run_cell(content) res = io.stdout if kernel.shell.last_known_outflag: etype, value, tb = kernel.shell._get_exc_info() res = str(etype) + " " + str(value) + "\n" elapsed = (time.time() - start) # replace this unnecessary message so output becomes blank if res and len(res) > 0: # if result not empty res = res.replace( "Populating the interactive namespace from numpy and matplotlib\n", "") display_results(block_end, res) # display it else: display_results(block_end, "") lisp.goto_char(block_end) # generate includegraphics command if show_replaced: lisp.forward_line(2) # skip over end verbatim, leave one line emtpy lisp.insert(include_graphics_command + '\n') lisp.backward_line_nomark( 1) # skip over end verbatim, leave one line emtpy lisp.goto_char(remember_where) lisp.replace_string("plt.show()", rpl, None, block_begin, block_end) lisp.goto_char(remember_where) if "plt.savefig" in content: lisp.preview_buffer() lisp.message("Ran in " + str(elapsed) + " seconds")
def run_py_code(): remember_where = lisp.point() # check if the line contains \inputminted lisp.beginning_of_line() l1 = lisp.point() lisp.end_of_line() l2 = lisp.point() line = lisp.buffer_substring(l1,l2) # if code comes from file if "\\inputminted" in line: block_begin = lisp.point() lisp.message(line) py_file = re.search("\{python\}\{(.*?)\}", line).groups(1)[0] # get code content from file curr_dir = os.path.dirname(lisp.buffer_file_name()) content = open(curr_dir + "/" + py_file).read() block_end = l2 # end of block happens to be end of include file line lisp.goto_char(remember_where) else: # get code content from latex block_begin,block_end,content = get_block_content("\\begin{minted}","\\end{minted}") # we have code content at this point # scan content to find plt.plot(). if there is, scan buffer # previous to *here* to determine order of _this_ plt.plot(), and # give it an appropiate index that will be appended to the end of # the .png image file, i.e. [buffer name]_[index].png. plt.plot() # commands will be replaced by the corresponding plt.savefig # command. # generate savefig for execution code (no output in emacs yet) bc = get_buffer_content_prev(block_begin) plt_count_before = len(re.findall('plt\.savefig\(',bc)) base = os.path.splitext(lisp.buffer_name())[0] f = '%s_%s.png' % (base, two_digit(plt_count_before+1)) rpl = "plt.hold(False)\nplt.savefig('%s')\nplt.hold(False)" % f show_replaced = True if "plt.show()" in content else False content=content.replace("plt.show()",rpl) include_graphics_command = "\\includegraphics[height=6cm]{%s}" % f #(ip) = get_kernel_pointer(lisp.buffer_name()) start = time.time() with capture_output() as io: res_code = ip.run_cell(content) res = io.stdout elapsed = (time.time() - start) display_results(block_end, res) # display it # generate includegraphics command if show_replaced: lisp.forward_line(2) # skip over end verbatim, leave one line emtpy lisp.insert(include_graphics_command + '\n') lisp.scroll_up(1) # skip over end verbatim, leave one line emtpy lisp.goto_char(remember_where) lisp.replace_string("plt.show()",rpl,None,block_begin,block_end) lisp.goto_char(remember_where) if "plt.savefig" in content: lisp.preview_buffer() lisp.message("Ran in " + str(elapsed) + " seconds")