php-kilo/src/Editor.php

956 lines
19 KiB
PHP
Raw Normal View History

2019-10-14 16:21:41 -04:00
<?php declare(strict_types=1);
2019-11-08 16:27:08 -05:00
namespace Aviat\Kilo;
use Aviat\Kilo\Enum\{
Key,
Highlight,
};
use Aviat\Kilo\Tokens\PHP;
2019-10-14 16:21:41 -04:00
/**
2019-10-25 16:36:03 -04:00
* // Don't highlight this!
* @property-read int numRows
*/
2019-10-14 16:21:41 -04:00
class Editor {
2019-11-19 15:57:51 -05:00
use Traits\MagicProperties;
private string $ab = '';
2019-10-14 16:21:41 -04:00
protected int $cursorX = 0;
protected int $cursorY = 0;
protected int $renderX = 0;
protected int $rowOffset = 0;
protected int $colOffset = 0;
2019-10-14 16:21:41 -04:00
protected int $screenRows = 0;
protected int $screenCols = 0;
2019-10-15 13:23:25 -04:00
/**
* Array of Row objects
*/
2019-10-25 16:36:03 -04:00
public array $rows = [];
2019-10-15 13:23:25 -04:00
2019-10-25 10:28:15 -04:00
public int $dirty = 0;
public string $filename = '';
protected string $statusMsg = '';
protected int $statusMsgTime;
2019-10-25 10:28:15 -04:00
public ?Syntax $syntax;
// Tokens for highlighting PHP
public array $tokens = [];
public static function new(): Editor
2019-10-15 13:23:25 -04:00
{
return new self();
2019-10-15 13:23:25 -04:00
}
private function __construct()
2019-10-14 16:21:41 -04:00
{
$this->statusMsgTime = time();
2019-10-14 16:21:41 -04:00
[$this->screenRows, $this->screenCols] = get_window_size();
2019-11-19 15:57:51 -05:00
// Remove a row for the status bar, and one for the message bar
$this->screenRows -= 2;
}
public function __get(string $name)
{
if ($name === 'numRows')
{
return count($this->rows);
}
return NULL;
2019-10-14 16:21:41 -04:00
}
2019-11-20 15:03:48 -05:00
public function __debugInfo(): array
{
return [
'colOffset' => $this->colOffset,
'cursorX' => $this->cursorX,
'cursorY' => $this->cursorY,
'dirty' => $this->dirty,
'filename' => $this->filename,
'renderX' => $this->renderX,
'rowOffset' => $this->rowOffset,
'rows' => $this->rows,
'screenCols' => $this->screenCols,
'screenRows' => $this->screenRows,
'statusMsg' => $this->statusMsg,
'syntax' => $this->syntax,
'tokens' => $this->tokens,
];
}
2019-10-14 16:21:41 -04:00
// ------------------------------------------------------------------------
// ! Terminal
// ------------------------------------------------------------------------
protected function readKey(): string
{
$c = read_stdin();
2019-10-14 16:21:41 -04:00
// @TODO Make this more DRY
switch ($c)
2019-10-14 16:21:41 -04:00
{
2019-10-22 12:09:11 -04:00
case "\x7f": return Key::BACKSPACE;
case "\r": return Key::ENTER;
case "\x1b[A": return Key::ARROW_UP;
case "\x1b[B": return Key::ARROW_DOWN;
case "\x1b[C": return Key::ARROW_RIGHT;
case "\x1b[D": return Key::ARROW_LEFT;
2019-10-14 16:21:41 -04:00
case "\x1b[3~": return Key::DEL_KEY;
case "\x1b[5~": return Key::PAGE_UP;
case "\x1b[6~": return Key::PAGE_DOWN;
case "\x1bOH":
case "\x1b[1~":
case "\x1b[7~":
case "\x1b[H":
return Key::HOME_KEY;
2019-10-14 16:21:41 -04:00
case "\x1bOF":
case "\x1b[4~":
case "\x1b[8~":
case "\x1b[F":
return Key::END_KEY;
2019-10-14 16:21:41 -04:00
2019-10-22 12:09:11 -04:00
case "\x1b":
return Key::ESCAPE;
default: return $c;
}
2019-10-14 16:21:41 -04:00
}
2019-10-25 10:28:15 -04:00
protected function selectSyntaxHighlight(): void
{
$this->syntax = NULL;
if (empty($this->filename))
{
return;
}
// In PHP, `strchr` and `strstr` are the same function
$ext = (string)strstr($this->filename, '.');
2019-11-08 21:48:46 -05:00
foreach (get_file_syntax_map() as $syntax)
2019-10-25 10:28:15 -04:00
{
foreach ($syntax->filematch as $searchExt)
2019-10-25 10:28:15 -04:00
{
$is_ext = (strpos($searchExt, '.') === 0);
2019-10-25 10:28:15 -04:00
if (
($is_ext && ( ! strcmp($ext, $searchExt))) ||
(( ! $is_ext) && strpos($this->filename, $searchExt) !== FALSE)
2019-10-25 10:28:15 -04:00
) {
$this->syntax = $syntax;
// Pre-tokenize the file
if ($this->syntax->filetype === 'PHP')
{
$this->tokens = PHP::getFileTokens($this->filename);
}
// Update the syntax highlighting for all the rows of the file
2019-11-05 12:37:25 -05:00
for ($i = 0; $i < $this->numRows; $i++)
2019-10-25 10:28:15 -04:00
{
2019-11-20 15:03:48 -05:00
// @codeCoverageIgnoreStart
2019-11-05 12:37:25 -05:00
$this->rows[$i]->updateSyntax();
2019-11-20 15:03:48 -05:00
// @codeCoverageIgnoreEnd
2019-10-25 10:28:15 -04:00
}
return;
}
}
}
}
2019-10-15 13:23:25 -04:00
// ------------------------------------------------------------------------
// ! Row Operations
// ------------------------------------------------------------------------
protected function rowCxToRx(Row $row, int $cx): int
{
$rx = 0;
for ($i = 0; $i < $cx; $i++)
{
if ($row->chars[$i] === "\t")
{
$rx += (KILO_TAB_STOP - 1) - ($rx % KILO_TAB_STOP);
}
$rx++;
}
return $rx;
}
protected function rowRxToCx(Row $row, int $rx): int
{
$cur_rx = 0;
for ($cx = 0; $cx < $row->size; $cx++)
{
if ($row->chars[$cx] === "\t")
{
$cur_rx += (KILO_TAB_STOP - 1) - ($cur_rx % KILO_TAB_STOP);
}
$cur_rx++;
if ($cur_rx > $rx)
{
return $cx;
}
}
return $cx;
}
protected function insertRow(int $at, string $s, bool $updateSyntax = TRUE): void
{
2019-10-22 16:16:28 -04:00
if ($at < 0 || $at > $this->numRows)
{
return;
}
2019-10-25 16:36:03 -04:00
$row = Row::new($this, $s, $at);
2019-10-22 16:16:28 -04:00
if ($at === $this->numRows)
{
2019-10-22 16:16:28 -04:00
$this->rows[] = $row;
}
else
{
$this->rows = [
...array_slice($this->rows, 0, $at),
$row,
...array_slice($this->rows, $at),
];
}
ksort($this->rows);
// Update row indexes
for ($i = 0; $i < $this->numRows; $i++)
{
$this->rows[$i]->idx = $i;
}
2019-11-05 12:28:10 -05:00
$this->rows[$at]->update();
$this->dirty++;
// Re-tokenize the file
2019-11-19 15:57:51 -05:00
if ($updateSyntax)
{
$this->refreshPHPSyntax();
}
2019-10-22 12:09:11 -04:00
}
protected function deleteRow(int $at): void
{
if ($at < 0 || $at >= $this->numRows)
{
return;
}
// Remove the row
unset($this->rows[$at]);
// Re-index the array of rows
$this->rows = array_values($this->rows);
2019-10-25 16:36:03 -04:00
for ($i = $at; $i < $this->numRows; $i++)
{
$this->rows[$i]->idx--;
}
2019-10-22 12:09:11 -04:00
// Re-tokenize the file
2019-11-19 15:57:51 -05:00
$this->refreshPHPSyntax();
2019-10-22 12:09:11 -04:00
$this->dirty++;
}
// ------------------------------------------------------------------------
// ! Editor Operations
// ------------------------------------------------------------------------
protected function insertChar(string $c): void
{
if ($this->cursorY === $this->numRows)
{
2019-10-22 16:16:28 -04:00
$this->insertRow($this->numRows, '');
2019-10-22 12:09:11 -04:00
}
2019-10-25 10:28:15 -04:00
$this->rows[$this->cursorY]->insertChar($this->cursorX, $c);
2019-10-22 12:09:11 -04:00
// Re-tokenize the file
2019-11-19 15:57:51 -05:00
$this->refreshPHPSyntax();
$this->cursorX++;
}
protected function insertNewline(): void
{
2019-10-22 16:16:28 -04:00
if ($this->cursorX === 0)
{
2019-11-05 12:28:10 -05:00
$this->insertRow($this->cursorY, '');
2019-10-22 16:16:28 -04:00
}
else
{
$row = $this->rows[$this->cursorY];
$chars = $row->chars;
2019-11-05 12:28:10 -05:00
$newChars = substr($chars, 0, $this->cursorX);
2019-10-22 16:16:28 -04:00
2019-11-05 12:28:10 -05:00
// Truncate the previous row
$row->chars = $newChars;
2019-10-22 16:16:28 -04:00
2019-11-05 12:28:10 -05:00
// Add a new row, with the contents from the cursor to the end of the line
$this->insertRow($this->cursorY + 1, substr($chars, $this->cursorX));
}
2019-11-05 12:28:10 -05:00
$this->cursorY++;
$this->cursorX = 0;
// Re-tokenize the file
2019-11-19 15:57:51 -05:00
$this->refreshPHPSyntax();
2019-10-22 16:16:28 -04:00
}
2019-10-22 12:09:11 -04:00
protected function deleteChar(): void
{
if ($this->cursorY === $this->numRows || ($this->cursorX === 0 && $this->cursorY === 0))
{
return;
}
$row = $this->rows[$this->cursorY];
if ($this->cursorX > 0)
{
2019-10-25 10:28:15 -04:00
$row->deleteChar($this->cursorX - 1);
2019-10-22 12:09:11 -04:00
$this->cursorX--;
}
else
{
$this->cursorX = $this->rows[$this->cursorY - 1]->size;
2019-10-25 10:28:15 -04:00
$this->rows[$this->cursorY -1]->appendString($row->chars);
2019-10-22 12:09:11 -04:00
$this->deleteRow($this->cursorY);
$this->cursorY--;
}
// Re-tokenize the file
2019-11-19 15:57:51 -05:00
$this->refreshPHPSyntax();
2019-10-15 13:23:25 -04:00
}
// ------------------------------------------------------------------------
// ! File I/O
// ------------------------------------------------------------------------
2019-10-22 12:09:11 -04:00
protected function rowsToString(): string
2019-10-15 13:23:25 -04:00
{
$lines = array_map(fn (Row $row) => (string)$row, $this->rows);
2019-10-22 12:09:11 -04:00
2019-10-22 16:44:55 -04:00
return implode('', $lines);
2019-10-22 12:09:11 -04:00
}
2019-10-22 12:09:11 -04:00
public function open(string $filename): void
{
2019-10-22 16:44:55 -04:00
// Copy filename for display
$this->filename = $filename;
2019-10-25 10:28:15 -04:00
$this->selectSyntaxHighlight();
2019-10-15 13:23:25 -04:00
// #TODO gracefully handle issues with loading a file
2019-10-22 16:44:55 -04:00
$handle = fopen($filename, 'rb');
2019-10-22 16:16:28 -04:00
if ($handle === FALSE)
{
2019-10-22 16:44:55 -04:00
write_stdout("\x1b[2J"); // Clear the screen
write_stdout("\x1b[H"); // Reposition cursor to top-left
2019-11-08 16:27:08 -05:00
Termios::disableRawMode();
2019-10-22 16:44:55 -04:00
print_r(error_get_last());
2019-10-22 16:16:28 -04:00
die();
}
2019-10-15 13:23:25 -04:00
while (($line = fgets($handle)) !== FALSE)
{
// Remove line endings when reading the file
$this->insertRow($this->numRows, rtrim($line), FALSE);
2019-10-15 13:23:25 -04:00
}
fclose($handle);
2019-10-22 12:09:11 -04:00
$this->dirty = 0;
}
protected function save(): void
{
if ($this->filename === '')
{
2019-10-22 16:44:55 -04:00
$newFilename = $this->prompt('Save as: %s');
if ($newFilename === '')
2019-10-22 16:16:28 -04:00
{
$this->setStatusMessage('Save aborted');
return;
}
2019-10-22 16:44:55 -04:00
$this->filename = $newFilename;
2019-10-25 10:28:15 -04:00
$this->selectSyntaxHighlight();
2019-10-22 12:09:11 -04:00
}
$contents = $this->rowsToString();
$res = file_put_contents($this->filename, $contents);
if ($res === strlen($contents))
{
$this->setStatusMessage('%d bytes written to disk', strlen($contents));
$this->dirty = 0;
return;
}
$this->setStatusMessage('Failed to save! I/O error: %s', error_get_last()['message']);
2019-10-15 13:23:25 -04:00
}
// ------------------------------------------------------------------------
// ! Find
// ------------------------------------------------------------------------
protected function findCallback(string $query, string $key): void
{
static $lastMatch = -1;
static $direction = 1;
static $savedHlLine = 0;
static $savedHl = [];
if ( ! empty($savedHl))
{
$this->rows[$savedHlLine]->hl = $savedHl;
$savedHl = [];
}
if ($key === "\r" || $key === "\x1b")
{
$lastMatch = -1;
$direction = 1;
return;
}
if ($key === Key::ARROW_RIGHT || $key === Key::ARROW_DOWN)
{
$direction = 1;
}
else if ($key === Key::ARROW_LEFT || $key === Key::ARROW_UP)
{
$direction = -1;
}
else
{
$lastMatch = -1;
$direction = 1;
}
if ($lastMatch === -1)
{
$direction = 1;
}
2019-10-25 16:36:03 -04:00
if (empty($query))
{
return;
}
$current = $lastMatch;
for ($i = 0; $i < $this->numRows; $i++)
{
$current += $direction;
if ($current === -1)
{
$current = $this->numRows - 1;
}
else if ($current === $this->numRows)
{
$current = 0;
}
$row = $this->rows[$current];
$match = strpos($row->render, $query);
if ($match !== FALSE)
{
$lastMatch = $current;
$this->cursorY = $current;
$this->cursorX = $this->rowRxToCx($row, $match);
$this->rowOffset = $this->numRows;
$savedHlLine = $current;
$savedHl = $row->hl;
// Update the highlight array of the relevant row with the 'MATCH' type
array_replace_range($row->hl, $match, strlen($query), Highlight::MATCH);
break;
}
}
}
protected function find(): void
{
$savedCx = $this->cursorX;
$savedCy = $this->cursorY;
$savedColOff = $this->colOffset;
$savedRowOff = $this->rowOffset;
$query = $this->prompt('Search: %s (Use ESC/Arrows/Enter)', [$this, 'findCallback']);
// If they pressed escape, the query will be empty,
// restore original cursor and scroll locations
if ($query === '')
{
$this->cursorX = $savedCx;
$this->cursorY = $savedCy;
$this->colOffset = $savedColOff;
$this->rowOffset = $savedRowOff;
}
}
2019-10-14 16:21:41 -04:00
// ------------------------------------------------------------------------
// ! Output
// ------------------------------------------------------------------------
2019-10-15 13:23:25 -04:00
protected function scroll(): void
{
$this->renderX = 0;
if ($this->cursorY < $this->numRows)
{
$this->renderX = $this->rowCxToRx($this->rows[$this->cursorY], $this->cursorX);
}
// Vertical Scrolling
if ($this->cursorY < $this->rowOffset)
2019-10-15 13:23:25 -04:00
{
$this->rowOffset = $this->cursorY;
}
2019-10-18 16:20:34 -04:00
if ($this->cursorY >= ($this->rowOffset + $this->screenRows))
{
$this->rowOffset = $this->cursorY - $this->screenRows + 1;
2019-10-15 13:23:25 -04:00
}
// Horizontal Scrolling
if ($this->renderX < $this->colOffset)
{
$this->colOffset = $this->renderX;
}
2019-10-18 16:20:34 -04:00
if ($this->renderX >= ($this->colOffset + $this->screenCols))
2019-10-15 13:23:25 -04:00
{
$this->colOffset = $this->renderX - $this->screenCols + 1;
2019-10-15 13:23:25 -04:00
}
}
2019-10-14 16:21:41 -04:00
protected function drawRows(): void
{
for ($y = 0; $y < $this->screenRows; $y++)
{
$filerow = $y + $this->rowOffset;
2019-10-18 16:20:34 -04:00
if ($filerow >= $this->numRows)
2019-10-14 16:21:41 -04:00
{
2019-11-05 13:51:21 -05:00
if ($this->numRows === 0 && $y === (int)($this->screenRows / 2))
2019-10-14 16:21:41 -04:00
{
2019-10-15 13:23:25 -04:00
$welcome = sprintf('PHP Kilo editor -- version %s', KILO_VERSION);
$welcomelen = strlen($welcome);
if ($welcomelen > $this->screenCols)
{
$welcomelen = $this->screenCols;
}
2019-10-14 16:21:41 -04:00
2019-10-15 13:23:25 -04:00
$padding = ($this->screenCols - $welcomelen) / 2;
if ($padding > 0)
2019-10-15 13:23:25 -04:00
{
$this->ab .= '~';
$padding--;
}
2019-10-25 16:36:03 -04:00
for ($i = 0; $i < $padding; $i++)
2019-10-15 13:23:25 -04:00
{
$this->ab .= ' ';
}
$this->ab .= substr($welcome, 0, $welcomelen);
2019-10-14 16:21:41 -04:00
}
2019-10-15 13:23:25 -04:00
else
2019-10-14 16:21:41 -04:00
{
2019-10-15 13:23:25 -04:00
$this->ab .= '~';
2019-10-14 16:21:41 -04:00
}
}
else
{
$len = $this->rows[$filerow]->rsize - $this->colOffset;
2019-10-15 13:23:25 -04:00
if ($len < 0)
{
$len = 0;
}
if ($len > $this->screenCols)
{
$len = $this->screenCols;
}
2019-10-23 13:36:16 -04:00
$c = substr($this->rows[$filerow]->render, $this->colOffset, $len);
$hl = array_slice($this->rows[$filerow]->hl, $this->colOffset, $len);
$currentColor = -1;
2019-10-23 13:36:16 -04:00
for ($i = 0; $i < $len; $i++)
{
2019-10-25 15:34:56 -04:00
// Handle 'non-printable' characters
if (is_cntrl($c[$i]))
{
$sym = (ord($c[$i]) <= 26)
? chr(ord('@') + ord($c[$i]))
: '?';
$this->ab .= "\x1b[7m";
$this->ab .= $sym;
$this->ab .= "\x1b[m";
if ($currentColor !== -1)
{
$this->ab .= sprintf("\x1b%dm", $currentColor);
}
}
else if ($hl[$i] === Highlight::NORMAL)
2019-10-23 13:36:16 -04:00
{
if ($currentColor !== -1)
{
$this->ab .= "\x1b[0m"; // Reset background color
2019-10-25 16:36:03 -04:00
$this->ab .= "\x1b[39m"; // Reset foreground color
$currentColor = -1;
}
2019-10-23 13:36:16 -04:00
$this->ab .= $c[$i];
}
else
{
$color = syntax_to_color($hl[$i]);
if ($color !== $currentColor)
{
$currentColor = $color;
$this->ab .= "\x1b[0m"; // Reset background color
$this->ab .= sprintf("\x1b[%dm", $color);
}
2019-10-23 13:36:16 -04:00
$this->ab .= $c[$i];
}
}
$this->ab .= "\x1b[0m";
2019-10-25 16:36:03 -04:00
$this->ab .= "\x1b[39m";
2019-10-14 16:21:41 -04:00
}
$this->ab .= "\x1b[K"; // Clear the current line
$this->ab .= "\r\n";
}
}
protected function drawStatusBar(): void
{
$this->ab .= "\x1b[7m";
$statusFilename = $this->filename !== '' ? $this->filename : '[No Name]';
2019-10-25 10:28:15 -04:00
$syntaxType = ($this->syntax !== NULL) ? $this->syntax->filetype : 'no ft';
2019-10-22 12:09:11 -04:00
$isDirty = ($this->dirty > 0) ? '(modified)' : '';
$status = sprintf('%.20s - %d lines %s', $statusFilename, $this->numRows, $isDirty);
2019-10-25 10:28:15 -04:00
$rstatus = sprintf('%s | %d/%d', $syntaxType, $this->cursorY + 1, $this->numRows);
$len = strlen($status);
$rlen = strlen($rstatus);
if ($len > $this->screenCols)
{
$len = $this->screenCols;
}
$this->ab .= substr($status, 0, $len);
while ($len < $this->screenCols)
{
if ($this->screenCols - $len === $rlen)
2019-10-14 16:21:41 -04:00
{
$this->ab .= substr($rstatus, 0, $rlen);
break;
2019-10-14 16:21:41 -04:00
}
$this->ab .= ' ';
$len++;
}
$this->ab .= "\x1b[m";
$this->ab .= "\r\n";
}
protected function drawMessageBar(): void
{
$this->ab .= "\x1b[K";
$len = strlen($this->statusMsg);
if ($len > $this->screenCols)
{
$len = $this->screenCols;
}
if ($len > 0 && (time() - $this->statusMsgTime) < 5)
{
$this->ab .= substr($this->statusMsg, 0, $len);
2019-10-14 16:21:41 -04:00
}
}
public function refreshScreen(): void
{
2019-10-15 13:23:25 -04:00
$this->scroll();
2019-10-14 16:21:41 -04:00
$this->ab = '';
$this->ab .= "\x1b[?25l"; // Hide the cursor
$this->ab .= "\x1b[H"; // Reposition cursor to top-left
$this->drawRows();
$this->drawStatusBar();
$this->drawMessageBar();
2019-10-14 16:21:41 -04:00
// Specify the current cursor position
$this->ab .= sprintf("\x1b[%d;%dH",
($this->cursorY - $this->rowOffset) + 1,
($this->renderX - $this->colOffset) + 1
);
2019-10-14 16:21:41 -04:00
$this->ab .= "\x1b[?25h"; // Show the cursor
echo $this->ab;
}
public function setStatusMessage(string $fmt, ...$args): void
{
$this->statusMsg = (count($args) > 0)
? sprintf($fmt, ...$args)
: $fmt;
$this->statusMsgTime = time();
2019-10-14 16:21:41 -04:00
}
// ------------------------------------------------------------------------
// ! Input
// ------------------------------------------------------------------------
protected function prompt(string $prompt, ?callable $callback = NULL): string
2019-10-22 16:16:28 -04:00
{
$buffer = '';
$modifiers = Key::getConstList();
2019-10-22 16:16:28 -04:00
while (TRUE)
{
$this->setStatusMessage($prompt, $buffer);
$this->refreshScreen();
$c = $this->readKey();
$cord = ord($c);
if ($c === Key::ESCAPE)
{
$this->setStatusMessage('');
if ($callback !== NULL)
{
$callback($buffer, $c);
}
2019-10-22 16:16:28 -04:00
return '';
}
if ($c === Key::ENTER && $buffer !== '')
{
$this->setStatusMessage('');
if ($callback !== NULL)
{
$callback($buffer, $c);
}
2019-10-22 16:16:28 -04:00
return $buffer;
}
if ($c === Key::DEL_KEY || $c === Key::BACKSPACE || $c === chr(ctrl_key('h')))
{
$buffer = substr($buffer, 0, -1);
}
else if (is_ascii($c) && ( ! is_cntrl($c)) && ! in_array($c, $modifiers, TRUE))
2019-10-22 16:16:28 -04:00
{
$buffer .= $c;
}
if ($callback !== NULL)
{
$callback($buffer, $c);
}
2019-10-22 16:16:28 -04:00
}
}
2019-10-14 16:21:41 -04:00
protected function moveCursor(string $key): void
{
$row = ($this->cursorY >= $this->numRows)
? NULL
: $this->rows[$this->cursorY];
2019-10-14 16:21:41 -04:00
switch ($key)
{
case Key::ARROW_LEFT:
if ($this->cursorX !== 0)
2019-10-14 16:21:41 -04:00
{
$this->cursorX--;
}
else if ($this->cursorX > 0)
{
$this->cursorY--;
$this->cursorX = $this->rows[$this->cursorY]->size;
2019-10-14 16:21:41 -04:00
}
break;
case Key::ARROW_RIGHT:
if ($row && $this->cursorX < $row->size)
{
$this->cursorX++;
}
else if ($row && $this->cursorX === $row->size)
2019-10-14 16:21:41 -04:00
{
$this->cursorY++;
$this->cursorX = 0;
2019-10-14 16:21:41 -04:00
}
break;
case Key::ARROW_UP:
if ($this->cursorY !== 0)
2019-10-14 16:21:41 -04:00
{
$this->cursorY--;
2019-10-14 16:21:41 -04:00
}
break;
case Key::ARROW_DOWN:
if ($this->cursorY < $this->numRows)
2019-10-14 16:21:41 -04:00
{
$this->cursorY++;
2019-10-14 16:21:41 -04:00
}
break;
}
$row = ($this->cursorY >= $this->numRows)
? NULL
: $this->rows[$this->cursorY];
$rowlen = $row ? $row->size : 0;
if ($this->cursorX > $rowlen)
{
$this->cursorX = $rowlen;
}
2019-10-14 16:21:41 -04:00
}
2019-10-16 22:14:30 -04:00
public function processKeypress(): ?string
2019-10-14 16:21:41 -04:00
{
2019-10-22 12:09:11 -04:00
static $quit_times = KILO_QUIT_TIMES;
2019-10-14 16:21:41 -04:00
$c = $this->readKey();
2019-10-16 22:14:30 -04:00
if ($c === "\0")
{
return '';
}
switch ($c)
2019-10-14 16:21:41 -04:00
{
2019-10-22 12:09:11 -04:00
case Key::ENTER:
2019-10-22 16:16:28 -04:00
$this->insertNewline();
2019-10-22 12:09:11 -04:00
break;
case chr(ctrl_key('q')):
if ($this->dirty > 0 && $quit_times > 0)
{
$this->setStatusMessage('WARNING!!! File has unsaved changes.' .
'Press Ctrl-Q %d more times to quit.', $quit_times);
$quit_times--;
return '';
}
write_stdout("\x1b[2J"); // Clear the screen
write_stdout("\x1b[H"); // Reposition cursor to top-left
return NULL;
break;
case chr(ctrl_key('s')):
$this->save();
break;
2019-10-14 16:21:41 -04:00
case Key::HOME_KEY:
$this->cursorX = 0;
2019-10-14 16:21:41 -04:00
break;
case Key::END_KEY:
if ($this->cursorY < $this->numRows)
{
$this->cursorX = $this->rows[$this->cursorY]->size - 1;
}
2019-10-14 16:21:41 -04:00
break;
case chr(ctrl_key('f')):
$this->find();
break;
2019-10-22 12:09:11 -04:00
case Key::BACKSPACE:
case chr(ctrl_key('h')):
case Key::DEL_KEY:
if ($c === Key::DEL_KEY)
{
$this->moveCursor(Key::ARROW_RIGHT);
}
$this->deleteChar();
break;
2019-10-14 16:21:41 -04:00
case Key::PAGE_UP:
case Key::PAGE_DOWN:
2019-10-15 13:23:25 -04:00
$this->pageUpOrDown($c);
2019-10-14 16:21:41 -04:00
break;
case Key::ARROW_UP:
case Key::ARROW_DOWN:
case Key::ARROW_LEFT:
case Key::ARROW_RIGHT:
$this->moveCursor($c);
break;
2019-10-22 12:09:11 -04:00
case chr(ctrl_key('l')):
case Key::ESCAPE:
// Do nothing
break;
2019-10-18 16:20:34 -04:00
default:
2019-10-22 12:09:11 -04:00
$this->insertChar($c);
break;
2019-10-14 16:21:41 -04:00
}
2019-10-22 12:09:11 -04:00
$quit_times = KILO_QUIT_TIMES;
2019-10-14 16:21:41 -04:00
return $c;
}
2019-10-15 13:23:25 -04:00
private function pageUpOrDown(string $c): void
2019-10-15 13:23:25 -04:00
{
if ($c === Key::PAGE_UP)
{
$this->cursorY = $this->rowOffset;
}
else if ($c === Key::PAGE_DOWN)
{
$this->cursorY = $this->rowOffset + $this->screenRows - 1;
if ($this->cursorY > $this->numRows)
{
$this->cursorY = $this->numRows;
}
}
2019-10-15 13:23:25 -04:00
$times = $this->screenRows;
2019-10-18 16:20:34 -04:00
for (; $times > 0; $times--)
2019-10-15 13:23:25 -04:00
{
$this->moveCursor($c === Key::PAGE_UP ? Key::ARROW_UP : Key::ARROW_DOWN);
}
}
private function refreshPHPSyntax(): void
{
2019-11-19 15:57:51 -05:00
if ($this->syntax->filetype !== 'PHP')
{
return;
}
$this->tokens = PHP::getTokens($this->rowsToString());
2019-11-05 12:37:25 -05:00
for ($i = 0; $i < $this->numRows; $i++)
{
$this->rows[$i]->update();
}
}
2019-10-24 20:21:44 -04:00
}