aboutsummaryrefslogtreecommitdiff
path: root/plugins/texcompile.lua
blob: 557e4046cd3c1130a134f5d7fd858ab91d78c6aa (plain)
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
-- mod-version:3
local core = require "core"
local config = require "core.config"
local command = require "core.command"
local common = require "core.common"
local console = require "plugins.console"
local keymap = require "core.keymap"

-- This plugin requires the console plugin to work. It can be found at:
--
-- https://github.com/lite-xl/console
--
-- Before using this plugin add in your user's config file something like:
--
-- config.texcompile = {
--   latex_command = "pdflatex",
--   view_command = "evince",
-- }
--
-- as long as the commands are in your PATH.
--
-- Options can be passed as part of the command for example like in:
--
-- latex_command = "latex -pdf -pdflatex -c".
--
-- On Windows, if the commands are not in your PATH, you may use the full path
-- of the executable like, for example:
--
-- config.texcompile = {
--   latex_command = [[C:\miktex\miktex\bin\x64\pdflatex.exe]],
--   view_command = [[C:\Program^ Files\SumatraPDF\SumatraPDF.exe]],
-- }
--
-- Note that in the example we have used "^ " for spaces that appear in the path.
-- It is required on Windows for path or file names that contains space characters.

command.add("core.docview!", {
  ["texcompile:tex-compile"] = function(dv)
    -- The current (La)TeX file and path
    local texname = dv:get_name()
    local texpath = common.dirname(dv:get_filename())
    local pdfname = texname:gsub("%.tex$", ".pdf")

    -- LaTeX compiler as configured in config.texcompile
    local texcmd = config.texcompile and config.texcompile.latex_command
    local viewcmd = config.texcompile and config.texcompile.view_command

    if not texcmd then
      core.log("No LaTeX compiler provided in config.")
    else
      core.log("LaTeX compiler is %s, compiling %s", texcmd, texname)

      console.run {
        command = string.format("%s %s && %s %s", texcmd, texname, viewcmd, pdfname),
        cwd = texpath,
        on_complete = function() core.log("Tex compiling command terminated.") end
      }
    end
  end,
})

keymap.add { ["ctrl+shift+t"] = "texcompile:tex-compile" }