Home

Awesome

Micropython-Editor

Description

A small text editor written in Python running on

It allows allows to edit files locally on the boards. The offspring was the editor widget of pfalcon at https://github.com/pfalcon/pyedit. I ported it to PyBoard and the other modules and added a lot of functions:

The editor assumes a VT100 terminal. It works in Insert mode. The following list shows most of the commands.:

Key(s)Function
Up Down Left RightCursor movement by one line or char
Ctrl-LeftMove the cursor left to the start of the (next) word
Ctrl-RightMove the cursor right behind the end of the (next) word
Shift-Up -Down -Left -RightHighlight the text or extend the highlighted area
Ctrl-Shift-Left -RightHighlight the next or previous word or extend the highlighted area
Ctrl-Up Ctr-DownScroll the windows down/up
Alt-Up Alt-DownMove the current line or highlighted area up/down by one line
Alt-Left Alt-RightMove the character under the cursor left/right
PgUp & PgDdPage up/down
HomeToggle the position between the start-of-code and the start of line
EndToggle the position between the end-of-the-code and end-of-line
EnterEnter a line break at the cursor position. Auto-indent is supported
BackspaceDelete char left to the cursor (The key must be set to ASCII-Del). If text are highlighted, delete the highlighted area
DelDelete the char under the cursor. At the end of the line join the next line. If autoindent is enabled, delete also the leading spaces of the joined line. If text are highlighted, delete the highlighted area. In line edit mode, Del as first keystroke will clear the entry.
Ctrl-DelDelete the word under the cursor or space up to the next non-space
Shift-DelDelete the actual line
Ctrl-OOpen a new file. If the file name is left empty, an empty buffer is opened
Ctrl-WSwitch to the next file buffer
Ctrl-QClose a file buffer or end line-edit
Ctrl-SSave to file with the option to change the file name. If a new name is given and that file already exists, ask for confirmation.
Ctrl-FFind
Ctrl-NRepeat the last find
Ctrl-H or Ctrl-RFind and Replace
Ctrl-GGo to a line
Ctrl-TGo to the first line
Ctrl-BGo to the last line
Ctrl-KGoto the bracket matching the one under the cursor
Ctrl-Home & Ctr-EndGo to the first/last line
Ctrl-PgUp & Ctrl-PgDdSwitch to the previous/next file
Alt-InsBookmark a location in a file
Alt-PgUp & Alt-PgDnCycle trough bookmarked locations
Alt-Home & Alt-EndCycle trough locations with changes, based on the Undo list
Ctrl-L or Ctrl-SpaceStart highlighting at the current position, or clear the highlight. The highlight can then be extended by moving the cursor
Ctrl-XCut the highlighted text
Ctrl-C or Ctrl-DCopy the highlighted text
Ctrl-VInsert the copied/cut text.
Ctrl-ZUndo the last change(s)
Ctrl-YRedo the last undo(s), repeating what had been undone by undo
Ctrl-PComment/Uncomment a line or highlighted area
Ctrl-AChange settings for tab size, search case sensitivity, auto-indent, comment string and writing tabs (opt)
Ctrl-ERedraw the screen. On the Micro devices it shows the amount of free memory
Mouse support
Mouse scroll wheelMove the cursor up and down, scrolling the content as needed.
Mouse left clickWithin the text: Double click starts highlighting at the current position, or clears the highlight. The click speed does not matter. If the cursor is on a word, the whole word will be highlighted. If not, just the character under the cursor. The highlight can then be extended by moving the cursor clicking at different positions.
Mouse right clickOpens the "find" dialogue in a text file and the "open file" dialogue in a directory listing.
Mouse left clickIn line edit mode: Clicking on a word copies it to the edit line. Clicking a second time at the same place acts as enter.

Instead of Ctrl-letter (e.g. Ctrl-Q), Alt-letter (e.g. Alt-Q) or ESC-letter (e.g. ESC followed by Q) can be used, avoiding conflicts with key binding of some terminal emulators.

The editor is contained in the files pye_gen.py and pye_core.py. Start pye from the REPL prompt e.g. with

