Recent

Author Topic: [SOLVED] Synedit copy /past not working on first try  (Read 734 times)

Tomu

  • New Member
  • *
  • Posts: 44
[SOLVED] Synedit copy /past not working on first try
« on: July 02, 2026, 03:41:26 pm »
I'm using a TSynedit control and at runtime the first time I copy from the control (ctrl-c) and then do a paste, what gets pasted is whatever was on the clipboard before I started the app (not what I just cut or copied). After that first malfunction subsequent copy/cut/paste works fine.
Running on Linux Mint.
« Last Edit: July 04, 2026, 08:24:04 pm by Tomu »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12539
  • Debugger - SynEdit - and more
    • wiki
Re: Synedit copy /past not working on first try
« Reply #1 on: July 02, 2026, 03:58:26 pm »
On Linux there are usually a primary and a 2ndary clipboard. And additionally there may be different clipboard-managers by the OS/Desktop.

I have several Linux installs, e.g. an OpenSuse, and an older Fedora and Ubuntu, and they seem to work ok.

So its possible that this needs an expert on Linux. And that the issue is either on the Linux side, or in the LCL/WidgetSetcode of Lazarus (which means it affects SynEdit, but I as the SynEdit maintainer wouldn't be able to do something about).



Ctrl-c should use the primary clipboard. And so should Ctrl-v by other apps.
If you use mouse middle button click to paste, that usually uses the 2ndary clipboard.
But I wouldn't see how a mismatch of those would change on 2nd attempt.


Also afaik (someone needs to confirm), on Linux Clipboard can also work without clipboard manager. The content is always queried of the "providing" app, at the time when it is pasted.
That is why on some Linux, if you copy something, close the app, and then try to paste => that fails. (At least I have been told..)
But again, I take it you still have the app with the SynEdit open?


I wonder, if it could be to do with the clipboard content marker... e.g. text/plain or unicode or whatever there is. IIRC I saw some Linux specific mention of that (maybe  search the bug tracker). If so that would be WS. E.g. a general problem of copy and paste (though TEdit/TMemo bypass that part of the WS afaik, but e.g. rich edit would probably use that too)



Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12539
  • Debugger - SynEdit - and more
    • wiki
Re: Synedit copy /past not working on first try
« Reply #2 on: July 02, 2026, 04:15:18 pm »
Also tested on my Mint 20.1 (Mate). Works on first copy.

My tests use the IDE as source for the copy (since the source edit is a SynEdit).
- open IDE, open Geany
- Copy some random text in geany (select, ctrl-c) to have something in the clipboard
- Copy some text in the IDE source edit (I usually just press ctrl-c and let the editor auto-select the current word)
- Paste in Geany  (Ctrl-v)
And I get the text from the IDE on the first Ctrl-v.




So my current assumption is that there is some difference in your setup that needs to be known to further look into the issue.

That doesn't mean the bug is in your setup. The bug can still be anywhere. So likely it is either in your Desktop or in the WidgetSet of the LCL. (Which needs the report to be about "Gtk2/QT/whatever WS do not copy to clipboard" => since that is maintained by some one else, and I can't help with it.




Mind you all my tests where gtk-2 (since all my Linux are in virtual machines, and I keep some of them deliberately old).

So if you used another WS, then maybe that needs to be known.


## EDIT: actually, my opensuse is currently using QT-6

Tomu

  • New Member
  • *
  • Posts: 44
Re: Synedit copy /past not working on first try
« Reply #3 on: July 02, 2026, 06:37:43 pm »
This problem does not occur in the IDE.
Also, I just started a new blank project, put a Synedit on the form, and ran. No problem.
I'll need to see what else is going on in my larger app, and will report back if I figure it out.

Thanks.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12539
  • Debugger - SynEdit - and more
    • wiki
Re: Synedit copy /past not working on first try
« Reply #4 on: July 02, 2026, 06:45:46 pm »
Maybe check
procedure TCustomSynEdit.CopyToClipboard;

You can debug that and see if Synedit writes to the clipboard:
      if not ClipHelper.WriteToClipboard(Clipboard) then

And follow the call into the clipboard to check if maybe something there is unexpected.

Tomu

  • New Member
  • *
  • Posts: 44
Re: Synedit copy /past not working on first try
« Reply #5 on: July 02, 2026, 06:52:22 pm »
If I start with a bare-bones project there is no problem.
If I add a Synedit highlighter, still no problem.
However once I add a TSynCompletion the problem appears.

Demo showing problem attached.

Tomu

  • New Member
  • *
  • Posts: 44
Re: Synedit copy /past not working on first try
« Reply #6 on: July 02, 2026, 07:18:43 pm »
If there is no TSynCompletion at design-time and I create one at runtime there is also no cut/paste problem.

For example
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   sec : TSynCompletion;
  4. begin
  5.   sec := TSynCompletion.Create(Self);
  6.   sec.Editor := SynEdit1;
  7.   sec.ItemList.Add('tom');
  8.   sec.ItemList.Add('dick');
  9.   sec.ItemList.Add('harry');
  10. end;
  11.  

Even after the button press there is no problem.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12539
  • Debugger - SynEdit - and more
    • wiki
Re: Synedit copy /past not working on first try
« Reply #7 on: July 02, 2026, 07:50:41 pm »
I will try to test that asap. But a bit busy now. Also probably traveling next week.

Tomu

  • New Member
  • *
  • Posts: 44
Re: Synedit copy /past not working on first try
« Reply #8 on: July 02, 2026, 08:14:21 pm »
Adding one TSynCompletion at design-time works (as above).
However adding a 2nd TSynCompletion has a problem - the short cut assigned to the 2nd one brings up the first one. The short-cut assigned the first one also brings up the first one.
Code: Pascal  [Select][+][-]
  1. implementation
  2.  
  3. uses
  4.    LCLType,LCLIntf, LCLProc;
  5.  
  6. {$R *.lfm}
  7.  
  8. { TForm1 }
  9.  
  10. procedure TForm1.Button1Click(Sender: TObject);
  11. var
  12.   sec : TSynCompletion;
  13.   Anotherone : TSynCompletion;
  14. begin
  15.   sec        := TSynCompletion.Create(Self);
  16.   Anotherone := TSynCompletion.Create(Self);
  17.  
  18.   sec.Editor        := SynEdit1;
  19.   Anotherone.Editor := SynEdit1;
  20.  
  21.   sec.ItemList.Add('tom');
  22.   sec.ItemList.Add('dick');
  23.   sec.ItemList.Add('harry');
  24.   sec.ShortCut:= KeyToShortCut(VK_T,[ssAlt]);
  25.  
  26.  
  27.    Anotherone.ItemList.Add('Monday');
  28.    Anotherone.ItemList.Add('Wednesday');
  29.    Anotherone.ItemList.Add('Friday');
  30.    Anotherone.ShortCut:= KeyToShortCut(VK_F,[ssAlt]);
  31.  
  32.  
  33. end;        
  34.  


Thaddy

  • Hero Member
  • *****
  • Posts: 19433
  • Glad to be alive.
Re: Synedit copy /past not working on first try
« Reply #9 on: July 02, 2026, 08:36:59 pm »
Shortcuts are mutually exclusive because they are program ( or even system) global.
You can mitigate that by redirecting the event  where you use PasteFromClipBoard to the focused control
« Last Edit: July 02, 2026, 08:39:34 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Tomu

  • New Member
  • *
  • Posts: 44
Re: Synedit copy /past not working on first try
« Reply #10 on: July 02, 2026, 09:00:25 pm »
The short-cut issue is separate and unrelated to the cut/paste problem.
The cut/paste problem does not occur when the TSynCompletion is created at runtime.
But creating 2 TSynCompletion objects at runtime seems to have a short-cut issue.

The cut/paste problem only seems to happen when a TSynCompletion is created at design time.



Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12539
  • Debugger - SynEdit - and more
    • wiki
Re: Synedit copy /past not working on first try
« Reply #11 on: July 02, 2026, 09:09:03 pm »
Adding one TSynCompletion at design-time works (as above).
However adding a 2nd TSynCompletion has a problem - the short cut assigned to the 2nd one brings up the first one. The short-cut assigned the first one also brings up the first one.

Is that related to the copy/paste issue?

------------------------

On its own, ignoring the copy paste...

In Synedit, every shortcut translates into an "editor command" (internally a number, there are constants like ecLeft, ecRight) => this one translates into ecSynCompletionExecute

Once the command is known, the command is then translated into the action (calling the code). The first completion will be the first to be able to catch the command.

You can assign a new command (from the ecUser) range to the 2nd completion
Code: Pascal  [Select][+][-]
  1. property ExecCommandID:

And, yes, I know documentation is badly missing... The entire completion could do with an overhaul. But there is plenty other to do first :(


While it is (probably / I never tested it) possible to have several completion, the original idea was to have one, and to adjust its proposal list on the fly.

-----------
For reference / if interested, the magic happens in
Code: Pascal  [Select][+][-]
  1.   TSynCompletion = class(TSynBaseCompletion)
  2.     procedure TranslateKey(Sender: TObject; Code: word; SState: TShiftState;
  3.       var Data: pointer; var IsStartOfCombo: boolean; var Handled: boolean;
  4.       var Command: TSynEditorCommand; FinishComboOnly: Boolean;
  5.       var ComboKeyStrokes: TSynEditKeyStrokes);
  6.     procedure ProcessSynCommand(Sender: TObject; AfterProcessing: boolean;
  7.               var Handled: boolean; var Command: TSynEditorCommand;
  8.               var AChar: TUTF8Char; Data: pointer; HandlerData: pointer);
  9.  

Tomu

  • New Member
  • *
  • Posts: 44
Re: Synedit copy /past not working on first try
« Reply #12 on: July 02, 2026, 09:56:58 pm »
"In Synedit, every shortcut translates into an "editor command" (internally a number, there are constants like ecLeft, ecRight) => this one translates into ecSynCompletionExecute"

Are you saying there could be a short-cut collision between the default Synedit short-cuts, and the ones I assign to the TSynCompletion controls I create at runtime? If so, that can't be it, because both VK_T and VK_F both work if assigned to the first created TSynCompletion.

As far as having 2 TSynCompletion controls attached to the editor, my larger app (where I discovered to cut/paste problem) has 2 TSynCompletion components which work fine except for the cut/paste problem.

Tomu

  • New Member
  • *
  • Posts: 44
Re: Synedit copy /past not working on first try
« Reply #13 on: July 02, 2026, 11:29:09 pm »
Got the short-cut working for both runtime created components.

The mysterious 'fix' was : Anotherone.ExecCommandID:= 100;
So now the cut/paste issue goes away if the TSynCompletion widgets are created at runtime instead of at design time.

The short-cut mystery (at least for me...) is avoided by the magic incantation "Anotherone.ExecCommandID:= 100"

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   sec : TSynCompletion;
  4.   Anotherone : TSynCompletion;
  5. begin
  6.   sec        := TSynCompletion.Create(Self);
  7.   Anotherone := TSynCompletion.Create(Self);
  8.  
  9.   sec.Editor        := SynEdit1;
  10.   Anotherone.Editor := SynEdit1;
  11.  
  12.   sec.ItemList.Add('tom');
  13.   sec.ItemList.Add('dick');
  14.   sec.ItemList.Add('harry');
  15.   sec.ShortCut:= KeyToShortCut(VK_F,[ssAlt]);
  16.  
  17.  
  18.    Anotherone.ItemList.Add('Monday');
  19.    Anotherone.ItemList.Add('Wednesday');
  20.    Anotherone.ItemList.Add('Friday');
  21.    Anotherone.ExecCommandID:= 100;
  22.  
  23.    Anotherone.ShortCut:= KeyToShortCut(VK_T,[ssAlt]);
  24.  
  25.  
  26. end;            
  27.  

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12539
  • Debugger - SynEdit - and more
    • wiki
Re: Synedit copy /past not working on first try
« Reply #14 on: July 02, 2026, 11:38:22 pm »
100 is not a user value... You got lucky so its currently not used...

function AllocatePluginKeyRange(1, False) returns unique free values.

Or start from ecUserFirst upwards.

Quote
Are you saying there could be a short-cut collision between the default Synedit short-cuts

Yes and no, more no...

There is a difference between the shortcut and the command.

E.g. if you use syncro edit, then the same shortcuts are changed to become other commands while syncro is active....

-------------
So what happens:
Stage 1: Translate shortcode (the keys from keyboard) into a command.
Stage 2: Execute the command.


If you have 2 completion dropdowns, and they both translate their keys to the same command, then it is no longer known what keycode  was used, or which plugin did return the code.

That happens even if you use keys, that aren't used by SynEdit at all. The problem is the overlap in the command that is found.




The reason is  that commands can also be injected by user code. You could have code that injects the command to start completion, based on stuff going on outside of synedit....

Or you can set up mouse clicks, that will execute commands. ...

And, if you use multi caret, then some commands are intercepted, because they wouldn't work. So the 2 phase about have to be done completely independent of each other.

« Last Edit: July 02, 2026, 11:41:58 pm by Martin_fr »

 

TinyPortal © 2005-2018