Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
editmenu_simple.py
import ui
import clipboard, editor
def select_action(self):
i=editor.get_selection()
editor.set_selection(i[0],i[1]+1)
def copy_action(sender):
i=editor.get_selection()
t=editor.get_text()
clipboard.set(t[i[0]:i[1]])
def paste_action(sender):
i=editor.get_selection()
t=editor.get_text()
editor.replace_text(i[0],i[1], clipboard.get())
editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text()))
def cut_action(sender):
i=editor.get_selection()
t=editor.get_text()
clipboard.set(t[i[0]:i[1]])
editor.replace_text(i[0],i[1], '')
editor.set_selection(i[0],i[0])
def comment_action(sender):
"""" comment out selected lines"""
import re
COMMENT='#'
i=editor.get_line_selection()
t=editor.get_text()
# replace every occurance of newline with ewline plus COMMENT, except last newline
editor.replace_text(i[0],i[1]-1,COMMENT+re.sub(r'\n',r'\n'+COMMENT,t[i[0]:i[1]-1]))
editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text()))
def uncomment_action(self):
"""" uncomment selected lines"""
import re
COMMENT='#'
i=editor.get_line_selection()
t=editor.get_text()
# replace every occurance of newline # with newline, except last newline
if all( [x.startswith('#') for x in t[i[0]:i[1]-1].split(r'\n')]):
editor.replace_text(i[0],i[1]-1,re.sub(r'^'+COMMENT,r'',t[i[0]:i[1]-1],flags=re.MULTILINE))
editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text()))
def execlines_action(self):
"""execute selected lines in console. """
import textwrap
a=editor.get_text()[editor.get_line_selection()[0]:editor.get_line_selection()[1]]
exec(textwrap.dedent(a))
v = ui.load_view()
v.present('panel')
[
{
"class" : "View",
"attributes" : {
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
"enabled" : true,
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
"flex" : ""
},
"frame" : "{{0, 0}, {159, 546}}",
"selected" : false,
"nodes" : [
{
"class" : "Button",
"attributes" : {
"font_size" : 20,
"uuid" : "64CC98A5-428B-40E6-A953-4BFE590FB702",
"name" : "button1",
"action" : "select_action",
"class" : "Button",
"frame" : "{{40, 257}, {80, 32}}",
"title" : "select"
},
"frame" : "{{34, 21}, {92, 46}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Button",
"attributes" : {
"font_size" : 20,
"uuid" : "0DF06111-4581-4556-8168-7C5953E8CE2D",
"name" : "button2",
"action" : "copy_action",
"class" : "Button",
"frame" : "{{40, 257}, {80, 32}}",
"title" : "copy"
},
"frame" : "{{34, 97}, {92, 42}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Button",
"attributes" : {
"font_size" : 20,
"uuid" : "B8362B05-6E7D-488B-9145-9D0670916B31",
"name" : "button4",
"action" : "paste_action",
"class" : "Button",
"frame" : "{{40, 257}, {80, 32}}",
"title" : "paste"
},
"frame" : "{{34, 264}, {92, 49}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Button",
"attributes" : {
"font_size" : 20,
"uuid" : "C7F21E13-4649-4081-A08F-EE2F09C9E389",
"name" : "button3",
"action" : "cut_action",
"class" : "Button",
"frame" : "{{40, 257}, {80, 32}}",
"title" : "cut"
},
"frame" : "{{34, 167}, {92, 51}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Button",
"attributes" : {
"font_size" : 20,
"uuid" : "63485672-58D1-4A75-8501-92254A7D1077",
"name" : "button5",
"action" : "comment_action",
"class" : "Button",
"frame" : "{{40, 257}, {80, 32}}",
"title" : "comment"
},
"frame" : "{{34, 333}, {92, 54}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Button",
"attributes" : {
"font_size" : 20,
"uuid" : "5A933710-D542-4D04-A819-08277984EDF3",
"name" : "button6",
"action" : "uncomment_action",
"class" : "Button",
"frame" : "{{40, 257}, {80, 32}}",
"title" : "uncomment"
},
"frame" : "{{15, 406}, {131, 55}}",
"selected" : false,
"nodes" : [
]
},
{
"class" : "Button",
"attributes" : {
"font_size" : 20,
"uuid" : "DECD9624-2BFC-4DA6-AAD2-EAD0813AE6F2",
"name" : "button7",
"action" : "execlines_action",
"class" : "Button",
"frame" : "{{40, 257}, {80, 32}}",
"title" : "execlines"
},
"frame" : "{{40, 480}, {86, 46}}",
"selected" : true,
"nodes" : [
]
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.