Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LaTeXtoUnicode files are not loaded lazily #269

Open
savq opened this issue Jul 18, 2021 · 11 comments
Open

LaTeXtoUnicode files are not loaded lazily #269

savq opened this issue Jul 18, 2021 · 11 comments

Comments

@savq
Copy link

savq commented Jul 18, 2021

In ftdetect/julia.vim there are some autocommands that call LaTeXtoUnicode#Refresh() for all filetypes, meaning that the files in autoload/ are loaded every time at startup and then every time a buffer opens.

This makes julia-vim one of the slowest plugins to load in my config, and the slowest filetype plugin by quite some margin (measured with tweekmonster/startuptime.vim).

It'd be great if this could be improved in the near future.

@clason
Copy link
Contributor

clason commented Jul 30, 2021

The design also makes it so I can't lazy-load the plugin myself (e.g., using https://github.com/wbthomason/packer.nvim on ft = 'julia').

I understand that it can be useful to expose the L2U substitution for other filetypes as well (and have used it in Markdown on occasion myself), but it feels very wrong when a filetype plugin like this one "spills out" to other files.

@clason
Copy link
Contributor

clason commented Aug 3, 2021

Specifically, I'd be interesting in hearing what the rationale is for registering these autocommands for all buffers instead of only Julia (and Julia Markdown) buffers:

autocmd FileType * call LaTeXtoUnicode#Refresh()
autocmd BufNew * call LaTeXtoUnicode#Refresh()
autocmd BufEnter * call LaTeXtoUnicode#Refresh()
autocmd CmdwinEnter * call LaTeXtoUnicode#Refresh()

Replacing * here (and in the function below) with *.jl,*.jmd should work just as well and reduce startup time (and the possibility of conflicts) for other file types.

@clason
Copy link
Contributor

clason commented Aug 3, 2021

...which is related to @wbthomason's suggestion in #263 (comment) -- checking g:latex_to_unicode_tab and, if disabled, instead setting the autocommands only for *.jl,*.jmd seems to me an easy and (according to my brief tests) working compromise?

@carlobaldassi
Copy link
Contributor

The reason for the * is that otherwise the L2U functionality would be limited to Julia files. Having it for other file types was proposed in #33 (also cross-referencing #271). The work-around of guarding it with g:latex_to_unicode_tab (actually it would need to check a few more options too, for the auto-sub and keymap versions) would mean that one could not activate the functionality without restarting vim, whereas now it can be switched on and off at any time on any file (I have a keymap for doing just that, for example).

@clason
Copy link
Contributor

clason commented Aug 3, 2021

I see where you are coming from, yes. I understand that the current setup is very convenient; it'd just be great if there was more choice (short of patching the plugin) :)

It would probably be possible to use the g:latex_to_unicode_file_types variable to set these autocommands only for the explicitly specified filetypes (with a special all value for *)?

@danlkv
Copy link

danlkv commented Feb 3, 2023

Is there any workaround to disable the L2U for non-julia files? I don't need to type unicode anywhere else and the delay in startup is almost half a second, which feels very long when editing some config file quickly.

@ndgnuh
Copy link

ndgnuh commented Jan 16, 2024

AFAIK, latex_to_unicode_file_types does not prevent the L2U init function from being called, it is only used to check if the L2U functionality should be enabled in the L2U init function.

Is there any workaround to disable the L2U for non-julia files? I don't need to type unicode anywhere else and the delay in startup is almost half a second, which feels very long when editing some config file quickly.

Here's my workaround for lazy loading. You can either use the this fork or this diff file. I'm not a Vim package developer so this is probably not the "correct" way to do this.

diff --git a/autoload/LaTeXtoUnicode.vim b/autoload/LaTeXtoUnicode.vim
index f6237dc..b4d6547 100644
--- a/autoload/LaTeXtoUnicode.vim
+++ b/autoload/LaTeXtoUnicode.vim
@@ -3,6 +3,10 @@
 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 
 function! s:L2U_Setup()
+  let b:l2u_did_buffer_setup = get(b:, "l2u_did_buffer_setup", 0)
+  if b:l2u_did_buffer_setup
+    return ''
+  endif
 
   call s:L2U_SetupGlobal()
 
