-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathrmd_rplugin.vim
More file actions
170 lines (149 loc) · 5.45 KB
/
rmd_rplugin.vim
File metadata and controls
170 lines (149 loc) · 5.45 KB
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
170
if exists("g:disable_r_ftplugin") || has("nvim")
finish
endif
" Source scripts common to R, Rrst, Rnoweb, Rhelp and Rdoc:
runtime r-plugin/common_global.vim
if exists("g:rplugin_failed")
finish
endif
" Some buffer variables common to R, Rmd, Rrst, Rnoweb, Rhelp and Rdoc need to
" be defined after the global ones:
runtime r-plugin/common_buffer.vim
if !exists("b:did_ftplugin") && !exists("g:rplugin_runtime_warn")
runtime ftplugin/rmd.vim
if !exists("b:did_ftplugin")
call RWarningMsgInp("Your runtime files seems to be outdated.\nSee: https://github.com/jalvesaq/R-Vim-runtime")
let g:rplugin_runtime_warn = 1
endif
endif
function! RmdIsInRCode(vrb)
let chunkline = search("^[ \t]*```[ ]*{r", "bncW")
let docline = search("^[ \t]*```$", "bncW")
if chunkline > docline && chunkline != line(".")
return 1
else
if a:vrb
call RWarningMsg("Not inside an R code chunk.")
endif
return 0
endif
endfunction
function! RmdPreviousChunk() range
let rg = range(a:firstline, a:lastline)
let chunk = len(rg)
for var in range(1, chunk)
let curline = line(".")
if RmdIsInRCode(0)
let i = search("^[ \t]*```[ ]*{r", "bnW")
if i != 0
call cursor(i-1, 1)
endif
endif
let i = search("^[ \t]*```[ ]*{r", "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! RmdNextChunk() range
let rg = range(a:firstline, a:lastline)
let chunk = len(rg)
for var in range(1, chunk)
let i = search("^[ \t]*```[ ]*{r", "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
function! RMakeRmd(t)
update
if a:t == "odt"
if has("win32") || has("win64")
let g:rplugin_soffbin = "soffice.exe"
else
let g:rplugin_soffbin = "soffice"
endif
if !executable(g:rplugin_soffbin)
call RWarningMsg("Is Libre Office installed? Cannot convert into ODT: '" . g:rplugin_soffbin . "' not found.")
return
endif
endif
let rmddir = expand("%:p:h")
if has("win32") || has("win64")
let rmddir = substitute(rmddir, '\\', '/', 'g')
endif
if a:t == "default"
let rcmd = 'vim.interlace.rmd("' . expand("%:t") . '", rmddir = "' . rmddir . '"'
else
let rcmd = 'vim.interlace.rmd("' . expand("%:t") . '", outform = "' . a:t .'", rmddir = "' . rmddir . '"'
endif
if (g:vimrplugin_openhtml == 0 && a:t == "html_document") || (g:vimrplugin_openpdf == 0 && (a:t == "pdf_document" || a:t == "beamer_presentation"))
let rcmd .= ", view = FALSE"
endif
let rcmd = rcmd . ', envir = ' . g:vimrplugin_rmd_environment . ')'
call g:SendCmdToR(rcmd)
endfunction
" Send Rmd chunk to R
function! SendRmdChunkToR(e, m)
if RmdIsInRCode(0) == 0
call RWarningMsg("Not inside an R code chunk.")
return
endif
let chunkline = search("^[ \t]*```[ ]*{r", "bncW") + 1
let docline = search("^[ \t]*```", "ncW") - 1
let lines = getline(chunkline, docline)
let ok = RSourceLines(lines, a:e)
if ok == 0
return
endif
if a:m == "down"
call RmdNextChunk()
endif
endfunction
let b:IsInRCode = function("RmdIsInRCode")
let b:PreviousRChunk = function("RmdPreviousChunk")
let b:NextRChunk = function("RmdNextChunk")
let b:SendChunkToR = function("SendRmdChunkToR")
"==========================================================================
" Key bindings and menu items
call RCreateStartMaps()
call RCreateEditMaps()
call RCreateSendMaps()
call RControlMaps()
call RCreateMaps("nvi", '<Plug>RSetwd', 'rd', ':call RSetWD()')
" Only .Rmd files use these functions:
call RCreateMaps("nvi", '<Plug>RKnit', 'kn', ':call RKnit()')
call RCreateMaps("nvi", '<Plug>RMakeRmd', 'kr', ':call RMakeRmd("default")')
call RCreateMaps("nvi", '<Plug>RMakePDFK', 'kp', ':call RMakeRmd("pdf_document")')
call RCreateMaps("nvi", '<Plug>RMakePDFKb', 'kl', ':call RMakeRmd("beamer_presentation")')
call RCreateMaps("nvi", '<Plug>RMakeHTML', 'kh', ':call RMakeRmd("html_document")')
call RCreateMaps("nvi", '<Plug>RMakeODT', 'ko', ':call RMakeRmd("odt")')
call RCreateMaps("ni", '<Plug>RSendChunk', 'cc', ':call b:SendChunkToR("silent", "stay")')
call RCreateMaps("ni", '<Plug>RESendChunk', 'ce', ':call b:SendChunkToR("echo", "stay")')
call RCreateMaps("ni", '<Plug>RDSendChunk', 'cd', ':call b:SendChunkToR("silent", "down")')
call RCreateMaps("ni", '<Plug>REDSendChunk', 'ca', ':call b:SendChunkToR("echo", "down")')
call RCreateMaps("n", '<Plug>RNextRChunk', 'gn', ':call b:NextRChunk()')
call RCreateMaps("n", '<Plug>RPreviousRChunk', 'gN', ':call b:PreviousRChunk()')
" Menu R
if has("gui_running")
runtime r-plugin/gui_running.vim
call MakeRMenu()
endif
let g:rplugin_has_pandoc = 0
let g:rplugin_has_soffice = 0
call RSetPDFViewer()
call RSourceOtherScripts()
if exists("b:undo_ftplugin")
let b:undo_ftplugin .= " | unlet! b:IsInRCode b:PreviousRChunk b:NextRChunk b:SendChunkToR"
else
let b:undo_ftplugin = "unlet! b:IsInRCode b:PreviousRChunk b:NextRChunk b:SendChunkToR"
endif