from pye import pye
res = pye(object_1, object_2, ..[, tabsize=n][, undo=n])

You may also use the combined version pye.py resp. pye.mpy:

from pye import pye
res = pye(object_1, object_2, ..[, tabsize=n][, undo=n])

If object_n is a string, it's considered as the name of a file to be edited or a directory to be opened. If it’s a file, the content will be loaded, and the name of the file will be returned when pye is closed. If the file does not exist, an error is displayed, but the edit window is given that name. If it’s a directory, the list of file names will be loaded to the edit window. If object_n is a list of other items, they will be converted to strings and edited, and the edited list will be returned as strings. If no object is named, pye() will show the list of files in the current directory, creating a list of strings, unless you save to a file. In that case, the file name will be returned. It is always the last buffer closed, which determines the return value of pye().

Optional named parameters:

tabsize=n     Tab step (integer). The default is 4
undo=n        Size of the undo stack (integer). The minimal size is 4.

The Linux/Darwin version can be called from the command line with:

python3 pye_ux.py [filename(s)]

or using the combined executable version pye:

pye [filename(s)]

Obviously, you may use micropython too.

More details can be found in the doc file. On reading files, tab characters are expanded to spaces with a tab size of 8, and trailing white space on a line will be discarded. Optionally, tabs can be written when saving the file, replacing spaces with tabs when possible. However, the original state of tabs will NOT be restored when the file is written. The screen size is determined, when the editor is started, when the Redraw-key (Ctrl-E) is hit or on any file window change (Ctrl-W).

The editor works also well in a Linux or MAC terminal environment (and also in some terminal apps of Android - tested with Termux) with both python3 and micropython. For that purpose, a small main() section is embedded in pye_ux.py, which when called with CPython also accepts data from a pipe or redirection.

Files

Base files

Derived files

Further front-ends

Branches

BranchFeatures
masterActual main line with slowly changing features.
pye2Similar to the master branch, but the column does not change during vertical moves (stale).
pye_minFeature-reduced version, which is somewhat smaller (stale).

Version History

1.0 Initial release with all the basic functions

1.1 Same function set, but simplified keyboard mapping.

1.2 Mouse support added, as well as some other minor changes.

1.3 UNDO added. Added a multilevel UNDO (Ctrl-Z) for most functions that

change the content. Other changes:

1.4 GET file added. Adding a function GET (Ctrl-O), which inserts the content of a file before the current line. Other changes:

1.5 WiPy Port and body shaping:

1.6 WiPy fixes and further trimming:

1.7 WiPy fixes and other minor changes

1.7a Size reduction for WiPy & Tabbify for PyBoard

1.7b Further size reduction for WiPy

1.7c Re-establish try-except for file-not-found error

1.8 Clean Copy & Paste, Indent, Un-Indent

1.9 Refinement of Highlight and Undo

1.10 Further refinement of Highlight

1.11 Minor fixes

1.12 Bracket Match and Minor changes

1.12b Fixing an inconsistency in the Save command

1.12c Speed up pasting again

1.12d Split undo of Indent/Un-Indent

2.0 Edit multiple files

2.1 Some shrinking for WiPy

2.2 Further cleaning and some slight improvements

2.3 Minor fixes & changes

2.4 Fix for the regular expression search variant

2.5 Fix a small bug of edit_line()'s paste command

2.6 Adapted to change lib names in micropython

2.7 Change file save method and settings dialogue

2.8 Support of UTF-8 characters on keyboard input

2.9 Support for teensy 3.5 and 3.6

2.10 Support for esp32; simplified mouse handling

2.11 Small changes to the esp8266 version

2.12 Use the kbd_intr() method to disable Keyboard Interrupts on Ctrl-C.

This method is supported by (almost) all micropython.org ports and maybe sometime also supported by the pycom.io ports. The method of trying to catch KeyboardInterrupt is still present.

2.13 Reduce the number of derived variants and make the full version the default one. Update the documentation.

2.14 Remove the PyBoard support using an UART as keyboard/display interface. Use kbd_intr() too on Pyboard.

