lervag / vimtex

VimTeX: A modern Vim and neovim filetype plugin for LaTeX files.
MIT License
5.47k stars 389 forks source link

Continuous Mode stopped after switching files #2996

Closed fhfuih closed 3 weeks ago

fhfuih commented 1 month ago

Description

I use VimTex with default configuration on compilation (there are some syntax configurations, but they shouldn't be related) on NeoVim. Whenever I switch to a new .tex file within the project folder, VimTex pauses with a message that the continuous mode is stopped. I wonder how it can be.

This may not be a VimTex issue, but probably I can get some help to start my debugging because I don't read Vimscript. (for example, when will VimTex decide to stop the continuous mode?) Also, maybe it helps identify compatibility issues for the neovim distro I use.

Steps to reproduce

Use AstroNvim distro. (Not sure if it really matters)

Use the following VimTex config

return {
  {
    "lervag/vimtex",
    ft = { "tex", "bib" },
    config = function()
      -- Highlight: use VimTeX, not TS.
      -- -- See *vimtex-faq-treesitter*
      -- -- Plus, current latex TS seems to not support expl3
      local g = vim.g
      -- Auto compile: use VimTeX, not LSP
      -- -- because my texlab sometimes detects wrong root file.
      -- -- And it will generate aux files for subfiles upon build.
      -- -- And there is subsequent undefined control sequences everywhere
      -- -- until I delete the wrong aux files
      -- -- This seems to be a recent problem though. Dunno why.
      -- -- I have also tried texlab.rootDirectory="." according to an issue. But no luck.
      vim.api.nvim_create_augroup("vimtex_config", {})
      vim.api.nvim_create_autocmd("User", {
        group = "vimtex_config",
        pattern = "VimtexEventInitPost",
        command = "VimtexCompile",
      })
      -- Auto view after compile: use VimTeX, not LSP
      -- -- It is related to what I use for auto compile
      -- -- Also, VimTeX can configure whether or not to give up window focus to PDF previewer
      if vim.fn.has "mac" then
        g.vimtex_view_method = "skim"
        g.vimtex_view_skim_sync = 1
        g.vimtex_view_skim_activate = 0
        g.vimtex_view_skim_reading_bar = 1
      end
      -- Diagnostic: use LSP, not VimTeX
      -- -- I can use a unified diagnostic view to see compilation errors and other lsp's outputs
      -- -- And also I hate it when a quickfix window popup when I am continuously building and writing
      g.vimtex_quickfix_enabled = 0
      -- Completion: use LSP, not VimTeX
      g.vimtex_complete_enabled = 0
      -- Fold: use VimTeX, not TS
      g.vimtex_fold_enabled = 1
      -- Configure conceals
      g.vimtex_syntax_conceal = {
        ligatures = 0,
        spacing = 0,
        sections = 1,
      }
      g.vimtex_syntax_conceal_cites = {
        ["type"] = "icon",
      }
      -- math mode imaps
      g.vimtex_imaps_leader = "@"
    end,
  },
}

Use the following test files

.
├── main.tex
└── sections
    └── section.tex
\documentclass{article}
\begin{document}
Hello world!
\input{sections/section.tex}
\end{document}
\textbf{something}

Open nvim with current directory at ., open sections/section.tex. VimTex should show a message that the compiler is started. Then switch to main.tex (I use Telescope and neotree)

Expected behavior

VimTex stays in continuous mode.

Actual behavior

VimTex stopped continuous mode. It echoes a message "Compiler Stopped (main.tex)"

Do you use a latexmkrc file?

No

VimtexInfo

System info:
  OS: macOS 13.2 (22D49)
  Vim version: NVIM v0.10.1
  Has clientserver: true
  Servername: /var/folders/6f/z0lqhhwd273d4_plbc4m5p000000gn/T/nvim.zeyu/4lA2zY/nvim.35251.0

VimTeX project: main
  base: main.tex
  root: /Users/zeyu/markups/test
  tex: /Users/zeyu/markups/test/main.tex
  main parser: recursive search
  document class: article
  source files:
    main.tex
    sections/section.tex
  compiler: latexmk
    engine: -pdf
    options:
      -verbose
      -file-line-error
      -synctex=1
      -interaction=nonstopmode
    callback: 1
    continuous: 1
    executable: latexmk
    job: 
      jobid: 6
      output: /var/folders/6f/z0lqhhwd273d4_plbc4m5p000000gn/T/nvim.zeyu/4lA2zY/0
      cmd: max_print_line=2000 latexmk -verbose -file-line-error -synctex=1 -interaction=nonstopmode  -pdf -pvc -pvctimeout- -view=none -e '$compiling_cmd = ($compiling_cmd ? $compiling_cmd . " ; " : "") . "echo vimtex_compiler_callback_compiling"' -e '$success_cmd = ($success_cmd ? $success_cmd . " ; " : "") . "echo vimtex_compiler_callback_success"' -e '$failure_cmd = ($failure_cmd ? $failure_cmd . " ; " : "") . "echo vimtex_compiler_callback_failure"' 'main.tex'
      pid: 0
  viewer: Skim
lervag commented 3 weeks ago

I use VimTex with default configuration on compilation (there are some syntax configurations, but they shouldn't be related) on NeoVim.

See below.

Steps to reproduce

Use AstroNvim distro. (Not sure if it really matters)

It matters in the sense that I don't know how that works. But let's assume it is not relevant right now.

Use the following VimTex config …

The config here is quite flawed. First, and very importantly, please don't lazy load VimTeX. It is lazy be design and using ft = ... will break the inverse search feature.

Next, please use init = function() instead of config = function(). This is because we want to ensure that the options are defined before VimTeX is initialized. For some reason, using the config key may lead to a race condition in which VimTeX was loaded for a buffer before the settings are defined.

Both of the above are, in my humble opinion, relatively clear from the README file. This indicates to me that you are using other sources for learning how to configure VimTeX. They may be good, but please, consider to also read or skim the official docs (README and :help vimtex).

I've attempted to improve your config with my above feedback, as well as a few adjustments where I've removed settings you didn't change from their defaults. I stripped the comments for clarity.

return {
  {
    "lervag/vimtex",
    lazy = false, -- this is only necessary if you've specified that lazy
                  -- should be the default for lazy.nvim
    init = function()
      if vim.fn.has "mac" then
        vim.g.vimtex_view_method = "skim"
        vim.g.vimtex_view_skim_sync = 1
        vim.g.vimtex_view_skim_reading_bar = 1
      end

      vim.g.vimtex_quickfix_enabled = 0
      vim.g.vimtex_complete_enabled = 0
      vim.g.vimtex_fold_enabled = 1
      vim.g.vimtex_syntax_conceal = {
        ligatures = 0,
        spacing = 0,
        sections = 1,
      }
      vim.g.vimtex_syntax_conceal_cites = {
        ["type"] = "icon",
      }
      vim.g.vimtex_imaps_leader = "@"
    end,
  },
}

Now, to ensure that VimTeX is used for highlighting instead of TS, you only need to add this to your nvim-treesitter config - I guess you already do, but just in case:

require("nvim-treesitter.configs").setup {
  -- ...
  highlight = {
    enable = true,
    disable = { "latex" },
  },

Finally, I removed your "auto compile" autocommand. I would strongly suggest that you instead start compilation manually. In fact, I think this is the reason for your actual issue. The autocommand VimtexEventInitPost will fire when you open a new buffer, even if the buffer is part of the same "project". And VimtexCompile is a toggle command, so it will stop compilation if it was already running.

If you really do want that autocommand, then I suggest the following custom version:

vim.api.nvim_create_augroup("vimtex_config", {})
vim.api.nvim_create_autocmd("User", {
  group = "vimtex_config",
  pattern = "VimtexEventInitPost",
  callback = function()
    local vimtex = vim.api.nvim_buf_get_var(0, "vimtex")
    if vimtex.compiler.status and vimtex.compiler.status < 1 then
      vim.cmd "VimtexCompile"
    end
  end
})
fhfuih commented 2 weeks ago

Thanks very much for the detailed instructions and additional helps on optimizing the config! I apologize for missing the lazy-loading-related instructions at the very top of the README. I must have directly jumped into the in-vim documentation for concrete configuration items.

lervag commented 2 weeks ago

My pleasure. 😸