Port of the maximize plugin for GVim by kAtremer (2007) in D. Interested?
Tim Moldazhan
tim.moldazhan at gmail.com
Sun Aug 4 08:12:58 UTC 2024
**dub.json:**
```json
{
"name": "maximize",
"targetType": "dynamicLibrary",
"sourceFiles": ["source/maximize.d"],
"dependencies": {},
"libs":["user32","kernel32"]
}
```
**maximize.d:**
```d
module maximize;
import core.sys.windows.windows;
import core.sys.windows.windef;
import core.sys.windows.winnt;
import core.stdc.string;
import std.string;
extern (Windows) BOOL DllMain( HMODULE hModule, DWORD
ul_reason_for_call,LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
default:break;
}
return TRUE;
}
extern(C) int strcmp(const(char)* s1, const(char)* s2);
extern (Windows) BOOL EnumThreadWndProc(HWND hwnd, LPARAM lParam)
{
HWND* pVimWin = cast(HWND*) lParam;
char[256] className;
GetClassNameA(hwnd, cast(LPSTR)className.ptr,
cast(int)className.length);
if (strcmp(className.toStringz(), "Vim") == 0 ||
strcmp(className.toStringz, "GVim") == 0)
{
*pVimWin = hwnd;
OutputDebugString("Found Vim/GVim window\n");
return FALSE; // Stop enumeration
}
return TRUE; // Continue enumeration
}
extern (C) export LONG Maximize(LONG param) {
HWND vimwin = null;
DWORD threadid = GetCurrentThreadId();
OutputDebugString("Starting EnumThreadWindows\n");
// Enumerate all windows in the current thread
EnumThreadWindows(threadid,
cast(WNDENUMPROC)&EnumThreadWndProc, cast(LPARAM)&vimwin);
if (vimwin != null) {
OutputDebugString("Vim/GVim window found\n");
if (param == 1) {
ShowWindow(vimwin, SW_MAXIMIZE); // Maximize window
}
else if (param == 0) {
ShowWindow(vimwin, SW_RESTORE); // Restore window
}
return 1; // Success
} else {
OutputDebugString("Vim/GVim window not found\n");
return 0; // Failure: Vim window not found
}
}
```
**maximize.vim:**
```vim
" Vim plugin file
" Maintainer: kAtremer <katremer at yandex.ru>
" Last changed: 2007 Oct 16
"
" maximize.vim
" maximize gVim's window on startup on Win32
"
" to install, put the script and maximize.dll
" in $VIM\vimfiles\plugin
" Execute only once {{{
if exists("g:loaded_maximize")
finish
endif
let g:loaded_maximize = 1
" }}}
" Set the default compatibility options {{{
" (don't know if they do any difference, in such a small
script...)
let s:save_cpoptions = &cpoptions
set cpoptions&vim
" }}}
" Define the path to the DLL file
let s:dllfile = expand('<sfile>:p:h') . './maximize.dll'
" Maximize GVim window on startup
autocmd GUIEnter * call libcallnr(s:dllfile, 'Maximize',
g:loaded_maximize)
" ToggleVimResize function
function! ToggleVimResize()
" Checking the current value of g:loaded_maximize
if g:loaded_maximize == 1
" If the window is maximized, change the value to 0
(reduce the window)
let g:loaded_maximize = 0
else
" If the window is not maximized, change the value to 1
(maximize the window)
let g:loaded_maximize = 1
endif
" Call the Maximize function with the new value
g:loaded_maximize
call libcallnr(s:dllfile, 'Maximize', g:loaded_maximize)
endfunction
" Restore the saved compatibility options {{{
let &cpoptions = s:save_cpoptions
" }}}
" vim:fdm=marker:fmr={{{,}}}
" Binding the ToggleVimResize() function to the F11 key in
various modes
nnoremap <F11> :call ToggleVimResize()<CR>
inoremap <F11> <C-o>:call ToggleVimResize()<CR>
vnoremap <F11> :<C-u>call ToggleVimResize()<CR>
```
More information about the Digitalmars-d
mailing list