Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function! AdjustFontSize(amount)
- if !has("gui_running")
- return
- endif
- let l:min_font_size = 5
- let l:max_font_size = 64
- let l:font_info = GetFontInfo()
- if l:font_info.name == '' || l:font_info.size == ''
- return
- endif
- let l:font_name = l:font_info.name
- let l:font_size = l:font_info.size
- " Decrease font size.
- if a:amount == '-'
- let l:font_size = l:font_size - 1
- " Increase font size.
- elseif a:amount == '+'
- let l:font_size = l:font_size + 1
- " Use a specific font size.
- elseif str2nr(a:amount)
- let l:font_size = str2nr(a:amount)
- endif
- " Clamp font size.
- let l:font_size = max([l:min_font_size, min([l:max_font_size, l:font_size])])
- if matchstr(&guifont, ':') == '' " Linux guifont style.
- " \v Very magical.
- " (\d+$) Capture group: Match [0-9] one-or-more times, at the end of the string.
- let l:font_size_pattern = '\v(\d+$)'
- else " Windows and macOS guifont style.
- " \v Very magical.
- " (:h)@<= Positive lookbehind: Match ':h'.
- " (\d+) Capture group: Match [0-9] one-or-more times.
- let l:font_size_pattern = '\v(:h)@<=(\d+)'
- endif
- " Update vim font size.
- let &guifont = substitute(&guifont, l:font_size_pattern, l:font_size, '')
- call DisplayFontInfo()
- endfunction
- function! DisplayFontSelector()
- if !has("gui_running")
- return
- endif
- " Display font selector.
- " NOTE: This only changes &guifont to '*' in terminal vim.
- set guifont=*
- " Display font info after font selector closes.
- call DisplayFontInfo()
- endfunction
- function! DisplayFontInfo()
- let l:font_info = GetFontInfo()
- if l:font_info.name == '' || l:font_info.size == ''
- return
- endif
- " Display font name and size.
- redraw | echomsg l:font_info.name . ' ' . l:font_info.size . '%'
- endfunction
- function! GetFontInfo()
- " Windows and macOS &guifont: Hack NF:h11:cANSI
- " 3270Medium_NF:h10:W500:cANSI:qDRAFT
- " Linux &guifont: Hack Nerd Font Mono Regular 10
- if matchstr(&guifont, ':') == '' " Linux guifont style.
- " \v Very magical.
- " (^.{-1,}) Capture group: Anchored at the start of the string, match any character one-or-more times non-greedy.
- " ( \d+$)@= Positive lookahead: Match ' ' followed by [0-9] one-or-more times, at the end of the string.
- let l:font_name_pattern = '\v(^.{-1,})( \d+$)@='
- " \v Very magical.
- " (\d+$) Capture group: Match [0-9] one-or-more times, at the end of the string.
- let l:font_size_pattern = '\v(\d+$)'
- else " Windows and macOS guifont style.
- " \v Very magical.
- " (^.{-1,}) Capture group: Anchored at the start of the string, match any character one-or-more times non-greedy.
- " (:)@= Positive lookahead: Match ':'.
- let l:font_name_pattern = '\v(^.{-1,})(:)@='
- " \v Very magical.
- " (:h)@<= Positive lookbehind: Match ':h'.
- " (\d+) Capture group: Match [0-9] one-or-more times.
- let l:font_size_pattern = '\v(:h)@<=(\d+)'
- endif
- let l:font_name = matchstr(&guifont, l:font_name_pattern)
- let l:font_size = matchstr(&guifont, l:font_size_pattern)
- return { 'name' : l:font_name, 'size' : l:font_size }
- endfunction
- " Bind Control + Mouse-wheel to zoom text.
- " NOTE: Starting in version 9.0 this works in Windows. Previously it only worked in Linux and macOS. SEE: :h scroll-mouse-wheel
- map <silent> <C-ScrollWheelDown> :call AdjustFontSize('-')<CR>
- map <silent> <C-ScrollWheelUp> :call AdjustFontSize('+')<CR>
- " Decrease font size.
- nnoremap <silent> <F11> :call AdjustFontSize('-')<CR>
- inoremap <silent> <F11> <Esc>:call AdjustFontSize('-')<CR>
- vnoremap <silent> <F11> <Esc>:call AdjustFontSize('-')<CR>
- cnoremap <silent> <F11> <Esc>:call AdjustFontSize('-')<CR>
- onoremap <silent> <F11> <Esc>:call AdjustFontSize('-')<CR>
- " Increase font size.
- nnoremap <silent> <F12> :call AdjustFontSize('+')<CR>
- inoremap <silent> <F12> <Esc>:call AdjustFontSize('+')<CR>
- vnoremap <silent> <F12> <Esc>:call AdjustFontSize('+')<CR>
- cnoremap <silent> <F12> <Esc>:call AdjustFontSize('+')<CR>
- onoremap <silent> <F12> <Esc>:call AdjustFontSize('+')<CR>
- " Set font size to my preferred size (10).
- nnoremap <silent> <S-F11> :call AdjustFontSize(10)<CR>
- inoremap <silent> <S-F11> <Esc>:call AdjustFontSize(10)<CR>
- vnoremap <silent> <S-F11> <Esc>:call AdjustFontSize(10)<CR>
- cnoremap <silent> <S-F11> <Esc>:call AdjustFontSize(10)<CR>
- onoremap <silent> <S-F11> <Esc>:call AdjustFontSize(10)<CR>
- " TODO: Sadly cannot bind Ctrl + 0 yet:
- " It would allow me to get rid of F11 F12 binding completely,
- " so just C-ScrollWheel and C-0 would be needed, and maybe Shift-0 to open font selector.
- " https://vi.stackexchange.com/questions/19358/cannot-map-ctrl-number-except-6-or
- " https://vimhelp.org/vim_faq.txt.html#faq-20.5
- "
- " Set font size to my preferred size (10).
- " nnoremap <silent> <C-0> :call AdjustFontSize(10)<CR>
- " inoremap <silent> <C-0> <Esc>:call AdjustFontSize(10)<CR>
- " vnoremap <silent> <C-0> <Esc>:call AdjustFontSize(10)<CR>
- " cnoremap <silent> <C-0> <Esc>:call AdjustFontSize(10)<CR>
- " onoremap <silent> <C-0> <Esc>:call AdjustFontSize(10)<CR>
- " Display font selector.
- nnoremap <silent> <S-F12> :call DisplayFontSelector()<CR>
- inoremap <silent> <S-F12> <Esc>:call DisplayFontSelector()<CR>
- vnoremap <silent> <S-F12> <Esc>:call DisplayFontSelector()<CR>
- cnoremap <silent> <S-F12> <Esc>:call DisplayFontSelector()<CR>
- onoremap <silent> <S-F12> <Esc>:call DisplayFontSelector()<CR>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement