I am aware that there are already some Python based tools that can achieve most of these items, but I was wondering if anyone would be willing to provide Lua based versions to use with the GeanyLua plugin. Each one would be its own separate script.
`All Uppercase` APPLE TREE
`All Lowercase` apple tree
`Capitalize just the first letter` Apple tree
`Capitalize the first letter of each word` Apple Tree
Thank You
If you use only letters of the Latin alphabet, then everything is very simple: ```lua local s = geany.selection() if (s ~= nil) and (s ~= "") then geany.selection(string.upper(s)) end ``` ```lua local s = geany.selection() if (s ~= nil) and (s ~= "") then geany.selection(string.lower(s)) end ``` ```lua local s = geany.selection() if (s ~= nil) and (s ~= "") then local s1= string.upper(string.sub(s, 1, 1)) local s1 = string.lower(string.sub(s, 2, -1)) geany.selection(s1 .. s2) end ``` ```lua local s = geany.selection() if (s ~= nil) and (s ~= "") then s = string.gsub(string.lower(s), "^(%w)", string.upper) s = string.gsub(s, "([^%w]%w)", string.upper) geany.selection(s) end ``` If you need support for other languages, then everything becomes interesting. UTF-8 support is required, [for example](http://lua-users.org/wiki/LuaUnicode). I use [luautf8](https://github.com/starwing/luautf8) in the scripts folder + ```lua local a if (geany.dirsep == '/') then a = '?.so'; -- Unix else a = '?.dll'; -- MS Windows end
local sf = geany.dirname(geany.script) local p = sf .. geany.dirsep .. a if not string.find(package.cpath, p) then package.cpath = package.cpath .. ';' .. p end
local utf8 = require "lua-utf8"
local s = geany.selection() if ... ``` and so on...
@Skif-off Thank you very much for working on this. All the tools worked great but one. The "Capitalize just the first letter" tool generated an error message.
I changed this ``` local s1= string.upper(string.sub(s, 1, 1)) local s1 = string.lower(string.sub(s, 2, -1)) ``` To this ``` local s1 = string.upper(string.sub(s, 1, 1)) local s2 = string.lower(string.sub(s, 2, -1)) ``` Which removed the error and works but it only applies the tool to one line or the first line if multiple lines ares selected. `The apple tree` `the pear tree`
If can figure out why this does not apply to multiple lines, you can just update the original version of the code you posted above. I noticed one more thing but I will mention in post below this one, if interested.
Thank You
These scripts apply to selected content within a document and then deselect that content after script is applied. Would it be possible to have that content remain selected after the script is applied?
I looked through the GeanyLua documentation a little and did not really see anything about keeping items selected after.
If this is possible, I like both, with and without selection after script is applied. If there is a way to achieve this with a simple code that can be added to each tool you have provided, can you show me one of them with this as an example so I can apply that "stay highlighted" code to each of them.
Still confused about what Lua can do with command line commands / bash shell scripts, I updated my other post with this "with selection" and "without selection" information as well, if interested #4070.
Thank You
Would it be possible to have that content remain selected after the script is applied?
Yes, but "restore" instead of "remain", see `geany.select()`: ```lua local start, stop = geany.select() local s = geany.selection() ... geany.selection(...) geany.select(start, stop) end ```
If can figure out why this does not apply to multiple lines
I can :) This is not provided, I don't know a suitable algorithm.
@Skif-off Okay first, thank you for your continued help. That is okay about the capitalize first letter tool if you cannot find a suitable algorithm, at least it works on single lines.
RESTORE SELECTION For the “restore” selection, I might not have applied this right but I tried adding the code provided to the top and also tried adding to the bottom of one of the case tool scripts and I received the following error dialog ``` Lua script error: <path-of-script>/<script-name.lua>:3: unexpected symbol near '...' ``` (the number :3: is the line it shows up on so that changes depending on where this code is added to)
Thank You
P.S. Also thank you for mentioning the “restore” not “remain”. I like when you do stuff like this, just lets me know what the script is actually doing to achieve what I am looking for, which is good to know, thank you.
Sorry, here I used ellipsis to just skip the extra text :) I meant, for example, "All Uppercase": ```lua local start, stop = geany.select() local s = geany.selection() if (s ~= nil) and (s ~= "") then geany.selection(string.upper(s)) geany.select(start, stop) end ``` and so on.
@Skif-off Thank you for your response, no problem. Okay, these all works now. Thank you so much for working on these, they work great :)
For any users interested in using these tools in the future, here are the final versions below.
APPLY CASE ``` local s = geany.selection() if (s ~= nil) and (s ~= "") then geany.selection(string.lower(s)) end
local s = geany.selection() if (s ~= nil) and (s ~= "") then geany.selection(string.upper(s)) end
local s = geany.selection() if (s ~= nil) and (s ~= "") then local s1 = string.upper(string.sub(s, 1, 1)) local s2 = string.lower(string.sub(s, 2, -1)) geany.selection(s1 .. s2) end
local s = geany.selection() if (s ~= nil) and (s ~= "") then s = string.gsub(string.lower(s), "^(%w)", string.upper) s = string.gsub(s, "([^%w]%w)", string.upper) geany.selection(s) end ```
APPLY CASE & KEEP ITEMS SELECTED ``` local start, stop = geany.select() local s = geany.selection() if (s ~= nil) and (s ~= "") then geany.selection(string.lower(s)) geany.select(start, stop) end
local start, stop = geany.select() local s = geany.selection() if (s ~= nil) and (s ~= "") then geany.selection(string.upper(s)) geany.select(start, stop) end
local start, stop = geany.select() local s = geany.selection() if (s ~= nil) and (s ~= "") then local s1 = string.upper(string.sub(s, 1, 1)) local s2 = string.lower(string.sub(s, 2, -1)) geany.selection(s1 .. s2) geany.select(start, stop) end
local start, stop = geany.select() local s = geany.selection() if (s ~= nil) and (s ~= "") then s = string.gsub(string.lower(s), "^(%w)", string.upper) s = string.gsub(s, "([^%w]%w)", string.upper) geany.selection(s) geany.select(start, stop) end ```
Keep in mind that these methods will work with Latin letters only and it's not very convenient.
github-comments@lists.geany.org