@@ -28,7 +32,8 @@ function! s:L2U_Setup()
   let b:l2u_tab_completing = 0
   " Are we calling the tab fallback?
   let b:l2u_in_fallback = 0
-
+  " Do not call setup the second time
+  let b:l2u_did_buffer_setup = 1
 endfunction
 
 function! s:L2U_SetupGlobal()
@@ -96,8 +101,10 @@ function! LaTeXtoUnicode#Refresh()
   endif
 endfunction
 
+
 function! LaTeXtoUnicode#Enable(...)
   let auto_set = a:0 > 0 ? a:1 : 0
+  call s:L2U_Setup()
 
   if b:l2u_enabled
     return ''
@@ -723,6 +730,7 @@ endfunction
 
 " Initialization. Can be used to re-init when global settings have changed.
 function! LaTeXtoUnicode#Init(...)
+
   let wait_insert_enter = a:0 > 0 ? a:1 : 1
 
   if !wait_insert_enter
@@ -738,6 +746,7 @@ function! LaTeXtoUnicode#Init(...)
   call s:L2U_SetTab(wait_insert_enter)
   call s:L2U_SetAutoSub(wait_insert_enter)
   call s:L2U_SetKeymap()
+
   return ''
 endfunction
 
diff --git a/ftdetect/julia.vim b/ftdetect/julia.vim
index 9a4e8cb..8ab89ad 100644
--- a/ftdetect/julia.vim
+++ b/ftdetect/julia.vim
@@ -7,20 +7,18 @@ endif
 
 autocmd BufRead,BufNewFile *.jl      set filetype=julia
 
-autocmd FileType *                   call LaTeXtoUnicode#Refresh()
-autocmd BufNew *                     call LaTeXtoUnicode#Refresh()
-autocmd BufEnter *                   call LaTeXtoUnicode#Refresh()
-autocmd CmdwinEnter *                call LaTeXtoUnicode#Refresh()
+" Use latex to unicode on these pattern only
+let s:l2u_patterns = get(g:, "latex_to_unicode_file_types", "*.jl,*.jmd")
 
 " This autocommand is used to postpone the first initialization of LaTeXtoUnicode as much as possible,
 " by calling LaTeXtoUnicode#SetTab and LaTeXtoUnicode#SetAutoSub only at InsertEnter or later
 function! s:L2UTrigger()
   augroup L2UInit
     autocmd!
-    autocmd InsertEnter *            let g:did_insert_enter = 1 | call LaTeXtoUnicode#Init(0)
+    execute 'autocmd InsertEnter ' . s:l2u_patterns . ' let g:did_insert_enter = 1 | call LaTeXtoUnicode#Enable()'
   augroup END
 endfunction
-autocmd BufEnter *                   call s:L2UTrigger()
+execute 'autocmd BufEnter ' . s:l2u_patterns . ' call s:L2UTrigger()'
 
 function! s:HighlightJuliaMarkdown()
   if !exists('g:markdown_fenced_languages')

Configuration (this have to be set before the package is loaded):

vim.g.latex_to_unicode_file_types = "*.jl,*.jmd,*.py"

or

let g:latex_to_unicode_file_types = '*.jl,*.jmd,*.py'

Demo:

  • Markdown (disabled):
    image
  • Python (enabled):
    image
  • Julia (enabled):
    image

@clason
Copy link
Contributor

clason commented Jan 16, 2024

I have (long since) dropped this plugin in favor of builtin filetype support and a separate unicode symbol inserter (based on Telescope).

(Excessive use of unicode identifiers in Julia is a scourge anyway, since it makes it really tedious to search and replace. And don't get me started on the "Language Server"...)

@dpo
Copy link

dpo commented Aug 21, 2024

What separate unicode symbol inserter?

@dpo
Copy link

dpo commented Aug 22, 2024

https://github.com/arthurxavierx/vim-unicoder seems to work well.

@wisp3rwind
Copy link

For nvim-cmp there's also https://github.com/amarakon/nvim-cmp-lua-latex-symbols.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants