1"
2" Filename: cream-macros.vim
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
25" Cream_macro_record() {{{1
26function! Cream_macro_record(...)
27" Record to register. Optional argument is register letter
28
29	if exists("a:1")
30		" test single register
31		if strlen(a:1) > 1
32			call confirm(
33				\ "Error: Multiple characters passed to Cream_macro_record().\n" .
34				\ "\n", "&Ok", 1, "Info")
35			return
36		endif
37		" test non-alpha
38		if match(a:1, '\a') == -1
39			call confirm(
40				\ "Error: Non-character argument in Cream_macro_record().\n" .
41				\ "\n", "&Ok", 1, "Info")
42			return
43		endif
44		let myreg = a:1
45	else
46		let myreg = Cream_macro_getregister()
47		if myreg == -1
48			return
49		endif
50	endif
51
52	if exists("g:recording")
53		" stop
54		unlet g:recording
55		let g:recording_fix = 1
56		normal q
57
58		" fixes
59		execute "let x = @" . myreg
60
61		" remove trailing <S-F8> mapping
62		"let x = substitute(x, nr2char(128) . nr2char(253) . nr2char(13) . "$", "", "")
63		"let x = substitute(x, "��
64", '', '')
65		let x = substitute(x, "...$", '', '')
66
67		execute "let @" . myreg . " = x"
68	else
69		" start
70		let g:recording = 1
71		execute "normal q" . myreg
72	endif
73
74endfunction
75
76
77" Cream_macro_play() {{{1
78function! Cream_macro_play(...)
79
80	if exists("a:1")
81		" test single register
82		if strlen(a:1) > 1
83			call confirm(
84				\ "Error: Multiple characters passed to Cream_macro_play().\n" .
85				\ "\n", "&Ok", 1, "Info")
86			return
87		endif
88		" test non-alpha
89		if match(a:1, '\a') == -1
90			call confirm(
91				\ "Error: Non-character argument in Cream_macro_play().\n" .
92				\ "\n", "&Ok", 1, "Info")
93			return
94		endif
95		let myreg = a:1
96	else
97		let myreg = Cream_macro_getregister()
98		if myreg == -1
99			return
100		endif
101	endif
102
103	" add an insertmode call to temp register depending on col position
104	execute "let x = @" . myreg
105	if   col('.') != col('$')
106	\ && col('.') != col('$') - 1
107		let posfix = "i"
108	else
109		let posfix = "a"
110	endif
111	let @1 = posfix . x
112
113	" play register
114	normal @1
115
116
117	" fix position unless on first column of empty line
118	if   col('.') != col('$')
119	\ && col('.') != col('$') - 1
120		normal l
121	endif
122
123	"""let myline = ('.')
124	"""normal l
125	"""if line('.') > myline
126	"""    normal ha
127	"""endif
128
129endfunction
130
131" Cream_macro_getregister() {{{1
132function! Cream_macro_getregister()
133" get register from user
134
135	let myreg = Inputdialog(
136		\ "Enter a register (A-z) to use for macro\n(only first letter used):", "q")
137
138	" test cancel
139	if myreg == "{cancel}"
140		" No warning, just quit silently.
141		return -1
142	endif
143	" test at least one character
144	if myreg == ""
145		call confirm(
146			\ "One alphabetic character is required.\n" .
147			\ "\n", "&Ok", 1, "Info")
148		return -1
149	endif
150	" test at least one character
151	if myreg == ""
152		" No warning, just quit silently.
153		return -1
154	endif
155	" test single register
156	if strlen(myreg) > 1
157		call confirm(
158			\ "Only one character is allowed.\n" .
159			\ "\n", "&Ok", 1, "Info")
160		return -1
161	endif
162	" test non-alpha
163	if match(myreg, '\a') == -1
164		call confirm(
165			\ "Register must be a letter (upper or lower case) A-Z, a-z.\n" .
166			\ "\n", "&Ok", 1, "Info")
167		return -1
168	endif
169
170	return myreg
171
172endfunction
173
174" 1}}}
175" vim:foldmethod=marker
176