julia-vim icon indicating copy to clipboard operation
julia-vim copied to clipboard

`LaTeXtoUnicode` files are not loaded lazily

Open savq opened this issue 2 years ago • 8 comments

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.

savq avatar Jul 18 '21 19:07 savq

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 avatar Jul 30 '21 08:07 clason

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: https://github.com/JuliaEditorSupport/julia-vim/blob/6ff9e9938badea96f64154f2991e165e8d340cf2/ftdetect/julia.vim#L10-L13

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 avatar Aug 03 '21 12:08 clason

...which is related to @wbthomason's suggestion in https://github.com/JuliaEditorSupport/julia-vim/issues/263#issuecomment-866388970 -- 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?

clason avatar Aug 03 '21 14:08 clason

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).

carlobaldassi avatar Aug 03 '21 20:08 carlobaldassi

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 *)?

clason avatar Aug 03 '21 20:08 clason

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.

danlkv avatar Feb 03 '23 17:02 danlkv

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

ndgnuh avatar Jan 16 '24 07:01 ndgnuh

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"...)

clason avatar Jan 16 '24 07:01 clason