cpl includes a powerful console screen editor. It serves a double purpose as a GNU readline look-alike, providing command completion and command history to cpl interactive input, and as a full-screen editor for program development, equipped with CPL word and syntax completion. Interactive cpl and the editor are actually a single integrated environment (IDE): programs can be run from within the editor, and the cpl and editor screens can be swapped at the touch of a key. Both tools display in a console, which makes for a terse graphical appearance but in exchange allows equally swift use in local and remote terminals, as well as in a terminal-only environment like the Windows subsystem for Linux, and even in a smartphone's ssh client if one feels the need for it.
Bash-like command completion and command history are traditionally provided by the GNU readline library, which supplies the familiar Tab and Up Arrow commands, input logging and more. The purpose of readline is to allow the user to edit each command line before committing it, and to keep a history of previous commands for rapid reuse. cpl readline accepts cursor-movement and other commands in one of three equivalent ways: dedicated editing keys, vi-like single-letter commands, and the mouse. Single-letter command mode is toggled in and out of by the Esc key. Full-screen mode is toggled in and out of by F1 or Esc `. Any cursor movement while Shift is pressed (or mouse movement with left button pressed) selects text. For single-letter functions which admit an inverse (such as undo - redo or search forwards - search backwards), the corresponding capital letter or the right mouse button selects its inverse.
With the exception of those control characters that are captured by the console (such as Ctrl-i, which is Tab, and Ctrl-h which is Backspace), Ctrl-<key> also works as an alternative to Esc followed by the same lower-case letter. As a further alternative, Alt-<key> in most terminals sends the sequence Esc <key>, and thus produces the same effect as switching command mode on and then pressing <key>. On macOS you may have to set Use Option as Meta key if you want the Option key to serve this purpose. See support.apple.com/en-bn/guide/terminal/trmlkbrd/mac
If full-screen mode is toggled on with no other file opened, the command history shows up in the full-screen editor, and the Enter key is now entered as a regular newline. On return to readline mode (when F1 or Esc ` is pressed again), the modified command history is replayed through interactive cpl as if this list of commands had been given afresh when it was launched. Other editing keys work just as in readline mode, except that history lines are now edited in place.
Most used editing keys:
- Left (Esc h), right arrow (Esc l): previous, next char
- Home (Esc ^), end (Esc $): beginning, end of line
- Ctrl-Home (Ctrl-^), Ctrl-End (Ctrl-$, Esc A): beginning, end of file
- Backspace: erase left
- Delete (Esc d): erase right
- Shift-delete (Esc D): erase line
- Up (Esc j), down arrow (Esc k): previous, next command in history. If a
partial line is entered, only lines containing it become visible. Previous
commands are immutable; when a character is added or deleted in a previous
command, this is copied to the most recent position before being modified.
- Tab: autocomplete keyword or identifier or filename. Successive tabs move
forward along the list of available completions; Shift-Tab moves backwards.
Shift-Tab without a prior Tab displays a list of all available completions.
- Enter: submit line.
- F1, Esc `: switch to full-screen mode.
- Esc u, Esc U: Undo, respectively redo, the last modification. (Undo)
- Esc \, Ctrl-\: add next (special or control) character verbatim
The full-screen editor is accessed by pressing F1 or Esc ` or by the edit command in interactive cpl, as cpl edit in the shell, or also as edit in the shell if no other edit command takes precedence.
Most used editing keys:
- Left (Esc h), right arrow (Esc l): previous, next char
- Home (Esc ^), End (Esc $): beginning, end of line or of selection
- Ctrl-Home (Ctrl-^), Ctrl-End (Ctrl-$, Esc A): beginning, end of file
- Backspace: erase left; Delete (Esc d): erase at cursor
- Shift-Delete (Esc D): erase line
- Insert (Esc i, Esc v, Ctrl-v): insert selection, see Select.
- Up arrow (Esc j), down arrow (Esc k): previous, next line.
- PgUp (Esc p), PgDn (Esc n): move to prev or next same indentation.
- Esc f (Ctrl-f, Esc /), Esc F (Esc ?): Find forwards, respectively backwards.
- Tab: autocomplete keyword or identifier or filename. Successive tabs move
forward along the list of available completions; Shift-Tab moves backwards.
Shift-Tab without a prior Tab displays a list of all available completions.
- Enter: Enter new line, and trigger possible autocompletion of CPL syntax
- Esc =: Autoindent current line or selection (Indent and complete)
- F1, Esc `: switch in and out of command-line mode.
- Esc u, Esc U: Undo, respectively redo, the last modification. (Undo)
- Esc \, Ctrl-\: add next (special or control) character verbatim.
Selection is achieved by Shift-arrow keys (Select), or by moving the mouse with its left button pressed. The center mouse button copies the current selection if the mouse is moved to a new position, or deletes it if the mouse sits on it. The right mouse button moves (cuts and pastes) the current selection.
Clicking the mouse on the bottom left corner, or pressing Esc followed by any unassigned key (say, Esc Space), opens up a menu of editor commands. Clicking the mouse on the bottom right corner opens context-sensitive help (through the info program), about the word where the cursor is, or this info page if the cursor is outside of any word.
The full-screen editor can work on multiple screens, in addition to the line-edited console. Simultaneously open buffers are rotated into view by clicking the mouse menu, which shows them as tabs, or by pressing Esc <digit>. Esc 0 or Esc ` or F1 brings up the console. Pressing the console key again toggles back to the previous screen. Loading and saving files in the current or in a new screen is described in Load and save.
run (Esc r or Esc R or Ctrl-r or F2) runs the current screen in the most sensible of several possible modes, after saving all unsaved screens so that possibly USEd files are up to date. If the current screen is the interactive console, run repeats the whole session from its beginning; if it is the full-screen editor, depending on the edited file's suffix, it either runs the current .cpl program (in compiled mode for lower-case r or clicking with the left button, and in interpreted mode for upper case R or clicking with the right button), or it compiles the current .tex file using pdflatex, or it runs the current .gnu file under gnuplot. If a console command was run previously in the same session, however, run's behaviour changes: the file suffix is disregarded and the last console command is run instead. This allows one to define a custom compilation command, for whatever file type is being edited, by entering it once in the console, and to repeat it at will when run is pressed afterwards.
options (Esc o) opens a menu of editor options.
Most prompts and menus can be aborted by pressing Esc again.
Selection of a text area is achieved either by keeping the left mouse button pressed while sweeping, or by pressing shift together with arrow keys. Selection can apply to the text enclosed between the starting and ending selected positions or to a rectangular tile with the given corners (a.k.a. selection by columns). Esc t toggles in and out of tile mode. Esc w or double-clicking the mouse selects the word under the cursor; if applied to an opening or closing bracket, Esc w or double clicking selects the entire parenthesis. Esc # or triple clicking selects the line under the cursor. Esc s, or clicking save, when some text is selected saves the current selection rather than the entire file.
When some text is selected, the character count of the selection is displayed in the status line in place of the current line number. At the same time the Home and End keys, or their equivalent command letters, lead to the begining and end of the selection.
Whenever text is selected, the Insert key (or Esc i, or Esc v, or Ctrl-v) inserts (pastes) the selected text at the current cursor position; no copy operation is needed as buffer coincides with selection. The Backspace and Delete keys (also aliased to Esc d, Esc x, Ctrl-x) erase the selected text if pressed immediately after a selection. At any other time these keys erase a single character, the preceding one for Backspace. Deleted text remains buffered and can be re-inserted after its deletion. Typing a new character, or pressing Insert, immediately after a selection without any intervening cursor movement replaces the current selection. In this case the function of Insert changes to inserting the butlast selection. Esc m moves (cuts and pastes) the butlast onto the last selection. Pressing the middle mouse button achieves the same purpose as Esc i; the right mouse button the same purpose as Esc m.
The CPL editor features a multiple-selection buffer: right after an insertion pressing Esc i, or Insert, again replaces the current selection by a previous one. Pressing Esc i or Insert multiple times cycles through the last three selections. Esc v and Ctrl-v behave classically and only insert one selection, allowing you to repeat the same insertion more than once; the same effect can be gotten by exiting Esc mode or moving the cursor between two Insert actions.
In the way of example, in order to:
delete: select, delete;
overtype: select, type;
copy and paste: select, move cursor, insert;
cut and paste: select, delete, move cursor, insert;
copy and replace: select, move, select, insert;
cut and replace: select, delete, move, select, insert.
type again: type something, move, insert;
type and replace: type something, move, select, insert.
insert the former selection: select, move, select, move, insert, insert.
If so many rules look complicated, just remember that you can recall and insert anything that you have recently selected, written or deleted; just try pressing Insert, and if what you see is not what you want, press Insert again.
The copy-and-paste functionality of the underlying xterm-compatible terminal is also available, and necessary if you want to copy some text to/from other windows. This is accessed by pressing Shift together with the corresponding mouse button (left button to select, center button to paste). Shift-Insert also pastes from the clipboard at the current cursor position. Notice that when the editor is run remotely, involved is the one of the two clipboards that resides on the machine where the terminal is running, which probably is what you want anyway. In addition, if the xsel program is installed, Esc ' copies the current selection to the clipboard (on the remote machine, unfortunately, when not the same; for those who know, this feature to be done properly would require OSC 52 support; writing only would be enough).
Esc !, respectively Esc @, turns the current selection to all uppercase or to all lowercase letters.
A file is opened in a new screen with the edit iteractive cpl command (also available in the menu, or as Esc e, or as the edit symbolic link from a unix shell), and closed with the close menu command (aka Esc c or Ctrl-c or Esc q or Ctrl-q). Capitalized Edit, or Esc E, or right-clicking the edit menu button, opens the file to be edited in a separate terminal window. The optional +<linenumber> parameter at the end of the edit or Edit commands opens the file at the given line number. The bottom status line contains the name of the currently displayed file and the current line number. A star trailing the file name denotes whether the in-memory buffer has been modified since last saved (and warns that changes would be lost if not saved again). Closing an unsaved file elicits a prompt asking for confirmation before exit.
load and save commands (Esc a and Esc s) respectively load (insert) a file at the current cursor position and save the current in-memory buffer to a file, with the original file name as a default. Ctrl-s immediately saves the in-memory buffer to its original file name. In case some text was just selected, Esc s instead opens a save selection as prompt which saves the current selection to a file.
A local cpl editor can access files on remote servers supporting the ssh protocol. edit, load and save accept extended filenames prefixed with the name of the server separated by a colon, as in my.server.com:example.cpl, and transparently connect through ssh as required. The connection is stateless and closed again after each operation, and can thus survive temporary network interruptions. Tab completion and Shift-Tab listing are available on remote filenames.
Authentication is managed by ssh, and for smoother operation should be set up as passwordless. A user name, if not implied, can be prefixed, separated by @ as in user@my.server.com:example.cpl. If a password is required it will be asked for during edit, load or save, but background services like filename completion and timestamping will only be unavailable on remote servers with passwordless authentication.
When the filename entered after either edit, load or save does not contain a pathname or begins with ../, the PWD is assumed to be the directory where the file currently on screen resides, and not the PWD of the underlying terminal. This is so that files in the same directory as the current file (or in its parent directory) can be rapidly accessed even on remote servers.
When a .cpl file is being edited, pressing Enter at the end of a freshly modified line automatically:
1) indents the new line according to the contents of the previous line;
2) if the line is the opening of a new program block, like a SUBROUTINE, LOOP or IF statement, it inserts the corresponding closing statement and the appropriate indentation for the block's contents;
3) it converts to upper case some CPL keywords when they are recognized as having been entered in lower case;
4) open brackets are automatically provided with a matching closing bracket; typing of the closing bracket is optional, and silently ignored if brackets match;
5) since CPL allows the usage of the three kinds of brackets like in mathematics ((cpl)Parentheses), the editor automatically alternates the three kinds even if only one kind is typed in.
Where needed, the following explicit indentation commands are available:
Esc ) : indent the current selection one more space to the right,
Esc ( : unindent the current selection by one space to the left,
Esc = : even the margin of the current selection, or indent as to CPL syntax.
In addition, word completion using the Tab key is available in full-screen just as well as in readline mode. Successive tabs move forward along the list of available completions; Shift-Tab moves backwards. Shift-Tab without a prior Tab displays a list of all available completions. A hint is shown under the cursor at the end of the current line, foreshowing in faint type the completion that pressing Tab would currently produce.
Parentheses and quote completion also operates, automatically adding closed parentheses and quotes at the time they are opened.
find (Esc f or Ctrl-f or Esc /) and goto (Esc g or Esc +) seek the cursor to a given string or to a given line number. Esc F (or Esc ?, or clicking find with the right mouse button) seeks backwards. The search is caseinsensitive if the first character of the search string is a lowercase letter, case-sensitive otherwise. The found string is automatically selected.
Esc , (lowercase <) repeats the last find backwards. Esc . (lowercase >) repeats the last find forwards. Finding with an empty argument also repeats the last find, possibly changing its direction. If some text is selected when one of this keys is pressed, the selected text will be searched.
In order to find and replace, find a given text, exit Esc mode, and start typing the replacement. Just typed text automatically goes to the insertion buffer; therefore after you search again, pressing Insert will insert the same replacement. Esc > is a shortcut for pressing Insert and Esc . one after the other, and thus serves as replace-and-find-again. Keeping Esc > pressed is a means to perform multiple replacements, possibly to the end of file. Esc < likewise replaces-and-finds backwards.
Esc Enter repeats the most recent command, for instance the last (forward or backward) find or the last replace-and-find-again.
Unlimited undo and redo functionality is supported, activated by Esc u and Esc U respectively (or Esc z and Esc Z as alternates, or Ctrl-z and Ctrl-y). As a distinctive feature of this editor (shared by emacs, with some distinctions, but almost no others), memory of the undone edits is retained even when further edits take place after an undo.
A standard editor allows the following pitfall to occur: you undo several times with the purpose of inspecting how your text was at an earlier stage, but with the intention to return to the present, and then before redo-ing, you accidentally hit a key (for example, redo is Ctrl-y and you hit y without Ctrl); now your redo buffer is gone forever, and all you typed in the meanwhile is gone with it.
Emacs and its kin keep memory of the complete history of commands including undo's. You do not redo in emacs, you undo an undo. You never lose anything, if no cap is posed on the length of the history, but once you start to undo an undo an undo the interface may become confusing, and the distance to any given past command increases rapidly.
In the CPL editor you can think of undo and redo commands as not changing anything, just scrolling backwards and forwards along your past edits. It is when you start typing again (or save) that actual changes take place, and they are recorded with no loss. If you redo all the way to the present, nothing is recorded and you have just inspected your previous history. If you start typing after moving back to a point in history, the undo buffer starts filling up again in such a manner that the next time you undo you will roll back the time series of actual changes, insertions and deletions, none excluded, but without seeing your previous undo's.
Multi-line motion commands (PgUp, PgDn, Ctrl-Home, Ctrl-End, find and go to line) are also saved in the undo buffer and can be undone using Esc b (back), with Esc B for redo; however, to keep the buffer from becoming unmanageable, only the sequence of movements that follows the latest text alteration is remembered. For the same purpose the editing of adjacent characters is automatically merged together in a single undo operation, and characters erased right after being written are not remembered at all. Also, past undo sequences are only remembered once, and are not reinserted into the buffer when undone again. The buffer's lifespan coincides with an editing session; while the whole history of the session is available at all times during the editing of a file, no history is preserved after the file is closed.
options (Esc o) opens a submenu of editor options. Lines can be selected by up and down arrows or by the mouse, and activated/deactivated by pressing y or left arrow for yes, or n or right arrow for no, or toggled by the space bar. Enter exits the menu. As a quick shortcut, the initial of each option toggles it and exits the menu at once.
The show bytes option toggles hex editor mode. In this mode individual bytes are displayed (non-printing ones as □), instead of being interpreted as UTF-8 Unicode, and at the same time a sliding window shows the hex representation of the line under the cursor. Typing in hexadecimal digits modifies the byte under the cursor. Space adds a new byte. Other alphanumeric keys are inactive.
The cpl editor is 8-bit safe, and correctly aligns UTF-8 Unicode characters displayed by the underlying console. Double-width Unicode characters are handled as well. The hex representation of a binary file can be displayed, if desired, by turning on hex editor mode.