Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling clipboard.Write in lua crashes micro #3320

Open
Neko-Box-Coder opened this issue May 27, 2024 · 13 comments
Open

Calling clipboard.Write in lua crashes micro #3320

Neko-Box-Coder opened this issue May 27, 2024 · 13 comments

Comments

@Neko-Box-Coder
Copy link
Contributor

Neko-Box-Coder commented May 27, 2024

Description of the problem or steps to reproduce

When calling clipboard.Write with anything inside a lua script, it just crashes micro.

Example code:

local clipboard = import("micro/clipboard")
-- ...
clipboard.Write("Test", -1)

Crash trace:

bindings.json
Micro encountered an error: runtime.errorString runtime error: invalid memory address or nil pointer dereference
runtime/panic.go:261 (0x4571f8)
runtime/panic.go:260 (0x4571c6)
github.com/yuin/[email protected]/table.go:316 (0x7d658c)
github.com/yuin/[email protected]/state.go:1245 (0x7d6595)
github.com/yuin/[email protected]/vm.go:413 (0x7ea0e5)
github.com/yuin/[email protected]/vm.go:31 (0x7e70a5)
github.com/yuin/[email protected]/state.go:1202 (0x7d605c)
github.com/yuin/[email protected]/state.go:836 (0x7ff905)
layeh.com/[email protected]/luar.go:265 (0x7fff18)
github.com/zyedidia/micro/v2/internal/action/command.go:1050 (0x8d3b23)
github.com/zyedidia/micro/v2/internal/action/actions.go:1603 (0x8c35c9)
github.com/zyedidia/micro/v2/internal/info/infobuffer.go:162 (0x892923)
github.com/zyedidia/micro/v2/internal/action/infopane.go:227 (0x8d7e5f)
github.com/zyedidia/micro/v2/internal/action/infopane.go:54 (0x8d6fb1)
github.com/zyedidia/micro/v2/internal/action/infopane.go:136 (0x8d784f)
github.com/zyedidia/micro/v2/internal/action/infopane.go:95 (0x8d745e)
github.com/zyedidia/micro/v2/cmd/micro/micro.go:478 (0x90fba6)
github.com/zyedidia/micro/v2/cmd/micro/micro.go:397 (0x90f4af)
runtime/internal/atomic/types.go:194 (0x44111d)
runtime/asm_amd64.s:1695 (0x4754e1)

If you can reproduce this error, please report it at https://github.com/zyedidia/micro/issues

Specifications

Commit hash: e9bd1b3
OS: Linux
Terminal: WezTerm

@Neko-Box-Coder
Copy link
Contributor Author

Neko-Box-Coder commented May 27, 2024

Right, it's not even exported to lua 🤦

I was assuming most of the things listed in https://pkg.go.dev/github.com/zyedidia/micro/v2/internal is accessible from lua since the documentation said what it listed is only a short list of the available functions.

Could we expose the clipboard module?

@dmaluka
Copy link
Collaborator

dmaluka commented May 27, 2024

Could we expose the clipboard module?

You could e.g. execute xclip -i command from Lua instead.

@Neko-Box-Coder
Copy link
Contributor Author

I use micro in both Windows and Linux so xclip is not feasible.
It's ugly but got a workaround working using buffers.

I think exposing the clipboard module would be beneficial for plugins.

@dmaluka
Copy link
Collaborator

dmaluka commented May 27, 2024

So you could use some cross-platform counterpart of xclip, or for example write a simple script which runs either xclip or whatever tool is available in Windows, depending on which system this script is running on. Then you could also use this script everywhere, not just with micro and lua but with any program capable of running external programs and with any language capable of running external programs.

I think exposing the clipboard module would be beneficial for plugins.

Writing text to clipboard or reading text from clipboard is not really a micro-specific functionality (like inserting text into micro's buffer, or opening a split pane in micro, and so on). It can be provided by any external tool or library.

@Neko-Box-Coder
Copy link
Contributor Author

If there are unzip and HttpRequest functions available for plugin, I don't think clipboard reading and writing is that far fetched.

Not to mention clipboard reading and writing is related to micro, since the clipboard interacts with micro (or the other way around).

And there can be use cases for it as well, like a clipboard history or clipboard manager or maybe some sort of clipboard content processing.

If we don't have the functions to export to lua, then yeah this might need a bit of work but this isn't the case.

I don't see why we need to make plugin developers' lives difficult when we can just export the functions we are already using in micro.

@dmaluka
Copy link
Collaborator

dmaluka commented May 29, 2024

We could keep ad-hoc exporting things like unzip and http requests and everything but the kitchen sink, whenever someone ask for it, but that is not a very scalable approach, is it? Whereas the "use an external program" approach doesn't depend on adding changes to micro and provides maximum flexibility. Unix way, bro.

That said, on second thought, you are right that micro's clipboard functions functionality is actually micro specific, since those functions respect the clipboard option value (external, internal or terminal) and automatically choose the corresponding clipboard method. If that is what you need (i.e. if you want your Lua code to use the clipboard chosen by user via the clipboard option, which is not necessarily the system clipboard), then indeed exposing this functionality might be a good idea.

Now, we'd need to think how exactly should we expose it. (It's always easy to add a poorly thought out interface, it's hard to remove it afterwards.)

There is no such thing as exposing an entire package to Lua, we only expose specific functions, global variables etc (those that make sense to expose). It seems in this case we'd need to expose:

  1. clipboard.Read and clipboard.Write functions
  2. ClipboardReg and PrimaryReg constants (so that the user doesn't need to pass magic -1 or -2 values)

We might also think whether it should also respect the useprimary option, i.e. avoid writing to PrimaryReg is useprimary is disabled (IOW, expose not clipboard.Write directly, but a simple wrapper which would check the useprimary value)... At the moment I'm inclined to think there is no need for that, a plugin could check the useprimary option on its own.

@Gavin-Holt
Copy link

Hi,

Is there any movement with clipboard access?

Many editors have functions cutappend and copyappend, I think micro would be stronger if we had similar.

Could these two functions be built-in actions to avoid exposing more in Lua?

Kind Regards Gavin Holt

@dmaluka
Copy link
Collaborator

dmaluka commented Dec 3, 2024

Many editors have functions cutappend and copyappend, I think micro would be stronger if we had similar.

Wait... are you talking about the functionality that is already (non-intuitively) provided by micro's CutLine action? Which was discussed (mostly negatively) in #3477, #2653 and other places?

@Gavin-Holt
Copy link

Hi,

Thank you for reminding of this previous discussion. From my point of view, actions affecting the clipboard should be precise and reflect the current selected text only.

I have many shortcuts to aid text selection, including line selection (thanks to those who helped me write the Lua functions).

Applying actions to unselected text is common in some editors, and I have adopted some of these for clearing (delete) actions e.g.

  • Ctrl+K - Delete to end of line
  • Shift+Ctrl+K - Delete to start of line
  • Ctrl+Y - Delete all lines with part selection

cutappend and copyappend could be very powerful as actions, acting at each cursor in multi-cursor mode. For example reordering tabular data.

I have used Autohotkey, which allows me to use almost any shortcut and translate into something micro will accept. A full list of my current keyboard shortcuts is appended below for interest.

Kind Regards Gavin Holt

Universal Keyboard Shortcuts (UKS)

Introduction

Instead of learning and re-learning keyboard shortcuts, I am only going to use editors that can be made to respond to the same keystrokes. From the list below those marked * do not work yet.

Actions and Workflow

In my trials of many editors, there are some editing actions and shortcuts I find useful, so I have adopted them.

Moving - local to cursor

Actions______________ Keys_________________
Move Word Left Ctrl_Left
Move Word Right Ctrl_Right
BOL Ctrl_B, Home
EOL Ctrl_E, End
Up Line Up
Down Line Down
Up Same Level Alt_<
Down Same Level Alt_>
Up Next Level Ctrl_<
Down Next Level Ctrl_>
Up Full Page PgUp
Down Full Page PgDn
BOF Ctrl_Home
EOF Ctrl_End
Jump Brackets Ctrl_J
Goto Line Number Ctrl_G
GoBackwards Regex-i Ctrl_[
GoForwards Regex-i Ctrl_]

Selecting

Actions______________ Keys_________________
Select All Ctrl_A
Select Word Ctrl_Space
Select Word Back Ctrl_Shift_Space
Select BOL Ctrl_Shift_B, S_Home
Select EOL Ctrl_Shift_E, S_End
Select Line(s) Down Ctrl_L
Select Line(s) Up Ctrl_Shift_L
Select Inner Ctrl_Shift_I
Select Outer Ctrl_Shift_J
SelectBackwards Regex-i Ctrl_Shift_[
SelectForwards Regex-i Ctrl_Shift_]
Swap Anchor<>Caret Ctrl_-

Basic editing actions

Actions______________ Keys_________________
Delete Word Left Ctrl_Bksp
Delete Word Right Ctrl_Del
Delete to EOL Ctrl_K
Delete to BOL Ctrl_Shift_K
Delete Line(s) Ctrl_Y
Insert Line Break Enter
Add Line Above Shift_Enter
Add Line Below Ctrl_Enter
Move Line(s) Up Ctrl_Shift_Up
Move Line(s) Down Ctrl_Shift_Down
Duplicate Line(s) Ctrl_D
Indent Line(s) Ctrl_Shift_>
Outdent Line(s) Ctrl_Shift_<
Undo Ctrl_Z
Undoundo Ctrl_Shift_Z
Autocomplete Word Ctrl_/
Toggle Comments Ctrl_Q
Toggle WordWrap Ctrl_Shift_W

Clipboard Actions

Actions______________ Keys_________________
Cut Ctrl_X
CutAppend* Ctrl_Shift_X
Copy Ctrl_C
CopyAppend* Ctrl_Shift_C
Paste Ctrl_V
PastePlain Ctrl_Shift_V

Searching

Actions______________ Keys_________________
Find Forward Ctrl_F
Find Word Ctrl_*
Find Regex Ctrl_Shift_F
Find in Files Ctrl_Alt_F
Next Find Ctrl_Down
Prev Find Ctrl_Up
Replace Ctrl_H
Replace All Ctrl_Shift_H
Replace In Selection Ctrl_Alt_H
Mark all Ctrl_M
UnMark all Ctrl_U

Navigating

Actions______________ Keys_________________
Goto Line Ctrl_G
Toggle Bookmark* Ctrl_F2
Next Bookmark* F2
Prev Bookmark* Shift_F2
Clear Bookmarks* Ctrl_Alt_F2
Next Message F12
Prev Massage Shift+F12
Next File Ctrl_Tab
Prev File Ctrl_Shift_Tab
Open Selection Ctrl_Shift_O
Open File Tree Ctrl_\

NB. Micro also has bindable actions for; FirstTab, PreviousTab, PreviousSplit, NextSplit, NextTab, and LastTab.

File commands

Actions______________ Keys_________________
File New Tab Ctrl_N
File New Window Ctrl_Alt_N
File Save Ctrl_S
File Revert* Ctrl_R
File Print Ctrl_P
File Close Ctrl_W
File Spell Check Ctrl_F7
File Backup Ctrl_Alt_B
File Run Ctrl_Alt_R
Run Makeit.bat Ctrl_Alt_M
Open Todo.txt Ctrl_Alt_T
Open Explorer Ctrl_Alt_E

Dialog boxes

There are some instances where a clickable dialog box can be very useful.
(Uses extrnal tools from https://www.robvanderwoude.com/dialogboxes.php)

Actions______________ Keys_________________
File Open DLG Ctrl_O
File Insert DLG Ctrl_I
File Save As DLG Ctrl_Shift_S

Multicursors

Actions______________ Keys_________________
Multicursor Up Ctrl_Shift_U
Multicursor Down Ctrl_Shift_D
Multicursor Next Ctrl_Shift_N
Multicursor Skip Next Ctrl_Shift_P
Multicursor Skip Back
Multicursor Remove Last Ctrl_Shift_R
Multicursor Edit All* Ctrl_Shift_A
Multicursor Edit Found* Ctrl_Shift_M
Multicursor Remove All Escape
Prefix Lines Alt_[
Postfix Lines Alt_]

Folding

TODO Micro won't fold yet! Scite will fold.

Actions______________ Keys_________________
Toggle Fold* Alt_Z
Toggle Fold All* Ctrl_Alt_Z
Fold Section* Ctrl_Alt_Left
Expand Fold* Ctrl_Alt_Right

Splitting Screens

Actions______________ Keys_________________
Current Split Right F8
Current Split Below F7
Close Split Right Shift_F8
Close Split Below Shift_F7
New Window Right Alt_F8
New Window Below Alt_F7
Move to Frame Down Alt_Down
Move to Frame Up Alt_Up
Move to Frame Left Alt_Left
Move to Frame Right Alt_Right
Toggle Log Ctrl_Alt_L

Mode Selection

Actions______________ Keys_________________
Command Mode Ctrl_:
Shell Mode Ctrl_#
Textfilter Mode Ctrl_@
Lua Mode Ctrl_=

LSP Actions

The LSP is a method to offload some smart featrues:

Actions______________ Keys_________________
Hover help* Alt_F1
CompleteWord* Ctrl_/
Format (Lint)* Alt_/
Definition Alt_D
References Alt_R
MessageNext F12
MessagePrev Shift_F12
Find TAGS* Ctrl_T
Make TAGS* Ctrl_Shift_T

FZF as an alternative interface

Fuzzy finder is a fantastic piece of software. There is enough power to replace almost all user interface widgets. I have attempted many functions with unmodified fzf.exe, with helpers such as bat.exe, findstr.exe and grep.exe.

Command line shortcuts

It is helpful to use a different colour scheme for the fzf screens to aid in context switching. When fzf is active it has it's own keybindings, again I have moved away from the defaults:

Actions______________ Keys_________________
BOL* CTRL_B
EOL CTRL_E
Next Word* CTRL_Right
Prev Word* CTRL_Left
Delete Word Left* CTRL_Bksp
Delete Word Right* CTRL_Del
Toggle selection up* TAB
Toggle selection down* Shift_TAB
Previous History CTRL_P
Next History CTRL_N

Command line syntax

Fzf has its own syntax to control filtering:

Symbol______________ Meaning _______________________________
^ (START) matches pattern at the beginning
$ (END) matches pattern at the end
! (NOT) exclude matches
' (EXACT) literal match
pipe (OR) alternate matches
" " (AND) space between multiple patterns

Note terms are separated by spaces. Use qotes for patterns with spaces. Not sure if braces work.

Keybindings list

A searchable list of keybindings, this is great method to increase discoverability:

Actions______________ Keys_________________
Keybindings FZF Alt_K
Edit default keybinds Alt_Shift_K

Command list

Powerful editors have command lines, this list includes annotations about the commands, merged with the command history:

Actions______________ Keys_________________
Default command bar Ctrl_:
Commands FZF Alt_:
Edit Command lists Alt_Shift_:

Goto list

This allows plain text searching of the current file, and then navigates to the selected position. (You can search for a line number)

Actions______________ Keys_________________
Goto text FZF Alt_G

Goto previous list

Keeping a log of editing locations during a session, allows easy backtracking.

Actions______________ Keys_________________
Previous location FZF Alt_P

Goto found list

Entering a target string filters the current file, then additional plain text searching is done using FZF.

Actions______________ Keys_________________
Find FZF Alt_F
Find Regex FZF Alt_Shift_F
Find in files FZF Alt_Ctrl_F

Goto buffer list

Switching between open buffers needs to be as painless as possible. This list includes open windows, recent files and favourites:

Actions______________ Keys_________________
Window list FZF Alt_W
Edit favourite list Alt_Shift_W

Open file list

This is one area where FZF shines. Using the current buffer path as proxy for the project domain, let the file finding begin:

Actions______________ Keys_________________
File list FZF Alt_O
Edit favourites list Alt_Shift_O

Open backup list

This works best for locally stored versions sharing similar filenames. e.g. .\zBackup\2023_09_23_67465_Filename.ext

Actions______________ Keys_________________
Backup list FZF Alt_B
Explore backups Alt_Shift_B

Open help list

I like to keep local copies of the runtime help files, add my own MarkDown files, and use cheatsheets:

Actions______________ Keys_________________
Help list FZF Alt_H
Edit help file FZF Alt_Shift_H

Diff against old versions

This works best for locally stored versions sharing similar filenames. e.g. .\zBackup\2023_09_23_67465_Filename.ext

Actions______________ Keys_________________
Version list FZF Alt_V
Explore version folder Alt_Shift_V

Insert file list

Insert file/filename from project:

Actions______________ Keys_________________
Insert file FZF Alt_I
Insert filename FZF Alt_Shift_I

Insert unique word list

Select unique words from the current open buffer, can help avoid typos in variable names:

Actions______________ Keys_________________
Unique word list FZF Alt_U

Insert line list

A list of useful annotated one liners, by file type:

Actions______________ Keys_________________
Line list FZF Alt_L
Edit lines Alt_Shift_L

Insert reference list

I have a script to extract citations from files, and insert as MarkDown footnotes.

Actions______________ Keys_________________
Reference list FZF Alt_Q
Edit References Alt_Shift_Q

Insert template list

From a selection of template files.

Actions______________ Keys_________________
Template list FZF Alt_T
Edit templates Alt_Shift_T

Insert snippet list

From a selection of snippets.

Actions______________ Keys_________________
Snippet list FZF Alt_S
Edit Snippets Alt_Shift_S

Shell command list

This list includes saved shell commands:

Actions______________ Keys_________________
Shell cmd list FZF Alt_#
Edit cmd list Alt_Shift_#
Shell COMSPEC Ctrl_Alt_#

Textfilter command list

This lists saved textfilter commands:

Actions______________ Keys_________________
Textfilter list FZF Alt_@
Edit textfilter list Alt_Shift_@

Macro list

This list includes recent saved macros:

Actions______________ Keys_________________
Macro list FZF* Alt_M
Edit macros FZF* Alt_Shift_M

Lua commands

Lua commands may be entered in a command line, or replaced in the text.

Actions______________ Keys_________________
Lua Mode Ctrl_=
Lua list FZF Alt_=
Edit Lua commands Alt_Shift_=
Interpret selection Ctrl_Alt_=

@dmaluka
Copy link
Collaborator

dmaluka commented Dec 4, 2024

My question is simply: what exactly are you referring to as cutappend? The existing micro's default behavior of Ctrl-X (when there is no selection) and Ctrl-K? Or some other behavior?

@Gavin-Holt
Copy link

Gavin-Holt commented Dec 4, 2024

Hi

cutappend

  • copy the highlighted text
  • append to the clipboard (for each cursor)
  • delete the highlighted text
  • return false if no selection, true if text added to the clipboard

copyappend

  • copy the highlighted text
  • append to the clipboard (for each cursor)
  • leave highlighted text unchanged
  • return false if no selection, true if text added to the clipboard

If selected, then end-of-line characters to be included. So highlighting with Home+Shift+End (SelectToEndOfLine) vs. Home+Shift+Down (SelectDown) will produce different clipboard contents.

The default for Ctrl+K and Ctrl+X (with no selection) are both using the CutLine action, which includes the whole line and EOL characters. I guess many users value the CutLine action to move lines of code, not so good for prose or table manipulations.

Naming things is hard - the current CutLine action is doing so much more:

  • StartofLine
  • SelectDown (includes EOL characters)
  • CutAppend

Kind Regards Gavin Holt

@dmaluka
Copy link
Collaborator

dmaluka commented Dec 4, 2024

Ok, got it. You want the equivalent of the current CutLine behavior but for selections, not for whole lines.

Naming things is hard - the current CutLine action is doing so much more

Agree, the name is unfortunate, I think it would be good to rename it to CutLineAppend, and make CutLine replace the clipboard, not append to it. And also, as was discussed in #2653, it seems good to change the default Ctrl-x behavior to the more expected one (make it replace the clipboard, not append to it, i.e. keep it bound to Cut|CutLine by default, but with the updated behavior of CutLine), while keep the default Ctrl-k behavior as is (i.e. rebind it to CutLineAppend instead of CutLine by default).

And yep, we could also implement CutAppend, CopyAppend, CopyLineAppend actions as you suggest. I'm not against it.

@MarkZaytsev
Copy link

I've prepared a pull request with CutLineAppend to avoid ambiguity in behaviour of CutLine. Not sure if copy variations are needed since they work as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants