Skip to content

Commit d51681b

Browse files
committed
Improvements to shell.c (contributed by Guo Yu)
- Make the full screen window top most (by Guo Yu) - Make it compile without warnings (Guo Yu added const everywhere, I removed assignments in conditional expressions) - Replace make file with batch script that automatically builds both the shell-x86.dll and shell-x64.dll binaries (by me)
1 parent 942e75a commit d51681b

File tree

8 files changed

+61
-44
lines changed

8 files changed

+61
-44
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Vim has a limited ability to call external libraries using the Vim script functi
9595

9696
Since then I switched to Linux and didn't look back, which meant the DLL sat in my `~/.vim/etc/` waiting to be revived. Now that I've published my [easytags.vim][easytags] plug-in and put a lot of effort into making it Windows compatible, the `execute()` function from the DLL would be very useful to run [Exuberant Ctags][ctags] in the background without stealing Vim's focus by opening a command prompt window. This is why I've decided to release the DLL. Because I switched to Linux I've also added an autoload script that wraps the DLL on Windows and calls out to external programs such as `wmctrl`, `gnome-open`, `kde-open`, and others on UNIX.
9797

98-
Before I go ahead and bundle the DLL files with my [easytags.vim][easytags] plug-in I need to make sure they're compatible with as many Windows Vim installations out there as possible, e.g. XP/Vista/7, different service packs, 32/64 bits, etc. I've uploaded a [ZIP archive including two compiled DLL files][download] to the [Vim scripts page][vim_scripts_entry] for this plug-in (build using the latest Windows SDK but targeting Windows XP x86/x64 DEBUG, should also work on Vista/7) and the source code is available in the [GitHub repository](http://github.com/xolox/vim-shell) (see the `NMAKE` [makefile](http://github.com/xolox/vim-shell/blob/master/dll/Makefile) for compile instructions).
98+
Before I go ahead and bundle the DLL files with my [easytags.vim][easytags] plug-in I need to make sure they're compatible with as many Windows Vim installations out there as possible, e.g. XP/Vista/7, different service packs, 32/64 bits, etc. I've uploaded a [ZIP archive including two compiled DLL files][download] to the [Vim scripts page][vim_scripts_entry] for this plug-in (build using the latest Windows SDK but targeting Windows XP x86/x64 RELEASE, should also work on Vista/7) and the source code is available in the [GitHub repository](http://github.com/xolox/vim-shell) (there's also a [Batch script](http://github.com/xolox/vim-shell/blob/master/dll/Makefile) with compile instructions).
9999

100100
## Other full-screen implementations
101101

autoload/xolox/shell.vim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
" Vim auto-load script
22
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: October 29, 2011
3+
" Last Change: November 11, 2011
44
" URL: http://peterodding.com/code/vim/shell/
55

6-
let g:xolox#shell#version = '0.9.20'
6+
let g:xolox#shell#version = '0.9.21'
77

88
if !exists('s:fullscreen_enabled')
99
let s:enoimpl = "%s() hasn't been implemented on your platform! %s"
@@ -340,7 +340,7 @@ if xolox#misc#os#is_win()
340340

341341
function! s:has_dll() " {{{2
342342
try
343-
return s:library_call('libversion', '') == '0.2'
343+
return s:library_call('libversion', '') == '0.3'
344344
catch
345345
return 0
346346
endtry

doc/shell.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ need to make sure they're compatible with as many Windows Vim installations
201201
out there as possible, e.g. XP/Vista/7, different service packs, 32/64 bits,
202202
etc. I've uploaded a ZIP archive including two compiled DLL files [8] to the
203203
Vim scripts page [9] for this plug-in (build using the latest Windows SDK but
204-
targeting Windows XP x86/x64 DEBUG, should also work on Vista/7) and the
205-
source code is available in the GitHub repository [10] (see the
206-
'NMAKE'makefile [11] for compile instructions).
204+
targeting Windows XP x86/x64 RELEASE, should also work on Vista/7) and the
205+
source code is available in the GitHub repository [10] (there's also a Batch
206+
script [11] with compile instructions).
207207

208208
===============================================================================
209209
*shell-other-full-screen-implementations*

misc/shell/Makefile

Lines changed: 0 additions & 12 deletions
This file was deleted.

misc/shell/make.cmd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
:: This Windows batch script compiles the Windows DLL for the
2+
:: shell.vim plug-in for the x86 and x64 processor architectures.
3+
4+
:: Build shell-x86.dll.
5+
CALL SETENV /Release /x86 /xp
6+
CL /nologo /Wall /LD shell.c /link /out:shell-x86.dll shell32.lib user32.lib
7+
DEL shell.exp shell.lib shell.obj
8+
9+
:: Build shell-x64.dll.
10+
CALL SETENV /Release /x64 /xp
11+
CL /nologo /Wall /LD shell.c /link /out:shell-x64.dll shell32.lib user32.lib
12+
DEL shell.exp shell.lib shell.obj

misc/shell/shell-x64.dll

512 Bytes
Binary file not shown.

misc/shell/shell-x86.dll

0 Bytes
Binary file not shown.

misc/shell/shell.c

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
*/
2727

2828
#define _WIN32_WINNT 0x0500 /* GetConsoleWindow() */
29-
#define WIN32_LEAN_AND_MEAN /* but keep it simple */
29+
#define WIN32_LEAN_AND_MEAN
3030
#include <windows.h>
31+
#include <ctype.h>
3132
#include <shellapi.h> /* ShellExecute? */
3233

3334
/* Dynamic strings are returned using a static buffer to avoid memory leaks */
@@ -36,9 +37,9 @@ static char buffer[1024 * 10];
3637
#undef MessageBox
3738
#define MessageBox(message) MessageBoxA(NULL, message, "Vim Library", 0)
3839

39-
static char *GetError(void) /* {{{1 */
40+
static const char *GetError(void) /* {{{1 */
4041
{
41-
int i;
42+
size_t i;
4243

4344
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
4445
NULL, GetLastError(), 0, buffer, sizeof buffer, NULL);
@@ -50,19 +51,19 @@ static char *GetError(void) /* {{{1 */
5051
return buffer;
5152
}
5253

53-
static char *Success(char *result) /* {{{1 */
54+
static const char *Success(const char *result) /* {{{1 */
5455
{
5556
/* printf("OK\n"); */
5657
return result;
5758
}
5859

59-
static char *Failure(char *result) /* {{{1 */
60+
static const char *Failure(const char *result) /* {{{1 */
6061
{
6162
/* if (result && strlen(result)) MessageBox(result); */
6263
return result;
6364
}
6465

65-
static char *execute(char *command, int wait) /* {{{1 */
66+
static const char *execute(char *command, int wait) /* {{{1 */
6667
{
6768
STARTUPINFO si;
6869
PROCESS_INFORMATION pi;
@@ -84,81 +85,96 @@ static char *execute(char *command, int wait) /* {{{1 */
8485
}
8586

8687
__declspec(dllexport)
87-
char *execute_synchronous(char *command) /* {{{1 */
88+
const char *execute_synchronous(char *command) /* {{{1 */
8889
{
8990
return execute(command, 1);
9091
}
9192

9293
__declspec(dllexport)
93-
char *execute_asynchronous(char *command) /* {{{1 */
94+
const char *execute_asynchronous(char *command) /* {{{1 */
9495
{
9596
return execute(command, 0);
9697
}
9798

9899
__declspec(dllexport)
99-
char *libversion(char *ignored) /* {{{1 */
100+
const char *libversion(const char *ignored) /* {{{1 */
100101
{
101102
(void)ignored;
102-
return Success("0.2");
103+
return Success("0.3");
103104
}
104105

