<p>I have prepared a script in Lua, that is able to implement the behavior described in my previous post:</p>
<pre><code>--[[
Script that reformats the C source code 
passing it via external command 
defined in variable "flt"
--]]
local flt = "astyle --indent-classes -Y"
line, column = geany.rowcol()
local fn=os.tmpname()
local fo=assert(io.open(fn,'w'))
assert(fo:write(geany.text()))
fo:close()
local cmd = "cat ".. fn .. " | " .. flt
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
os.remove(fn)
geany.text(s)
position=geany.rowcol(line,column)
geany.caret(position)
</code></pre>
<p>The cursor remains in the original location, or near to it. The only problem is that the position of the text on the screen may be changed (Usually after the processing, the line with the cursor is near to the bottom of the window).<br>
Of course you must activate the "Lua Script" plugin to use the above script.<br>
The script should be placed in <code>~/.config/geany/plugins/geanylua</code> directory or <code>~/.geany/plugins/geanylua</code> directory  (depending on distribution).<br>
You may also create a <code>hotkeys.cfg</code> file in the same directory, and put the filename of the above script in a line in that file. That allows you to assign a keyboard shortcut (e.g., CTRL+ALT+F) to that autoformatting script via <code>Edit->Preferences->Key bindings->Lua Script</code> .</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/geany/geany/issues/1913#issuecomment-409580277">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/ABDrJ3cmcfLEBlmVj5S-SlnQIY35bNkJks5uMbFTgaJpZM4VqVzB">mute the thread</a>.<img src="https://github.com/notifications/beacon/ABDrJzrSZx8WiSkRo2750A8CXE_vQMmwks5uMbFTgaJpZM4VqVzB.gif" height="1" width="1" alt="" /></p>
<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/geany/geany","title":"geany/geany","subtitle":"GitHub repository","main_image_url":"https://assets-cdn.github.com/images/email/message_cards/header.png","avatar_image_url":"https://assets-cdn.github.com/images/email/message_cards/avatar.png","action":{"name":"Open in GitHub","url":"https://github.com/geany/geany"}},"updates":{"snippets":[{"icon":"PERSON","message":"@wzab in #1913: I have prepared a script in Lua, that is able to implement the behavior described in my previous post:\r\n\r\n    --[[\r\n    Script that reformats the C source code \r\n    passing it via external command \r\n    defined in variable \"flt\"\r\n    --]]\r\n    local flt = \"astyle --indent-classes -Y\"\r\n    line, column = geany.rowcol()\r\n    local fn=os.tmpname()\r\n    local fo=assert(io.open(fn,'w'))\r\n    assert(fo:write(geany.text()))\r\n    fo:close()\r\n    local cmd = \"cat \".. fn .. \" | \" .. flt\r\n    local f = assert(io.popen(cmd, 'r'))\r\n    local s = assert(f:read('*a'))\r\n    f:close()\r\n    os.remove(fn)\r\n    geany.text(s)\r\n    position=geany.rowcol(line,column)\r\n    geany.caret(position)\r\n\r\nThe cursor remains in the original location, or near to it. The only problem is that the position of the text on the screen may be changed (Usually after the processing, the line with the cursor is near to the bottom of the window).\r\nOf course you must activate the \"Lua Script\" plugin to use the above script.\r\nThe script should be placed in `~/.config/geany/plugins/geanylua` directory or `~/.geany/plugins/geanylua` directory  (depending on distribution).\r\nYou may also create a `hotkeys.cfg` file in the same directory, and put the filename of the above script in a line in that file. That allows you to assign a keyboard shortcut (e.g., CTRL+ALT+F) to that autoformatting script via `Edit-\u003ePreferences-\u003eKey bindings-\u003eLua Script` .\r\n"}],"action":{"name":"View Issue","url":"https://github.com/geany/geany/issues/1913#issuecomment-409580277"}}}</script>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/geany/geany/issues/1913#issuecomment-409580277",
"url": "https://github.com/geany/geany/issues/1913#issuecomment-409580277",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
},
{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"hideOriginalBody": "false",
"originator": "AF6C5A86-E920-430C-9C59-A73278B5EFEB",
"title": "Re: [geany/geany] After reformatting the code via CTRL+A and CTRL+1 (\"Send selection to\") the cursor position is lost (#1913)",
"sections": [
{
"text": "",
"activityTitle": "**Wojciech M. Zabolotny**",
"activityImage": "https://assets-cdn.github.com/images/email/message_cards/avatar.png",
"activitySubtitle": "@wzab",
"facts": [

]
}
],
"potentialAction": [
{
"name": "Add a comment",
"@type": "ActionCard",
"inputs": [
{
"isMultiLine": true,
"@type": "TextInput",
"id": "IssueComment",
"isRequired": false
}
],
"actions": [
{
"name": "Comment",
"@type": "HttpPOST",
"target": "https://api.github.com",
"body": "{\n\"commandName\": \"IssueComment\",\n\"repositoryFullName\": \"geany/geany\",\n\"issueId\": 1913,\n\"IssueComment\": \"{{IssueComment.value}}\"\n}"
}
]
},
{
"name": "Close issue",
"@type": "HttpPOST",
"target": "https://api.github.com",
"body": "{\n\"commandName\": \"IssueClose\",\n\"repositoryFullName\": \"geany/geany\",\n\"issueId\": 1913\n}"
},
{
"targets": [
{
"os": "default",
"uri": "https://github.com/geany/geany/issues/1913#issuecomment-409580277"
}
],
"@type": "OpenUri",
"name": "View on GitHub"
},
{
"name": "Unsubscribe",
"@type": "HttpPOST",
"target": "https://api.github.com",
"body": "{\n\"commandName\": \"MuteNotification\",\n\"threadId\": 363420865\n}"
}
],
"themeColor": "26292E"
}
]</script>