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

Feature: Text object for math "symbol" or "atom" #2025

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions autoload/vimtex.vim
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ function! s:init_default_mappings() abort " {{{1
call s:map(1, 'x', 'am', '<plug>(vimtex-am)')
call s:map(1, 'o', 'im', '<plug>(vimtex-im)')
call s:map(1, 'o', 'am', '<plug>(vimtex-am)')
call s:map(1, 'x', 'ix', '<plug>(vimtex-ix)')
call s:map(1, 'x', 'ax', '<plug>(vimtex-ax)')
call s:map(1, 'o', 'ix', '<plug>(vimtex-ix)')
call s:map(1, 'o', 'ax', '<plug>(vimtex-ax)')

if vimtex#text_obj#targets#enabled()
call vimtex#text_obj#targets#init()
Expand Down
49 changes: 49 additions & 0 deletions autoload/vimtex/text_obj.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function! vimtex#text_obj#init_buffer() abort " {{{1
\ ['$', 'delimited', 'env_math'],
\ ['P', 'sections', ''],
\ ['m', 'items', ''],
\ ['x', 'symbols', ''],
\]
let l:optional = empty(l:opt) ? '' : ',''' . l:opt . ''''
execute printf('xnoremap <silent><buffer> <plug>(vimtex-i%s) :<c-u>call vimtex#text_obj#%s(1, 1%s)<cr>', l:map, l:name, l:optional)
Expand Down Expand Up @@ -229,6 +230,54 @@ function! vimtex#text_obj#items(is_inner, mode) abort " {{{1
call vimtex#pos#set_cursor(l:pos_end)
endfunction

" }}}1
function! vimtex#text_obj#symbols(is_inner, mode) abort " {{{1
" Disable for now
return

if a:is_inner
call vimtex#log#warning('Not implemented!')
return
endif

" We are looking for constructs like this:
"
" a^{...}_{...}
" \cmd^{...}_{...}

" Repeat the following until finished

if !vimtex#syntax#in_mathzone()
\ || getline('.')[cursor] ==# '$'
return
endif

" 1. Find beginning: \ or word boundary
" 1.1 move cursor backwards as long as it is over [ ^_{}]
" 1.2 note position of character (this is start if type=char)
" 1.3 move backwards to check if symbol is a command
" 1.4 register type: l:type = cmd|char

" 2. Look for ^ or _ groups (finished if not found)
" 2.1 start from start position
" 2.2 go forward until [ ^_]
" 2.3 if white-space: set end position, go to 3
" 2.4 match either single character or {...} group, then continue with
" search for next ^ or _ (sort of repeat from 2.2)

" 3. Region from 1 to 2 is now a candidate region
" 3.1 reject if the region is trivial length (0 or 1)
" 3.2 reject if the region does not contain the cursor position
" 3.3 if rejected: move active cursor position to the position before the
" boundary detected in step 1 and repeat from step 0.

" let l:pos_save = vimtex#pos#get_cursor()

" call vimtex#pos#set_cursor(l:pos_start)
" normal! v
" call vimtex#pos#set_cursor(l:pos_end)
endfunction

" }}}1

function! s:get_sel_delimited_visual(is_inner, type, startpos) abort " {{{1
Expand Down
35 changes: 23 additions & 12 deletions doc/vimtex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ FEATURE OVERVIEW *vimtex-features*
- `i$` `a$` Inline math structures
- `iP` `aP` Sections
- `im` `am` Items
- `ix` `ax` Symbols with sub- and superscripts
- Other mappings
- Delete the surrounding command, environment or delimiter with
`dsc`/`dse`/`ds$`/`dsd`
Expand Down Expand Up @@ -812,6 +813,8 @@ This feature is explained in more detail later, see |vimtex-imaps|.
iP |<plug>(vimtex-iP)| `xo`
am |<plug>(vimtex-am)| `xo`
im |<plug>(vimtex-im)| `xo`
ax |<plug>(vimtex-ax)| `xo`
ix |<plug>(vimtex-ix)| `xo`
% |<plug>(vimtex-%)| `nxo`
]] |<plug>(vimtex-]])| `nxo`
][ |<plug>(vimtex-][)| `nxo`
Expand Down Expand Up @@ -3316,6 +3319,8 @@ MAP DEFINITIONS *vimtex-mappings*
*<plug>(vimtex-iP)*
*<plug>(vimtex-am)* Items
*<plug>(vimtex-im)*
*<plug>(vimtex-ax)* Symbol with sub- and superscripts (in math mode only)
*<plug>(vimtex-ix)*
These are all text object mappings for the indicated types of objects , see
|vimtex-text-objects| for more info.

Expand Down Expand Up @@ -3664,26 +3669,32 @@ a simple table that shows the original text on the left, the keys that are
typed in the middle, and the result on the right. The bar "|" indicates the
cursor position before the operation. >

BEFORE KEYS AFTER
\comm|and{arg} dic \command{}
\command{a|rg} gUac \COMMAND{ARG}
BEFORE KEYS AFTER
\comm|and{arg} dic \command{}
\command{a|rg} gUac \COMMAND{ARG}

\lef|t( asd \right) cid \left(| \right)
\lef|t( asd \right) cid \left(| \right)

\begin{x} die \begin{x}
hello world| \end{x}
\begin{x} die \begin{x}
hello world| \end{x}
\end{x}

$math | here$ da$
$math | here$ da$

\begin{itemize} \begin{itemize}
\item hello moon| cim \item |
\end{itemize} \end{itemize}
\begin{itemize} \begin{itemize}
\item hello moon| cim \item |
\end{itemize} \end{itemize}

\begin{itemize} \begin{itemize}
\item hello moon| dam \end{itemize}
\begin{itemize} \begin{itemize}
\item hello moon| dam \end{itemize}
\end{itemize}

$C|x_0^{1/2} y$ dax $x_0^{1/2} y$
$Cx|_0^{1/2} y$ dax $C y$
$\left( as|d \right)^{...}$ dax $\left( ad \right)^{...}$
$\left( asd \rig|ht)^{...}$ dax $$
$e^{a x|^3}$ dax $e^{a }$

Associated settings:
* |g:vimtex_text_obj_enabled|
* |g:vimtex_text_obj_linewise_operators|
Expand Down
2 changes: 1 addition & 1 deletion test/test-textobj/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ TESTS := $(TESTS:.vim=)

.PHONY: test $(TESTS)

test: $(TESTS)
test: $(filter-out test-symbols,$(TESTS))

$(TESTS):
@$(MYVIM) -u $@.vim
27 changes: 27 additions & 0 deletions test/test-textobj/test-symbols.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
set nocompatible
let &rtp = '../..,' . &rtp
filetype plugin on
syntax on

set nomore

setfiletype tex

for [s:keys, s:input, s:expect] in [
\ ['dax', '$Cx_0^{1/2} y$', '$x_0^{1/2} y$'],
\ ['ldax', '$Cx_0^{1/2} y$', '$x_0^{1/2} y$'],
\ ['lldax', '$Cx_0^{1/2} y$', '$C y$'],
\ ['dax', '$e^{a x^3}$', '$$'],
\ ['fadax', '$e^{a x^3}$', '$e^{ x^3}$'],
\ ['fxdax', '$e^{a x^3}$', '$e^{a }$'],
\ ['2ldax', '$\sin_k^{a x^3}$', '$$'],
\ ['fxdax', '$\sin_k^{a x^3}$', '$\sin_k^{a }$'],
\ ['fadax', '$( asd )^{...}$', '$( sd )^{...}$'],
\ ['f}dax', '$( asd )^{...}$', '$$'],
\ ['fsdax', '$\left( asd \right)^{...}$', '$\left( ad \right)^{...}$'],
\ ['fgdax', '$\left( asd \right)^{...}$', '$$'],
\]
call vimtex#test#keys(s:keys, s:input, s:expect)
endfor

call vimtex#test#finished()