2.15 Make a combined MicroPython version, which runs on all boards except Teensy 3.5 / 3.6. For teensy, a short wrapper is included.

2.16 Optimize search with Regular expressions

2.17 Remove all option variants from the source files, and make pye_sml.py a pure micropython boards version with reduced function set. The only options remaining are Linux/CPython vs. MicroPython

2.18 On deleting the end of a line, remove space characters from the joined line, if autoindent is active. This behavior mirrors autoindent.

2.19 Add a toggle key for commenting/uncommenting a line or highlighted area. The default comment string is '#', but can be changed through the settings command.

2.20 Change the End-Key to toggle between EOL and last non-space char.

2.21 Add Ctrl-\ as alternative key to close a file

2.22 Add Ctrl-Space asl alternative to highlight line (Ctrl-L), and Block comment adds the comment string after leading spaces.

2.23 Change the End key to toggle between end-of-line and end-of-code

2.24 Changed the Paste key in line edit mode, in that it pastes the word under the cursor of the active window.

2.25 Version number is shown with redraw command, and thus at startup and window change

2.26 Better separation of port-specific and OS-specific flags

2.27 Change Home/End key behavior. Py w/o filename will open a window with the list of files in the current dir

2.28 Add word left & right with ctrl-left and ctrl-right

2.29 Add shift-up and shift-down for setting/extending the highlighted area

2.30 Add Delete Word with Ctr-Del

2.31 Re-Map Ctrl-Up and Ctrl-Down for scrolling

2.32 Comment toggle ignores empty lines

2.33 Scroll step is 1 for keyboard and 3 for mouse

2.34 Added a branch with character mode highlight/cut/paste/delete. Intended to be the new master

2.35 Change behavior of the column position during vertical moves, in that it tries to keep the position

2.36 Add the redo function, which restates changes undone by undo.

2.37 Entering text replaces an highlighted area.

2.38 Add Ctrl-Shift-Left and Ctrl-Shift-Right for move & highlight

2.39 Move a line with Alt-Up and Alt-Dn

2.40 Extend move up/down to move highlighted areas too.

2.47 Move all terminal control strings into an list.

2.48 Split pye.py into a core file and port specific front-ends. If a single file is required, just put the core and the front-end into a single file.

2.49 Change the default search item behavior of find and replace as well as open file.

2.50 Limit the span for bracket match to 50 lines and improve the speed

2.51 Re-establish automatic screen redraw on resize for Linux

2.52 Fix regression error on file creation

2.53 Fix regression error on handling directories

2.54 Fix regression error in the Linux variant

2.55 Cope with the change from renaming the sys module to usys

2.56 Fix regression error when opening multiple files

2.57 Slight speed improvement when pasting

2.58 Fix a glitch in redo after undo after pasting a full line.

2.59 Change mouse support. Clicking twice at the same position enables/disables marking.

2.60 Extend mouse support

2.61 Further straighten mouse support. Double Click is click at the cursor position, and the mark can be extended to both sides with the mouse without affecting what was already marked.

2.62 Ask for confirmation before overwriting an existing file other than the one initially opened.

2.63 Change mouse behavior again. Right click opens the "find" dialogue in a text window or "open file" dialogue in a directory listing.

2.64 Enable additional file name chars in the file save dialogue (was omitted)

2.65 Check for invalid key sequence. Show the search item in "not found" messages

2.66 Fix an display error when doing a left click beyond the end of text during line edit

2.67 Make double click in the text more consistent. Mark/Unmark always requires a double click.

2.68 Make double click timed. Time is < 2 seconds

2.69 Minor reorganisation in the function get_input. Firt version of a Window 10 front-end.

2.70 Move character under the cursor left/right with the Alt-Left/Alt-Right key.

2.72 Add delete line and force quit

2.73 Use Ctrl-PgUp and Ctr-PgDn to switch files.

2.74 Add functions to bookmark locations and go to them, using Alt-Home and Alt-PgUp / Alt-PgDn

2.75 Fix an error with character swap.

2.76 Cycle through places with changes.

2.77 Change key binding for bookmark location to Alt-Ins, using Alt-Home and Alt-End to cycle through locations with changes.