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

Textobj for functions, including its args #2343

Closed
patnr opened this issue Mar 3, 2022 · 6 comments
Closed

Textobj for functions, including its args #2343

patnr opened this issue Mar 3, 2022 · 6 comments

Comments

@patnr
Copy link
Contributor

patnr commented Mar 3, 2022

Thanks again for this wonderful plugin.

I'm not sure if it's easily doable, but it would be cool to have a textobj for functions, because I often find myself copy-pasting the arguments in a second step after dealing with the function (name) itself.

MWE containing 4 different cases of functions.

\documentclass[11pt]{article}
\newcommand{\myfun}[0]{\ensuremath{h}}
\begin{document}
$\textnormal{function}(a, b) = g(a + b) / \myfun(a, b) * f[a, b]$
\end{document}
@lervag
Copy link
Owner

lervag commented Mar 4, 2022

Thanks again for this wonderful plugin.

I'm glad to hear it! :)

it would be cool to have a textobj for functions

What does this mean?

MWE containing 4 different cases of functions.

Sorry to be "daft", but can you be more precise about what you mean this text object should match and what it should not match? Also, what would the distinction between i and a be?

@patnr
Copy link
Contributor Author

patnr commented Mar 5, 2022

I mean so that I could place my cursor at any the indicated positions below,

$\textnormal{function}(a, b) = g(a + b) / \myfun(a, b) * f[a, b]$
          1       1     1      222.         3     3      444

and if I did normal-mode daf it would delete

  • \textnormal{function}(a, b) at pos 1
  • g(a + b) at pos 2
  • \myfun(a, b) at pos 3
  • f[a, b] at pos 4

I imagine it could build on vimtex's existing, extended delimiter support, but also include the Word in front. Now that I say that perhaps I should just make the mapping myself. Not sure what the distinction between i and a would be.

@patnr
Copy link
Contributor Author

patnr commented Mar 5, 2022

Here's a first attempt.

nmap daF yadBv`]d

The first yad uses the delmiter textobj to move to the start of the parentheses/brackets. Then B moves back to include the function name. Next, visual-select until the mark ] which will be set to the trailing delimiter, and delete.

Drawbacks:

  • Only works from within delimiters, not the function label/name.
  • Includes the leading $ in the first example above.

@lervag
Copy link
Owner

lervag commented Mar 5, 2022

I think this is partly related to #2025, which I'm sorry to say I have still not completed. I would propose that I focus on that one first, as it seems to be an integral part of the "equation" for going into this particular issue.

However, I have to admit I'm not convinced that I should add the current suggestion as a text object. But if you think it is OK, then I propose that you follow the related #2025 (you can subscribe to notifications) and raise this suggestion again after I've finished implementing it.

@patnr
Copy link
Contributor Author

patnr commented Mar 6, 2022

I was a bit hasty in proposing this (though I did search the issues for something similar). As much as I like #2025, I don't think this idea should build on it because this idea is all about using vimtexs delimiter textobj for its magic.

Right now I use this map

nmap vaf yad:call search("\\(\\s\\zs.\\\|\\\\\\)", "b")<CR>v`]

(the search is just for the previous whitespace or backslash, but involves some horrible escaping).
It works really well, tested on the above MWE, except that the cursor has to be inside of the defining parantheses/brackets.

@patnr patnr closed this as completed Mar 6, 2022
@lervag
Copy link
Owner

lervag commented Mar 6, 2022

I guess you could simplify this somewhat, e.g.

nmap vaf vadoB

I didn't test this, but the idea is: first select current delimiter vad, go to left side of selection with o, then move back one WORD with B. I believe it should also work quite well with similar limitations.

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

2 participants