script plugin

Script Plugin (LUA) development forum
jfouche
CodeLite Guru
Posts: 351
Joined: Mon Oct 20, 2008 7:26 pm
Genuine User: Yes
IDE Question: C++
Location: France
Contact:

Re: script plugin

Post by jfouche »

eranif wrote:I am new to GitHub, but I suspect that we could try their "pull request" mechanism as a method for patching codelite (when the plugin is mature enough)
As i'm curious, I would like to try it also
eranif wrote:Does the plugin need the Lua libraries, if so, how are they obtained? (build them locally, download them etc)
Lua is very light, and I currently embed the source in the plugin. But I think that I will externlize it in the 3rd party lib, as a static lib (like sqlite). It will be easier to update the lua library if needed / wanted.
eranif wrote:Do you have access to a Linux / OSX machines where this plugin can be tested?, or are you going to focus only on Windows?
That's the point... I only have a win machine. I can try to use a linux virtual machine, but not very confident about this, sorry :oops:
Jérémie
petah
CodeLite Expert
Posts: 231
Joined: Sat Nov 24, 2012 8:04 pm
Genuine User: Yes
IDE Question: c++
Location: Los Angeles
Contact:

Re: script plugin

Post by petah »

Hi guys,
jfouche wrote:
eranif wrote:Does the plugin need the Lua libraries, if so, how are they obtained? (build them locally, download them etc)
Lua is very light, and I currently embed the source in the plugin. But I think that I will externlize it in the 3rd party lib, as a static lib (like sqlite). It will be easier to update the lua library if needed / wanted.
Whatever embeds Lua usually exports its symbol table so that 3rd party Lua libraries can bind to it without duplicating code.

People usually don't believe how light Lua is before seeing it: http://www.lua.org/download.html (don't blink while the LuaVM compiles because you might miss it ;) ). The author's (Roberto Ierusalimschy) Lua reference manual is 100 pages, I often confuse it with K&R's ANSI C ref manual when I pull it out of my bookshelf :)
jfouche wrote:
eranif wrote:Do you have access to a Linux / OSX machines where this plugin can be tested?, or are you going to focus only on Windows?
That's the point... I only have a win machine. I can try to use a linux virtual machine, but not very confident about this, sorry :oops:
Don't worry I'll help. Actually I wrote my custom Debian installer in Lua (yes I'm a Lua freak), attached here. The Mac I have has been lent to me but I may get another one for future testing.

If your PC is powerful enough I can create a Qemu virtual machine container for you with Debian & Xfce, then convert it to a VDI that you can load into VirtualBox. You'd then just have to install Oracle's driver extensions.

cheers,

-- p
You do not have the required permissions to view the files attached to this post.
main: Debian Jessie x64 + custom wxTrunk
jfouche
CodeLite Guru
Posts: 351
Joined: Mon Oct 20, 2008 7:26 pm
Genuine User: Yes
IDE Question: C++
Location: France
Contact:

Re: script plugin

Post by jfouche »

Hi,

I uploaded a repo on github : https://github.com/jfouche/codelite
It's on the LUA_SCRIPT branch

For those who want to give a try : I attached the windows plugin, based on trunk (it will not work vs 5.4 because the interface changed few days ago), but i can provide it.

A sample script which a comment the current selection (if any) :

Code: Select all

editor = cl_manager:GetActiveEditor()
if editor then
   sel = editor:GetSelection()
   if #sel then
      text = "/* " .. sel .. " */"
      editor:ReplaceSelection(text)
   end
end
Enjoy ;)
You do not have the required permissions to view the files attached to this post.
Jérémie
petah
CodeLite Expert
Posts: 231
Joined: Sat Nov 24, 2012 8:04 pm
Genuine User: Yes
IDE Question: c++
Location: Los Angeles
Contact:

Re: script plugin

Post by petah »

Champagne! :D

I'll try to build it on Linux this week-end, possibly on Mac too... as soon as I figure how to download only your plugin dir into my local CL trunk repo.

cheers,

-- p
main: Debian Jessie x64 + custom wxTrunk
jfouche
CodeLite Guru
Posts: 351
Joined: Mon Oct 20, 2008 7:26 pm
Genuine User: Yes
IDE Question: C++
Location: France
Contact:

Re: script plugin

Post by jfouche »

Hello Petah,
petah wrote:as soon as I figure how to download only your plugin dir into my local CL trunk repo.
If you already have a codelite repo, you just have to add a remote on my github :

Code: Select all

git remote add jfouche https://github.com/jfouche/codelite
This way, you can track both repo. Then you can merge or rebase your local work with my branch : jfouche/LUA_SCRIPT

Moreover, I will try to maintain sync my work with Eran: I'll merge or rebase my branch on Eran's master branch as much as possible, so that my branch should be up to date.
Jérémie
petah
CodeLite Expert
Posts: 231
Joined: Sat Nov 24, 2012 8:04 pm
Genuine User: Yes
IDE Question: c++
Location: Los Angeles
Contact:

Re: script plugin

Post by petah »

damn I just lost my previous post :o

I read up on a bunch of different git features but none seemed to return a subpath like SVN would, i.e. not without downloading the entire remote repo so I just have a full copy of your repo next to Eran's, which is fine really.

I think if found 2 bugs my compiling with clang: lua_stack_dump() needs parentheses around

Code: Select all

oss << (lua_toboolean(L, i) ? "true" : "false");
and RunScript() should return a bool. Also clang complains about

Code: Select all

extern "C" EXPORT IPlugin*
because it's returning a C++ object, should this be ignored?

I spent a while trying to configure a CL build... then in the end saw Eran recommends a CMake :D. In general I use a bunch of env vars (system-wide, CL-wide, then project-wide in common settings -> environment) so I can redirect to my custom wx location, CL source and CL bin, also I guess I should move the hardcoded win path in OnRun() to an env var for testing?

