Recent

Author Topic: [Solved] How to Add Google Search to popupmenu in Editor.  (Read 2103 times)

loaded

  • Hero Member
  • *****
  • Posts: 569
[Solved] How to Add Google Search to popupmenu in Editor.
« on: January 15, 2022, 09:42:48 am »
Hi All,
Can I add items to the popup menu on the editor?
In the menu I added;
What do I need to do to be able to use the selected text on the editor in Google Searches?
« Last Edit: January 15, 2022, 12:11:56 pm by loaded »
If Ide=Lazarus 2.0.10 32 Bit and Os=Win 10 Home 64 Bit then Get up and do something useful! Because God is the helper of those who start again;

winni

  • Hero Member
  • *****
  • Posts: 3057
Re: How to Add Google Search to popupmenu in Editor.
« Reply #1 on: January 15, 2022, 10:11:13 am »
Hi!

A simple solution:
Code: Pascal  [Select][+][-]
  1. uses ...,LCLIntf;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var s : string;
  5.     i: integer;
  6. begin
  7. s := edit1.text;
  8. for i := 1 to length(s) do
  9.    if s[i] = #32 then s[i] := '+';
  10. openUrl('https://www.google.com/search?q='+s);
  11. end;                


Winni

loaded

  • Hero Member
  • *****
  • Posts: 569
Re: How to Add Google Search to popupmenu in Editor.
« Reply #2 on: January 15, 2022, 10:16:29 am »
Thank you very much for reply winni.
Yes it can be done with external code, but I want the IDE to do it.
If Ide=Lazarus 2.0.10 32 Bit and Os=Win 10 Home 64 Bit then Get up and do something useful! Because God is the helper of those who start again;

Thaddy

  • Hero Member
  • *****
  • Posts: 11544
Re: How to Add Google Search to popupmenu in Editor.
« Reply #3 on: January 15, 2022, 10:29:18 am »
Michael van Canneyt wrote a tutorial on how to extend the Lazarus IDE.
See https://www.freepascal.org/~michael/articles/lazide3/lazide3.pdf
It is rather old, though. I have no idea how current it is.
« Last Edit: January 15, 2022, 10:36:12 am by Thaddy »
Путин преступник. Россияне дезинформированы.

loaded

  • Hero Member
  • *****
  • Posts: 569
Re: How to Add Google Search to popupmenu in Editor.
« Reply #4 on: January 15, 2022, 10:36:59 am »
Thank you very much for reply Thaddy.
Yes this looks like a nice resource, I hope it helps me achieve what I want.

If Ide=Lazarus 2.0.10 32 Bit and Os=Win 10 Home 64 Bit then Get up and do something useful! Because God is the helper of those who start again;

GetMem

  • Hero Member
  • *****
  • Posts: 3745
Re: How to Add Google Search to popupmenu in Editor.
« Reply #5 on: January 15, 2022, 10:53:23 am »
@loaded

This is quite simple. Create a DesignTime only package, add the following code on the registration method:

Code: Pascal  [Select][+][-]
  1. uses MenuIntf, SrcEditorIntf, LCLIntf;  
  2.  
  3. procedure DoOnMenuClick;
  4. var
  5.   Str: String;
  6.   I: Integer;
  7. begin
  8.   Str := SourceEditorManagerIntf.ActiveSourceWindow.ActiveEditor.Selection;
  9.   //apply winni sugestion here
  10.   for I := 1 to length(Str) do
  11.     if Str[I] = #32 then
  12.       Str[I] := '+';
  13.   OpenUrl('https://www.google.com/search?q=' + Str);
  14. end;
  15.  
  16. procedure Register;
  17. var
  18.   IDEMenuItem: TIDEMenuItem;
  19.   M: TMethod;
  20. begin
  21.   IDEMenuItem := TIDEMenuItem.Create('Google search...');
  22.   M.Data := nil;
  23.   M.Code := @DoOnMenuClick;
  24.   IDEMenuItem.OnClick := TNotifyEvent(M);
  25.   SourceEditorMenuRoot.AddFirst(IDEMenuItem);
  26. end;
  27.  
Compile then install the package.



PS: Actually this is very good idea. It should be implemented in Lazarus trunk, directly in sourceeditor not in a package of course.
« Last Edit: January 15, 2022, 11:02:23 am by GetMem »

loaded

  • Hero Member
  • *****
  • Posts: 569
Re: How to Add Google Search to popupmenu in Editor.
« Reply #6 on: January 15, 2022, 12:11:40 pm »
Master Getmem, once again I express my gratitude to you. Numbers are not enough to say thank you anymore.
Thanks to you, I both solved my problem and made me sweat a bit, but I wrote my first package.
Maybe it will help someone;
I'm attaching the package I wrote here, it may be an amateur, don't laugh.  ;D
May God increase your knowledge.
If Ide=Lazarus 2.0.10 32 Bit and Os=Win 10 Home 64 Bit then Get up and do something useful! Because God is the helper of those who start again;

GetMem

  • Hero Member
  • *****
  • Posts: 3745
Re: [Solved] How to Add Google Search to popupmenu in Editor.
« Reply #7 on: January 15, 2022, 12:31:07 pm »
@loaded

You're welcome! Small correction: you should check if the string is empty before opening the browser:
Code: Pascal  [Select][+][-]
  1. uses Dialogs;
  2.  
  3. procedure DoOnMenuClick(Sender: TObject);
  4. var
  5.   Str: String;
  6.   I: Integer;
  7. begin
  8.   Str := SourceEditorManagerIntf.ActiveSourceWindow.ActiveEditor.Selection;
  9.   if Trim(Str) = '' then //add this
  10.   begin
  11.     MessageDlg('Please select something first!', mtInformation, [mbOk], 0);
  12.     Exit;
  13.   end;
  14.   //apply winni sugestion here
  15.   for I := 1 to length(Str) do
  16.     if Str[I] = #32 then
  17.       Str[I] := '+';
  18.   OpenUrl('https://www.google.com/search?q=' + Str);
  19. end;

Also instead of google use DuckDuckGo. :)

loaded

  • Hero Member
  • *****
  • Posts: 569
Re: [Solved] How to Add Google Search to popupmenu in Editor.
« Reply #8 on: January 15, 2022, 12:55:04 pm »
Master Getmem, yes, you are right as usual, I am making the relevant corrections.
There is so much to learn from you Masters !!!
If Ide=Lazarus 2.0.10 32 Bit and Os=Win 10 Home 64 Bit then Get up and do something useful! Because God is the helper of those who start again;

 

TinyPortal © 2005-2018