105106
__declspec(dllexport)
106-
char *openurl(char *path) /* {{{1 */
107+
const char *openurl(const char *path) /* {{{1 */
107108
{
108109
ShellExecute(NULL, "open", path, NULL, NULL, SW_SHOWNORMAL);
109110
return Success(NULL);
110111
}
111112

112113
__declspec(dllexport)
113-
char *fullscreen(long enable) /* {{{1 */
114+
const char *fullscreen(long enable) /* {{{1 */
114115
{
115116
HWND window;
116117
LONG styles;
118+
LONG exStyle;
117119
HMONITOR monitor;
118120
MONITORINFO info = { sizeof info };
119121

120-
if (!(window = GetForegroundWindow()))
122+
window = GetForegroundWindow();
123+
if (!window)
121124
return Failure("Could not get handle to foreground window!");
122125

123-
if (!(styles = GetWindowLong(window, GWL_STYLE)))
126+
styles = GetWindowLong(window, GWL_STYLE);
127+
if (!styles)
124128
return Failure("Could not query window styles!");
125129

126-
if (enable) styles ^= WS_CAPTION | WS_THICKFRAME;
127-
else styles |= WS_CAPTION | WS_THICKFRAME;
130+
exStyle = GetWindowLong(window, GWL_EXSTYLE);
131+
if (!exStyle)
132+
return Failure("Could not query window ex style!");
133+
134+
if (enable) {
135+
styles ^= WS_CAPTION | WS_THICKFRAME;
136+
exStyle |= WS_EX_TOPMOST;
137+
} else {
138+
styles |= WS_CAPTION | WS_THICKFRAME;
139+
exStyle &= ~WS_EX_TOPMOST;
140+
}
128141

129142
if (!SetWindowLong(window, GWL_STYLE, styles))
130143
return Failure("Could not apply window styles!");
131144

145+
if (!SetWindowLong(window, GWL_EXSTYLE, exStyle))
146+
return Failure("Could not apply window ex style!");
147+
132148
if (enable) {
133-
if (!(monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST)))
149+
monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST);
150+
if (!monitor)
134151
return Failure("Could not get handle to monitor!");
135152
if (!GetMonitorInfo(monitor, &info))
136153
return Failure("Could not get monitor information!");
137-
if (!SetWindowPos(window, HWND_TOP,
154+
if (!SetWindowPos(window, HWND_TOPMOST,
138155
info.rcMonitor.left,
139156
info.rcMonitor.top,
140157
info.rcMonitor.right - info.rcMonitor.left,
141158
info.rcMonitor.bottom - info.rcMonitor.top,
142159
SWP_SHOWWINDOW))
143160
return Failure("Could not resize window!");
144-
} else if (!SetWindowPos(window, HWND_TOP, 0, 0, 0, 0,
145-
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED))
161+
} else if (!SetWindowPos(window, HWND_NOTOPMOST, 0, 0, 0, 0,
162+
SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED))
146163
return Failure("Could not restore window!");
147164

148165
return Success(NULL);
149166
}
150167

151168
__declspec(dllexport)
152-
char *console(char *command) /* {{{1 */
169+
const char *console(char *command) /* {{{1 */
153170
{
154171
/* TODO: The quest to embedding a command prompt in Vim :)
155172
* This doesn't quite work and I'm afraid it never will.
156173
*/
157-
158174
HWND gvim, console;
159-
LONG styles;
160175

161-
if (!(gvim = GetForegroundWindow()))
176+
gvim = GetForegroundWindow();
177+
if (!gvim)
162178
return Failure("Could not get handle to foreground window");
163179

164180
// destroy old console?
@@ -169,7 +185,8 @@ char *console(char *command) /* {{{1 */
169185
return Failure("Could not allocate console");
170186

171187
// get handle to console window
172-
if (!(console = GetConsoleWindow()))
188+
console = GetConsoleWindow();
189+
if (!console)
173190
return Failure("Could not get handle to console window");
174191

175192
// embed console inside foreground window

0 commit comments

Comments
 (0)