diff --git a/README.md b/README.md index 425d6ac..2763617 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ This plugin for gvim adds the ability to change font size (and therefore window size too) using the same methods used in most web browsers, terminal emulators and some other graphical text editors. +In console Vim or Neovim the plugin exits and doesn’t do anything. + I may add Mac support if this plugin attracts any interest. ## Usage @@ -15,28 +17,44 @@ I may add Mac support if this plugin attracts any interest. |Ctrl + =| Increases text size | |Ctrl + -| Decreases text size | |Ctrl + 0| Resets text to original size | -|Ctrl + mousewheel| Resize text | +|Ctrl + mousewheel | Increases/Decreases text size | + +If you already have any of these keys mapped, your mappings will not be overridden. +There are also a few limitations when using older Windows gvim versions. +First, before v9.0 patch 1112 Ctrl + - will not work. +Instead, Ctrl + =, = has been made the default. +Second, the mousewheel mappings will not work if you’re using a version before v8.2 patch 5069. + +If you don’t like these mappings, you can map your own in your .vimrc with: + +```vim +nnoremap {LHS} CfsPlus +nnoremap {LHS} CfsMinus +nnoremap {LHS} CfsOriginal +``` -In terminal vim or neovim the plugin exits and doesn't do anything. +...where `{LHS}` is the key combination you want. ## Install ### Plugin manager + Installation can be done via a plugin manager. -For [vim-plug](https://github.com/junegunn/vim-plug) it's: +For [vim-plug](https://github.com/junegunn/vim-plug) it’s: ```vim Plug 'eggbean/resize-font.gvim' ``` + ### No plugin manager -If you don't use a plugin manager you can just place [the script](https://github.com/eggbean/resize-font.gvim/blob/master/plugin/change_font_size.vim) in... +If you don’t use a plugin manager you can just place [the scripts](https://github.com/eggbean/resize-font.gvim/blob/master/plugin/) `change_font_*.vim` in… -`~/.vim/plugin` on Linux, or... +`~/.vim/plugin` on Linux, or… `%USERPROFILE%\vimfiles\plugin` on Windows. ## Not working with your font? -This should work with all modern Unicode fonts, but if you find one which doesn't work tell me and I'll modify the regex. This is only likely to occur with Windows. +This should work with all modern Unicode fonts but if you find one that doesn’t work tell me and I’ll modify the regex. This is only likely to occur with Windows. diff --git a/plugin/change_font_size.vim b/plugin/change_font_size.vim index 49eed43..96bc9bc 100644 --- a/plugin/change_font_size.vim +++ b/plugin/change_font_size.vim @@ -3,12 +3,17 @@ " Author: Jason Gomez " Credit: Originally based on https://vi.stackexchange.com/a/3104/37532 -if !has('gui_running') || exists('g:loaded_change_font_size') +" Finish when any of the following apply: +" * Version 9 or 8.2 with patch 4807? Use the vim9script version. +" * No GUI running or Neovim? So, N/A. +" * Plugin already loaded? Don't it load again! +if v:version >= 900 || (v:version == 802 && has('patch4807')) || !has('gui_running') || has('nvim') || exists('g:loaded_change_font_size') finish endif -let g:loaded_change_font_size= 1 +let g:loaded_change_font_size = "vimscript" " For testing which version -if has('gui_gtk2') || has('gui_gtk3') +if has('gui_gtk') + " gui_gtk includes gui_gtk2 and gui_gtk3 - refer :h has-patch autocmd GUIEnter * let g:gf_size_orig = matchstr(&guifont, '\( \)\@<=\d\+$') function! FontSizePlus() let l:gf_size_whole = matchstr(&guifont, '\( \)\@<=\d\+$') @@ -64,8 +69,39 @@ elseif has('gui_win32') endfunction endif -nmap :call FontSizePlus() -nmap  :call FontSizeMinus() -nmap :call FontSizeOriginal() -map :call FontSizePlus() -map :call FontSizeMinus() +" Mappings. Only when one of the supported GUIs is running: +" - Create mappings, and +" - Apply mappings (but only when not already specified - 'Do no harm'). +" NB: 1. In Windows, the mappings will not work with versions +" before 8.2 with patch 5069 (they do nothing). Refer: +" https://github.com/vim/vim/commit/ebb01bdb273216607f60faddf791a1b378cccfa8 +" 2. In Windows, mappings will not work before 8.2 with +" patch 4807, which has been set as the cut-off for using the +" vim9script version. Therefore, different keys are needed in +" Windows - that's not been done here *yet* (testing of to +" in Windows was _somewhat_ functional, though seemed to +" stop/start working for some reason). +" So, the and mappings have been left as per Jason's +" initial setup, notwithstanding none will work in Windows and whether they +" work in *gtk* for 8.2 before patch 4807 ... ??? +if has('gui_gtk') || has('gui_win32') + + map CfsPlus execute "call FontSizePlus()" + " and/or to FontSizePlus() when not already mapped + execute maparg('', 'n') == '' ? ':nnoremap CfsPlus' : '' + execute maparg('', '') == '' ? ':noremap CfsPlus' : '' + + map CfsMinus execute "call FontSizeMinus()" + " U+001F (, i.e., ) and/or to FontSizeMinus() when not already mapped + " Specifically, before v9.0 patch 1112 will not work on Windows + " Refer: https://github.com/vim/vim/commit/7b0afc1d7698a79423c7b066a5d8d20dbb8a295a + " That's academic because before 8.2 patch 4807 no CfsMinus' : '' + execute maparg('', '') == '' ? ':noremap CfsMinus' : '' + + map CfsOriginal execute "call FontSizeOriginal()" + " to FontSizeOriginal() only when not already mapped + execute maparg('', '') == '' ? ':nnoremap CfsOriginal' : '' + +endif diff --git a/plugin/change_font_size_vim9script.vim b/plugin/change_font_size_vim9script.vim new file mode 100644 index 0000000..ebf2a96 --- /dev/null +++ b/plugin/change_font_size_vim9script.vim @@ -0,0 +1,119 @@ +" +" Δ FFFF OOO NNN TTT SSS III ZZZZ EEEE +" Δ Δ F O O N N T S I ZZ E +" Δ Δ FFFF O O N N T SSS I ZZ EEE +" Δ Δ F O O N N T S I ZZ E +" ΔΔΔΔΔΔΔΔΔ F OOO N N T SSS III ZZZZ EEEE +" +" File: change_font_size_vim9script.vim +" Description: Change gvim font size using keyboard or mousewheel +" Author: Peter Kenny +" Credit: Based on Jason Gomez's github.com/eggbean/resize-font.gvim, +" (itself citing vi.stackexchange.com/a/3104/37532) +" +" vim9-mix for running vim9script. +" Finish when any of the following apply: +" * Version < 8.2 (inclusive of any version of Neovim, which returns 801) +" * Version 8.2 without patch 4807 (Win32 64-bit gvim at/above that works) +" * Console version, so no GUI is running, or +" * The plugin has already been loaded. +if (v:version < 802 || (v:version == 802 && !has('patch4807')) || !has('gui_running') || exists('g:loaded_change_font_size')) + finish +endif + +vim9script + +g:loaded_change_font_size = "vim9script" + +# initialisation of variables +var gf_size_whole: number +var new_font_size: string +var gf_weight: string +var gf_bold: string +var gf_cansi: string +var gf_qdraft: string + +# gui_gtk2 and gui_gtk3 - refer :h has-patch (gui_gtk does addresses both) +if has('gui_gtk') + autocmd GUIEnter * g:gf_size_orig = matchstr(&guifont, '\( \)\@<=\d\+$') + def g:FontSizePlus(): void + gf_size_whole = str2nr(matchstr(&guifont, '\( \)\@<=\d\+$')) + if gf_size_whole < 28 + gf_size_whole += 1 + new_font_size = ' ' .. gf_size_whole + &guifont = substitute(&guifont, ' \d\+$', new_font_size, '') + endif + enddef + def g:FontSizeMinus(): void + gf_size_whole = str2nr(matchstr(&guifont, '\( \)\@<=\d\+$')) + if gf_size_whole > 1 + gf_size_whole -= 1 + new_font_size = ' ' .. gf_size_whole + &guifont = substitute(&guifont, ' \d\+$', new_font_size, '') + endif + enddef + def g:FontSizeOriginal(): void + &guifont = substitute(&guifont, ' \d\+$', ' ' .. g:gf_size_orig, '') + enddef +elseif has('gui_win32') + autocmd GUIEnter * g:gf_size_orig = matchstr(&guifont, '\(:h\)\@<=\d\+\(:W\d\+\)\?\(:b\)\?\(:cANSI\)\?\(:qDRAFT\)\?$') + def g:FontSizePlus(): void + gf_size_whole = str2nr(matchstr(&guifont, '\(:h\)\@<=\d\+\(:W\d\+\)\?\(:b\)\?\(:cANSI\)\?\(:qDRAFT\)\?$')) + gf_weight = matchstr(&guifont, '\(:W\d\+\)\?$') + gf_bold = matchstr(&guifont, '\(:b\)\?$') + gf_cansi = matchstr(&guifont, '\(:cANSI\)\?$') + gf_qdraft = matchstr(&guifont, '\(:qDRAFT\)\?$') + if gf_size_whole < 28 + gf_size_whole += 1 + new_font_size = ':h' .. gf_size_whole + &guifont = substitute(&guifont, '\(:h\d\+\)\(:W\d\+\)\?\(:b\)\?\(:cANSI\)\?\(:qDRAFT\)\?$', new_font_size .. gf_weight .. gf_bold .. gf_cansi .. gf_qdraft, '') + endif + enddef + def g:FontSizeMinus(): void + gf_size_whole = str2nr(matchstr(&guifont, '\(:h\)\@<=\d\+\(:W\d\+\)\?\(:b\)\?\(:cANSI\)\?\(:qDRAFT\)\?$')) + gf_weight = matchstr(&guifont, '\(:W\d\+\)\?$') + gf_bold = matchstr(&guifont, '\(:b\)\?$') + gf_cansi = matchstr(&guifont, '\(:cANSI\)\?$') + gf_qdraft = matchstr(&guifont, '\(:qDRAFT\)\?$') + if gf_size_whole > 1 + gf_size_whole -= 1 + new_font_size = ':h' .. gf_size_whole + &guifont = substitute(&guifont, '\(:h\d\+\)\(:W\d\+\)\?\(:b\)\?\(:cANSI\)\?\(:qDRAFT\)\?$', new_font_size .. gf_weight .. gf_bold .. gf_cansi .. gf_qdraft, '') + endif + enddef + def g:FontSizeOriginal(): void + gf_weight = matchstr(&guifont, '\(:W\d\+\)\?$') + gf_bold = matchstr(&guifont, '\(:b\)\?$') + gf_cansi = matchstr(&guifont, '\(:cANSI\)\?$') + gf_qdraft = matchstr(&guifont, '\(:qDRAFT\)\?$') + &guifont = substitute(&guifont, '\(:h\d\+\)\(:W\d\+\)\?\(:b\)\?\(:cANSI\)\?\(:qDRAFT\)\?$', ':h' .. g:gf_size_orig .. gf_weight .. gf_bold .. gf_cansi .. gf_qdraft, '') + enddef +endif + +# Mappings. Only when one of the supported GUIs is running: +# - Create mappings, and +# - Apply them only when they're not already specified ('Do no harm'!). +if has('gui_gtk') || has('gui_win32') # has('gui_running') is too permissive + + map CfsPlus execute "call g:FontSizePlus()" + # and/or to FontSizePlus() when not already mapped + execute maparg('', 'n') == '' ? ':nnoremap CfsPlus' : '' + execute maparg('', '') == '' ? ':noremap CfsPlus' : '' + + map CfsMinus execute "call g:FontSizeMinus()" + # U+001F ( i.e., ) and/or to FontSizeMinus() when not already mapped + # NB: Before v9.0 patch 1112 will not work on Windows - refer: + # https://github.com/vim/vim/commit/7b0afc1d7698a79423c7b066a5d8d20dbb8a295a + if !has('win32') || (has('win32') && v:versionlong >= 9001112) + execute maparg('', 'n') == '' ? ':nnoremap  CfsMinus' : '' + else + # is unavailable, as noted above, so use = instead + execute maparg('=', 'n') == '' ? ':nnoremap = CfsMinus' : '' + endif + execute maparg('', '') == '' ? ':noremap CfsMinus' : '' + + map CfsOriginal execute "call g:FontSizeOriginal()" + # to FontSizeOriginal() when not already mapped + execute maparg('', '') == '' ? ':nnoremap CfsOriginal' : '' + +endif diff --git a/resizing_gvim_WSL.gif b/resizing_gvim_WSL.gif new file mode 100644 index 0000000..53ee89a Binary files /dev/null and b/resizing_gvim_WSL.gif differ