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

Allow users to specify additional sentence terminators #17

Open
wants to merge 3 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
31 changes: 29 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,41 @@ key mappings via your `.vimrc`:
let g:textobj#sentence#move_n = ')'
```

### Additional custom sentence terminators

Additional sentence terminators can be added by setting
`g:re_extra_sentence_term`. For example, the following will count
semicolons as sentence terminators:

```vim
let g:re_extra_sentence_term = '|;'
call textobj#sentence#init()
```

Note this variable is a regular expression, and evey entry needs to be
preceeded by `|`.

The following can be used to count LaTeX environments as sentence
terminators, which is convenient for automatic rewrapping (e.g., via
`gqis`) that does not touch environments which are part of the sentence:

```vim
let g:re_extra_sentence_term =
\ '|\\begin\{[a-zA-Z]*\}' .
\ '|\\end\{[a-zA-Z]*\}'
call textobj#sentence#init()
```

## See also

If you find this plugin useful, check out these others by [@reedes][re]:

* [vim-colors-pencil][cp] - color scheme for Vim inspired by IA Writer
* [vim-lexical][lx] - building on Vim’s spell-check and thesaurus/dictionary completion
* [vim-litecorrect][lc] - lightweight auto-correction for Vim
* [vim-one][vo] - make use of Vim’s _+clientserver_ capabilities
* [vim-one][vo] - make use of Vim’s _+clientserver_ capabilities
* [vim-pencil][pn] - rethinking Vim as a tool for writers
* [vim-textobj-quote][qu] - extends Vim to support typographic (‘curly’) quotes
* [vim-thematic][th] - modify Vim’s appearance to suit your task and environment
Expand All @@ -146,7 +173,7 @@ If you find this plugin useful, check out these others by [@reedes][re]:

If you’ve spotted a problem or have an idea on improving this plugin,
please post it to the github project issue page. Pull requests that add
new regression tests (even failing ones that demonstrate a bug!) are
new regression tests (even failing ones that demonstrate a bug!) are
welcome too.

<!-- vim: set tw=74 :-->
8 changes: 7 additions & 1 deletion autoload/textobj/sentence.vim
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ function! textobj#sentence#select_i()
return s:select(b:textobj_sentence_re_i)
endfunction

if !exists('g:re_extra_sentence_term')
let g:re_extra_sentence_term = ''
endif

function! textobj#sentence#init(...)
let l:args = a:0 ? a:1 : {}

Expand Down Expand Up @@ -87,7 +91,9 @@ function! textobj#sentence#init(...)

" matching against end of sentence, '!', '?', and non-abbrev '.'
let l:re_term =
\ '([!?]|(' .
\ '([!?]' .
\ g:re_extra_sentence_term .
Comment on lines +94 to +95
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A cursory look at your question ... wouldn't the extra expressions you are trying to inject need to go inside the character class expression rather than after it? Something like:

Suggested change
\ '([!?]' .
\ g:re_extra_sentence_term .
\ '([!?'. g:re_extra_sentence_term .']' .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh hmm you aren't adding characters you're adding whole expressions. We probably need to setup all our matches as an array of expressions and join them at the last minute using (one|two|three) option grouping instead of a character class.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh hmm you aren't adding characters you're adding whole expressions. We probably need to setup all our matches as an array of expressions and join them at the last minute using (one|two|three) option grouping instead of a character class.

Yes, I want to allow whole expressions. Let me give an example and demonstrate the problem:

\documentclass{minimal}
\begin{document}

\newcommand{\beq}{\begin{equation}}
\newcommand{\eeq}{\end{equation}}

This is a long sentence, exceeding eighty characters and containing an equation environment below, starting here:
\beq
  x = 1 \,,
\eeq
and it is followed by another long sentence that exceeds eighty characters and should be wrapped by \texttt{gqis}.

\end{document}

Consider this with

let g:re_extra_sentence_term =
  \ '|\\beq' .
  \ '|\\eeq'
  
call textobj#sentence#init()

With my proposed implementation, when I place the cursor on the line before the equation and hit gqis, it does (almost) the right thing:

This is a long sentence, exceeding eighty characters and containing an equation
environment below, starting here: \beq
...

(I noticed only now it's not perfect, the \beq should actually stay on its own line ideally, but that may be a paragraph issue rather than a sentence issue).

Setting that aside, the main trouble is that hitting gqis when the cursor is in the line after the \eeq, it does not rewrap that but rather the cursor jumps to above the equation and gives again

This is a long sentence, exceeding eighty characters and containing an equation
environment below, starting here: \beq
...

As far as I can tell, the reason is that the negative lookback jumps over the \eeq and \beq somehow.

\ '|(' .
\ l:re_abbrev_neg_lookback .
\ '\.))+[' .
\ l:trailing .
Expand Down