Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Addons Plugin] - Keybinding that inserts divider separator lines #1384

Open
advice2020 opened this issue Nov 14, 2024 · 7 comments
Open

[Addons Plugin] - Keybinding that inserts divider separator lines #1384

advice2020 opened this issue Nov 14, 2024 · 7 comments

Comments

@advice2020
Copy link

I was wondering if anyone would be interested in creating / adding on to an existing plugin.
I see there is an existing plugin called “Addons” which is for various small features added to Geany, maybe it can be added to this plugin if anyone is interested.

This plugin would allow user to setup a keybinding that would create divider/separator lines when the keybinding is applied.
User would be able to setup how they want the divider/separator lines to display visually and because each language has a different "comment" symbol, there would have to be a way for user to be able to set this up per language/file type.

EXAMPLES
(plain regular text file which does not require a comment symbol)
(not sure if any other users would find this useful, but ability to use this in plain text files for me would be very helpful)
txt
######################################

xml
<!--#################################-->

lua
--#####################################

Thank You

@advice2020
Copy link
Author

@Skif_off
Most of my requests for this program unfortunately cannot be solved with Lua, but if interested, this is the only one I have left that might be? I am not sure though.

Would Lua in this case be able to know the language/file type of the currently opened document, in order to know which divider to use?

I / the user would just need a small “template” for how to type in the file type / desired divider.

If something like this could actually be achieved with Lua/GeanyLua plugin, I imagine they would be "if" statements
something like

if "txt"
use
######################################

if "xml"
use

if "lua"
use
--#####################################

Then user would just assign a single keybinding to the Lua script and it would generate a divider based on the file type template/list from the Lua script.
As mentioned not sure though, this might be something that would require Geany plugin specifically for something like this?

If interested let me know.
Thank you again for all your help :)

@Skif-off
Copy link
Contributor

@advice2020, you misspelled my nickname :) I didn't receive the notification, I saw it by accident.

Maybe something like this (but I didn't check it)

local aTypes = {
{"Lua", ".lua", "--#####################################"},
{"",    ".txt", "######################################"},
{"XML", ".xml", "<!--#################################-->"}
}
local aFI = geany.fileinfo()
local sExt, sStr
for i = 1, #aTypes do
  if aTypes[i][1] == aFI.type then
    sStr = aTypes[i][3]
    break
  end
end
if sStr == nil then
  sExt = string.lower(aFI.ext)
  for i = 1, #aTypes do
    if aTypes[i][2] == sExt then
      sStr = aTypes[i][3]
      break
    end
  end
end
if sStr == nil then
  geany.status("Warning: Unknown file type!")
  return
end
local iEOL = geany.scintilla("SCI_GETEOLMODE")
local sLE
if iEOL == 0 then
  sLE = "\r\n"
elseif iEOL == 1 then
  sLE = "\r"
elseif iEOL == 2 then
  sLE = "\n"
else
  sLE = "\n"
end
local iCurPos = geany.caret()
local iL, iC = geany.rowcol(iCurPos)
iCurPos = geany.scintilla("SCI_POSITIONFROMLINE", iL - 1)
geany.select(iCurPos)
geany.selection(sStr .. sLE)

aTypes:

{"file type", "file extension", "string"}

where "file extension" with dot, "file type" - see filetype_extensions.conf (online).

@advice2020
Copy link
Author

advice2020 commented Nov 22, 2024

@Skif-off
When I first saw that you provided a script so soon I was amazed, I was so distracted by the script that I missed what you said at the top. Now I just read the beginning of your post and now I am even more amazed :)
You are so helpful I do not even need to spell your name right, you just find me :)

No problem, as mentioned you never have to check it, just the fact that you are willing to provide anything means so much, I have no problem testing things out.
Speaking of which, I was able to test this and this works perfectly! Also amazing that you could create something like this and not have to test it out :)
Noticed this adds the separator, creating a new line, no matter where the cursor is in the line which is nice.

Thank you so much for this! I will use this one a great deal, really appreciate it.
Hopefully other users in the future will find this script useful as well.
I will mention a couple of tweaks I came across, below if any future users are interested.

P.S.
Not sure if you use GeanyLua scripts plugin for your own use but I noticed something that could cause some issues, how it handles keybindings. Not sure if all plugin hotkeys are like this?
Here is the link if you or anyone else is interested
#1388

@advice2020
Copy link
Author

For any users in the future, just couple things I came across that might be helpful.


Sometimes .txt files in Linux do not contain an extension in the filename, the following line will allow you to use this script with those files (remember to add coma at the end of this line if in the middle of your template list).
{"", "", "######################################"}


The original script adds separator to the above line where the cursor is located.
If for whatever reason you would like the separator to be added to the line below the cursor you can change
iCurPos = geany.scintilla("SCI_POSITIONFROMLINE", iL - 1)
to
iCurPos = geany.scintilla("SCI_POSITIONFROMLINE", iL - 0)
which seems to work.
I always like to know little tweaks like this just in case certain situations benefit from one or the other.


Anyways, hopefully this will be helpful to someone someday.

@advice2020
Copy link
Author

Not sure what to do with this thread now?
The issue has been solved, but if I close it then users will not be able to see it as easily.
I guess could keep it open, maybe still valuable to be added to plugin for users who do not use GeanyLua plugin.

@Skif-off
Copy link
Contributor

@advice2020, I just use the program and see the repositories from time to time.

The original script adds separator to the above line where the cursor is located.

The script moves the cursor to the beginning of the line and inserts the text and the corresponding line ending character(s). The line ending character is appended to the divider separator line before being inserted into the document.
I.e. it looks like: the Home button > type the text or Ctrl+V > Enter.

iCurPos = geany.scintilla("SCI_POSITIONFROMLINE", iL - 1)

iCurPos is the position in the document that corresponds to the beginning of the current line. iL is the line with the cursor. "- 1" because GeanyLua counts the line number from 1, but Scintilla counts from 0.

@advice2020
Copy link
Author

advice2020 commented Nov 23, 2024

@Skif-off
No that worked out great, I am just glad that you were able to see my post, did not even realize I held Shift when hitting "-" when tagging your name :)
Again thank you so much for this script, really useful tool.

I think I see what you mean, I just did not describe it properly when I mentioned to other users that it "adds the separator above the line". The way you created the script is how I plan on using it, but as mentioned I always like to have little tweaks that can change little behaviors in scripts likes this.
But the advice I shared for other users about changing the "iL - 1" to "iL - 0" is okay right? This appeared to work in my quick tests of it. Hopefully I did not share incorrect information.
I know that what I just mentioned above is a little different than the 1 & 0 information you just shared, but that is good to know that the GeanyLua plugin handles the line numbers differently, because that could be confusing.
This makes sense now because when I originally tried editing the "iL - 1", I tried "iL - -1" thinking that it would require a negative number but that just made it skip another line. I then took a guess and changed it to "iL - 0" which worked.

Thank You again

P.S.
I just posted a minor Lua script post in the Geany "Discussions" section
https://github.com/geany/geany/discussions 4070 (do not want to link it) if interested.
The only other one I can think of is maybe a very simple case tool (which can probably use already created DC script :)
but a lot simpler in the context of Geany. If I ever make that post I can tag your name if interested, just have to spell it right this time :)
Other than that I cannot really think of any other Lua script tools to be used in the context of Geany.

EDIT:
Oh no I might have lost you, I had better luck spelling your name wrong :)
Probably not, but if interested I just made post I mentioned above about "case" tools.
Can be found here https://github.com/geany/geany/discussions (4086)
Thanks again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants