All posts


Posted On Feb 23, 2023

Clipboard support

Copy/paste is a must have for any editor. To implement this, the flow would look like this:

│User Click and Drag                                 │ 
│ │                                                  │ 
│ └► Trigger SELECT event──► Highlight selected cells│ 
│User Pressed <Meta>+X / <Meta>+X                    │ 
│ │                                                  │ 
│ └► Get highlighted cells──► Compute selected area  │ 
│                   ┌──────────────────┘             │ 
│                   🭭                                │ 
│             Get content in the──► Put to Clipboard │ 
│             selected area                          │ 

For both select/highlight and extracting content, we have the same behavior. We defined a selected
range as a rectangle:

selected_range = Rect(start_row, start_col, end_row, end_col)
selected_width = selected_range.width
selected_height = selected_range.height

We can access the selected cells of the character grid (which is a 1D array) with the following snippet:

for row in 0..selected_height {
  for col in 0..selected_width {
    let index = (start_row + row) * grid_width + (start_col + col);
    // access character_grid[index] here

We can toggle each cell’s highlight property on/off. And to build the content string for the selected range,
we just need to build a string where each line content the whole selected row.

You can check the commit for more details fdd2e02.

For usage:

  • In cursor mode, click and drag on the grid to select content
  • Use MetaC to copy the selected content
  • Use MetaX to cut the selected content
  • Use MetaV to paste the clipboard content into the mouse position