mle.lua 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. function table_to_string(tbl)
  2. local result = "{"
  3. for k, v in pairs(tbl) do
  4. -- Check the key type (ignore any numerical keys - assume its an array)
  5. if type(k) == "string" then
  6. result = result.."[\""..k.."\"]".."="
  7. end
  8. -- Check the value type
  9. if type(v) == "table" then
  10. result = result..table_to_string(v)
  11. elseif type(v) == "boolean" then
  12. result = result..tostring(v)
  13. else
  14. result = result.."\""..v.."\""
  15. end
  16. result = result..","
  17. end
  18. -- Remove leading commas from the result
  19. if result ~= "" then
  20. result = result:sub(1, result:len()-1)
  21. end
  22. return result.."}"
  23. end
  24. local offset;
  25. mle.editor_register_observer("cmd:cmd_save:before", function (ctx)
  26. offset = mle.mark_get_offset(ctx["mark"], 0);
  27. local buf = mle.buffer_get(ctx["buffer"])["ret_data"];
  28. local result = buf:gsub("[ \t]+%f[\r\n%z]", "");
  29. result = result:match("^%s*(.-)%s*$") .. "\n";
  30. mle.buffer_set(ctx["buffer"], result, string.len(result));
  31. end)
  32. mle.editor_register_observer("cmd:cmd_save:after", function (ctx)
  33. mle.mark_move_offset(ctx["mark"], offset["ret_offset"]);
  34. mle.bview_center_viewport_y(ctx["bview"]);
  35. end)
  36. mle.editor_register_cmd("cmd_lua_test", function (ctx)
  37. name = mle.editor_prompt("Enter your name")
  38. if name then
  39. print("hello <" .. name .. "> from lua")
  40. else
  41. print("you hit cancel")
  42. end
  43. end)
  44. mle.editor_register_cmd("cmd_delete_forward_to_char", function (ctx)
  45. local c = utf8.char(ctx["wildcard_params"][0]);
  46. mle.cursor_drop_anchor(ctx["cursor"], 0);
  47. mle.mark_move_next_str(ctx["mark"], c, string.len(c));
  48. mle.cursor_cut_copy(ctx["cursor"], 1, 0, 0);
  49. end)
  50. mle.editor_register_cmd("cmd_delete_back_to_char", function (ctx)
  51. local c = utf8.char(ctx["wildcard_params"][0]);
  52. mle.cursor_drop_anchor(ctx["cursor"], 0);
  53. mle.mark_move_prev_str(ctx["mark"], c, string.len(c));
  54. mle.cursor_cut_copy(ctx["cursor"], 1, 0, 0);
  55. end)