/
rnoweb.vim
170 lines (146 loc) · 4.69 KB
/
rnoweb.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
" This program is free software; you can redistribute it and/or modify
" it under the terms of the GNU General Public License as published by
" the Free Software Foundation; either version 2 of the License, or
" (at your option) any later version.
"
" This program is distributed in the hope that it will be useful,
" but WITHOUT ANY WARRANTY; without even the implied warranty of
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
" GNU General Public License for more details.
"
" A copy of the GNU General Public License is available at
" http://www.r-project.org/Licenses/
"==========================================================================
" ftplugin for R files
"
" Authors: Jakson Alves de Aquino <jalvesaq@gmail.com>
" Jose Claudio Faria
"
" Last Change: Tue Aug 30, 2011 09:25AM
"==========================================================================
" Only do this when not yet done for this buffer
if exists("b:did_rnoweb_ftplugin") || exists("disable_r_ftplugin")
finish
endif
" Don't load another plugin for this buffer
let b:did_rnoweb_ftplugin = 1
" Enables Vim-Latex-Suite if it is installed
runtime ftplugin/tex_latexSuite.vim
" Enable syntax highlight of LaTeX errors in R Console (if using Conque
" Shell)
let syn_rout_latex = 1
" Source scripts common to R, Rnoweb, Rhelp and Rdoc:
runtime r-plugin/common_global.vim
if exists("g:rplugin_failed")
finish
endif
" Some buffer variables common to R, Rnoweb, Rhelp and Rdoc need to be defined
" after the global ones:
runtime r-plugin/common_buffer.vim
setlocal iskeyword=@,48-57,_,.
function! RWriteChunk()
if getline(".") =~ "^\\s*$" && RnwIsInRCode() == 0
call setline(line("."), "<<>>=")
exe "normal! o@"
exe "normal! 0kl"
else
exe "normal! a<"
endif
endfunction
function! RnwIsInRCode()
let chunkline = search("^<<", "bncW")
let docline = search("^@", "bncW")
if chunkline > docline
return 1
else
return 0
endif
endfunction
function! RnwPreviousChunk() range
echon
let rg = range(a:firstline, a:lastline)
let chunk = len(rg)
for var in range(1, chunk)
let curline = line(".")
if RnwIsInRCode()
let i = search("^<<.*$", "bnW")
if i != 0
call cursor(i-1, 1)
endif
endif
let i = search("^<<.*$", "bnW")
if i == 0
call cursor(curline, 1)
call RWarningMsg("There is no previous R code chunk to go.")
return
else
call cursor(i+1, 1)
endif
endfor
return
endfunction
function! RnwNextChunk() range
echon
let rg = range(a:firstline, a:lastline)
let chunk = len(rg)
for var in range(1, chunk)
let i = search("^<<.*$", "nW")
if i == 0
call RWarningMsg("There is no next R code chunk to go.")
return
else
call cursor(i+1, 1)
endif
endfor
return
endfunction
" Sweave and compile the current buffer content
function! RMakePDF(bibtex)
update
call RSetWD()
let pdfcmd = "source('" . g:rplugin_home . "/r-plugin/vimSweave.R') ; .vim.Sweave('" . expand("%:t") . "'"
if g:vimrplugin_latexcmd != "pdflatex"
let pdfcmd = pdfcmd . ", latexcmd = '" . g:vimrplugin_latexcmd . "'"
endif
if a:bibtex == "bibtex"
let pdfcmd = pdfcmd . ", bibtex = TRUE"
endif
if exists("g:vimrplugin_sweaveargs")
let pdfcmd = pdfcmd . ", " . g:vimrplugin_sweaveargs
endif
let pdfcmd = pdfcmd . ")"
let b:needsnewomnilist = 1
let ok = SendCmdToR(pdfcmd)
if ok == 0
return
endif
echon
endfunction
" Sweave the current buffer content
function! RSweave()
update
let b:needsnewomnilist = 1
call RSetWD()
call SendCmdToR('Sweave("' . expand("%:t") . '")')
echon
endfunction
if g:vimrplugin_rnowebchunk == 1
" Write code chunk in rnoweb files
imap <buffer> < <Esc>:call RWriteChunk()<CR>a
endif
"==========================================================================
" Key bindings and menu items
call RCreateStartMaps()
call RCreateEditMaps()
call RCreateSendMaps()
call RControlMaps()
call RCreateMaps("nvi", '<Plug>RSetwd', 'rd', ':call RSetWD()')
" Only .Rnw files use these functions:
call RCreateMaps("nvi", '<Plug>RSweave', 'sw', ':call RSweave()')
call RCreateMaps("nvi", '<Plug>RMakePDF', 'sp', ':call RMakePDF("nobib")')
call RCreateMaps("nvi", '<Plug>RBibTeX', 'sb', ':call RMakePDF("bibtex")')
call RCreateMaps("nvi", '<Plug>RIndent', 'si', ':call RnwToggleIndentSty()')
nmap <buffer> gn :call RnwNextChunk()<CR>
nmap <buffer> gN :call RnwPreviousChunk()<CR>
" Menu R
call MakeRMenu()