aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md26
-rw-r--r--VERSION1
-rw-r--r--autoload/regex_escape.vim69
-rw-r--r--doc/regex_escape.txt63
-rw-r--r--plugin/regex_escape.vim24
5 files changed, 183 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..8b779fc
--- /dev/null
+++ b/README.md
@@ -0,0 +1,26 @@
+regex\_escape.vim
+=================
+
+This plugin provides normal and visual mode mapping targets to insert escaping
+backslash characters before regular expression metacharacters for text selected
+by a motion, as a quick way to put a literal string into a regular expression
+context.
+
+It's useful generally as a shortcut, but the author particularly likes it for
+keeping track of backslash-heavy expressions where counting gets tiresome.
+
+For example, in the default BRE mode, this string:
+
+ foo * ^bar $\ baz \ quux
+
+Becomes:
+
+ foo \* \^bar \$\\ baz \\ quux
+
+License
+-------
+
+Copyright (c) [Tom Ryder][1]. Distributed under the same terms as Vim itself.
+See `:help license`.
+
+[1]: https://sanctum.geek.nz/
diff --git a/VERSION b/VERSION
new file mode 100644
index 0000000..6e8bf73
--- /dev/null
+++ b/VERSION
@@ -0,0 +1 @@
+0.1.0
diff --git a/autoload/regex_escape.vim b/autoload/regex_escape.vim
new file mode 100644
index 0000000..06b3c07
--- /dev/null
+++ b/autoload/regex_escape.vim
@@ -0,0 +1,69 @@
+" Define Vim pattern character classes of characters that should have an
+" escape character added before them to make them literal, for use in
+" substitute()
+let s:classes = {
+ \ 'bre': '[][\.*?^$]',
+ \ 'ere': '[][\.*?^$+{}()/]',
+ \ 'vim': '[][\.*^$~]'
+ \ }
+
+" This function does the actual translation, defined as 'operatorfunc' for the
+" mapping in both normal and visual mode
+function! regex_escape#Operatorfunc(type) abort
+
+ " Save the current value of the unnamed register and the current value of
+ " the 'clipboard' and 'selection' options into a dictionary for restoring
+ " after this is all done
+ let l:save = {
+ \ 'register': @@,
+ \ 'clipboard': &clipboard,
+ \ 'selection': &selection
+ \ }
+
+ " Don't involve any system clipboard for the duration of this function
+ set clipboard-=unnamed
+ set clipboard-=unnamedplus
+
+ " Ensure that we include end-of-line and final characters in selections
+ set selection=inclusive
+
+ " Select or re-select text, depending on how we were invoked
+ if a:type ==# 'line'
+ execute "normal! '[V']y"
+ elseif a:type ==# 'block' " Typically doesn't work too well
+ execute "normal! `[\<C-V>`]y" "
+ else
+ execute "normal! `[v`]y"
+ endif
+
+ " Determine the regex flavor to use; if one is defined for the buffer, use
+ " that; failing that, if one is defined globally in g:regex_escape_flavor,
+ " use that; failing that, just use 'bre'
+ let l:flavor = get(b:, 'regex_escape_flavor',
+ \ get(g:, 'regex_escape_flavor', 'bre'))
+
+ " Get the corresponding character class
+ let l:class = s:classes[l:flavor]
+
+ " Perform the substitution on the unnamed register's contents, inserting a
+ " backslash before every instance of any character in that class
+ let @@ = substitute(@@, l:class, '\\&', 'g')
+
+ " Paste our substituted changes back in over the top of the previously
+ " selected text, by reselecting it before the paste
+ normal! gvp
+
+ " Restore contents of the unnamed register and the previous values of the
+ " 'clipboard' and 'selection' options.
+ let @@ = l:save['register']
+ let &clipboard = l:save['clipboard']
+ let &selection = l:save['selection']
+
+endfunction
+
+" Expression mapping target function; set the 'operatorfunc' and return the
+" key sequence to active it
+function! regex_escape#Map() abort
+ set operatorfunc=regex_escape#Operatorfunc
+ return 'g@'
+endfunction
diff --git a/doc/regex_escape.txt b/doc/regex_escape.txt
new file mode 100644
index 0000000..1377fcd
--- /dev/null
+++ b/doc/regex_escape.txt
@@ -0,0 +1,63 @@
+*regex_escape.txt* For Vim version 7.0 Last change: 2018 Dec 29
+
+DESCRIPTION *regex_escape*
+
+This plugin provides normal and visual mode mapping targets to insert escaping
+backslash characters before regular expression metacharacters for text
+selected by a motion, as a quick way to put a literal string into a regular
+expression context.
+
+It's useful generally as a shortcut, but the author particularly likes it for
+keeping track of backslash-heavy expressions where counting gets tiresome.
+
+For example, in the default BRE mode, this string: >
+ foo * ^bar $\ baz \ quux
+Becomes: >
+ foo \* \^bar \$\\ baz \\ quux
+
+OPTIONS *regex_escape-options*
+
+ *b:regex_escape_flavor*
+Set `b:regex_escape_flavor` to one of "bre", "ere", or "vim", to choose a
+regex flavor for a particular buffer. It might be a good idea to set (and
+clear) this in filetype plugins.
+
+ *g:regex_escape_flavor*
+Set `g:regex_escape_flavor` to one of "bre", "ere", or "vim", to choose a
+default global regex flavor for any buffer where `b:regex_escape_flavor` is
+not set.
+
+If neither of these options are set, "bre" is used.
+
+The ERE class includes / as a character, just because it's always the
+delimiter in AWK, and so often (but not always) the delimiter in Perl. This
+may mean that slash gets escaped unnecessarily in Perl patterns, and possibly
+anywhere you use ERE outside of AWK.
+
+The Vim set assumes 'magic', just because the author never messes with that.
+You might like to add e.g. "vim_magic", "vim_nomagic" etc classes if that
+matters to you.
+
+REQUIREMENTS *regex_escape-requirements*
+
+This plugin only loads if 'compatible' is not set.
+
+MAPPINGS *regex_escape-mappings*
+
+ *<Plug>(RegexEscape)*
+The normal and visual mode mapping targets are both named
+|<Plug>(RegexEscape)|. There are no default key mappings; you should define
+one yourself in your |vimrc|. For example:
+>
+ nmap <Leader>\ <Plug>(ReplaceOperator)
+ xmap <Leader>\ <Plug>(ReplaceOperator)
+<
+AUTHOR *regex_escape-author*
+
+Written and maintained by Tom Ryder <tom@sanctum.geek.nz>.
+
+LICENSE *regex_escape-license*
+
+Licensed for distribution under the same terms as Vim itself (see |license|).
+
+ vim:tw=78:ts=8:ft=help:norl:
diff --git a/plugin/regex_escape.vim b/plugin/regex_escape.vim
new file mode 100644
index 0000000..fb11932
--- /dev/null
+++ b/plugin/regex_escape.vim
@@ -0,0 +1,24 @@
+"
+" regex_escape.vim: Operator to escape regular expression metacharacters to
+" make them literal, appropriate to a configurable flavor of regular
+" expression, so that:
+" foo * ^bar $\ baz \ quux
+" becomes:
+" foo \* \^bar \$\\ baz \\ quux
+"
+" Author: Tom Ryder <tom@sanctum.geek.nz>
+" License: Same as Vim itself
+"
+if exists('g:loaded_regex_escape') || &compatible
+ finish
+endif
+if v:version < 700
+ finish
+endif
+let g:loaded_regex_escape = 1
+
+" Set up mapping
+nnoremap <expr> <Plug>(RegexEscape)
+ \ regex_escape#Map()
+xnoremap <expr> <Plug>(RegexEscape)
+ \ regex_escape#Map()