Files
gnatstudio/examples/python/misc_text_utils.py

83 lines
1.8 KiB
Python
Raw Permalink Normal View History

"""miscellaneous text utilities, on top of text_utils, used by other plug-ins
"""
import GPS
import text_utils
# inserts text at current position in current file with a newline at the end
def insert_line(text):
GPS.Editor.insert_text("\n" + text)
2024-06-05 14:41:24 +04:00
# end insert_line
# replaces current line in current_file with specified new line
def replace_line(current_file, new_line):
line_num = GPS.Editor.cursor_get_line(current_file)
line = GPS.Editor.get_chars(current_file, line_num, 0)
2024-06-05 14:41:24 +04:00
if line[len(line) - 1] == "\n":
GPS.Editor.replace_text(current_file, line_num, 0, new_line, 0, len(line) - 1)
else:
2024-06-05 14:41:24 +04:00
GPS.Editor.replace_text(current_file, line_num, 0, new_line, 0, len(line))
# end if
2024-06-05 14:41:24 +04:00
# end replace_line
# get current line from current file
def get_line():
file = GPS.current_context().file().name()
line_num = GPS.current_context().location().line()
str = GPS.Editor.get_chars(file, line_num, 0)
return str[:-1] # omit the '\n'
2024-06-05 14:41:24 +04:00
# end get_line
# move up 'count' lines in the current file
def up(count=1):
file = GPS.current_context().file().name()
line = GPS.current_context().location().line()
GPS.Editor.cursor_set_position(file, line - count)
2024-06-05 14:41:24 +04:00
# end up
# move down 'count' lines in the current file
def down(count=1):
text_utils.next_line(count)
2024-06-05 14:41:24 +04:00
# end down
# attempt to move up 'count' lines in the current file, returning
# success/failure indication
def attempt_up(count=1):
line = GPS.current_context().location().line()
if line - count > 0:
file = GPS.current_context().file().name()
GPS.Editor.cursor_set_position(file, line - count)
return True
else:
return False
# end if
2024-06-05 14:41:24 +04:00
# end attempt_up
# return 'width' blanks
def blanks(width):
2024-06-05 14:41:24 +04:00
return " " * width
# end blanks