Skip to content

Commit 30664f3

Browse files
Yuheng Xievim-scripts
Yuheng Xie
authored andcommitted
Version 1.1.2
\n now first try to clear this mark (i.e. the mark under the cursor), before clearing all marks. mapped * and # for jumping to the next and previous occurrence of this mark. mark patterns now also added to the search history
0 parents  commit 30664f3

File tree

2 files changed

+262
-0
lines changed

2 files changed

+262
-0
lines changed

README

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
This is a mirror of http://www.vim.org/scripts/script.php?script_id=1238
2+
3+
This script is written to highlight several words in different colors simultaneously. For example, when you are browsing a big program file, you could highlight some key variables. This will make it easier to trace the source code.
4+
5+
I think this script is functional similar with MultipleSearch vimscript #479.
6+
7+
Usage:
8+
9+
Highlighting:
10+
Normal mode:
11+
\m mark or unmark the word under (or before) the cursor
12+
Place the cursor under the word to be highlighted, press \m, then the word will be colored.
13+
\r manually input a regular expression
14+
To highlight an arbitrary regular expression, press \r and input the regexp.
15+
\n clear this mark (i.e. the mark under the cursor), or clear all highlighted marks
16+
Visual mode:
17+
\m mark or unmark a visual selection
18+
Select some text in Visual mode, press \m, then the selection will be colored.
19+
\r manually input a regular expression (base on the selection text)
20+
Command line:
21+
:Mark regexp to mark a regular expression
22+
:Mark regexp with exactly the same regexp to unmark it
23+
:Mark to clear all marks
24+
Searching:
25+
Normal mode:
26+
* # \* \# \/ \? use these six keys to jump to the other marks
27+
and you could also use VIM's / and ? to search, since the mark patterns have
28+
been added to the search history.
29+
30+
Here is a sumerization of * # \* \# \/ \?:
31+
32+
" First of all, \#, \? and # behave just like \*, \/ and *, respectively,
33+
" except that \#, \? and # search backward.
34+
"
35+
" \*, \/ and *'s behaviors differ base on whether the cursor is currently
36+
" placed over an active mark:
37+
"
38+
" Cursor over mark Cursor not over mark
39+
" ---------------------------------------------------------------------------
40+
" \* jump to the next occurrence of jump to the next occurrence of
41+
" current mark, and remember it "last mark".
42+
" as "last mark".
43+
"
44+
" \/ jump to the next occurrence of same as left
45+
" ANY mark.
46+
"
47+
" * if \* is the most recently used, do VIM's original *
48+
" do a \*; otherwise (\/ is the
49+
" most recently used), do a \/.
50+
51+
Screenshot:
52+
http://elephant.net.cn/files/vim_screenshot.png (It is also the screenshot of my colorscheme vimscript #1253.)
53+
54+
Bugs and Notes:
55+
Some words which have been already colored by syntax scripts could not be highlighted.
56+
57+
mark.vim should be re-sourced after any changing to colors. For example, if you
58+
:set background=dark OR
59+
:colorscheme default
60+
you should
61+
:source PATH_OF_PLUGINS/mark.vim
62+
after that. Otherwise, you won't see the colors.
63+
Unfortunately, it seems that .gvimrc runs after plugin scripts. So if you set any color settings in .gvimrc, you have to add one line to the end of .gvimrc to source mark.vim.

plugin/mark.vim

