forked from nvim-orgmode/orgmode
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.lua
More file actions
252 lines (221 loc) · 6.15 KB
/
init.lua
File metadata and controls
252 lines (221 loc) · 6.15 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
local utils = require('orgmode.utils')
local config = require('orgmode.config')
local Menu = require('orgmode.ui.menu')
---@class OrgExport
local Export = {}
---@param cmd table
---@param on_success? function
---@param on_error? function
function Export._exporter(cmd, target, on_success, on_error)
utils.echo_info('Exporting...')
local output = {}
local read_data = function(_, data, _)
for _, i in ipairs(data) do
if i and i ~= '' then
table.insert(output, i)
end
end
end
vim.fn.jobstart(cmd, {
on_stdout = read_data,
on_stderr = read_data,
on_exit = function(_, code, _)
if code ~= 0 then
if on_error then
return on_error(output)
end
return utils.echo_error(string.format('Export error:\n%s', table.concat(output, '\n')))
end
if on_success then
return on_success(output)
end
local menu = Menu:new({
title = string.format('Exported to %s', target),
prompt = 'Open?',
})
menu:add_separator({ length = 34 })
menu:add_option({
label = 'Yes',
key = 'y',
action = function()
return vim.ui.open(target)
end,
})
menu:add_option({ label = 'No', key = 'n' })
return menu:open()
end,
})
end
---@param opts table
function Export.pandoc(opts)
local file = utils.current_file_path()
local target = vim.fn.fnamemodify(file, ':p:r') .. '.' .. opts.extension
if vim.fn.executable('pandoc') ~= 1 then
return utils.echo_error('pandoc executable not found. Make sure pandoc is in $PATH.')
end
local cmd = { 'pandoc', file, '-o', target }
if opts.format then
table.insert(cmd, '-t')
table.insert(cmd, opts.format)
end
return Export._exporter(cmd, target)
end
---@param opts table
---@param skip_config? boolean
function Export.emacs(opts, skip_config)
local file = utils.current_file_path()
local target = vim.fn.fnamemodify(file, ':p:r') .. '.' .. opts.extension
local emacs = config.emacs_config.executable_path
local emacs_config_path = config.emacs_config.config_path
if not emacs_config_path and not skip_config then
local paths = {
'~/.config/emacs/init.el',
'~/.emacs.d/init.el',
'~/.emacs.el',
}
for _, path in ipairs(paths) do
if vim.uv.fs_stat(vim.fn.fnamemodify(path, ':p')) then
emacs_config_path = vim.fn.fnamemodify(path, ':p')
break
end
end
end
if vim.fn.executable(emacs) ~= 1 then
return utils.echo_error('emacs executable not found. Make sure emacs is in $PATH.')
end
local cmd = {
emacs,
'-nw',
'--batch',
}
if emacs_config_path and not skip_config then
table.insert(cmd, '--load')
table.insert(cmd, emacs_config_path)
end
table.insert(cmd, ('--visit=%s'):format(file))
table.insert(cmd, ('--funcall=%s'):format(opts.command))
return Export._exporter(cmd, target, nil, function(err)
table.insert(err, '')
table.insert(err, 'NOTE: Emacs export issues are most likely caused by bad or missing emacs configuration.')
utils.echo_error(string.format('Export error:\n%s', table.concat(err, '\n')))
if not skip_config then
if vim.fn.input('Attempt to export again without a configuration file? [y/n]') == 'y' then
return Export.emacs(opts, true)
end
end
end)
end
Export.emacs_beamer = Export.emacs
function Export.prompt()
local keys = {
emacs = 'e',
emacs_beamer = 'b',
pandoc = 'p',
}
local submenu = function(key, label, extension, exporters)
local commands = {}
local exporters_names = {}
for name, opts in utils.sorted_pairs(exporters) do
table.insert(exporters_names, name)
opts.extension = extension
local exporter_label = name
if opts.command then
exporter_label = string.format('%s (%s)', name, opts.command)
else
exporter_label = name
end
table.insert(commands, {
label = exporter_label,
key = keys[name],
action = function()
return Export[name](opts)
end,
})
end
table.sort(commands, function(lhs, rhs)
return lhs.label < rhs.label
end)
table.sort(exporters_names, function(lhs, rhs)
return lhs < rhs
end)
local action
if #commands > 1 then
action = function()
Menu:new({
title = label .. ' via',
items = commands,
prompt = label .. ' via',
}):open()
end
table.insert(commands, {
label = 'quit',
key = 'q',
})
else
action = commands[1].action
end
return {
label = string.format('%s (%s)', label, table.concat(exporters_names, '/')),
key = key,
action = action,
}
end
local opts = {
submenu('h', 'Export to HTML file', 'html', {
emacs = {
command = 'org-html-export-to-html',
},
pandoc = {},
}),
submenu('l', 'Export to LaTex file', 'tex', {
emacs = {
command = 'org-latex-export-to-latex',
},
emacs_beamer = {
command = 'org-beamer-export-to-latex',
},
pandoc = {},
}),
submenu('p', 'Export to PDF file', 'pdf', {
emacs = {
command = 'org-latex-export-to-pdf',
},
emacs_beamer = {
command = 'org-beamer-export-to-pdf',
},
pandoc = {},
}),
submenu('m', 'Export to Markdown file', 'md', {
emacs = {
command = 'org-md-export-to-markdown',
},
pandoc = {
format = 'gfm',
},
}),
submenu('i', 'Export to iCalendar file', 'ics', {
emacs = {
command = 'org-icalendar-export-to-ics',
},
}),
}
if not vim.tbl_isempty(config.org_custom_exports) then
for key, data in utils.sorted_pairs(config.org_custom_exports) do
table.insert(opts, {
key = key,
label = data.label,
action = function()
return data.action(Export._exporter)
end,
})
end
end
table.insert(opts, { label = 'quit', key = 'q' })
table.insert(opts, { icon = ' ', length = 1 })
return Menu:new({
title = 'Export options',
items = opts,
prompt = 'Export command',
}):open()
end
return Export