mav_it
Group: Members
Posts: 7
Joined: Mar. 2007 |
|
Posted: Nov. 05 2007,22:54 |
|
I have managed to reach what I think is a show-stopper.. I have addressed many missing (unimplmented ? ) functions, but currently I cannot impement serching within the text buffer, since using the Fl_text_buffer:search_forward(), or Fl_textBuffer:search backward(), murgaLua gives a "search_forward nil method" error.
Anyway, by using keyboard, the editor is working, except for find/replace functions.
The portion of the code that is not working is :
Code Sample | -- SEARCH MENU CALLBACKS function find_cb() local str local fnd str = fltk.fl_input("Insert a string to search", lastsearch) if (str ~= 0) then fnd = tb:search_forward(0,str,fnd,0) if (fnd ~= 0) then tb:select(fnd, tb:word_end(fnd)) else fltk.fl_message("String " .. str .. "Not found!") end end end
|
Here, the tb:search_forward(0,str,fnd,0) (tb is a Text_Buffer variable) does not work, and MurgaLua gives:
Code Sample | attempt to call method "search_forward" : a nil value
|
I suspect that this method has not been implemented in MurgaLua.
The complete editor, should anyone find it useful for anything, is the following (note that the replace dialog is not implemented, being not able to perform the search...
Code Sample | #!/bin/lua
version = "0.01"
ww = 1024 wh = 768
title = "MURGA LUA FLTK EDITOR - "
-- -- Main window w = fltk:Fl_Double_Window(ww,wh,title) -- -- Text Buffer to hold file to edit tb = fltk:Fl_Text_Buffer();
changed = false filename = "" saved = true newfile = false lastsearch = ""
-- This will be used to hold copied / cutted text clipboard = ""
-- Editor widget will work for standard keyboard shortcuts ed = fltk:Fl_Text_Editor(10,30,ww-20,wh-60) ed:buffer(tb)
function ask_save(object) if (not saved) then if fltk.fl_choice("There are unsevd Buffers. Do you want to save ?","No",nil,"Yes") >= 1 then save_cb() end end end
-- check if we need to save to close function check_save(object) if (not saved) then if fltk.fl_choice("There are unsevd Buffers. Do you Really want to exit ?","No",nil,"Yes") >= 1 then w:hide() end else w:hide() end end
w:callback(check_save)
-- Callback for menu bar function menuItem(object) --print(mb:text() .. ' --> ' .. mb:value()) if (mb:text()=="&New File") then new_cb() -- FILE MENU elseif (mb:text()=="&Open File") then open_cb() elseif (mb:text()=="&Insert File") then insert_cb() elseif (mb:text()=="&Save File") then save_cb() elseif (mb:text()=="Save File &As") then saveas_cb() elseif (mb:text()=="E&xit") then exit_cb() -- EDIT MENU elseif (mb:text()=="&Undo") then undo_cb() elseif (mb:text()=="Cu&t") then cut_cb() elseif (mb:text()=="&Copy") then copy_cb() elseif (mb:text()=="&Paste") then paste_cb() elseif (mb:text()=="&Delete") then delete_cb() -- SEARCH MENU elseif (mb:text()=="&Find...") then find_cb() elseif (mb:text()=="Find &Again") then findagain_cb() elseif (mb:text()=="&Replace...") then replace_cb() elseif (mb:text()=="Re&place Again") then replaceagain_cb() -- HELP MENU elseif (mb:value()==24) then about_cb() end end
-- FILE MENU CALLBACKS
function new_cb() ask_save() newfile = true filename = "untitled" saved = false tb:remove(1,tb:length()) w:label(title .. filename) end
function open_cb() ask_save() filename = fltk.fl_file_chooser("Select File to open", nil, nil, 0) if (filename ~= nil) then tb:loadfile(filename) w:label(title .. filename) saved = false end end
function insert_cb() local fname = fltk.fl_file_chooser("Select File to insert", nil, nil, 0) if (fname ~= nil) then local pos = ed:insert_position() tb:insertfile(fname,pos) end end
function save_cb() if (untitled) then saveas_cb() elseif (not saved) then if (tb:savefile(filename)) then saved = true fltk.fl_message("File " .. filename .. " Saved.") else fltk.fl_alert("Error writing file " .. filename) end end end
function saveas_cb() local fname = fltk.fl_file_chooser("FILENAME TO SAVE", nil, nil, 0) if (fname ~= nil) then if (tb:savefile(fname)) then filename = fname w:label(title .. filename) saved = true fltk.fl_message("File " .. filename .. " Saved.") else fltk.fl_alert("Error writing file " .. filename) end end end
function close_cb() ask_saved() end
function exit_cb() check_save() end
-- EDIT MENU CALLBACKS function undo_cb() end
function cut_cb() if ( tb:selected() ~= 0) then clipboard = tb:selection_text() tb:remove_selection() end end
function copy_cb() if (tb:selected() ~= 0) then clipboard = tb:selection_text() end end
function paste_cb() local pos = ed:insert_position() tb:insert(pos,clipboard) end
function delete_cb() if (tb:selected() ~= 0) then tb:remove_selection() end end
-- SEARCH MENU CALLBACKS function find_cb() local str local fnd str = fltk.fl_input("Insert a string to search", lastsearch) if (str ~= 0) then fnd = tb:search_forward(0,str,fnd,0) if (fnd ~= 0) then tb:select(fnd, tb:word_end(fnd)) else fltk.fl_message("String " .. str .. "Not found!") end end end
function findagain_cb() end
function replace_cb() replace_dlg:show() end
function replaceagain_cb() end
-- HELP CALLBACKS function about_cb() fltk.fl_message("MURGA LUA FLTK EDITOR\n\nMurga Lua Version of an FLTK editor example\nBy mav_it\n\nVersion " .. version) end
--- Build Up dynamically the menu: mb = fltk:Fl_Menu_Bar(0,0,ww,30) mb:callback(menuItem)
mb:add("&File/&New File", 0, 0) mb:add("&File/&Open File", 0, 0) mb:add("&File/&Insert File", 0, 0) mb:add("&File/&Save File", 0, 0) mb:add("&File/Save File &As", 0, 0) mb:add("&File/E&xit", 0, 0)
mb:add("&Edit/&Undo", 0, 0) mb:add("&Edit/Cu&t", 0, 0) mb:add("&Edit/&Copy", 0, 0) mb:add("&Edit/&Paste", 0, 0) mb:add("&Edit/&Delete", 0, 0)
mb:add("&Search/&Find...", 0, 0) mb:add("&Search/Find &Again", 0, 0) mb:add("&Search/&Replace...", 0, 0) mb:add("&Search/Re&place Again",0, 0)
mb:add("&Help/&About", 0, 0)
--This does not seem to be implemented in MurgaLua FLTK --tb:add_modify_callback(changed_cb, w) --tb:call_modify_callbacks()
-- REPLACE DIALOG replace_dlg = fltk:Fl_Window(300, 105, "Replace") replace_find = fltk:Fl_Input(70, 10, 200, 25, "Find:") replace_with = fltk:Fl_Input(70, 40, 200, 25, "Replace:") replace_all = fltk:Fl_Button(10, 70, 90, 25, "Replace All") replace_next = fltk:Fl_Return_Button(105, 70, 120, 25, "Replace Next") replace_cancel = fltk:Fl_Button(230, 70, 60, 25, "Cancel")
-- CALLBACKS FOR REPLACE DIALOG replace_cancel:callback(function () replace_dlg:hide() end )
-- MAIN PROGRAM w:show() Fl:run()
|
|