After read the https://sourceforge.net/p/scintilla/bugs/2438/ report, I think the best way to automatically set the cursor position on the visible line is surely to track the SCN_MARGINCLICK event. I did some modification to handle this with the commit [5608b74](https://github.com/geany/geany-plugins/pull/1326/commits/5608b74cba25b571428...) .
Thanks for noticing the problem with line wrapping. I think your general approach is right - I've just slightly rewritten the `doc_line_from_visible_delta()` function to be easier to grasp by my poor brain, please check https://github.com/geany/geany-plugins/pull/1338/commits/fa7025ba9d58fb4680f... if it looks good to you.
There are a few things in your patch which I don't think are completely correct though - I'll add some inline comments to your code.