I was wondering if anyone knew if GeanyLua plugin / Lua scripts in general can run Linux command line commands, similar to the Geany build in feature "Send to Custom Commands", and apply them to documents in the same way.
For a quick example, it was pointed out to me that the "sort" command can be used within documents. `sort -f` Which changes the sort order of the highlighted items in alphabetical order.
Does anyone by any chance know if this is possible to do the same using GeanyLua plugin with a Lua script? If so would anyone be willing to provide a kind of "template" Lua script that can achieve this? Template can use the same example mentioned above "sort -f" Then me/other users can just edit the "template" script changing the command and any options desired to apply to document in Geany.
Thank You
Again not sure what abilities Lua scripts have with handling Linux command line commands / bash shell scripts. If this can be done I noticed something in a different script that I thought I would mention for this one as well. I noticed that Lua scripts get applied to highlighted/selected text, but after the scripts are applied they deselect all the text. Was wondering if someone could provide example/template for each of the following.... 1.) Deselect text after Lua script gets applied 2.) Text remains still selected after Lua script is applied Can still use example I mentioned above with "sort -f"
Thank you if anyone is interested in sharing some ideas.
Ahh I was hoping something like this would have been possible. Especially because achieving this in Lua would allow for the items selected to stay selected, as I mentioned in my post above. Currently the built in command line capabilities of the internal Geany feature "Send to Custom Commands" cannot achieve this.
So Lua / GeanyLua plugin cannot run command line commands within Lua scripts?
So Lua / GeanyLua plugin cannot run command line commands within Lua scripts?
This is a wrong conclusion.
@Skif-off Thank you for your response. I also have seen you responded to my other posts, thank you for those as well, I wanted to respond to them but I am having a couple issues preventing me from testing them out fully at the moment.
You are right I did not say this correctly, I meant more like this cannot be done in a "template" style approach that I was looking for. My all purpose, easy "template" to modify Lua requests usually are not possible :) Do you know if that is true for this case as well? There is no way to have a Lua script that can just in essence mimic a command line? Meaning I / other users can type the command line command + options (such as "Sort -f") into a line in the Lua script and it applies to the document, like how Geany built in feature "Send to Custom Commands" does. but then I / other user can then replace "Sort -f" command with a different command. Or does each Lua script need to be specifically made per command line command?
At the moment, the main command I would be looking for with Lua would be to use "Sort -f" but it would be nice to be able to have a "template" style script that could be edited for any command line command to be used.
Thank you again for all your help
Let's try to use a temporary file, other ways are not very clear to me yet. ```lua local aTemplate = 'sort -f "$TMPFILE"'
local sTmpFile, sTmpStr, bFullText, sCommand
local aFileInfo = geany.fileinfo() if aFileInfo.ext == nil then sTmpFile = os.tmpname() .. ".txt" else sTmpFile = os.tmpname() .. aFileInfo.ext end
local iStart, iStop = geany.select() local h = io.open(sTmpFile, "wb") if h == nil then geany.message("Fail 1.") return end sTmpStr = geany.selection() if sTmpStr == nil then geany.message("Fail 2.") return elseif sTmpStr == "" then sTmpStr = geany.text() bFullText = true end h:write(sTmpStr) h:close()
sCommand = string.gsub(sTemplate, "%$TMPFILE", sTmpFile) h = io.popen(sCommand, "r") if h == nil then geany.message("Fail 3.") return end sTmpStr = h:read("*a") h:close() sTmpStr = string.gsub(sTmpStr, "\r*[\r\n]$", "") if bFullText == true then geany.text(sTmpStr) geany.select(iStart) else geany.selection(sTmpStr) geany.select(iStart, iStop) end geany.message("Done.") ``` See the template in the value of the `sTemplate` variable (first line). If you want to use, for example, `grep`, then try ```lua local sTemplate = 'cat "$TMPFILE" | grep "some text"' ``` Probably, in this case it is better to use the `geany.choose` function instead of separate scripts.
Try with and without selection.
@Skif-off Thank you for working on this tool as well
`"sort -f" TOOL` (I will remove this part after I know you have read and after you have made quick edit to script you provided above) At first when I ran this script an error dialog appeared ``` Lua script error: <path>/<script-name.lua>:29:bad argument #1 to ‘gsub’ (string expected, got nil) stack traceback: [C]: in function ‘gsub’ <path>/<script-name.lua>:29: in main chunk ``` I changed `aTemplate` to `sTemplate` in the first line and then this worked. If this correct? this is the quick edit I mentioned above, could you update your script above.
------------------------------------------------------------
`"grep" TOOL` This worked great. Funny because the grep example you mentioned helped out in multiple ways. -I tested it out by replacing the first line with it which helped me to realize the “aTemplate” needed to be “sTemplate” in the “sort -f” tool. -“grep” was one of the commands I was planning on trying, glad you provided this example, I probably would not have been able to figure that out -I was trying to figure out how to develop a find tool using the “find” command, to use with the divider tool you created. Because of “grep” tool example, you mentioned `geany.choose`, I will look into this and see if it has any benefit for that tool.
------------------------------------------------------------
`WITH & WITHOUT SELECTION` Good thing you mentioned to try this also without selection. Wow! Thank you for thinking of and figuring out how to combine this into the same tool. This works perfectly.
------------------------------------------------------------
I obviously have not tried this out with every available command :) but the few that I have tried so far have worked great! :) Thank you again so much for providing this script and examples, really appreciate it.
Fixed. I wanted to use `geany.choose` because it would allow to choose from several commands, but then I remembered the problem with this function (it has already been fixed) and missed the variable name... :(
you mentioned `geany.choose`, I will look into this and see if it has any benefit for that tool.
See screenshots [here](https://github.com/geany/geany-plugins/issues/690).
About `grep`, `find` and other, just keep in mind: script creates a temporary file (`$TMPFILE`) with the selected text in the "/tmp" directory and the command that will use it must write something to the terminal output.
github-comments@lists.geany.org