I have to return to my day job project now, I guess it'll take a few ping-pongs to get win & linux targets building in harmony.

a+

-- p
main: Debian Jessie x64 + custom wxTrunk
User avatar
eranif
CodeLite Plugin
Posts: 6367
Joined: Wed Feb 06, 2008 9:29 pm
Genuine User: Yes
IDE Question: C++
Contact:

Re: script plugin

Post by eranif »

Code: Select all

extern "C" EXPORT IPlugin*
This can be ignored (just a warning) I will fix this

Eran
Make sure you have read the HOW TO POST thread
petah
CodeLite Expert
Posts: 231
Joined: Sat Nov 24, 2012 8:04 pm
Genuine User: Yes
IDE Question: c++
Location: Los Angeles
Contact:

Re: script plugin

Post by petah »

Salut Jeremie, j'ai pas mon keyboard plugin pour les accents, je vais devoir t'appeller "Big Jim" (non je deconne).

Desole de pas etre plus actif sur le plugin Lua, je dois finir un projet infernal avec un port iOS que je connais pas du tout et Xcode me rend barge, c'est un trou noir de productivite ;) J'espere avoir plus de temps la semaine prochaine pour te prouver que je suis un accro a Lua.

Sorry for the short French message; it's a low-traffic sub-group for now anway.

a+

-- p
main: Debian Jessie x64 + custom wxTrunk
jfouche
CodeLite Guru
Posts: 351
Joined: Mon Oct 20, 2008 7:26 pm
Genuine User: Yes
IDE Question: C++
Location: France
Contact:

Re: script plugin

Post by jfouche »

Hi

I finally succeded at managing "hook scripts". So, here is the current design :
- There are 2 kind of scripts : on-demand scripts and hook scripts
- The on-demand script are called by the user, on demand. Those scripts are stored in "codelite user data"/scripts directory.
- The hook scripts reacts to codelite events. Those scripts are stored in "codelite user data"/hooks directory.

Here is a "on-demand" script :

Code: Select all

editor = codelite.manager:GetActiveEditor()
if editor then
	sel = editor:GetSelection()
	if #sel > 0 then
		text = "/* " .. sel .. " */"
		editor:ReplaceSelection(text)
	end
end
and here is a "hook" script :

Code: Select all

local function do_something(event)
	-- do what need
	-- The codelite variable is available
end

codelite.Bind(wxEVT_FILE_SAVED, do_something)
Now, I need to implement the user interface to manage those scripts:
1 - A settings UI, which allow the user ta add/remove/edit hooks and on-demand scripts
2 - A mini frame that only shows the user the "on-demand" scripts. A double click on a script name should launch it.

If anybody have feedback on the design, and feature requests, tell me
Petah, I'm pretty sure you have lots of ideas and suggestions ;)
Jérémie
petah
CodeLite Expert
Posts: 231
Joined: Sat Nov 24, 2012 8:04 pm
Genuine User: Yes
IDE Question: c++
Location: Los Angeles
Contact:

Re: script plugin

Post by petah »

half-way reading through your post I was getting ready to propose a single Lua table to define hooks, but actually I think your solution to explicitly bind hooks from Lua is even more elegant :D so... proceeding with humility:

- if editor:GetSelection() is empty I recommend returning nil (with lua_pushnil()) and if it can potentially return an error, push the error string as a 2nd argument. That would follow Lua convention.

- about [1], given that hooks are bound from Lua, the point is just to pass the UI state to Lua, which will then hook the appropriate function right? I'm thinking a generic 2D property table UI defined from a [name][type][widget][val] tuple/list, which on edit populates a codelite.config Lua table, then calls "OnConfigUpdated()" Lua hook would do the trick no?

The Lua script could then parse the table and rebind hooks, plus the UI property table could be used to customize anything really. F.ex remapping on-demand scripts (like a look-up table), set debug flags or other variables. You could even have the UI query Lua for the definition table before it's displayed by your plugin so the UI can be constructed conditionally.

F.ex UI plugin C++ function calls OnRequestUIDef() hook in Lua

Code: Select all

function OnRequestUIDef()
  local def = {}
  table.insert(def, { name = "debug", type = "boolean", widget = "checkbox", val = codelite.config.debug })
  if (codelite.config.debug) then
    table.insert(def, { name = "debug level", type = "integer", widget = "spinctrl", val = codelite.config.level or 0, min = 0, max = 5})
  end
  table.insert(def, { name = "tab size", type = "integer", widget = "slider", val = codelite.config.tab_size or 4, min = 1, max = 16})
  return def
end
(j'ecris au pif, donc ca compile si ca veut ;) )

On first invocation the UI shows only the debug checkbox, unchecked, upon checking the debug option it'll show the debug level spinCtrl too. Not the most useful example but you get the idea; it's a generic property editor that can solve different problems. The "integer" type is just to help the UI builder; obviously it's just a number to Lua.

- about [2] I would suggest a similar model: Lua returns a table of { "action_name", "action_function_name" } string pairs the UI shows in a listbox. Returning function names as strings vs the Lua functions themselves so prefs can be saved to disk.

- I'm not sure about [1]'s edit function, I'm guessing you can hardcode opening a CL window with the script.

- you'd ship standard/template Lua scripts to do all this, which most users would probably use as-is and never edit

- with all this low-level ping-pong you'd need solid Lua error checking, probably wrap arguments in a "return {...}" chunk you call luaL_dostring() on before it's passed between C++ and Lua contexts so on error it's caught & displayed immediately.

Overkill? Nah :D

a+

-- p
main: Debian Jessie x64 + custom wxTrunk
Post Reply