+199
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
" Script Name: mark.vim
2+
" Version: 1.1.2
3+
" Last Change: March 23, 2005
4+
" Author: Yuheng Xie <[email protected]>
5+
"
6+
" Description: a little script to highlight several words in different colors
7+
" simultaneously
8+
"
9+
" Usage: call Mark(regexp) to mark a regular expression.
10+
" call Mark(regexp) with exactly the same regexp to unmark it.
11+
" call Mark() to clear all marks.
12+
"
13+
" You may map keys for the call in your vimrc file for
14+
" convenience. The default keys is:
15+
" Highlighting:
16+
" Normal \m mark or unmark the word under or before the cursor
17+
" \r manually input a regular expression
18+
" \n clear this mark (i.e. the mark under the cursor),
19+
" or clear all marks
20+
" Visual \m mark or unmark a visual selection
21+
" Searching:
22+
" Normal * jump to the next occurrence of this mark
23+
" # jump to the previous occurrence of this mark
24+
" combined with VIM's / and ? etc.
25+
"
26+
" The default colors/groups setting is for marking six
27+
" different words in different colors. You may define your own
28+
" colors in your vimrc file. That is to define highlight group
29+
" names as "MarkWordN", where N is a number. An example could be
30+
" found below.
31+
"
32+
" Bugs: some colored words could not be highlighted
33+
34+
" default colors/groups
35+
" you may define your own colors in you vimrc file, in the form as below:
36+
hi MarkWord1 ctermbg=Cyan ctermfg=Black guibg=#8CCBEA guifg=Black
37+
hi MarkWord2 ctermbg=Green ctermfg=Black guibg=#A4E57E guifg=Black
38+
hi MarkWord3 ctermbg=Yellow ctermfg=Black guibg=#FFDB72 guifg=Black
39+
hi MarkWord4 ctermbg=Red ctermfg=Black guibg=#FF7272 guifg=Black
40+
hi MarkWord5 ctermbg=Magenta ctermfg=Black guibg=#FFB3FF guifg=Black
41+
hi MarkWord6 ctermbg=Blue ctermfg=Black guibg=#9999FF guifg=Black
42+
43+
" you may map keys to call Mark() in your vimrc file to trigger the functions.
44+
" examples:
45+
" mark or unmark the word under or before the cursor
46+
nmap \m :let w=PrevWord()<bar>if w!=""<bar>cal Mark("\\<".w."\\>")<bar>en<cr>
47+
" manually input a regular expression
48+
nmap \r :cal inputsave()<bar>let r=input("@")<bar>cal inputrestore()<bar>if r!=""<bar>cal Mark(r)<bar>en<cr>
49+
" clear the mark under the cursor, or clear all marks
50+
nmap \n :cal Mark(ThisMark())<cr>
51+
" jump to the next occurrence of this mark
52+
nnoremap * :let w=ThisMark()<bar>if w!=""<bar>cal search(w)<bar>el<bar>exe "norm! *"<bar>en<cr>
53+
" jump to the previous occurrence of this mark
54+
nnoremap # :let w=ThisMark()<bar>if w!=""<bar>cal search(w,"b")<bar>el<bar>exe "norm! #"<bar>en<cr>
55+
" mark or unmark a visual selection
56+
vnoremap \m "my:cal Mark("\\V".substitute(@m,"\\n","\\\\n","g"))<cr>
57+
58+
" define variables if they don't exist
59+
function! InitMarkVaribles()
60+
if !exists("g:mwCycleMax")
61+
let i = 1
62+
while hlexists("MarkWord" . i)
63+
let i = i + 1
64+
endwhile
65+
let g:mwCycleMax = i - 1
66+
endif
67+
if !exists("b:mwCycle")
68+
let b:mwCycle = 1
69+
endif
70+
let i = 1
71+
while i <= g:mwCycleMax
72+
if !exists("b:mwWord" . i)
73+
let b:mwWord{i} = ""
74+
endif
75+
let i = i + 1
76+
endwhile
77+
endfunction
78+
79+
" return the word under or before the cursor
80+
function! PrevWord()
81+
let line = getline(".")
82+
if line[col(".") - 1] =~ "\\w"
83+
return expand("<cword>")
84+
else
85+
return substitute(strpart(line, 0, col(".") - 1), "^.\\{-}\\(\\w\\+\\)\\W*$", "\\1", "")
86+
endif
87+
endfunction
88+
89+
" mark or unmark a regular expression
90+
function! Mark(...) " Mark(regexp)
91+
" define variables if they don't exist
92+
call InitMarkVaribles()
93+
94+
" clear all marks if regexp is null
95+
let regexp = ""
96+
if a:0 > 0
97+
let regexp = a:1
98+
endif
99+
if regexp == ""
100+
let i = 1
101+
while i <= g:mwCycleMax
102+
if b:mwWord{i} != ""
103+
let b:mwWord{i} = ""
104+
exe "syntax clear MarkWord" . i
105+
endif
106+
let i = i + 1
107+
endwhile
108+
return
109+
endif
110+
111+
" clear the mark if it has been marked
112+
let i = 1
113+
while i <= g:mwCycleMax
114+
if regexp == b:mwWord{i}
115+
let b:mwWord{i} = ""
116+
exe "syntax clear MarkWord" . i
117+
return
118+
endif
119+
let i = i + 1
120+
endwhile
121+
122+
" add to history
123+
call histadd("/", regexp)
124+
call histadd("@", regexp)
125+
126+
" quote regexp with / etc. e.g. pattern => /pattern/
127+
let quote = "/?~!@#$%^&*+-=,.:"
128+
let i = 0
129+
while i < strlen(quote)
130+
if stridx(regexp, strpart(quote, i, 1)) < 0
131+
let quoted_regexp = strpart(quote, i, 1) . regexp . strpart(quote, i, 1)
132+
break
133+
endif
134+
let i = i + 1
135+
endwhile
136+
if i >= strlen(quote)
137+
return
138+
endif
139+
140+
" choose an unused mark group
141+
let i = 1
142+
while i <= g:mwCycleMax
143+
if b:mwWord{i} == ""
144+
let b:mwWord{i} = regexp
145+
if i < g:mwCycleMax
146+
let b:mwCycle = i + 1
147+
else
148+
let b:mwCycle = 1
149+
endif
150+
exe "syntax clear MarkWord" . i
151+
exe "syntax match MarkWord" . i . " " . quoted_regexp . " containedin=ALL"
152+
return
153+
endif
154+
let i = i + 1
155+
endwhile
156+
157+
" choose a mark group by cycle
158+
let i = 1
159+
while i <= g:mwCycleMax
160+
if b:mwCycle == i
161+
let b:mwWord{i} = regexp
162+
if i < g:mwCycleMax
163+
let b:mwCycle = i + 1
164+
else
165+
let b:mwCycle = 1
166+
endif
167+
exe "syntax clear MarkWord" . i
168+
exe "syntax match MarkWord" . i . " " . quoted_regexp . " containedin=ALL"
169+
return
170+
endif
171+
let i = i + 1
172+
endwhile
173+
endfunction
174+
175+
" return the mark string under the cursor. multi-lines marks not supported
176+
function! ThisMark()
177+
" define variables if they don't exist
178+
call InitMarkVaribles()
179+
180+
let line = getline(".")
181+
let i = 1
182+
while i <= g:mwCycleMax
183+
if b:mwWord{i} != ""
184+
let start = 0
185+
while start >= 0 && start < strlen(line) && start < col(".")
186+
let b = match(line, b:mwWord{i}, start)
187+
let e = matchend(line, b:mwWord{i}, start)
188+
if b < col(".") && col(".") <= e
189+
return b:mwWord{i}
190+
endif
191+
let start = e
192+
endwhile
193+
endif
194+
let i = i + 1
195+
endwhile
196+
return ""
197+
endfunction
198+
199+
" vim: ts=2 sw=2

0 commit comments

Comments
 (0)