English 中文(简体)
我如何选择一种落后的计算方法?
原标题:How do I visual select a calculation backwards?

我想看一下一下,一个计算页的后面。

200 + 3 This is my text -300 +2 + (9*3)
                        |-------------|*

This is text 0,25 + 2.000 + sqrt(15/1.5)
             |-------------------------|*

The reason is that I will use it in insert mode. After writing a calculation I want to select the calculation (using a map) and put the results of the calculation in the text.

What the regex must do is:
- select from the cursor (see * in above example) backwards to the start of the calculation
(including /-+*:.,^).
- the calculation can start only with log/sqrt/abs/round/ceil/floor/sin/cos/tan or with a positive or negative number
- the calculation can also start at the beginning of the line but it never goes back to a previous line

I tried in all ways but could not find the correct regex.
I noted that backward searching is different then forward searching.

谁能帮助我?

Edit
Forgot to mention that it must include also the = if there is one and if the = is before the cursor or if there is only space between the cursor and = .
It must not include other = signs.

200 + 3 = 203 -300 +2 + (9*3) =
          |-------------------|<SPACES>*

200 + 3 = 203 -300 +2 + (9*3)
          |-----------------|<SPACES>*

最佳回答

A regex that comes close in pure vim is

vcs*zs(s{-}(((sqrt|log|sin|cos|tan|exp)?(.{-}))|(-?[0-9,.]+(e-?[0-9]+)?)|([-+*/%^]+)))+(s*=?)?s*

存在一些限制:低压(包括职能论点)为平价。 你们需要使用适当的克法教区来这样做,我不建议这样做。

Operator Mapping

To enable using this a bit like text-objects, use something like this in your $MYVIMRC:

func! DetectExpr(flag)
    let regex =  vcs*zs(s{-}(((sqrt|log|sin|cos|tan|exp)?(.{-}))|(-?[0-9,.]+(e-?[0-9]+)?)|([-+*/%^]+)))+(s*=?)?s*  
    return searchpos(regex, a:flag .  ncW , line( . ))
endf

func! PositionLessThanEqual(a, b)
    "echo  a:   . string(a:a)
    "echo  b:   . string(a:b)
    if (a:a[0] == a:b[0])
        return (a:a[1] <= a:b[1]) ? 1 : 0
    else
        return (a:a[0] <= a:b[0]) ? 1 : 0
    endif
endf

func! SelectExpr(mustthrow)
    let cpos  = getpos(".")
    let cpos  = [cpos[1], cpos[2]] " use only [lnum,col] elements
    let begin = DetectExpr( b )
    if ( ((begin[0] == 0) && (begin[1] == 0))
       || !PositionLessThanEqual(begin, cpos) )
        if (a:mustthrow)
            throw "Cursor not inside a valid expression"
        else
            "echoerr "not satisfied: " . string(begin) . " < " . string(cpos)
        endif
        return 0
    endif
    "echo "satisfied: " . string(begin) . " < " . string(cpos)

    call setpos( . , [0, begin[0], begin[1], 0])
    let end = DetectExpr( e )
    if ( ((end[0] == 0) || (end[1] == 0))
       || !PositionLessThanEqual(cpos,  end) )
        call setpos( . , [0, cpos[0], cpos[1], 0])
        if (a:mustthrow)
            throw "Cursor not inside a valid expression"
        else
            "echoerr "not satisfied: " . string(begin) . " < " . string(cpos) . " < " . string(end) 
        endif
        return 0
    endif
    "echo "satisfied: " . string(begin) . " < " . string(cpos) . " < " . string(end) 

    norm! v
    call setpos( . , [0, end[0],   end[1],   0])
    return 1
endf

silent! unmap X
silent! unmap <M-.>

xnoremap <silent>X :<C-u>call SelectExpr(0)<CR>
onoremap <silent>X :<C-u>call SelectExpr(0)<CR>

现在,你可以围绕(或之后)曲线的位置进行最接近的表述:

  • vX - [v]isually select e[X]pression
  • dX - [d]elete current e[X]pression
  • yX - [y]ank current e[X]pression
  • "ayX - id. to register a

作为一种骗子,为了mo的目的,利用以下手段从被占领土上获得精确的乙 art(virtualed<>/em>):

Insert mode mapping

针对聊天:

" if you want trailing spaces/equal sign to be eaten:
imap <M-.> <C-o>:let @e=""<CR><C-o>"edX<C-r>=substitute(@e,  ^v(.{-})(s*=?)?s*$ ,  =string(eval(submatch(1))) ,   )<CR>

" but I m assuming you wanted them preserved:
imap <M-.> <C-o>:let @e=""<CR><C-o>"edX<C-r>=substitute(@e,  ^v(.{-})(s*=?s*)?$ ,  =string(eval(submatch(1))) . submatch(2) ,   )<CR>

允许你打上Alt-.,插入方式,目前的表述改为评价。 治疗器在结果结束时添加方式。

200 + 3 This is my text -300 +2 + (9*3)

This is text 0.25 + 2.000 + sqrt(15/1.5)

以“Alt-.为试验,插入3次:

203 This is my text -271

This is text 5.412278

For Fun: ascii art

vXoyoEsc`<jPvXr-r|e.

易于测试:

:let @q="vXoyox1b`<jPvXr-r|e.a*x1b"
:set virtualedit=all

Now you can @q anywhere and it will ascii-decorate the nearest expression :)

200 + 3 = 203 -300 +2 + (9*3) =
|-------|*
          |-------------------|*

200 + 3 = 203 -300 +2 + (9*3)
          |-----------------|*
|-------|*

This is text 0,25 + 2.000 + sqrt(15/1.5)
             |-------------------------|*

1 consider using Vim s python integration to do such parsing

问题回答

这似乎是一项复杂的任务,毕竟要靠reg子来完成,这样,如果你能够避免,就会试图这样做。

I ve created a regex that works for a few examples - give it a try and see if it does the trick:

^(?:[A-Za-z]|s)+((?:[^A-Za-z]+)?(?:log|sqrt|abs|round|ceil|floor|sin|cos|tan)[^A-Za-z]+)(?:[A-Za-z]|s)*$

你感兴趣的部分应当放在第一个配对小组。

让我知道,你是否需要解释。

EDIT:

(?:[A-Za-z]|s)+ - match everything that s a letter or a space once or more

match and capture the following 3:
((?:[^A-Za-z]+)? - match everything that s NOT a letter (i.e. in your case numbers or operators)

<>>>>。 - 符合您的关键词

[^A-Za-z]+) - match everything that s NOT a letter (i.e. in your case numbers or operators)

(?:[A-Za-z >]——与字母或空间零或多倍以上的一切相匹配

$ - match the end of the line





相关问题
Autoupdate VIM Plugins?

Is it possible to update vim plugins automatically?

how to unindent in vim without leaving edit mode?

I m writing a lot of python code recently, and i used the tab-to-space mode in vim. I was just wondering how would i unindent in vim without leaving edit mode for example after i finished if...: block....

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

Vim - Deleting XML Comments

How do I delete comments in XML? If the opening and the closing comment tags are on the same line, I use :g/^<!--.*-->$/d to delete the comment. How to delete the comments that are spread ...

Limiting a match in vim to certain filetypes?

I have the following in my .vimrc to highlight lines longer than 80 chars: highlight OverLength ctermbg=red ctermfg=white guibg=#592929 match OverLength /\%81v.*/ This works quite well. However, the ...

Profiling Vim startup time

I’ve got a lot of plugins enabled when using Vim – I have collected plugins over the years. I’m a bit fed up with how long Vim takes to start now, so I’d like to profile its startup and see which of ...

热门标签