1"
2" cream-bookmarks.vim -- Practical, visible, anonymous bookmarks.
3"
4" Cream -- An easy-to-use configuration of the famous Vim text editor
5" [ http://cream.sourceforge.net ] Copyright (C) 2001-2011 Steve Hall
6"
7" License:
8" This program is free software; you can redistribute it and/or modify
9" it under the terms of the GNU General Public License as published by
10" the Free Software Foundation; either version 3 of the License, or
11" (at your option) any later version.
12" [ http://www.gnu.org/licenses/gpl.html ]
13"
14" This program is distributed in the hope that it will be useful, but
15" WITHOUT ANY WARRANTY; without even the implied warranty of
16" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17" General Public License for more details.
18"
19" You should have received a copy of the GNU General Public License
20" along with this program; if not, write to the Free Software
21" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22" 02111-1307, USA.
23"
24" These functions are adapted from original material, used with
25" permission, by:
26"	Anthony Kruize
27" Source: http://vim.sourceforge.net/scripts/script.php?script_id=152
28"	Wolfram 'Der WOK' Esser
29" Source: http://vim.sourceforge.net/scripts/script.php?script_id=66
30"
31"----------------------------------------------------------------------
32"	Bookmark display
33"
34" Cream Notes:
35" * Functions and calls have had "Cream_" prepended to them for parallel
36"   installation with the original.
37" * All functions defined with the script scope ("s:") have been re-scoped to
38"   global.
39" * Indication of actual character names for the marks have been eliminated.
40"   (The names were changing whenever any mark was added/removed anyway. The names
41"   served no purpose.)
42" * Bookmarks are turned on by default for every buffer. (If no marks exist in
43"   the document, the two character margin is simply not expanded.)
44
45"......................................................................
46" Source: http://vim.sourceforge.net/scripts/script.php?script_id=152
47"
48" Name:        ShowMarks
49" Description: Visually displays the location of marks local to a buffer.
50" Authors:     Anthony Kruize <trandor@labyrinth.net.au>
51"              Michael Geddes <michaelrgeddes@optushome.com.au>
52" Version:     1.1
53" Modified:    6 February 2002
54" License:     Released into the public domain.
55" ChangeLog:   1.1 - Added support for the A-Z marks.
56"                    Fixed sign staying placed if the line it was on is deleted.
57"                    Clear autocommands before making new ones.
58"              1.0 - First release.
59" Usage:       Copy this file into the plugins directory so it will be
60"              automatically sourced.
61"
62"              Default key mappings are:
63"                <Leader>mt  - Toggles ShowMarks on and off.
64"                <Leader>mh  - Hides a mark.
65"
66"              Hiding a mark doesn't actually remove it, it simply moves it to
67"              line 1 and hides it visually.
68"......................................................................
69
70" Don't load if signs feature not available
71if !has("signs")
72	if !exists("g:CREAM_SIGNSCHECK")
73		let g:CREAM_SIGNSCHECK = 1
74	endif
75	if g:CREAM_SIGNSCHECK < 3
76		let g:CREAM_SIGNSCHECK = g:CREAM_SIGNSCHECK + 1
77		call confirm(
78			\"Unable to use Cream bookmarking features,\n" .
79			\"no compiled support for signs. (Please use the \n" .
80			\"\"configure --with-features=big\" if you compiled \n" .
81			\"Vim from source yourself.) (" . g:CREAM_SIGNSCHECK . " of 2 warnings)\n" .
82			\"\n", "&Ok", 1, "Info")
83	endif
84	finish
85endif
86
87
88" Check if we should continue loading
89if exists("Cream_loaded_showmarks")
90	finish
91endif
92let Cream_loaded_showmarks = 1
93
94""	Book marking
95"" Jump forward/backward and toggle 'anonymous' marks for lines (by using marks a-z)
96"imap <silent> <F2> <C-b>:call Cream_WOK_mark_next()<CR>
97"imap <silent> <S-F2> <C-b>:call Cream_WOK_mark_prev()<CR>
98"imap <silent> <M-F2> <C-b>:call Cream_WOK_mark_toggle()<CR>
99"imap <silent> <M-S-F2> <C-b>:call Cream_WOK_mark_killall()<CR>
100"imap <silent> <C-F2> <C-b>:call Cream_ShowMarksToggle()<CR>
101
102"+++ Cream: moved to autocmd file
103"" AutoCommands:
104"" CursorHold checks the marks and set the signs
105"" GuiEnter loads the default theme for graphical icons
106"augroup Cream_ShowMarks
107"    autocmd!
108"    "autocmd CursorHold * call Cream_ShowMarks()
109"    " always turn on bookmarks and show when opening a new file
110"    autocmd BufRead * let b:Cream_ShowMarks_Enabled=1
111"    autocmd VimEnter,BufRead * call Cream_ShowMarks()
112"augroup END
113"+++
114
115"+++ Cream: Moved to colors modules
116"" highlight settings for bookmark
117"highlight default ShowMarksHL ctermfg=blue ctermbg=lightblue cterm=bold guifg=blue guibg=lightblue gui=bold
118"+++
119
120" Setup the sign definitions for each mark
121function! Cream_ShowMarksSetup()
122	let n = 0
123	while n < 26
124		let c = nr2char(char2nr('a') + n)
125		let C = nr2char(char2nr('A') + n)
126		"if &encoding == "latin1"
127		"    execute 'sign define Cream_ShowMark' . c . ' text=�� texthl=Cream_ShowMarksHL'
128		"    execute 'sign define Cream_ShowMark' . C . ' text=�� texthl=Cream_ShowMarksHL'
129		"else
130			execute 'sign define Cream_ShowMark' . c . ' text==> texthl=Cream_ShowMarksHL'
131			execute 'sign define Cream_ShowMark' . C . ' text==> texthl=Cream_ShowMarksHL'
132		"endif
133		let n = n + 1
134	endwhile
135endfunction
136call Cream_ShowMarksSetup()
137
138" Toggle display of bookmarks
139function! Cream_ShowMarksToggle()
140	" if un-initialized
141	if !exists("b:Cream_ShowMarks_Enabled")
142		let b:Cream_ShowMarks_Enabled = 1
143	endif
144
145	" if off
146	if b:Cream_ShowMarks_Enabled == 0
147		let b:Cream_ShowMarks_Enabled = 1
148		"+++ Cream: tampering
149		"augroup Cream_ShowMarks
150		"    "autocmd CursorHold * call Cream_ShowMarks()
151		"    autocmd BufRead * let b:Cream_ShowMarks_Enabled=1
152		"    autocmd BufRead * call Cream_ShowMarks()
153		"augroup END
154		call Cream_ShowMarks()
155		"+++
156	" if on
157	else
158		let b:Cream_ShowMarks_Enabled = 0
159		let n = 0
160		" find next available tag
161		while n < 26
162			let c = nr2char(char2nr('a') + n)
163			let C = nr2char(char2nr('A') + n)
164			let id = n + 52 * winbufnr(0)
165			let ID = id + 26
166			if exists('b:Cream_placed_' . c)
167				let b:Cream_placed_{c} = 1
168				execute 'sign unplace ' . id . ' buffer=' . winbufnr(0)
169			endif
170			if exists('b:Cream_placed_' . C)
171				let b:Cream_placed_{C} = 1
172				execute 'sign unplace ' . ID . ' buffer=' . winbufnr(0)
173			endif
174			let n = n + 1
175		endwhile
176		"+++ Cream: tampering
177		"augroup Cream_ShowMarks
178		"    autocmd!
179		"augroup END
180		"+++
181	endif
182endfunction
183
184function! Cream_ShowMarks_Exist()
185" returns 1 if a sign has been placed, 0 if not
186
187	let n = 0
188	" find next available tag
189	while n < 26
190		let c = 'b:Cream_placed_' . nr2char(char2nr('a') + n)
191		let C = 'b:Cream_placed_' . nr2char(char2nr('A') + n)
192		if exists(c)
193			execute "let test = b:Cream_placed_" . nr2char(char2nr('a') + n)
194			if test > 0
195				return 1
196			endif
197		endif
198		if exists(C)
199			execute "let test = b:Cream_placed_" . nr2char(char2nr('A') + n)
200			if test > 0
201				return 1
202			endif
203		endif
204		let n = n + 1
205	endwhile
206	return 0
207
208endfunction
209
210
211" This function is called on the CursorHold autocommand.
212" It runs through all the marks and displays or removes signs as appropriate.
213function! Cream_ShowMarks()
214	if exists("b:Cream_ShowMarks_Enabled")
215		if b:Cream_ShowMarks_Enabled == 0
216			return
217		endif
218	endif
219	let n = 0
220	while n < 26
221		let c = nr2char(char2nr('a') + n)
222		let id = n + 52 * winbufnr(0)
223		let ln = line("'" . c)
224		let C = nr2char(char2nr('A') + n)
225		let ID = id + 26
226		let LN = line("'" . C)
227
228		if ln == 0 && (exists('b:Cream_placed_' . c) && b:Cream_placed_{c} != ln )
229			execute 'sign unplace ' . id . ' buffer=' . winbufnr(0)
230		elseif ln != 0 && (!exists('b:Cream_placed_' . c) || b:Cream_placed_{c} != ln )
231			execute 'sign unplace ' . id . ' buffer=' . winbufnr(0)
232			execute 'sign place '. id . ' name=Cream_ShowMark' . c . ' line=' . ln . ' buffer=' . winbufnr(0)
233		endif
234
235		if LN == 0 && (exists('b:Cream_placed_' . C) && b:Cream_placed_{C} != LN )
236			execute 'sign unplace ' . ID . ' buffer=' . winbufnr(0)
237		elseif LN != 0 && (!exists('b:Cream_placed_' . C) || b:Cream_placed_{C} != LN )
238			execute 'sign unplace ' . ID . ' buffer=' . winbufnr(0)
239			execute 'sign place ' . ID . ' name=Cream_ShowMark' . C . ' line=' . LN . ' buffer=' . winbufnr(0)
240		endif
241
242		let b:Cream_placed_{c} = ln
243		let b:Cream_placed_{C} = LN
244	let n = n + 1
245	endwhile
246endfunction
247
248
249" Hide the mark at the current line.
250" This simply moves the mark to line 1 and hides the sign.
251"+++ Cream: Unused
252function! Cream_HideMark()
253	let ln = line(".")
254	let n = 0
255	while n < 26
256		let c = nr2char(char2nr('a') + n)
257		let C = nr2char(char2nr('A') + n)
258		let markln = line("'" . c)
259		let markLN = line("'" . C)
260		if ln == markln
261			let id = n + 52 * winbufnr(0)
262			execute 'sign unplace ' . id . ' buffer=' . winbufnr(0)
263			execute '1 mark ' . c
264			let b:Cream_placed_{c} = 1
265		endif
266		if ln == markLN
267			let ID = (n + 52 * winbufnr(0)) + 26
268			execute 'sign unplace ' . ID . ' buffer=' . winbufnr(0)
269			execute '1 mark ' . C
270			let b:Cream_placed_{C} = 1
271		endif
272		let n = n + 1
273	endwhile
274endfunction
275"+++
276
277
278"----------------------------------------------------------------------
279"	Bookmark management
280"
281"......................................................................
282" Source: http://vim.sourceforge.net/scripts/script.php?script_id=66
283" Name:   _vim_wok_visualcpp01.zip
284"
285" Wolfram 'Der WOK' Esser, 2001-08-21
286" mailto:wolfram@derwok.de
287" http://www.derwok.de
288" Version 0.1, 2001-08-21 - initial release
289"......................................................................
290
291" WOK: 2001-06-08
292" simulates anonymous marks with named marks "a-z"
293" Jumps to next following mark after current cursor
294" Wraps at bottom of file
295function! Cream_WOK_mark_next()
296
297	let curline = line(".")
298	let nextmark = 99999999
299	let firstmark = 99999999
300
301	" ASCII A-Z, a-z == 65-90, 97-122
302	let i = 97
303	"let i = 65
304	while i < 123
305		"if i == 91
306		"	let i = 97
307		"endif
308		let m = nr2char(i)
309		let ml = line("'" . m)
310		if(ml != 0)
311			if (ml > curline && ml < nextmark)
312				let nextmark = ml
313			endif
314			if (ml < firstmark)
315				let firstmark = ml
316			endif
317		endif
318
319		let i = i + 1
320	endwhile
321
322	if (nextmark != 99999999)
323		execute "normal " . nextmark . "G<CR>"
324		normal 0
325	elseif (firstmark != 99999999)
326		execute "normal " . firstmark . "G<CR>"
327		normal 0
328		"echo "WRAP TO FIRST MARK"
329	else
330		"echo "SORRY, NO MARKS (set with CTRL-F2)"
331	endif
332
333endfunction
334
335" WOK: 2001-06-08
336" simulates anonymous marks with named marks "a-z"
337" Jumps to first Preceding Mark before current cursor
338" Wraps at top of file
339function! Cream_WOK_mark_prev()
340
341	let curline = line(".")
342	let prevmark = -1
343	let lastmark = -1
344
345	" ASCII A-Z, a-z == 65-90, 97-122
346	let i = 97
347	"let i = 65
348	while i < 123
349		"if i == 91
350		"	let i = 97
351		"endif
352		let m = nr2char(i)
353		let ml = line("'" . m)
354		if(ml != 0)
355			if (ml < curline && ml > prevmark)
356				let prevmark = ml
357			endif
358			if (ml > lastmark)
359				let lastmark = ml
360			endif
361		endif
362
363		let i = i + 1
364	endwhile
365
366	if (prevmark != -1)
367		execute "normal " . prevmark . "G<CR>"
368		normal 0
369	elseif (lastmark != -1)
370		execute "normal " . lastmark . "G<CR>"
371		normal 0
372		"echo "WRAP TO LAST MARK"
373	else
374		"echo "SORRY, NO MARKS (set with CTRL-F2)"
375	endif
376
377endfunction
378
379" WOK: 2001-06-08
380" simulates anonymous marks with named marks "a-z"
381" Adds/Deletes 'anonymous' Mark on current line
382function! Cream_WOK_mark_toggle()
383
384	let curline = line(".")
385	let freemark = ""
386	let foundfree = 0
387	let mymodified = &modified
388
389	" suchen, ob Zeile ein Mark hat, wenn ja, alle sammeln in killmarks!
390	let killmarks = ""
391	let killflag = 0
392
393	" ASCII A-Z, a-z == 65-90, 97-122
394	let i = 97
395	"let i = 65
396	while i < 123
397		"if i == 91
398		"	let i = 97
399		"endif
400		let m = nr2char(i)
401		let ml = line("'" . m)
402		if (ml == curline)
403			let killmarks = killmarks . "m" . m
404			let killflag = 1
405		endif
406
407		let i = i + 1
408	endwhile
409
410	if (killflag)
411		" alte Mark(s) l�schen??
412		"echo "KILLMARKS: '" . killmarks . "'"
413		normal o
414		execute "normal " . killmarks . "<CR>"
415		normal dd
416		normal 0
417		normal k
418		"+++ Cream:
419		"normal h
420		"+++
421	else
422		" oder neues Mark einf�gen?
423
424		" ASCII A-Z, a-z == 65-90, 97-122
425		let i = 97
426		"let i = 65
427		while i < 123
428			"if i == 91
429			"	let i = 97
430			"endif
431			" erstes freies suchen...
432			let m = nr2char(i)
433			let ml = line("'" . m)
434			if(ml == 0)
435				let freemark = m
436				let foundfree = 1
437				let i = 130
438			endif
439
440			let i = i + 1
441		endwhile
442
443		if (foundfree)
444			"echo "NEWMARK: '" . freemark . "'"
445			execute "normal m" . freemark . "<CR>"
446		endif
447	endif
448
449	"+++ Cream:
450	" turn on bookmark indications if using this feature
451	let b:Cream_ShowMarks_Enabled = 1
452	" required to refresh
453	call Cream_ShowMarks()
454	"+++
455
456	let &modified = mymodified
457
458endfunction
459
460" WOK: 2001-06-08
461" simulates anonymous marks with named marks "a-z"
462" removes all marks "a" - "z"
463function! Cream_WOK_mark_killall()
464	let killmarks = ""
465	let killflag = 0
466
467	" ASCII A-Z, a-z == 65-90, 97-122
468	let i = 97
469	"let i = 65
470	while i < 123
471		"if i == 91
472		"	let i = 97
473		"endif
474		let m = nr2char(i)
475		let ml = line("'" . m)
476		" ex. dieses Mark?
477		if (ml != 0)
478			let killmarks = killmarks . "m" . m
479			let killflag = 1
480		endif
481
482		let i = i + 1
483	endwhile
484
485	if (killflag)
486		" alte Mark(s) l�schen
487		"echo "KILL ALL MARKS IN FILE: '" . killmarks . "'"
488		normal o
489		execute "normal " . killmarks . "<CR>"
490		normal dd
491		normal 0
492		normal k
493	endif
494
495	"+++ Cream:
496	" turn on bookmark indications if using this feature
497	let b:Cream_ShowMarks_Enabled = 1
498	" required to refresh
499	call Cream_ShowMarks()
500	"+++
501
502endfunction
503
504
505