Is it possible to access the [range] argument in lua evaluated with :lua?

I’m trying to write a telescope.nvim version of :tselect that uses the current word or visual selection as the search target.

To do this I need to determine what mode we were in just before the picker was invoked. The typical approach would be to define two functions and map them separately for visual and normal mode. However, in the case of implementing a telescope.nvim picker this won’t work because I don’t want to have two separate pickers one specific to visual mode and one specific to normal mode. Obviously I could pass which mode we’re coming from as a parameter to the picker to get around having two pickers, but I’m trying to avoid this whole mess.

In order to do this I need to be able to determine if there was a visual selection when the picker got invoked and if so the text that was selected. I’m thinking that I could do this by using the [range] argument to :[range]lua <expr>. When typing a command when we start out in visual mode we get the prepopulated :'<,'>lua <expr> so we could write our code so that it assumes that if we have a range specified we should use that range to determine what name to search for, and if there is no current range specified we could assume that the name to search for should be the current word.

Unfortunately as far as I can tell the information in [range] isn’t exposed to the lua code at all. Have I overlooked something, or would it take a feature request to neovim to be able to inspect that information.

I think you can define a user command using :command! or nvim_add_user_command().

After thinking after this a bit more + reading the documentation of :command! thoroughly I think I’ve realized that the range argument won’t be sufficient for my purpose because at least for user defined commands it is expanded to just a line number via <line1> or <line2> and the range isn’t passed literally so it isn’t possible to determine exactly what marks were used.

Probably will just end up having to go with separate bindings for normal/visual modes.