Пример #1
0
def new_from_buffer(buffer, language):
    """Pastes the contents of buffer"""
    lisp.set_buffer(buffer)
    code = unicode(lisp.buffer_string())
    #code = lisp.buffer_substring(lisp.point_min(), lisp.point_max())
    filename = lisp.buffer_file_name()
    new_paste(code, language, filename=filename)
Пример #2
0
def new_from_buffer(buffer, language):
    """Pastes the contents of buffer"""
    lisp.set_buffer(buffer)
    code = unicode(lisp.buffer_string())
    #code = lisp.buffer_substring(lisp.point_min(), lisp.point_max())
    filename = lisp.buffer_file_name()
    new_paste(code, language, filename=filename)
Пример #3
0
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")
Пример #4
0
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")
Пример #5
0
 def get_text(self):
     end = lisp.buffer_size() + 1
     old_min = lisp.point_min()
     old_max = lisp.point_max()
     narrowed = (old_min != 1 or old_max != end)
     if narrowed:
         lisp.narrow_to_region(1, lisp.buffer_size() + 1)
     try:
         return lisp.buffer_string()
     finally:
         if narrowed:
             lisp.narrow_to_region(old_min, old_max)
Пример #6
0
 def get_text(self):
     end = lisp.buffer_size() + 1
     old_min = lisp.point_min()
     old_max = lisp.point_max()
     narrowed = (old_min != 1 or old_max != end)
     if narrowed:
         lisp.narrow_to_region(1, lisp.buffer_size() + 1)
     try:
         return lisp.buffer_string()
     finally:
         if narrowed:
             lisp.narrow_to_region(old_min, old_max)
Пример #7
0
 def _get_text(self):
     if not lisp.buffer_modified_p():
         return self._get_resource().read()
     end = lisp.buffer_size() + 1
     old_min = lisp.point_min()
     old_max = lisp.point_max()
     narrowed = (old_min != 1 or old_max != end)
     if narrowed:
         lisp.narrow_to_region(1, lisp.buffer_size() + 1)
     try:
         return lisp.buffer_string()
     finally:
         if narrowed:
             lisp.narrow_to_region(old_min, old_max)
Пример #8
0
def update_state():
    """
    Updates the current state by looking for included files and parsing them
    """
    lisp.message("Loading... this might take a while.")
    this_text = lisp.buffer_string()
    cwd = os.path.dirname(lisp.buffer_file_name())
    includes = []
    j = cwd
    while j!="/":
        p = os.path.join(j,".lemacs")
        if os.path.isfile(p):
            includes+=parse_lemacs(p)['include']
        j = os.path.join(os.path.split(j)[:-1])[0]
    cpp.parse_from_includes(this_text,cwd = cwd,inclDIRS = includes)
    lisp.message("Loaded auto-completion data!")
Пример #9
0
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.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")
Пример #10
0
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")