blob: 0a7f346dbc6d16076e6bc84b81caaa322e83ae6e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI/Motif support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10
11#include "vim.h"
12
Bram Moolenaar30613902019-12-01 22:11:18 +010013// Structure containing all the GUI information
Bram Moolenaar071d4272004-06-13 20:20:40 +000014gui_T gui;
15
Bram Moolenaar13505972019-01-24 15:04:48 +010016#if !defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010017static void set_guifontwide(char_u *font_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010019static void gui_check_pos(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020020static void gui_reset_scroll_region(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010021static void gui_outstr(char_u *, int);
22static int gui_screenchar(int off, int flags, guicolor_T fg, guicolor_T bg, int back);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020023static int gui_outstr_nowrap(char_u *s, int len, int flags, guicolor_T fg, guicolor_T bg, int back);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010024static void gui_delete_lines(int row, int count);
25static void gui_insert_lines(int row, int count);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020026static int gui_xy2colrow(int x, int y, int *colp);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000027#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010028static int gui_has_tabline(void);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000029#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010030static void gui_do_scrollbar(win_T *wp, int which, int enable);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010031static void gui_update_horiz_scrollbar(int);
32static void gui_set_fg_color(char_u *name);
33static void gui_set_bg_color(char_u *name);
Bram Moolenaar0630bb62019-11-04 22:52:12 +010034static win_T *xy2win(int x, int y, mouse_find_T popup);
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020036#ifdef GUI_MAY_FORK
Bram Moolenaard25c16e2016-01-29 22:13:30 +010037static void gui_do_fork(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020038
Bram Moolenaard25c16e2016-01-29 22:13:30 +010039static int gui_read_child_pipe(int fd);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020040
Bram Moolenaar30613902019-12-01 22:11:18 +010041// Return values for gui_read_child_pipe
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020042enum {
43 GUI_CHILD_IO_ERROR,
44 GUI_CHILD_OK,
45 GUI_CHILD_FAILED
46};
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020047#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020048
Bram Moolenaard25c16e2016-01-29 22:13:30 +010049static void gui_attempt_start(void);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020050
Bram Moolenaar30613902019-12-01 22:11:18 +010051static int can_update_cursor = TRUE; // can display the cursor
52static int disable_flush = 0; // If > 0, gui_mch_flush() is disabled.
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
54/*
55 * The Athena scrollbars can move the thumb to after the end of the scrollbar,
56 * this makes the thumb indicate the part of the text that is shown. Motif
57 * can't do this.
58 */
59#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MAC)
60# define SCROLL_PAST_END
61#endif
62
63/*
64 * gui_start -- Called when user wants to start the GUI.
65 *
66 * Careful: This function can be called recursively when there is a ":gui"
67 * command in the .gvimrc file. Only the first call should fork, not the
68 * recursive call.
69 */
70 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +020071gui_start(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000072{
73 char_u *old_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 static int recursive = 0;
Bram Moolenaar68cbb142019-05-09 14:14:42 +020075#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +020076 char *msg = NULL;
77#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
79 old_term = vim_strsave(T_NAME);
80
Bram Moolenaar30613902019-12-01 22:11:18 +010081 settmode(TMODE_COOK); // stop RAW mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 if (full_screen)
Bram Moolenaar30613902019-12-01 22:11:18 +010083 cursor_on(); // needed for ":gui" in .vimrc
Bram Moolenaar071d4272004-06-13 20:20:40 +000084 full_screen = FALSE;
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 ++recursive;
87
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020088#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020089 /*
90 * Quit the current process and continue in the child.
91 * Makes "gvim file" disconnect from the shell it was started in.
92 * Don't do this when Vim was started with "-f" or the 'f' flag is present
93 * in 'guioptions'.
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020094 * Don't do this when there is a running job, we can only get the status
95 * of a child from the parent.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +020096 */
Bram Moolenaarb0b98d52018-05-05 21:01:00 +020097 if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1
98# ifdef FEAT_JOB_CHANNEL
99 && !job_any_running()
100# endif
101 )
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200102 {
103 gui_do_fork();
104 }
105 else
106#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200107#ifdef GUI_MAY_SPAWN
108 if (gui.dospawn
109# ifdef EXPERIMENTAL_GUI_CMD
110 && gui.dofork
111# endif
112 && !vim_strchr(p_go, GO_FORG)
113 && !anyBufIsChanged()
114# ifdef FEAT_JOB_CHANNEL
115 && !job_any_running()
116# endif
117 )
118 {
Bram Moolenaar68cbb142019-05-09 14:14:42 +0200119# ifdef EXPERIMENTAL_GUI_CMD
120 msg =
121# endif
122 gui_mch_do_spawn(arg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200123 }
124 else
125#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200126 {
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200127#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100128 // If there is 'f' in 'guioptions' and specify -g argument,
129 // gui_mch_init_check() was not called yet.
Bram Moolenaar6a3c1b42012-05-25 14:06:36 +0200130 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100131 getout_preserve_modified(1);
Bram Moolenaarae084bb2012-05-27 00:37:51 +0200132#endif
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200133 gui_attempt_start();
134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
Bram Moolenaar30613902019-12-01 22:11:18 +0100136 if (!gui.in_use) // failed to start GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100138 // Back to old term settings
139 //
140 // FIXME: If we got here because a child process failed and flagged to
141 // the parent to resume, and X11 is enabled with FEAT_TITLE, this will
142 // hit an X11 I/O error and do a longjmp(), leaving recursive
143 // permanently set to 1. This is probably not as big a problem as it
144 // sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
145 // return "OK" unconditionally, so it would be very difficult to
146 // actually hit this case.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200147 termcapinit(old_term);
Bram Moolenaar30613902019-12-01 22:11:18 +0100148 settmode(TMODE_RAW); // restart RAW mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#ifdef FEAT_TITLE
Bram Moolenaar30613902019-12-01 22:11:18 +0100150 set_title_defaults(); // set 'title' and 'icon' again
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200152#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
153 if (msg)
154 emsg(msg);
155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 }
157
158 vim_free(old_term);
159
Bram Moolenaar30613902019-12-01 22:11:18 +0100160 // If the GUI started successfully, trigger the GUIEnter event, otherwise
161 // the GUIFailed event.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200162 gui_mch_update();
163 apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
164 NULL, NULL, FALSE, curbuf);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200165 --recursive;
166}
167
168/*
169 * Set_termname() will call gui_init() to start the GUI.
170 * Set the "starting" flag, to indicate that the GUI will start.
171 *
172 * We don't want to open the GUI shell until after we've read .gvimrc,
173 * otherwise we don't know what font we will use, and hence we don't know
174 * what size the shell should be. So if there are errors in the .gvimrc
175 * file, they will have to go to the terminal: Set full_screen to FALSE.
176 * full_screen will be set to TRUE again by a successful termcapinit().
177 */
178 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100179gui_attempt_start(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200180{
181 static int recursive = 0;
182
183 ++recursive;
184 gui.starting = TRUE;
185
186#ifdef FEAT_GUI_GTK
187 gui.event_time = GDK_CURRENT_TIME;
188#endif
189
190 termcapinit((char_u *)"builtin_gui");
191 gui.starting = recursive - 1;
192
Bram Moolenaar9372a112005-12-06 19:59:18 +0000193#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 if (gui.in_use)
Bram Moolenaar727c8762010-10-20 19:17:48 +0200195 {
196# ifdef FEAT_EVAL
197 Window x11_window;
198 Display *x11_display;
199
200 if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
201 set_vim_var_nr(VV_WINDOWID, (long)x11_window);
202# endif
203
Bram Moolenaar30613902019-12-01 22:11:18 +0100204 // Display error messages in a dialog now.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 display_errors();
Bram Moolenaar727c8762010-10-20 19:17:48 +0200206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 --recursive;
209}
210
Bram Moolenaarb0b98d52018-05-05 21:01:00 +0200211#ifdef GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200212
Bram Moolenaar30613902019-12-01 22:11:18 +0100213// for waitpid()
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200214# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
215# include <sys/wait.h>
216# endif
217
218/*
219 * Create a new process, by forking. In the child, start the GUI, and in
220 * the parent, exit.
221 *
222 * If something goes wrong, this will return with gui.in_use still set
223 * to FALSE, in which case the caller should continue execution without
224 * the GUI.
225 *
226 * If the child fails to start the GUI, then the child will exit and the
227 * parent will return. If the child succeeds, then the parent will exit
228 * and the child will return.
229 */
230 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100231gui_do_fork(void)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200232{
Bram Moolenaar30613902019-12-01 22:11:18 +0100233 int pipefd[2]; // pipe between parent and child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200234 int pipe_error;
235 int status;
236 int exit_status;
237 pid_t pid = -1;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200238
Bram Moolenaar30613902019-12-01 22:11:18 +0100239 // Setup a pipe between the child and the parent, so that the parent
240 // knows when the child has done the setsid() call and is allowed to
241 // exit.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200242 pipe_error = (pipe(pipefd) < 0);
243 pid = fork();
Bram Moolenaar30613902019-12-01 22:11:18 +0100244 if (pid < 0) // Fork error
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200245 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100246 emsg(_("E851: Failed to create a new process for the GUI"));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200247 return;
248 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100249 else if (pid > 0) // Parent
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200250 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100251 // Give the child some time to do the setsid(), otherwise the
252 // exit() may kill the child too (when starting gvim from inside a
253 // gvim).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200254 if (!pipe_error)
255 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100256 // The read returns when the child closes the pipe (or when
257 // the child dies for some reason).
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200258 close(pipefd[1]);
259 status = gui_read_child_pipe(pipefd[0]);
260 if (status == GUI_CHILD_FAILED)
261 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100262 // The child failed to start the GUI, so the caller must
263 // continue. There may be more error information written
264 // to stderr by the child.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200265# ifdef __NeXT__
266 wait4(pid, &exit_status, 0, (struct rusage *)0);
267# else
268 waitpid(pid, &exit_status, 0);
269# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100270 emsg(_("E852: The child process failed to start the GUI"));
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200271 return;
272 }
273 else if (status == GUI_CHILD_IO_ERROR)
274 {
275 pipe_error = TRUE;
276 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100277 // else GUI_CHILD_OK: parent exit
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200278 }
279
280 if (pipe_error)
Bram Moolenaareda1da02019-11-17 17:06:33 +0100281 ui_delay(301L, TRUE);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200282
Bram Moolenaar30613902019-12-01 22:11:18 +0100283 // When swapping screens we may need to go to the next line, e.g.,
284 // after a hit-enter prompt and using ":gui".
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200285 if (newline_on_exit)
286 mch_errmsg("\r\n");
287
288 /*
289 * The parent must skip the normal exit() processing, the child
290 * will do it. For example, GTK messes up signals when exiting.
291 */
292 _exit(0);
293 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100294 // Child
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200295
Bram Moolenaar29695702012-05-18 17:03:18 +0200296#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100297 // Call gtk_init_check() here after fork(). See gui_init_check().
Bram Moolenaar29695702012-05-18 17:03:18 +0200298 if (gui_mch_init_check() != OK)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100299 getout_preserve_modified(1);
Bram Moolenaar29695702012-05-18 17:03:18 +0200300#endif
301
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200302# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
303 /*
304 * Change our process group. On some systems/shells a CTRL-C in the
305 * shell where Vim was started would otherwise kill gvim!
306 */
307# if defined(HAVE_SETSID)
308 (void)setsid();
309# else
310 (void)setpgid(0, 0);
311# endif
312# endif
313 if (!pipe_error)
314 close(pipefd[0]);
315
316# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
Bram Moolenaar30613902019-12-01 22:11:18 +0100317 // Tell the session manager our new PID
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200318 gui_mch_forked();
319# endif
320
Bram Moolenaar30613902019-12-01 22:11:18 +0100321 // Try to start the GUI
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200322 gui_attempt_start();
323
Bram Moolenaar30613902019-12-01 22:11:18 +0100324 // Notify the parent
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200325 if (!pipe_error)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200326 {
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200327 if (gui.in_use)
328 write_eintr(pipefd[1], "ok", 3);
329 else
330 write_eintr(pipefd[1], "fail", 5);
331 close(pipefd[1]);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200332 }
333
Bram Moolenaar30613902019-12-01 22:11:18 +0100334 // If we failed to start the GUI, exit now.
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200335 if (!gui.in_use)
Bram Moolenaar6d8d8492016-03-19 14:48:31 +0100336 getout_preserve_modified(1);
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200337}
338
339/*
340 * Read from a pipe assumed to be connected to the child process (this
341 * function is called from the parent).
342 * Return GUI_CHILD_OK if the child successfully started the GUI,
343 * GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
344 * some other error.
345 *
346 * The file descriptor will be closed before the function returns.
347 */
348 static int
349gui_read_child_pipe(int fd)
350{
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200351 long bytes_read;
352#define READ_BUFFER_SIZE 10
353 char buffer[READ_BUFFER_SIZE];
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200354
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200355 bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
356#undef READ_BUFFER_SIZE
357 close(fd);
358 if (bytes_read < 0)
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200359 return GUI_CHILD_IO_ERROR;
Bram Moolenaarcd6fe972011-10-20 21:28:01 +0200360 buffer[bytes_read] = NUL;
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200361 if (strcmp(buffer, "ok") == 0)
362 return GUI_CHILD_OK;
363 return GUI_CHILD_FAILED;
364}
365
Bram Moolenaar30613902019-12-01 22:11:18 +0100366#endif // GUI_MAY_FORK
Bram Moolenaar7f78bd72011-09-14 19:04:39 +0200367
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368/*
369 * Call this when vim starts up, whether or not the GUI is started
370 */
371 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100372gui_prepare(int *argc, char **argv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373{
Bram Moolenaar30613902019-12-01 22:11:18 +0100374 gui.in_use = FALSE; // No GUI yet (maybe later)
375 gui.starting = FALSE; // No GUI yet (maybe later)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 gui_mch_prepare(argc, argv);
377}
378
379/*
380 * Try initializing the GUI and check if it can be started.
381 * Used from main() to check early if "vim -g" can start the GUI.
382 * Used from gui_init() to prepare for starting the GUI.
383 * Returns FAIL or OK.
384 */
385 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100386gui_init_check(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
388 static int result = MAYBE;
389
390 if (result != MAYBE)
391 {
392 if (result == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100393 emsg(_("E229: Cannot start the GUI"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 return result;
395 }
396
397 gui.shell_created = FALSE;
398 gui.dying = FALSE;
Bram Moolenaar30613902019-12-01 22:11:18 +0100399 gui.in_focus = TRUE; // so the guicursor setting works
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 gui.dragged_sb = SBAR_NONE;
401 gui.dragged_wp = NULL;
402 gui.pointer_hidden = FALSE;
403 gui.col = 0;
404 gui.row = 0;
405 gui.num_cols = Columns;
406 gui.num_rows = Rows;
407
408 gui.cursor_is_valid = FALSE;
409 gui.scroll_region_top = 0;
410 gui.scroll_region_bot = Rows - 1;
411 gui.scroll_region_left = 0;
412 gui.scroll_region_right = Columns - 1;
413 gui.highlight_mask = HL_NORMAL;
414 gui.char_width = 1;
415 gui.char_height = 1;
416 gui.char_ascent = 0;
417 gui.border_width = 0;
418
419 gui.norm_font = NOFONT;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200420#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 gui.bold_font = NOFONT;
422 gui.ital_font = NOFONT;
423 gui.boldital_font = NOFONT;
424# ifdef FEAT_XFONTSET
425 gui.fontset = NOFONTSET;
426# endif
427#endif
Bram Moolenaardb250522013-06-17 22:43:25 +0200428 gui.wide_font = NOFONT;
Bram Moolenaar13505972019-01-24 15:04:48 +0100429#ifndef FEAT_GUI_GTK
Bram Moolenaardb250522013-06-17 22:43:25 +0200430 gui.wide_bold_font = NOFONT;
431 gui.wide_ital_font = NOFONT;
432 gui.wide_boldital_font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +0200433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434
435#ifdef FEAT_MENU
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200436# ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437# ifdef FONTSET_ALWAYS
438 gui.menu_fontset = NOFONTSET;
439# else
440 gui.menu_font = NOFONT;
441# endif
442# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100443 gui.menu_is_active = TRUE; // default: include menu
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444# ifndef FEAT_GUI_GTK
445 gui.menu_height = MENU_DEFAULT_HEIGHT;
446 gui.menu_width = 0;
447# endif
448#endif
449#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
450 gui.toolbar_height = 0;
451#endif
452#if defined(FEAT_FOOTER) && defined(FEAT_GUI_MOTIF)
453 gui.footer_height = 0;
454#endif
455#ifdef FEAT_BEVAL_TIP
456 gui.tooltip_fontset = NOFONTSET;
457#endif
458
459 gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
460 gui.prev_wrap = -1;
461
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200462#if defined(ALWAYS_USE_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 result = OK;
464#else
Bram Moolenaar29695702012-05-18 17:03:18 +0200465# ifdef FEAT_GUI_GTK
466 /*
467 * Note: Don't call gtk_init_check() before fork, it will be called after
468 * the fork. When calling it before fork, it make vim hang for a while.
469 * See gui_do_fork().
470 * Use a simpler check if the GUI window can probably be opened.
471 */
Bram Moolenaar717e1962016-08-10 21:28:44 +0200472 result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200473# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 result = gui_mch_init_check();
Bram Moolenaar29695702012-05-18 17:03:18 +0200475# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476#endif
477 return result;
478}
479
480/*
481 * This is the call which starts the GUI.
482 */
483 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100484gui_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485{
486 win_T *wp;
487 static int recursive = 0;
488
489 /*
490 * It's possible to use ":gui" in a .gvimrc file. The first halve of this
491 * function will then be executed at the first call, the rest by the
492 * recursive call. This allow the shell to be opened halfway reading a
493 * gvimrc file.
494 */
495 if (!recursive)
496 {
497 ++recursive;
498
499 clip_init(TRUE);
500
Bram Moolenaar30613902019-12-01 22:11:18 +0100501 // If can't initialize, don't try doing the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 if (gui_init_check() == FAIL)
503 {
504 --recursive;
505 clip_init(FALSE);
506 return;
507 }
508
509 /*
Bram Moolenaarb23c3382005-01-31 19:09:12 +0000510 * Reset 'paste'. It's useful in the terminal, but not in the GUI. It
511 * breaks the Paste toolbar button.
512 */
513 set_option_value((char_u *)"paste", 0L, NULL, 0);
514
515 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 * Set up system-wide default menus.
517 */
518#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
519 if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
520 {
521 sys_menu = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000522 do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 sys_menu = FALSE;
524 }
525#endif
526
527 /*
528 * Switch on the mouse by default, unless the user changed it already.
529 * This can then be changed in the .gvimrc.
530 */
531 if (!option_was_set((char_u *)"mouse"))
532 set_string_option_direct((char_u *)"mouse", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000533 (char_u *)"a", OPT_FREE, SID_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534
535 /*
536 * If -U option given, use only the initializations from that file and
537 * nothing else. Skip all initializations for "-U NONE" or "-u NORC".
538 */
539 if (use_gvimrc != NULL)
540 {
541 if (STRCMP(use_gvimrc, "NONE") != 0
542 && STRCMP(use_gvimrc, "NORC") != 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000543 && do_source(use_gvimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100544 semsg(_("E230: Cannot read from \"%s\""), use_gvimrc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
546 else
547 {
548 /*
549 * Get system wide defaults for gvim, only when file name defined.
550 */
551#ifdef SYS_GVIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000552 do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553#endif
554
555 /*
556 * Try to read GUI initialization commands from the following
557 * places:
558 * - environment variable GVIMINIT
559 * - the user gvimrc file (~/.gvimrc)
560 * - the second user gvimrc file ($VIM/.gvimrc for Dos)
561 * - the third user gvimrc file ($VIM/.gvimrc for Amiga)
562 * The first that exists is used, the rest is ignored.
563 */
564 if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000565 && do_source((char_u *)USR_GVIMRC_FILE, TRUE,
566 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567#ifdef USR_GVIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000568 && do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
569 DOSO_GVIMRC) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200571#ifdef USR_GVIMRC_FILE3
572 && do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
573 DOSO_GVIMRC) == FAIL
574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 )
576 {
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200577#ifdef USR_GVIMRC_FILE4
578 (void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579#endif
580 }
581
582 /*
583 * Read initialization commands from ".gvimrc" in current
584 * directory. This is only done if the 'exrc' option is set.
585 * Because of security reasons we disallow shell and write
586 * commands now, except for unix if the file is owned by the user
587 * or 'secure' option has been reset in environment of global
588 * ".gvimrc".
589 * Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
590 * USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
591 */
592 if (p_exrc)
593 {
594#ifdef UNIX
595 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200596 stat_T s;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaar30613902019-12-01 22:11:18 +0100598 // if ".gvimrc" file is not owned by user, set 'secure'
599 // mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
601 secure = p_secure;
602 }
603#else
604 secure = p_secure;
605#endif
606
607 if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200608 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609#ifdef SYS_GVIMRC_FILE
610 && fullpathcmp((char_u *)SYS_GVIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200611 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612#endif
613#ifdef USR_GVIMRC_FILE2
614 && fullpathcmp((char_u *)USR_GVIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200615 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616#endif
617#ifdef USR_GVIMRC_FILE3
618 && fullpathcmp((char_u *)USR_GVIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200619 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200621#ifdef USR_GVIMRC_FILE4
622 && fullpathcmp((char_u *)USR_GVIMRC_FILE4,
Bram Moolenaar99499b12019-05-23 21:35:48 +0200623 (char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar22971aa2013-06-12 20:35:58 +0200624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000626 do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
628 if (secure == 2)
629 need_wait_return = TRUE;
630 secure = 0;
631 }
632 }
633
634 if (need_wait_return || msg_didany)
635 wait_return(TRUE);
636
637 --recursive;
638 }
639
Bram Moolenaar30613902019-12-01 22:11:18 +0100640 // If recursive call opened the shell, return here from the first call
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 if (gui.in_use)
642 return;
643
644 /*
645 * Create the GUI shell.
646 */
Bram Moolenaar30613902019-12-01 22:11:18 +0100647 gui.in_use = TRUE; // Must be set after menus have been set up
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 if (gui_mch_init() == FAIL)
649 goto error;
650
Bram Moolenaar30613902019-12-01 22:11:18 +0100651 // Avoid a delay for an error message that was printed in the terminal
652 // where Vim was started.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 emsg_on_display = FALSE;
654 msg_scrolled = 0;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100655 clear_sb_text(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 need_wait_return = FALSE;
657 msg_didany = FALSE;
658
659 /*
660 * Check validity of any generic resources that may have been loaded.
661 */
662 if (gui.border_width < 0)
663 gui.border_width = 0;
664
665 /*
666 * Set up the fonts. First use a font specified with "-fn" or "-font".
667 */
668 if (font_argument != NULL)
669 set_option_value((char_u *)"gfn", 0L, (char_u *)font_argument, 0);
670 if (
671#ifdef FEAT_XFONTSET
672 (*p_guifontset == NUL
673 || gui_init_font(p_guifontset, TRUE) == FAIL) &&
674#endif
675 gui_init_font(*p_guifont == NUL ? hl_get_font_name()
676 : p_guifont, FALSE) == FAIL)
677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100678 emsg(_("E665: Cannot start GUI, no valid font found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 goto error2;
680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 if (gui_get_wide_font() == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100682 emsg(_("E231: 'guifontwide' invalid"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
684 gui.num_cols = Columns;
685 gui.num_rows = Rows;
686 gui_reset_scroll_region();
687
Bram Moolenaar30613902019-12-01 22:11:18 +0100688 // Create initial scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 FOR_ALL_WINDOWS(wp)
690 {
691 gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
692 gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
693 }
694 gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
695
696#ifdef FEAT_MENU
697 gui_create_initial_menus(root_menu);
698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699#ifdef FEAT_SIGN_ICONS
700 sign_gui_started();
701#endif
702
Bram Moolenaar30613902019-12-01 22:11:18 +0100703 // Configure the desired menu and scrollbars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 gui_init_which_components(NULL);
705
Bram Moolenaar30613902019-12-01 22:11:18 +0100706 // All components of the GUI have been created now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 gui.shell_created = TRUE;
708
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100709#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4814ccb2018-12-22 18:44:53 +0100710 // Set the shell size, adjusted for the screen size. For GTK this only
711 // works after the shell has been opened, thus it is further down.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100712 // If the window is already maximized (e.g. when --windowid is passed in),
713 // we want to use the system-provided dimensions by passing FALSE to
714 // mustset. Otherwise, we want to initialize with the default rows/columns.
715 if (gui_mch_maximized())
716 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
717 else
718 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar4fc8e2f2019-03-30 22:26:38 +0100719#else
720# ifndef FEAT_GUI_GTK
721 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723#endif
724#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
Bram Moolenaar30613902019-12-01 22:11:18 +0100725 // Need to set the size of the menubar after all the menus have been
726 // created.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 gui_mch_compute_menu_height((Widget)0);
728#endif
729
730 /*
731 * Actually open the GUI shell.
732 */
733 if (gui_mch_open() != FAIL)
734 {
735#ifdef FEAT_TITLE
736 maketitle();
737 resettitle();
738#endif
739 init_gui_options();
740#ifdef FEAT_ARABIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100741 // Our GUI can't do bidi.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 p_tbidi = FALSE;
743#endif
Bram Moolenaar9372a112005-12-06 19:59:18 +0000744#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +0100745 // Give GTK+ a chance to put all widget's into place.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 gui_mch_update();
Bram Moolenaar18144c82006-04-12 21:52:12 +0000747
748# ifdef FEAT_MENU
Bram Moolenaar30613902019-12-01 22:11:18 +0100749 // If there is no 'm' in 'guioptions' we need to remove the menu now.
750 // It was still there to make F10 work.
Bram Moolenaar18144c82006-04-12 21:52:12 +0000751 if (vim_strchr(p_go, GO_MENUS) == NULL)
752 {
753 --gui.starting;
754 gui_mch_enable_menu(FALSE);
755 ++gui.starting;
756 gui_mch_update();
757 }
758# endif
759
Bram Moolenaar30613902019-12-01 22:11:18 +0100760 // Now make sure the shell fits on the screen.
Bram Moolenaar372674f2019-03-30 20:31:22 +0100761 if (gui_mch_maximized())
762 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
763 else
764 gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100766 // When 'lines' was set while starting up the topframe may have to be
767 // resized.
Bram Moolenaar41bfd302005-04-24 21:59:46 +0000768 win_new_shellsize();
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000769
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100770#ifdef FEAT_BEVAL_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100771 // Always create the Balloon Evaluation area, but disable it when
772 // 'ballooneval' is off.
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100773 if (balloonEval != NULL)
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200774 {
775# ifdef FEAT_VARTABS
776 vim_free(balloonEval->vts);
777# endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100778 vim_free(balloonEval);
Bram Moolenaarca4b6132018-06-28 12:05:11 +0200779 }
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100780 balloonEvalForTerm = FALSE;
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000781# ifdef FEAT_GUI_GTK
782 balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
783 &general_beval_cb, NULL);
784# else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000785# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000786 {
787 extern Widget textArea;
788 balloonEval = gui_mch_create_beval_area(textArea, NULL,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +0000789 &general_beval_cb, NULL);
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000790 }
791# else
Bram Moolenaar4f974752019-02-17 17:44:42 +0100792# ifdef FEAT_GUI_MSWIN
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +0000793 balloonEval = gui_mch_create_beval_area(NULL, NULL,
794 &general_beval_cb, NULL);
795# endif
796# endif
797# endif
798 if (!p_beval)
799 gui_mch_disable_beval_area(balloonEval);
800#endif
801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
803 if (!im_xim_isvalid_imactivate())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100804 emsg(_("E599: Value of 'imactivatekey' is invalid"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805#endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100806 // When 'cmdheight' was set during startup it may not have taken
807 // effect yet.
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000808 if (p_ch != 1L)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +0000809 command_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811 return;
812 }
813
814error2:
815#ifdef FEAT_GUI_X11
Bram Moolenaar30613902019-12-01 22:11:18 +0100816 // undo gui_mch_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 gui_mch_uninit();
818#endif
819
820error:
821 gui.in_use = FALSE;
822 clip_init(FALSE);
823}
824
825
826 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100827gui_exit(int rc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828{
Bram Moolenaar30613902019-12-01 22:11:18 +0100829 // don't free the fonts, it leads to a BUS error
830 // richard@whitequeen.com Jul 99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 free_highlight_fonts();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 gui.in_use = FALSE;
833 gui_mch_exit(rc);
834}
835
Bram Moolenaar9372a112005-12-06 19:59:18 +0000836#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) || defined(PROTO)
Bram Moolenaarda68cf32006-10-10 15:35:57 +0000838# define NEED_GUI_UPDATE_SCREEN 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839/*
840 * Called when the GUI shell is closed by the user. If there are no changed
841 * files Vim exits, otherwise there will be a dialog to ask the user what to
842 * do.
843 * When this function returns, Vim should NOT exit!
844 */
845 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100846gui_shell_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847{
848 cmdmod_T save_cmdmod;
849
850 save_cmdmod = cmdmod;
851
Bram Moolenaar30613902019-12-01 22:11:18 +0100852 // Only exit when there are no changed files
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 exiting = TRUE;
854# ifdef FEAT_BROWSE
855 cmdmod.browse = TRUE;
856# endif
857# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
858 cmdmod.confirm = TRUE;
859# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100860 // If there are changed buffers, present the user with a dialog if
861 // possible, otherwise give an error message.
Bram Moolenaar027387f2016-01-02 22:25:52 +0100862 if (!check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 getout(0);
864
865 exiting = FALSE;
866 cmdmod = save_cmdmod;
Bram Moolenaar30613902019-12-01 22:11:18 +0100867 gui_update_screen(); // redraw, window may show changed buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868}
869#endif
870
871/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200872 * Set the font. "font_list" is a comma separated list of font names. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 * first font name that works is used. If none is found, use the default
874 * font.
875 * If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
876 * Return OK when able to set the font. When it failed FAIL is returned and
877 * the fonts are unchanged.
878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100880gui_init_font(char_u *font_list, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881{
882#define FONTLEN 320
883 char_u font_name[FONTLEN];
884 int font_list_empty = FALSE;
885 int ret = FAIL;
886
887 if (!gui.in_use)
888 return FAIL;
889
890 font_name[0] = NUL;
891 if (*font_list == NUL)
892 font_list_empty = TRUE;
893 else
894 {
895#ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +0100896 // When using a fontset, the whole list of fonts is one name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (fontset)
898 ret = gui_mch_init_font(font_list, TRUE);
899 else
900#endif
901 while (*font_list != NUL)
902 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100903 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 (void)copy_option_part(&font_list, font_name, FONTLEN, ",");
905
Bram Moolenaar30613902019-12-01 22:11:18 +0100906 // Careful!!! The Win32 version of gui_mch_init_font(), when
907 // called with "*" will change p_guifont to the selected font
908 // name, which frees the old value. This makes font_list
909 // invalid. Thus when OK is returned here, font_list must no
910 // longer be used!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (gui_mch_init_font(font_name, FALSE) == OK)
912 {
Bram Moolenaar13505972019-01-24 15:04:48 +0100913#if !defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +0100914 // If it's a Unicode font, try setting 'guifontwide' to a
915 // similar double-width font.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 if ((p_guifontwide == NULL || *p_guifontwide == NUL)
917 && strstr((char *)font_name, "10646") != NULL)
918 set_guifontwide(font_name);
919#endif
920 ret = OK;
921 break;
922 }
923 }
924 }
925
926 if (ret != OK
927 && STRCMP(font_list, "*") != 0
928 && (font_list_empty || gui.norm_font == NOFONT))
929 {
930 /*
931 * Couldn't load any font in 'font_list', keep the current font if
932 * there is one. If 'font_list' is empty, or if there is no current
933 * font, tell gui_mch_init_font() to try to find a font we can load.
934 */
935 ret = gui_mch_init_font(NULL, FALSE);
936 }
937
938 if (ret == OK)
939 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +0200940#ifndef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +0100941 // Set normal font as current font
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942# ifdef FEAT_XFONTSET
943 if (gui.fontset != NOFONTSET)
944 gui_mch_set_fontset(gui.fontset);
945 else
946# endif
947 gui_mch_set_font(gui.norm_font);
948#endif
Bram Moolenaar372674f2019-03-30 20:31:22 +0100949 gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951
952 return ret;
953}
954
Bram Moolenaar13505972019-01-24 15:04:48 +0100955#ifndef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956/*
957 * Try setting 'guifontwide' to a font twice as wide as "name".
958 */
959 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100960set_guifontwide(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961{
962 int i = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +0100963 char_u wide_name[FONTLEN + 10]; // room for 2 * width and '*'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 char_u *wp = NULL;
965 char_u *p;
966 GuiFont font;
967
968 wp = wide_name;
969 for (p = name; *p != NUL; ++p)
970 {
971 *wp++ = *p;
972 if (*p == '-')
973 {
974 ++i;
Bram Moolenaar30613902019-12-01 22:11:18 +0100975 if (i == 6) // font type: change "--" to "-*-"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 {
977 if (p[1] == '-')
978 *wp++ = '*';
979 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100980 else if (i == 12) // found the width
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 {
982 ++p;
983 i = getdigits(&p);
984 if (i != 0)
985 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100986 // Double the width specification.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 sprintf((char *)wp, "%d%s", i * 2, p);
988 font = gui_mch_get_font(wide_name, FALSE);
989 if (font != NOFONT)
990 {
Bram Moolenaard8b0cf12004-12-12 11:33:30 +0000991 gui_mch_free_font(gui.wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 gui.wide_font = font;
993 set_string_option_direct((char_u *)"gfw", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000994 wide_name, OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
996 }
997 break;
998 }
999 }
1000 }
1001}
Bram Moolenaar30613902019-12-01 22:11:18 +01001002#endif // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004/*
1005 * Get the font for 'guifontwide'.
1006 * Return FAIL for an invalid font name.
1007 */
1008 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001009gui_get_wide_font(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010{
1011 GuiFont font = NOFONT;
1012 char_u font_name[FONTLEN];
1013 char_u *p;
1014
Bram Moolenaar30613902019-12-01 22:11:18 +01001015 if (!gui.in_use) // Can't allocate font yet, assume it's OK.
1016 return OK; // Will give an error message later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
1018 if (p_guifontwide != NULL && *p_guifontwide != NUL)
1019 {
1020 for (p = p_guifontwide; *p != NUL; )
1021 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001022 // Isolate one comma separated font name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 (void)copy_option_part(&p, font_name, FONTLEN, ",");
1024 font = gui_mch_get_font(font_name, FALSE);
1025 if (font != NOFONT)
1026 break;
1027 }
1028 if (font == NOFONT)
1029 return FAIL;
1030 }
1031
1032 gui_mch_free_font(gui.wide_font);
Bram Moolenaar13505972019-01-24 15:04:48 +01001033#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001034 // Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 if (font != NOFONT && gui.norm_font != NOFONT
1036 && pango_font_description_equal(font, gui.norm_font))
1037 {
1038 gui.wide_font = NOFONT;
1039 gui_mch_free_font(font);
1040 }
1041 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 gui.wide_font = font;
Bram Moolenaar13505972019-01-24 15:04:48 +01001044#ifdef FEAT_GUI_MSWIN
Bram Moolenaar0f272122013-01-23 18:37:40 +01001045 gui_mch_wide_font_changed();
Bram Moolenaar13505972019-01-24 15:04:48 +01001046#else
Bram Moolenaardb250522013-06-17 22:43:25 +02001047 /*
1048 * TODO: setup wide_bold_font, wide_ital_font and wide_boldital_font to
1049 * support those fonts for 'guifontwide'.
1050 */
Bram Moolenaar13505972019-01-24 15:04:48 +01001051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 return OK;
1053}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001055 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001056gui_set_cursor(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057{
1058 gui.row = row;
1059 gui.col = col;
1060}
1061
1062/*
1063 * gui_check_pos - check if the cursor is on the screen.
1064 */
1065 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001066gui_check_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
1068 if (gui.row >= screen_Rows)
1069 gui.row = screen_Rows - 1;
1070 if (gui.col >= screen_Columns)
1071 gui.col = screen_Columns - 1;
1072 if (gui.cursor_row >= screen_Rows || gui.cursor_col >= screen_Columns)
1073 gui.cursor_is_valid = FALSE;
1074}
1075
1076/*
1077 * Redraw the cursor if necessary or when forced.
1078 * Careful: The contents of ScreenLines[] must match what is on the screen,
1079 * otherwise this goes wrong. May need to call out_flush() first.
1080 */
1081 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001082gui_update_cursor(
Bram Moolenaar30613902019-12-01 22:11:18 +01001083 int force, // when TRUE, update even when not moved
1084 int clear_selection) // clear selection under cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
1086 int cur_width = 0;
1087 int cur_height = 0;
1088 int old_hl_mask;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001089 cursorentry_T *shape;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 int id;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001091#ifdef FEAT_TERMINAL
1092 guicolor_T shape_fg = INVALCOLOR;
1093 guicolor_T shape_bg = INVALCOLOR;
1094#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01001095 guicolor_T cfg, cbg, cc; // cursor fore-/background color
1096 int cattr; // cursor attributes
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 int attr;
1098 attrentry_T *aep = NULL;
1099
Bram Moolenaar30613902019-12-01 22:11:18 +01001100 // Don't update the cursor when halfway busy scrolling or the screen size
1101 // doesn't match 'columns' and 'lines. ScreenLines[] isn't valid then.
Bram Moolenaar1b8d33b2008-08-06 12:37:44 +00001102 if (!can_update_cursor || screen_Columns != gui.num_cols
1103 || screen_Rows != gui.num_rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 return;
1105
1106 gui_check_pos();
1107 if (!gui.cursor_is_valid || force
1108 || gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1109 {
1110 gui_undraw_cursor();
1111 if (gui.row < 0)
1112 return;
Bram Moolenaar6e35a112018-03-04 21:36:05 +01001113#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 if (gui.row != gui.cursor_row || gui.col != gui.cursor_col)
1115 im_set_position(gui.row, gui.col);
1116#endif
1117 gui.cursor_row = gui.row;
1118 gui.cursor_col = gui.col;
1119
Bram Moolenaar30613902019-12-01 22:11:18 +01001120 // Only write to the screen after ScreenLines[] has been initialized
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 if (!screen_cleared || ScreenLines == NULL)
1122 return;
1123
Bram Moolenaar30613902019-12-01 22:11:18 +01001124 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (clear_selection)
1126 clip_may_clear_selection(gui.row, gui.row);
Bram Moolenaar30613902019-12-01 22:11:18 +01001127 // Check that the cursor is inside the shell (resizing may have made
1128 // it invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 if (gui.row >= screen_Rows || gui.col >= screen_Columns)
1130 return;
1131
1132 gui.cursor_is_valid = TRUE;
1133
1134 /*
1135 * How the cursor is drawn depends on the current mode.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001136 * When in a terminal window use the shape/color specified there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001138#ifdef FEAT_TERMINAL
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001139 if (terminal_is_active())
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001140 shape = term_get_cursor_shape(&shape_fg, &shape_bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 else
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001142#endif
1143 shape = &shape_table[get_shape_idx(FALSE)];
1144 if (State & LANGMAP)
1145 id = shape->id_lm;
1146 else
1147 id = shape->id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148
Bram Moolenaar30613902019-12-01 22:11:18 +01001149 // get the colors and attributes for the cursor. Default is inverted
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 cfg = INVALCOLOR;
1151 cbg = INVALCOLOR;
1152 cattr = HL_INVERSE;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001153 gui_mch_set_blinking(shape->blinkwait,
1154 shape->blinkon,
1155 shape->blinkoff);
Bram Moolenaar1dcada12017-11-13 21:10:04 +01001156 if (shape->blinkwait == 0 || shape->blinkon == 0
1157 || shape->blinkoff == 0)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001158 gui_mch_stop_blink(FALSE);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001159#ifdef FEAT_TERMINAL
1160 if (shape_bg != INVALCOLOR)
1161 {
1162 cattr = 0;
1163 cfg = shape_fg;
1164 cbg = shape_bg;
1165 }
1166 else
1167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 if (id > 0)
1169 {
1170 cattr = syn_id2colors(id, &cfg, &cbg);
Bram Moolenaar54612582019-11-21 17:13:31 +01001171#if defined(HAVE_INPUT_METHOD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
1173 static int iid;
1174 guicolor_T fg, bg;
1175
Bram Moolenaarc236c162008-07-13 17:41:49 +00001176 if (
Bram Moolenaar54612582019-11-21 17:13:31 +01001177# if defined(FEAT_GUI_GTK) && defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00001178 preedit_get_status()
1179# else
1180 im_get_status()
1181# endif
1182 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
1184 iid = syn_name2id((char_u *)"CursorIM");
1185 if (iid > 0)
1186 {
1187 syn_id2colors(iid, &fg, &bg);
1188 if (bg != INVALCOLOR)
1189 cbg = bg;
1190 if (fg != INVALCOLOR)
1191 cfg = fg;
1192 }
1193 }
1194 }
1195#endif
1196 }
1197
1198 /*
1199 * Get the attributes for the character under the cursor.
1200 * When no cursor color was given, use the character color.
1201 */
1202 attr = ScreenAttrs[LineOffset[gui.row] + gui.col];
1203 if (attr > HL_ALL)
1204 aep = syn_gui_attr2entry(attr);
1205 if (aep != NULL)
1206 {
1207 attr = aep->ae_attr;
1208 if (cfg == INVALCOLOR)
1209 cfg = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1210 : aep->ae_u.gui.fg_color);
1211 if (cbg == INVALCOLOR)
1212 cbg = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1213 : aep->ae_u.gui.bg_color);
1214 }
1215 if (cfg == INVALCOLOR)
1216 cfg = (attr & HL_INVERSE) ? gui.back_pixel : gui.norm_pixel;
1217 if (cbg == INVALCOLOR)
1218 cbg = (attr & HL_INVERSE) ? gui.norm_pixel : gui.back_pixel;
1219
1220#ifdef FEAT_XIM
1221 if (aep != NULL)
1222 {
1223 xim_bg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.fg_color
1224 : aep->ae_u.gui.bg_color);
1225 xim_fg_color = ((attr & HL_INVERSE) ? aep->ae_u.gui.bg_color
1226 : aep->ae_u.gui.fg_color);
1227 if (xim_bg_color == INVALCOLOR)
1228 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1229 : gui.back_pixel;
1230 if (xim_fg_color == INVALCOLOR)
1231 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1232 : gui.norm_pixel;
1233 }
1234 else
1235 {
1236 xim_bg_color = (attr & HL_INVERSE) ? gui.norm_pixel
1237 : gui.back_pixel;
1238 xim_fg_color = (attr & HL_INVERSE) ? gui.back_pixel
1239 : gui.norm_pixel;
1240 }
1241#endif
1242
1243 attr &= ~HL_INVERSE;
1244 if (cattr & HL_INVERSE)
1245 {
1246 cc = cbg;
1247 cbg = cfg;
1248 cfg = cc;
1249 }
1250 cattr &= ~HL_INVERSE;
1251
1252 /*
1253 * When we don't have window focus, draw a hollow cursor.
1254 */
1255 if (!gui.in_focus)
1256 {
1257 gui_mch_draw_hollow_cursor(cbg);
1258 return;
1259 }
1260
1261 old_hl_mask = gui.highlight_mask;
Bram Moolenaar54612582019-11-21 17:13:31 +01001262 if (shape->shape == SHAPE_BLOCK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
1264 /*
1265 * Draw the text character with the cursor colors. Use the
1266 * character attributes plus the cursor attributes.
1267 */
1268 gui.highlight_mask = (cattr | attr);
Bram Moolenaar54612582019-11-21 17:13:31 +01001269 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 GUI_MON_IS_CURSOR | GUI_MON_NOCLEAR, cfg, cbg, 0);
1271 }
1272 else
1273 {
Bram Moolenaar13505972019-01-24 15:04:48 +01001274#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 int col_off = FALSE;
1276#endif
1277 /*
1278 * First draw the partial cursor, then overwrite with the text
1279 * character, using a transparent background.
1280 */
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001281 if (shape->shape == SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 {
1283 cur_height = gui.char_height;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001284 cur_width = (gui.char_width * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286 else
1287 {
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001288 cur_height = (gui.char_height * shape->percentage + 99) / 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 cur_width = gui.char_width;
1290 }
Bram Moolenaar367329b2007-08-30 11:53:22 +00001291 if (has_mbyte && (*mb_off2cells)(LineOffset[gui.row] + gui.col,
1292 LineOffset[gui.row] + screen_Columns) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001294 // Double wide character.
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001295 if (shape->shape != SHAPE_VER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 cur_width += gui.char_width;
Bram Moolenaar13505972019-01-24 15:04:48 +01001297#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 if (CURSOR_BAR_RIGHT)
1299 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001300 // gui.col points to the left halve of the character but
1301 // the vertical line needs to be on the right halve.
1302 // A double-wide horizontal line is also drawn from the
1303 // right halve in gui_mch_draw_part_cursor().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 col_off = TRUE;
1305 ++gui.col;
1306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 gui_mch_draw_part_cursor(cur_width, cur_height, cbg);
Bram Moolenaar13505972019-01-24 15:04:48 +01001310#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (col_off)
1312 --gui.col;
1313#endif
1314
Bram Moolenaar30613902019-12-01 22:11:18 +01001315#ifndef FEAT_GUI_MSWIN // doesn't seem to work for MSWindows
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 gui.highlight_mask = ScreenAttrs[LineOffset[gui.row] + gui.col];
1317 (void)gui_screenchar(LineOffset[gui.row] + gui.col,
1318 GUI_MON_TRS_CURSOR | GUI_MON_NOCLEAR,
1319 (guicolor_T)0, (guicolor_T)0, 0);
1320#endif
1321 }
1322 gui.highlight_mask = old_hl_mask;
1323 }
1324}
1325
1326#if defined(FEAT_MENU) || defined(PROTO)
1327 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001328gui_position_menu(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329{
Bram Moolenaar9372a112005-12-06 19:59:18 +00001330# if !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 if (gui.menu_is_active && gui.in_use)
1332 gui_mch_set_menu_pos(0, 0, gui.menu_width, gui.menu_height);
1333# endif
1334}
1335#endif
1336
1337/*
1338 * Position the various GUI components (text area, menu). The vertical
1339 * scrollbars are NOT handled here. See gui_update_scrollbars().
1340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001342gui_position_components(int total_width UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
1344 int text_area_x;
1345 int text_area_y;
1346 int text_area_width;
1347 int text_area_height;
1348
Bram Moolenaar30613902019-12-01 22:11:18 +01001349 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 ++hold_gui_events;
1351
1352 text_area_x = 0;
1353 if (gui.which_scrollbars[SBAR_LEFT])
1354 text_area_x += gui.scrollbar_width;
1355
1356 text_area_y = 0;
1357#if defined(FEAT_MENU) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON))
1358 gui.menu_width = total_width;
1359 if (gui.menu_is_active)
1360 text_area_y += gui.menu_height;
1361#endif
1362#if defined(FEAT_TOOLBAR) && defined(FEAT_GUI_MSWIN)
1363 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1364 text_area_y = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1365#endif
1366
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001367# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
Bram Moolenaar367329b2007-08-30 11:53:22 +00001368 || defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_MAC))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001369 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001370 text_area_y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001371#endif
1372
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA))
1374 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1375 {
1376# ifdef FEAT_GUI_ATHENA
1377 gui_mch_set_toolbar_pos(0, text_area_y,
1378 gui.menu_width, gui.toolbar_height);
1379# endif
1380 text_area_y += gui.toolbar_height;
1381 }
1382#endif
1383
1384 text_area_width = gui.num_cols * gui.char_width + gui.border_offset * 2;
1385 text_area_height = gui.num_rows * gui.char_height + gui.border_offset * 2;
1386
1387 gui_mch_set_text_area_pos(text_area_x,
1388 text_area_y,
1389 text_area_width,
1390 text_area_height
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001391#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 + xim_get_status_area_height()
1393#endif
1394 );
1395#ifdef FEAT_MENU
1396 gui_position_menu();
1397#endif
1398 if (gui.which_scrollbars[SBAR_BOTTOM])
1399 gui_mch_set_scrollbar_pos(&gui.bottom_sbar,
1400 text_area_x,
1401 text_area_y + text_area_height,
1402 text_area_width,
1403 gui.scrollbar_height);
1404 gui.left_sbar_x = 0;
1405 gui.right_sbar_x = text_area_x + text_area_width;
1406
1407 --hold_gui_events;
1408}
1409
Bram Moolenaar02743632005-07-25 20:42:36 +00001410/*
1411 * Get the width of the widgets and decorations to the side of the text area.
1412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001414gui_get_base_width(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
1416 int base_width;
1417
1418 base_width = 2 * gui.border_offset;
1419 if (gui.which_scrollbars[SBAR_LEFT])
1420 base_width += gui.scrollbar_width;
1421 if (gui.which_scrollbars[SBAR_RIGHT])
1422 base_width += gui.scrollbar_width;
1423 return base_width;
1424}
1425
Bram Moolenaar02743632005-07-25 20:42:36 +00001426/*
1427 * Get the height of the widgets and decorations above and below the text area.
1428 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001430gui_get_base_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
1432 int base_height;
1433
1434 base_height = 2 * gui.border_offset;
1435 if (gui.which_scrollbars[SBAR_BOTTOM])
1436 base_height += gui.scrollbar_height;
1437#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01001438 // We can't take the sizes properly into account until anything is
1439 // realized. Therefore we recalculate all the values here just before
1440 // setting the size. (--mdcki)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441#else
1442# ifdef FEAT_MENU
1443 if (gui.menu_is_active)
1444 base_height += gui.menu_height;
1445# endif
1446# ifdef FEAT_TOOLBAR
1447 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1448# if defined(FEAT_GUI_MSWIN) && defined(FEAT_TOOLBAR)
1449 base_height += (TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT);
1450# else
1451 base_height += gui.toolbar_height;
1452# endif
1453# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001454# if defined(FEAT_GUI_TABLINE) && (defined(FEAT_GUI_MSWIN) \
1455 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001456 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001457 base_height += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00001458# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459# ifdef FEAT_FOOTER
1460 if (vim_strchr(p_go, GO_FOOTER) != NULL)
1461 base_height += gui.footer_height;
1462# endif
1463# if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
1464 base_height += gui_mch_text_area_extra_height();
1465# endif
1466#endif
1467 return base_height;
1468}
1469
1470/*
1471 * Should be called after the GUI shell has been resized. Its arguments are
1472 * the new width and height of the shell in pixels.
1473 */
1474 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001475gui_resize_shell(int pixel_width, int pixel_height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476{
1477 static int busy = FALSE;
1478
Bram Moolenaar30613902019-12-01 22:11:18 +01001479 if (!gui.shell_created) // ignore when still initializing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 return;
1481
1482 /*
1483 * Can't resize the screen while it is being redrawn. Remember the new
1484 * size and handle it later.
1485 */
1486 if (updating_screen || busy)
1487 {
1488 new_pixel_width = pixel_width;
1489 new_pixel_height = pixel_height;
1490 return;
1491 }
1492
1493again:
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001494 new_pixel_width = 0;
1495 new_pixel_height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 busy = TRUE;
1497
Bram Moolenaar30613902019-12-01 22:11:18 +01001498 // Flush pending output before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 out_flush();
1500
1501 gui.num_cols = (pixel_width - gui_get_base_width()) / gui.char_width;
Bram Moolenaar6f7743e2008-02-06 16:33:58 +00001502 gui.num_rows = (pixel_height - gui_get_base_height()) / gui.char_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
1504 gui_position_components(pixel_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 gui_reset_scroll_region();
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001506
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 /*
1508 * At the "more" and ":confirm" prompt there is no redraw, put the cursor
1509 * at the last line here (why does it have to be one row too low?).
1510 */
1511 if (State == ASKMORE || State == CONFIRM)
1512 gui.row = gui.num_rows;
1513
Bram Moolenaar30613902019-12-01 22:11:18 +01001514 // Only comparing Rows and Columns may be sufficient, but let's stay on
1515 // the safe side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 if (gui.num_rows != screen_Rows || gui.num_cols != screen_Columns
1517 || gui.num_rows != Rows || gui.num_cols != Columns)
1518 shell_resized();
1519
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 gui_update_scrollbars(TRUE);
1521 gui_update_cursor(FALSE, TRUE);
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001522#if defined(FEAT_XIM) && !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 xim_set_status_area();
1524#endif
1525
1526 busy = FALSE;
Bram Moolenaare45828b2006-02-15 22:12:56 +00001527
Bram Moolenaar30613902019-12-01 22:11:18 +01001528 // We may have been called again while redrawing the screen.
1529 // Need to do it all again with the latest size then. But only if the size
1530 // actually changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 if (new_pixel_height)
1532 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001533 if (pixel_width == new_pixel_width && pixel_height == new_pixel_height)
1534 {
1535 new_pixel_width = 0;
1536 new_pixel_height = 0;
1537 }
1538 else
1539 {
1540 pixel_width = new_pixel_width;
1541 pixel_height = new_pixel_height;
1542 goto again;
1543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 }
1545}
1546
1547/*
1548 * Check if gui_resize_shell() must be called.
1549 */
1550 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001551gui_may_resize_shell(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 if (new_pixel_height)
Bram Moolenaar30613902019-12-01 22:11:18 +01001554 // careful: gui_resize_shell() may postpone the resize again if we
1555 // were called indirectly by it
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001556 gui_resize_shell(new_pixel_width, new_pixel_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557}
1558
1559 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001560gui_get_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561{
1562 Rows = gui.num_rows;
1563 Columns = gui.num_cols;
1564 return OK;
1565}
1566
1567/*
1568 * Set the size of the Vim shell according to Rows and Columns.
Bram Moolenaar02743632005-07-25 20:42:36 +00001569 * If "fit_to_display" is TRUE then the size may be reduced to fit the window
1570 * on the screen.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001571 * When "mustset" is TRUE the size was set by the user. When FALSE a UI
1572 * component was added or removed (e.g., a scrollbar).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001575gui_set_shellsize(
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001576 int mustset UNUSED,
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001577 int fit_to_display,
Bram Moolenaar30613902019-12-01 22:11:18 +01001578 int direction) // RESIZE_HOR, RESIZE_VER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579{
1580 int base_width;
1581 int base_height;
1582 int width;
1583 int height;
1584 int min_width;
1585 int min_height;
1586 int screen_w;
1587 int screen_h;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001588#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001589 int un_maximize = mustset;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001590 int did_adjust = 0;
Bram Moolenaar09736232009-09-23 16:14:49 +00001591#endif
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001592 int x = -1, y = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594 if (!gui.shell_created)
1595 return;
1596
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02001597#if defined(MSWIN) || defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01001598 // If not setting to a user specified size and maximized, calculate the
1599 // number of characters that fit in the maximized window.
Bram Moolenaar8ac44152017-11-09 18:33:29 +01001600 if (!mustset && (vim_strchr(p_go, GO_KEEPWINSIZE) != NULL
1601 || gui_mch_maximized()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 {
1603 gui_mch_newfont();
1604 return;
1605 }
1606#endif
1607
1608 base_width = gui_get_base_width();
1609 base_height = gui_get_base_height();
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001610 if (fit_to_display)
Bram Moolenaar30613902019-12-01 22:11:18 +01001611 // Remember the original window position.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001612 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001613
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01001614 width = Columns * gui.char_width + base_width;
1615 height = Rows * gui.char_height + base_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
1617 if (fit_to_display)
1618 {
1619 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001620 if ((direction & RESIZE_HOR) && width > screen_w)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
1622 Columns = (screen_w - base_width) / gui.char_width;
1623 if (Columns < MIN_COLUMNS)
1624 Columns = MIN_COLUMNS;
1625 width = Columns * gui.char_width + base_width;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001626#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001627 ++did_adjust;
1628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001630 if ((direction & RESIZE_VERT) && height > screen_h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 {
1632 Rows = (screen_h - base_height) / gui.char_height;
1633 check_shellsize();
1634 height = Rows * gui.char_height + base_height;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001635#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001636 ++did_adjust;
1637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 }
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001639#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001640 if (did_adjust == 2 || (width + gui.char_width >= screen_w
1641 && height + gui.char_height >= screen_h))
Bram Moolenaar30613902019-12-01 22:11:18 +01001642 // don't unmaximize if at maximum size
Bram Moolenaar09736232009-09-23 16:14:49 +00001643 un_maximize = FALSE;
1644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 }
Bram Moolenaare057d402013-06-30 17:51:51 +02001646 limit_screen_size();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 gui.num_cols = Columns;
1648 gui.num_rows = Rows;
1649
1650 min_width = base_width + MIN_COLUMNS * gui.char_width;
1651 min_height = base_height + MIN_LINES * gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001652 min_height += tabline_height() * gui.char_height;
Bram Moolenaar09736232009-09-23 16:14:49 +00001653
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001654#ifdef FEAT_GUI_GTK
Bram Moolenaar09736232009-09-23 16:14:49 +00001655 if (un_maximize)
1656 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001657 // If the window size is smaller than the screen unmaximize the
1658 // window, otherwise resizing won't work.
Bram Moolenaar09736232009-09-23 16:14:49 +00001659 gui_mch_get_screen_dimensions(&screen_w, &screen_h);
1660 if ((width + gui.char_width < screen_w
1661 || height + gui.char_height * 2 < screen_h)
1662 && gui_mch_maximized())
1663 gui_mch_unmaximize();
1664 }
1665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666
1667 gui_mch_set_shellsize(width, height, min_width, min_height,
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00001668 base_width, base_height, direction);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
Bram Moolenaarc5d5d012010-01-27 21:05:05 +01001670 if (fit_to_display && x >= 0 && y >= 0)
1671 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001672 // Some window managers put the Vim window left of/above the screen.
1673 // Only change the position if it wasn't already negative before
1674 // (happens on MS-Windows with a secondary monitor).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 gui_mch_update();
1676 if (gui_mch_get_winpos(&x, &y) == OK && (x < 0 || y < 0))
1677 gui_mch_set_winpos(x < 0 ? 0 : x, y < 0 ? 0 : y);
1678 }
1679
1680 gui_position_components(width);
1681 gui_update_scrollbars(TRUE);
1682 gui_reset_scroll_region();
1683}
1684
1685/*
1686 * Called when Rows and/or Columns has changed.
1687 */
1688 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001689gui_new_shellsize(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
1691 gui_reset_scroll_region();
1692}
1693
1694/*
1695 * Make scroll region cover whole screen.
1696 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001697 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001698gui_reset_scroll_region(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
1700 gui.scroll_region_top = 0;
1701 gui.scroll_region_bot = gui.num_rows - 1;
1702 gui.scroll_region_left = 0;
1703 gui.scroll_region_right = gui.num_cols - 1;
1704}
1705
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001706 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001707gui_start_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708{
Bram Moolenaar30613902019-12-01 22:11:18 +01001709 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 gui.highlight_mask = mask;
Bram Moolenaar30613902019-12-01 22:11:18 +01001711 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 gui.highlight_mask |= mask;
1713}
1714
1715 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001716gui_stop_highlight(int mask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
Bram Moolenaar30613902019-12-01 22:11:18 +01001718 if (mask > HL_ALL) // highlight code
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 gui.highlight_mask = HL_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001720 else // mask
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 gui.highlight_mask &= ~mask;
1722}
1723
1724/*
1725 * Clear a rectangular region of the screen from text pos (row1, col1) to
1726 * (row2, col2) inclusive.
1727 */
1728 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001729gui_clear_block(
1730 int row1,
1731 int col1,
1732 int row2,
1733 int col2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734{
Bram Moolenaar30613902019-12-01 22:11:18 +01001735 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 clip_may_clear_selection(row1, row2);
1737
1738 gui_mch_clear_block(row1, col1, row2, col2);
1739
Bram Moolenaar30613902019-12-01 22:11:18 +01001740 // Invalidate cursor if it was in this block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 if ( gui.cursor_row >= row1 && gui.cursor_row <= row2
1742 && gui.cursor_col >= col1 && gui.cursor_col <= col2)
1743 gui.cursor_is_valid = FALSE;
1744}
1745
1746/*
1747 * Write code to update the cursor later. This avoids the need to flush the
1748 * output buffer before calling gui_update_cursor().
1749 */
1750 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001751gui_update_cursor_later(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001753 OUT_STR(IF_EB("\033|s", ESC_STR "|s"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754}
1755
1756 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001757gui_write(
1758 char_u *s,
1759 int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 char_u *p;
1762 int arg1 = 0, arg2 = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01001763 int force_cursor = FALSE; // force cursor update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 int force_scrollbar = FALSE;
1765 static win_T *old_curwin = NULL;
1766
Bram Moolenaar30613902019-12-01 22:11:18 +01001767// #define DEBUG_GUI_WRITE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#ifdef DEBUG_GUI_WRITE
1769 {
1770 int i;
1771 char_u *str;
1772
1773 printf("gui_write(%d):\n ", len);
1774 for (i = 0; i < len; i++)
1775 if (s[i] == ESC)
1776 {
1777 if (i != 0)
1778 printf("\n ");
1779 printf("<ESC>");
1780 }
1781 else
1782 {
1783 str = transchar_byte(s[i]);
1784 if (str[0] && str[1])
1785 printf("<%s>", (char *)str);
1786 else
1787 printf("%s", (char *)str);
1788 }
1789 printf("\n");
1790 }
1791#endif
1792 while (len)
1793 {
1794 if (s[0] == ESC && s[1] == '|')
1795 {
1796 p = s + 2;
Bram Moolenaar4a6c6702016-07-01 15:48:05 +02001797 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(*(p + 1))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 {
1799 arg1 = getdigits(&p);
1800 if (p > s + len)
1801 break;
1802 if (*p == ';')
1803 {
1804 ++p;
1805 arg2 = getdigits(&p);
1806 if (p > s + len)
1807 break;
1808 }
1809 }
1810 switch (*p)
1811 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001812 case 'C': // Clear screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 clip_scroll_selection(9999);
1814 gui_mch_clear_all();
1815 gui.cursor_is_valid = FALSE;
1816 force_scrollbar = TRUE;
1817 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001818 case 'M': // Move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 gui_set_cursor(arg1, arg2);
1820 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001821 case 's': // force cursor (shape) update
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 force_cursor = TRUE;
1823 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001824 case 'R': // Set scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 if (arg1 < arg2)
1826 {
1827 gui.scroll_region_top = arg1;
1828 gui.scroll_region_bot = arg2;
1829 }
1830 else
1831 {
1832 gui.scroll_region_top = arg2;
1833 gui.scroll_region_bot = arg1;
1834 }
1835 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001836 case 'V': // Set vertical scroll region
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 if (arg1 < arg2)
1838 {
1839 gui.scroll_region_left = arg1;
1840 gui.scroll_region_right = arg2;
1841 }
1842 else
1843 {
1844 gui.scroll_region_left = arg2;
1845 gui.scroll_region_right = arg1;
1846 }
1847 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001848 case 'd': // Delete line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 gui_delete_lines(gui.row, 1);
1850 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001851 case 'D': // Delete lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 gui_delete_lines(gui.row, arg1);
1853 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001854 case 'i': // Insert line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 gui_insert_lines(gui.row, 1);
1856 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001857 case 'I': // Insert lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 gui_insert_lines(gui.row, arg1);
1859 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001860 case '$': // Clear to end-of-line
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 gui_clear_block(gui.row, gui.col, gui.row,
1862 (int)Columns - 1);
1863 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001864 case 'h': // Turn on highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 gui_start_highlight(arg1);
1866 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001867 case 'H': // Turn off highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 gui_stop_highlight(arg1);
1869 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01001870 case 'f': // flash the window (visual bell)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 gui_mch_flash(arg1 == 0 ? 20 : arg1);
1872 break;
1873 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01001874 p = s + 1; // Skip the ESC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 break;
1876 }
1877 len -= (int)(++p - s);
1878 s = p;
1879 }
1880 else if (
1881#ifdef EBCDIC
Bram Moolenaar30613902019-12-01 22:11:18 +01001882 CtrlChar(s[0]) != 0 // Ctrl character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883#else
Bram Moolenaar30613902019-12-01 22:11:18 +01001884 s[0] < 0x20 // Ctrl character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885#endif
1886#ifdef FEAT_SIGN_ICONS
1887 && s[0] != SIGN_BYTE
1888# ifdef FEAT_NETBEANS_INTG
1889 && s[0] != MULTISIGN_BYTE
1890# endif
1891#endif
1892 )
1893 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001894 if (s[0] == '\n') // NL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
1896 gui.col = 0;
1897 if (gui.row < gui.scroll_region_bot)
1898 gui.row++;
1899 else
1900 gui_delete_lines(gui.scroll_region_top, 1);
1901 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001902 else if (s[0] == '\r') // CR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 {
1904 gui.col = 0;
1905 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001906 else if (s[0] == '\b') // Backspace
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
1908 if (gui.col)
1909 --gui.col;
1910 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001911 else if (s[0] == Ctrl_L) // cursor-right
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
1913 ++gui.col;
1914 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001915 else if (s[0] == Ctrl_G) // Beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
1917 gui_mch_beep();
1918 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001919 // Other Ctrl character: shouldn't happen!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
Bram Moolenaar30613902019-12-01 22:11:18 +01001921 --len; // Skip this char
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 ++s;
1923 }
1924 else
1925 {
1926 p = s;
1927 while (len > 0 && (
1928#ifdef EBCDIC
1929 CtrlChar(*p) == 0
1930#else
1931 *p >= 0x20
1932#endif
1933#ifdef FEAT_SIGN_ICONS
1934 || *p == SIGN_BYTE
1935# ifdef FEAT_NETBEANS_INTG
1936 || *p == MULTISIGN_BYTE
1937# endif
1938#endif
1939 ))
1940 {
1941 len--;
1942 p++;
1943 }
1944 gui_outstr(s, (int)(p - s));
1945 s = p;
1946 }
1947 }
1948
Bram Moolenaar30613902019-12-01 22:11:18 +01001949 // Postponed update of the cursor (won't work if "can_update_cursor" isn't
1950 // set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 if (force_cursor)
1952 gui_update_cursor(TRUE, TRUE);
1953
Bram Moolenaar30613902019-12-01 22:11:18 +01001954 // When switching to another window the dragging must have stopped.
1955 // Required for GTK, dragged_sb isn't reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (old_curwin != curwin)
1957 gui.dragged_sb = SBAR_NONE;
1958
Bram Moolenaar30613902019-12-01 22:11:18 +01001959 // Update the scrollbars after clearing the screen or when switched
1960 // to another window.
1961 // Update the horizontal scrollbar always, it's difficult to check all
1962 // situations where it might change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 if (force_scrollbar || old_curwin != curwin)
1964 gui_update_scrollbars(force_scrollbar);
1965 else
1966 gui_update_horiz_scrollbar(FALSE);
1967 old_curwin = curwin;
1968
1969 /*
1970 * We need to make sure this is cleared since Athena doesn't tell us when
1971 * he is done dragging. Do the same for GTK.
1972 */
Bram Moolenaar9372a112005-12-06 19:59:18 +00001973#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 gui.dragged_sb = SBAR_NONE;
1975#endif
1976
Bram Moolenaar30613902019-12-01 22:11:18 +01001977 gui_may_flush(); // In case vim decides to take a nap
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978}
1979
1980/*
1981 * When ScreenLines[] is invalid, updating the cursor should not be done, it
1982 * produces wrong results. Call gui_dont_update_cursor() before that code and
1983 * gui_can_update_cursor() afterwards.
1984 */
1985 void
Bram Moolenaar107abd22016-08-12 14:08:25 +02001986gui_dont_update_cursor(int undraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
1988 if (gui.in_use)
1989 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001990 // Undraw the cursor now, we probably can't do it after the change.
Bram Moolenaar107abd22016-08-12 14:08:25 +02001991 if (undraw)
1992 gui_undraw_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 can_update_cursor = FALSE;
1994 }
1995}
1996
1997 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001998gui_can_update_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999{
2000 can_update_cursor = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01002001 // No need to update the cursor right now, there is always more output
2002 // after scrolling.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003}
2004
Bram Moolenaara338adc2018-01-31 20:51:47 +01002005/*
2006 * Disable issuing gui_mch_flush().
2007 */
2008 void
2009gui_disable_flush(void)
2010{
2011 ++disable_flush;
2012}
2013
2014/*
2015 * Enable issuing gui_mch_flush().
2016 */
2017 void
2018gui_enable_flush(void)
2019{
2020 --disable_flush;
2021}
2022
2023/*
2024 * Issue gui_mch_flush() if it is not disabled.
2025 */
2026 void
2027gui_may_flush(void)
2028{
2029 if (disable_flush == 0)
2030 gui_mch_flush();
2031}
2032
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002034gui_outstr(char_u *s, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
2036 int this_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 int cells;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038
2039 if (len == 0)
2040 return;
2041
2042 if (len < 0)
2043 len = (int)STRLEN(s);
2044
2045 while (len > 0)
2046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 if (has_mbyte)
2048 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002049 // Find out how many chars fit in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 cells = 0;
2051 for (this_len = 0; this_len < len; )
2052 {
2053 cells += (*mb_ptr2cells)(s + this_len);
2054 if (gui.col + cells > Columns)
2055 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002056 this_len += (*mb_ptr2len)(s + this_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
2058 if (this_len > len)
Bram Moolenaar30613902019-12-01 22:11:18 +01002059 this_len = len; // don't include following composing char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 }
2061 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (gui.col + len > Columns)
2063 this_len = Columns - gui.col;
2064 else
2065 this_len = len;
2066
2067 (void)gui_outstr_nowrap(s, this_len,
2068 0, (guicolor_T)0, (guicolor_T)0, 0);
2069 s += this_len;
2070 len -= this_len;
Bram Moolenaar30613902019-12-01 22:11:18 +01002071 // fill up for a double-width char that doesn't fit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 if (len > 0 && gui.col < Columns)
2073 (void)gui_outstr_nowrap((char_u *)" ", 1,
2074 0, (guicolor_T)0, (guicolor_T)0, 0);
Bram Moolenaar30613902019-12-01 22:11:18 +01002075 // The cursor may wrap to the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 if (gui.col >= Columns)
2077 {
2078 gui.col = 0;
2079 gui.row++;
2080 }
2081 }
2082}
2083
2084/*
2085 * Output one character (may be one or two display cells).
2086 * Caller must check for valid "off".
2087 * Returns FAIL or OK, just like gui_outstr_nowrap().
2088 */
2089 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002090gui_screenchar(
Bram Moolenaar30613902019-12-01 22:11:18 +01002091 int off, // Offset from start of screen
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002092 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002093 guicolor_T fg, // colors for cursor
2094 guicolor_T bg, // colors for cursor
2095 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 char_u buf[MB_MAXBYTES + 1];
2098
Bram Moolenaar30613902019-12-01 22:11:18 +01002099 // Don't draw right halve of a double-width UTF-8 char. "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 if (enc_utf8 && ScreenLines[off] == 0)
2101 return OK;
2102
2103 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002104 // Draw UTF-8 multi-byte character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 return gui_outstr_nowrap(buf, utfc_char2bytes(off, buf),
2106 flags, fg, bg, back);
2107
2108 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2109 {
2110 buf[0] = ScreenLines[off];
2111 buf[1] = ScreenLines2[off];
2112 return gui_outstr_nowrap(buf, 2, flags, fg, bg, back);
2113 }
2114
Bram Moolenaar30613902019-12-01 22:11:18 +01002115 // Draw non-multi-byte character or DBCS character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 return gui_outstr_nowrap(ScreenLines + off,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002117 enc_dbcs ? (*mb_ptr2len)(ScreenLines + off) : 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 flags, fg, bg, back);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119}
2120
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002121#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122/*
2123 * Output the string at the given screen position. This is used in place
2124 * of gui_screenchar() where possible because Pango needs as much context
2125 * as possible to work nicely. It's a lot faster as well.
2126 */
2127 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002128gui_screenstr(
Bram Moolenaar30613902019-12-01 22:11:18 +01002129 int off, // Offset from start of screen
2130 int len, // string length in screen cells
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002131 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002132 guicolor_T fg, // colors for cursor
2133 guicolor_T bg, // colors for cursor
2134 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135{
2136 char_u *buf;
2137 int outlen = 0;
2138 int i;
2139 int retval;
2140
Bram Moolenaar30613902019-12-01 22:11:18 +01002141 if (len <= 0) // "cannot happen"?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 return OK;
2143
2144 if (enc_utf8)
2145 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002146 buf = alloc(len * MB_MAXBYTES + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002148 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
2150 for (i = off; i < off + len; ++i)
2151 {
2152 if (ScreenLines[i] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002153 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
2155 if (ScreenLinesUC[i] == 0)
2156 buf[outlen++] = ScreenLines[i];
2157 else
2158 outlen += utfc_char2bytes(i, buf + outlen);
2159 }
2160
Bram Moolenaar30613902019-12-01 22:11:18 +01002161 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2163 vim_free(buf);
2164
2165 return retval;
2166 }
2167 else if (enc_dbcs == DBCS_JPNU)
2168 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002169 buf = alloc(len * 2 + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 if (buf == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002171 return OK; // not much we could do here...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172
2173 for (i = off; i < off + len; ++i)
2174 {
2175 buf[outlen++] = ScreenLines[i];
2176
Bram Moolenaar30613902019-12-01 22:11:18 +01002177 // handle double-byte single-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 if (ScreenLines[i] == 0x8e)
2179 buf[outlen++] = ScreenLines2[i];
2180 else if (MB_BYTE2LEN(ScreenLines[i]) == 2)
2181 buf[outlen++] = ScreenLines[++i];
2182 }
2183
Bram Moolenaar30613902019-12-01 22:11:18 +01002184 buf[outlen] = NUL; // only to aid debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 retval = gui_outstr_nowrap(buf, outlen, flags, fg, bg, back);
2186 vim_free(buf);
2187
2188 return retval;
2189 }
2190 else
2191 {
2192 return gui_outstr_nowrap(&ScreenLines[off], len,
2193 flags, fg, bg, back);
2194 }
2195}
Bram Moolenaar30613902019-12-01 22:11:18 +01002196#endif // FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
2198/*
2199 * Output the given string at the current cursor position. If the string is
2200 * too long to fit on the line, then it is truncated.
2201 * "flags":
2202 * GUI_MON_IS_CURSOR should only be used when this function is being called to
2203 * actually draw (an inverted) cursor.
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002204 * GUI_MON_TRS_CURSOR is used to draw the cursor text with a transparent
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 * background.
2206 * GUI_MON_NOCLEAR is used to avoid clearing the selection when drawing over
2207 * it.
2208 * Returns OK, unless "back" is non-zero and using the bold trick, then return
2209 * FAIL (the caller should start drawing "back" chars back).
2210 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002211 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002212gui_outstr_nowrap(
2213 char_u *s,
2214 int len,
2215 int flags,
Bram Moolenaar30613902019-12-01 22:11:18 +01002216 guicolor_T fg, // colors for cursor
2217 guicolor_T bg, // colors for cursor
2218 int back) // backup this many chars when using bold trick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219{
2220 long_u highlight_mask;
2221 long_u hl_mask_todo;
2222 guicolor_T fg_color;
2223 guicolor_T bg_color;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002224 guicolor_T sp_color;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002225#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 GuiFont font = NOFONT;
Bram Moolenaardb250522013-06-17 22:43:25 +02002227 GuiFont wide_font = NOFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228# ifdef FEAT_XFONTSET
2229 GuiFontset fontset = NOFONTSET;
2230# endif
2231#endif
2232 attrentry_T *aep = NULL;
2233 int draw_flags;
2234 int col = gui.col;
2235#ifdef FEAT_SIGN_ICONS
2236 int draw_sign = FALSE;
Bram Moolenaarc7283072019-07-16 20:12:44 +02002237 int signcol = 0;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002238 char_u extra[18];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239# ifdef FEAT_NETBEANS_INTG
2240 int multi_sign = FALSE;
2241# endif
2242#endif
2243
2244 if (len < 0)
2245 len = (int)STRLEN(s);
2246 if (len == 0)
2247 return OK;
2248
2249#ifdef FEAT_SIGN_ICONS
2250 if (*s == SIGN_BYTE
2251# ifdef FEAT_NETBEANS_INTG
2252 || *s == MULTISIGN_BYTE
2253# endif
Bram Moolenaar0231f832019-07-12 19:22:22 +02002254 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 {
2256# ifdef FEAT_NETBEANS_INTG
2257 if (*s == MULTISIGN_BYTE)
2258 multi_sign = TRUE;
2259# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002260 // draw spaces instead
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002261 if (*curwin->w_p_scl == 'n' && *(curwin->w_p_scl + 1) == 'u' &&
2262 (curwin->w_p_nu || curwin->w_p_rnu))
2263 {
2264 sprintf((char *)extra, "%*c ", number_width(curwin), ' ');
2265 s = extra;
2266 }
2267 else
2268 s = (char_u *)" ";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 if (len == 1 && col > 0)
2270 --col;
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02002271 len = (int)STRLEN(s);
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002272 if (len > 2)
Bram Moolenaar0231f832019-07-12 19:22:22 +02002273 // right align sign icon in the number column
2274 signcol = col + len - 3;
2275 else
2276 signcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 draw_sign = TRUE;
2278 highlight_mask = 0;
2279 }
2280 else
2281#endif
2282 if (gui.highlight_mask > HL_ALL)
2283 {
2284 aep = syn_gui_attr2entry(gui.highlight_mask);
Bram Moolenaar30613902019-12-01 22:11:18 +01002285 if (aep == NULL) // highlighting not set
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 highlight_mask = 0;
2287 else
2288 highlight_mask = aep->ae_attr;
2289 }
2290 else
2291 highlight_mask = gui.highlight_mask;
2292 hl_mask_todo = highlight_mask;
2293
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002294#if !defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002295 // Set the font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 if (aep != NULL && aep->ae_u.gui.font != NOFONT)
2297 font = aep->ae_u.gui.font;
2298# ifdef FEAT_XFONTSET
2299 else if (aep != NULL && aep->ae_u.gui.fontset != NOFONTSET)
2300 fontset = aep->ae_u.gui.fontset;
2301# endif
2302 else
2303 {
2304# ifdef FEAT_XFONTSET
2305 if (gui.fontset != NOFONTSET)
2306 fontset = gui.fontset;
2307 else
2308# endif
2309 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2310 {
2311 if ((hl_mask_todo & HL_ITALIC) && gui.boldital_font != NOFONT)
2312 {
2313 font = gui.boldital_font;
2314 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT | HL_ITALIC);
2315 }
2316 else if (gui.bold_font != NOFONT)
2317 {
2318 font = gui.bold_font;
2319 hl_mask_todo &= ~(HL_BOLD | HL_STANDOUT);
2320 }
2321 else
2322 font = gui.norm_font;
2323 }
2324 else if ((hl_mask_todo & HL_ITALIC) && gui.ital_font != NOFONT)
2325 {
2326 font = gui.ital_font;
2327 hl_mask_todo &= ~HL_ITALIC;
2328 }
2329 else
2330 font = gui.norm_font;
Bram Moolenaardb250522013-06-17 22:43:25 +02002331
Bram Moolenaardb250522013-06-17 22:43:25 +02002332 /*
2333 * Choose correct wide_font by font. wide_font should be set with font
2334 * at same time in above block. But it will make many "ifdef" nasty
2335 * blocks. So we do it here.
2336 */
2337 if (font == gui.boldital_font && gui.wide_boldital_font)
2338 wide_font = gui.wide_boldital_font;
2339 else if (font == gui.bold_font && gui.wide_bold_font)
2340 wide_font = gui.wide_bold_font;
2341 else if (font == gui.ital_font && gui.wide_ital_font)
2342 wide_font = gui.wide_ital_font;
2343 else if (font == gui.norm_font && gui.wide_font)
2344 wide_font = gui.wide_font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346# ifdef FEAT_XFONTSET
2347 if (fontset != NOFONTSET)
2348 gui_mch_set_fontset(fontset);
2349 else
2350# endif
2351 gui_mch_set_font(font);
2352#endif
2353
2354 draw_flags = 0;
2355
Bram Moolenaar30613902019-12-01 22:11:18 +01002356 // Set the color
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 bg_color = gui.back_pixel;
2358 if ((flags & GUI_MON_IS_CURSOR) && gui.in_focus)
2359 {
2360 draw_flags |= DRAW_CURSOR;
2361 fg_color = fg;
2362 bg_color = bg;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002363 sp_color = fg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 }
2365 else if (aep != NULL)
2366 {
2367 fg_color = aep->ae_u.gui.fg_color;
2368 if (fg_color == INVALCOLOR)
2369 fg_color = gui.norm_pixel;
2370 bg_color = aep->ae_u.gui.bg_color;
2371 if (bg_color == INVALCOLOR)
2372 bg_color = gui.back_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002373 sp_color = aep->ae_u.gui.sp_color;
2374 if (sp_color == INVALCOLOR)
2375 sp_color = fg_color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
2377 else
Bram Moolenaar3918c952005-03-15 22:34:55 +00002378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 fg_color = gui.norm_pixel;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002380 sp_color = fg_color;
2381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382
2383 if (highlight_mask & (HL_INVERSE | HL_STANDOUT))
2384 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002385#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 gui_mch_set_colors(bg_color, fg_color);
2387#else
2388 gui_mch_set_fg_color(bg_color);
2389 gui_mch_set_bg_color(fg_color);
2390#endif
2391 }
2392 else
2393 {
Bram Moolenaare60acc12011-05-10 16:41:25 +02002394#if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 gui_mch_set_colors(fg_color, bg_color);
2396#else
2397 gui_mch_set_fg_color(fg_color);
2398 gui_mch_set_bg_color(bg_color);
2399#endif
2400 }
Bram Moolenaar3918c952005-03-15 22:34:55 +00002401 gui_mch_set_sp_color(sp_color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
Bram Moolenaar30613902019-12-01 22:11:18 +01002403 // Clear the selection if we are about to write over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 if (!(flags & GUI_MON_NOCLEAR))
2405 clip_may_clear_selection(gui.row, gui.row);
2406
2407
Bram Moolenaar30613902019-12-01 22:11:18 +01002408 // If there's no bold font, then fake it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 if (hl_mask_todo & (HL_BOLD | HL_STANDOUT))
2410 draw_flags |= DRAW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
2412 /*
2413 * When drawing bold or italic characters the spill-over from the left
2414 * neighbor may be destroyed. Let the caller backup to start redrawing
2415 * just after a blank.
2416 */
2417 if (back != 0 && ((draw_flags & DRAW_BOLD) || (highlight_mask & HL_ITALIC)))
2418 return FAIL;
2419
Bram Moolenaare60acc12011-05-10 16:41:25 +02002420#if defined(FEAT_GUI_GTK)
Bram Moolenaar30613902019-12-01 22:11:18 +01002421 // If there's no italic font, then fake it.
2422 // For GTK2, we don't need a different font for italic style.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 if (hl_mask_todo & HL_ITALIC)
2424 draw_flags |= DRAW_ITALIC;
2425
Bram Moolenaar30613902019-12-01 22:11:18 +01002426 // Do we underline the text?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 if (hl_mask_todo & HL_UNDERLINE)
2428 draw_flags |= DRAW_UNDERL;
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002429
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430#else
Bram Moolenaar30613902019-12-01 22:11:18 +01002431 // Do we underline the text?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002432 if ((hl_mask_todo & HL_UNDERLINE) || (hl_mask_todo & HL_ITALIC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 draw_flags |= DRAW_UNDERL;
2434#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01002435 // Do we undercurl the text?
Bram Moolenaar3918c952005-03-15 22:34:55 +00002436 if (hl_mask_todo & HL_UNDERCURL)
2437 draw_flags |= DRAW_UNDERC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
Bram Moolenaar30613902019-12-01 22:11:18 +01002439 // Do we strikethrough the text?
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02002440 if (hl_mask_todo & HL_STRIKETHROUGH)
2441 draw_flags |= DRAW_STRIKE;
2442
Bram Moolenaar30613902019-12-01 22:11:18 +01002443 // Do we draw transparently?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 if (flags & GUI_MON_TRS_CURSOR)
2445 draw_flags |= DRAW_TRANSP;
2446
2447 /*
2448 * Draw the text.
2449 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002450#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01002451 // The value returned is the length in display cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 len = gui_gtk2_draw_string(gui.row, col, s, len, draw_flags);
2453#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 if (enc_utf8)
2455 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002456 int start; // index of bytes to be drawn
2457 int cells; // cellwidth of bytes to be drawn
2458 int thislen; // length of bytes to be drawn
2459 int cn; // cellwidth of current char
2460 int i; // index of current char
2461 int c; // current char value
2462 int cl; // byte length of current char
2463 int comping; // current char is composing
2464 int scol = col; // screen column
2465 int curr_wide = FALSE; // use 'guifontwide'
Bram Moolenaar45933962013-01-23 17:43:57 +01002466 int prev_wide = FALSE;
2467 int wide_changed;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002468# ifdef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01002469 int sep_comp = FALSE; // Don't separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002470# else
Bram Moolenaar30613902019-12-01 22:11:18 +01002471 int sep_comp = TRUE; // Separate composing chars.
Bram Moolenaar13505972019-01-24 15:04:48 +01002472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
Bram Moolenaar30613902019-12-01 22:11:18 +01002474 // Break the string at a composing character, it has to be drawn on
2475 // top of the previous character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 start = 0;
2477 cells = 0;
2478 for (i = 0; i < len; i += cl)
2479 {
2480 c = utf_ptr2char(s + i);
2481 cn = utf_char2cells(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 comping = utf_iscomposing(c);
Bram Moolenaar30613902019-12-01 22:11:18 +01002483 if (!comping) // count cells from non-composing chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 cells += cn;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002485 if (!comping || sep_comp)
2486 {
2487 if (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002488# ifdef FEAT_XFONTSET
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002489 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002490# endif
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002491 && wide_font != NOFONT)
2492 curr_wide = TRUE;
2493 else
2494 curr_wide = FALSE;
2495 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002496 cl = utf_ptr2len(s + i);
Bram Moolenaar30613902019-12-01 22:11:18 +01002497 if (cl == 0) // hit end of string
2498 len = i + cl; // len must be wrong "cannot happen"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
Bram Moolenaar45933962013-01-23 17:43:57 +01002500 wide_changed = curr_wide != prev_wide;
2501
Bram Moolenaar30613902019-12-01 22:11:18 +01002502 // Print the string so far if it's the last character or there is
2503 // a composing character.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002504 if (i + cl >= len || (comping && sep_comp && i > start)
2505 || wide_changed
Bram Moolenaar13505972019-01-24 15:04:48 +01002506# if defined(FEAT_GUI_X11)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 || (cn > 1
Bram Moolenaar13505972019-01-24 15:04:48 +01002508# ifdef FEAT_XFONTSET
Bram Moolenaar30613902019-12-01 22:11:18 +01002509 // No fontset: At least draw char after wide char at
2510 // right position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 && fontset == NOFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512# endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002513 )
2514# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 )
2516 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002517 if ((comping && sep_comp) || wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 thislen = i - start;
2519 else
2520 thislen = i - start + cl;
2521 if (thislen > 0)
2522 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002523 if (prev_wide)
Bram Moolenaardb250522013-06-17 22:43:25 +02002524 gui_mch_set_font(wide_font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 gui_mch_draw_string(gui.row, scol, s + start, thislen,
2526 draw_flags);
Bram Moolenaar45933962013-01-23 17:43:57 +01002527 if (prev_wide)
2528 gui_mch_set_font(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 start += thislen;
2530 }
2531 scol += cells;
2532 cells = 0;
Bram Moolenaar30613902019-12-01 22:11:18 +01002533 // Adjust to not draw a character which width is changed
2534 // against with last one.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002535 if (wide_changed && !(comping && sep_comp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
Bram Moolenaar45933962013-01-23 17:43:57 +01002537 scol -= cn;
2538 cl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 }
2540
Bram Moolenaar13505972019-01-24 15:04:48 +01002541# if defined(FEAT_GUI_X11)
Bram Moolenaar30613902019-12-01 22:11:18 +01002542 // No fontset: draw a space to fill the gap after a wide char
2543 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 if (cn > 1 && (draw_flags & DRAW_TRANSP) == 0
Bram Moolenaar13505972019-01-24 15:04:48 +01002545# ifdef FEAT_XFONTSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 && fontset == NOFONTSET
Bram Moolenaar13505972019-01-24 15:04:48 +01002547# endif
Bram Moolenaar45933962013-01-23 17:43:57 +01002548 && !wide_changed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 gui_mch_draw_string(gui.row, scol - 1, (char_u *)" ",
2550 1, draw_flags);
Bram Moolenaar13505972019-01-24 15:04:48 +01002551# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002553 // Draw a composing char on top of the previous char.
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02002554 if (comping && sep_comp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
Bram Moolenaar13505972019-01-24 15:04:48 +01002556# if defined(__APPLE_CC__) && TARGET_API_MAC_CARBON
Bram Moolenaar30613902019-12-01 22:11:18 +01002557 // Carbon ATSUI autodraws composing char over previous char
Bram Moolenaarfd91ecb2005-03-07 23:06:25 +00002558 gui_mch_draw_string(gui.row, scol, s + i, cl,
2559 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002560# else
Bram Moolenaarf25fd512005-09-30 21:15:37 +00002561 gui_mch_draw_string(gui.row, scol - cn, s + i, cl,
2562 draw_flags | DRAW_TRANSP);
Bram Moolenaar13505972019-01-24 15:04:48 +01002563# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 start = i + cl;
2565 }
Bram Moolenaar45933962013-01-23 17:43:57 +01002566 prev_wide = curr_wide;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002568 // The stuff below assumes "len" is the length in screen columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 len = scol - col;
2570 }
2571 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
2573 gui_mch_draw_string(gui.row, col, s, len, draw_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 if (enc_dbcs == DBCS_JPNU)
2575 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002576 // Get the length in display cells, this can be different from the
2577 // number of bytes for "euc-jp".
Bram Moolenaar72597a52010-07-18 15:31:08 +02002578 len = mb_string2cells(s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002581#endif // !FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
2583 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2584 gui.col = col + len;
2585
Bram Moolenaar30613902019-12-01 22:11:18 +01002586 // May need to invert it when it's part of the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 if (flags & GUI_MON_NOCLEAR)
2588 clip_may_redraw_selection(gui.row, col, len);
2589
2590 if (!(flags & (GUI_MON_IS_CURSOR | GUI_MON_TRS_CURSOR)))
2591 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002592 // Invalidate the old physical cursor position if we wrote over it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 if (gui.cursor_row == gui.row
2594 && gui.cursor_col >= col
2595 && gui.cursor_col < col + len)
2596 gui.cursor_is_valid = FALSE;
2597 }
2598
2599#ifdef FEAT_SIGN_ICONS
2600 if (draw_sign)
Bram Moolenaar30613902019-12-01 22:11:18 +01002601 // Draw the sign on top of the spaces.
Bram Moolenaar2b044ff2019-06-24 05:45:14 +02002602 gui_mch_drawsign(gui.row, signcol, gui.highlight_mask);
Bram Moolenaar173c9852010-09-29 17:27:01 +02002603# if defined(FEAT_NETBEANS_INTG) && (defined(FEAT_GUI_X11) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01002604 || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 if (multi_sign)
2606 netbeans_draw_multisign_indicator(gui.row);
2607# endif
2608#endif
2609
2610 return OK;
2611}
2612
2613/*
2614 * Un-draw the cursor. Actually this just redraws the character at the given
2615 * position. The character just before it too, for when it was in bold.
2616 */
2617 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002618gui_undraw_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619{
2620 if (gui.cursor_is_valid)
2621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 if (gui_redraw_block(gui.cursor_row, gui.cursor_col,
2623 gui.cursor_row, gui.cursor_col, GUI_MON_NOCLEAR)
2624 && gui.cursor_col > 0)
2625 (void)gui_redraw_block(gui.cursor_row, gui.cursor_col - 1,
2626 gui.cursor_row, gui.cursor_col - 1, GUI_MON_NOCLEAR);
Bram Moolenaar54612582019-11-21 17:13:31 +01002627 // Cursor_is_valid is reset when the cursor is undrawn, also reset it
2628 // here in case it wasn't needed to undraw it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 gui.cursor_is_valid = FALSE;
2630 }
2631}
2632
2633 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002634gui_redraw(
2635 int x,
2636 int y,
2637 int w,
2638 int h)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639{
2640 int row1, col1, row2, col2;
2641
2642 row1 = Y_2_ROW(y);
2643 col1 = X_2_COL(x);
2644 row2 = Y_2_ROW(y + h - 1);
2645 col2 = X_2_COL(x + w - 1);
2646
2647 (void)gui_redraw_block(row1, col1, row2, col2, GUI_MON_NOCLEAR);
2648
2649 /*
2650 * We may need to redraw the cursor, but don't take it upon us to change
2651 * its location after a scroll.
2652 * (maybe be more strict even and test col too?)
2653 * These things may be outside the update/clipping region and reality may
2654 * not reflect Vims internal ideas if these operations are clipped away.
2655 */
2656 if (gui.row == gui.cursor_row)
2657 gui_update_cursor(TRUE, TRUE);
2658}
2659
2660/*
2661 * Draw a rectangular block of characters, from row1 to row2 (inclusive) and
2662 * from col1 to col2 (inclusive).
2663 * Return TRUE when the character before the first drawn character has
2664 * different attributes (may have to be redrawn too).
2665 */
2666 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002667gui_redraw_block(
2668 int row1,
2669 int col1,
2670 int row2,
2671 int col2,
Bram Moolenaar30613902019-12-01 22:11:18 +01002672 int flags) // flags for gui_outstr_nowrap()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 int old_row, old_col;
2675 long_u old_hl_mask;
2676 int off;
Bram Moolenaar3918c952005-03-15 22:34:55 +00002677 sattr_T first_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 int idx, len;
2679 int back, nback;
2680 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 int orig_col1, orig_col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
Bram Moolenaar30613902019-12-01 22:11:18 +01002683 // Don't try to update when ScreenLines is not valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 if (!screen_cleared || ScreenLines == NULL)
2685 return retval;
2686
Bram Moolenaar30613902019-12-01 22:11:18 +01002687 // Don't try to draw outside the shell!
2688 // Check everything, strange values may be caused by a big border width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 col1 = check_col(col1);
2690 col2 = check_col(col2);
2691 row1 = check_row(row1);
2692 row2 = check_row(row2);
2693
Bram Moolenaar30613902019-12-01 22:11:18 +01002694 // Remember where our cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 old_row = gui.row;
2696 old_col = gui.col;
2697 old_hl_mask = gui.highlight_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 orig_col1 = col1;
2699 orig_col2 = col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 for (gui.row = row1; gui.row <= row2; gui.row++)
2702 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002703 // When only half of a double-wide character is in the block, include
2704 // the other half.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 col1 = orig_col1;
2706 col2 = orig_col2;
2707 off = LineOffset[gui.row];
2708 if (enc_dbcs != 0)
2709 {
2710 if (col1 > 0)
2711 col1 -= dbcs_screen_head_off(ScreenLines + off,
2712 ScreenLines + off + col1);
2713 col2 += dbcs_screen_tail_off(ScreenLines + off,
2714 ScreenLines + off + col2);
2715 }
2716 else if (enc_utf8)
2717 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002718 if (ScreenLines[off + col1] == 0)
2719 {
2720 if (col1 > 0)
2721 --col1;
2722 else
Bram Moolenaar9a853462018-12-07 14:10:37 +01002723 {
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002724 // FIXME: how can the first character ever be zero?
Bram Moolenaar9a853462018-12-07 14:10:37 +01002725 // Make this IEMSGN when it no longer breaks Travis CI.
2726 vim_snprintf((char *)IObuff, IOSIZE,
2727 "INTERNAL ERROR: NUL in ScreenLines in row %ld",
2728 (long)gui.row);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002729 msg((char *)IObuff);
Bram Moolenaar9a853462018-12-07 14:10:37 +01002730 }
Bram Moolenaar4087bfd2018-12-07 13:26:39 +01002731 }
Bram Moolenaar13505972019-01-24 15:04:48 +01002732#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 if (col2 + 1 < Columns && ScreenLines[off + col2 + 1] == 0)
2734 ++col2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01002736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 gui.col = col1;
2738 off = LineOffset[gui.row] + gui.col;
2739 len = col2 - col1 + 1;
2740
Bram Moolenaar30613902019-12-01 22:11:18 +01002741 // Find how many chars back this highlighting starts, or where a space
2742 // is. Needed for when the bold trick is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 for (back = 0; back < col1; ++back)
2744 if (ScreenAttrs[off - 1 - back] != ScreenAttrs[off]
2745 || ScreenLines[off - 1 - back] == ' ')
2746 break;
2747 retval = (col1 > 0 && ScreenAttrs[off - 1] != 0 && back == 0
2748 && ScreenLines[off - 1] != ' ');
2749
Bram Moolenaar30613902019-12-01 22:11:18 +01002750 // Break it up in strings of characters with the same attributes.
2751 // Print UTF-8 characters individually.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 while (len > 0)
2753 {
2754 first_attr = ScreenAttrs[off];
2755 gui.highlight_mask = first_attr;
Bram Moolenaar13505972019-01-24 15:04:48 +01002756#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 if (enc_utf8 && ScreenLinesUC[off] != 0)
2758 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002759 // output multi-byte character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 nback = gui_screenchar(off, flags,
2761 (guicolor_T)0, (guicolor_T)0, back);
2762 if (gui.col < Columns && ScreenLines[off + 1] == 0)
2763 idx = 2;
2764 else
2765 idx = 1;
2766 }
2767 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
2768 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002769 // output double-byte, single-width character separately
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 nback = gui_screenchar(off, flags,
2771 (guicolor_T)0, (guicolor_T)0, back);
2772 idx = 1;
2773 }
2774 else
2775#endif
2776 {
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02002777#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 for (idx = 0; idx < len; ++idx)
2779 {
2780 if (enc_utf8 && ScreenLines[off + idx] == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002781 continue; // skip second half of double-width char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 if (ScreenAttrs[off + idx] != first_attr)
2783 break;
2784 }
Bram Moolenaar30613902019-12-01 22:11:18 +01002785 // gui_screenstr() takes care of multibyte chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 nback = gui_screenstr(off, idx, flags,
2787 (guicolor_T)0, (guicolor_T)0, back);
2788#else
2789 for (idx = 0; idx < len && ScreenAttrs[off + idx] == first_attr;
2790 idx++)
2791 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002792 // Stop at a multi-byte Unicode character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 if (enc_utf8 && ScreenLinesUC[off + idx] != 0)
2794 break;
2795 if (enc_dbcs == DBCS_JPNU)
2796 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002797 // Stop at a double-byte single-width char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 if (ScreenLines[off + idx] == 0x8e)
2799 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002800 if (len > 1 && (*mb_ptr2len)(ScreenLines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 + off + idx) == 2)
Bram Moolenaar30613902019-12-01 22:11:18 +01002802 ++idx; // skip second byte of double-byte char
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 }
2805 nback = gui_outstr_nowrap(ScreenLines + off, idx, flags,
2806 (guicolor_T)0, (guicolor_T)0, back);
2807#endif
2808 }
2809 if (nback == FAIL)
2810 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002811 // Must back up to start drawing where a bold or italic word
2812 // starts.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 off -= back;
2814 len += back;
2815 gui.col -= back;
2816 }
2817 else
2818 {
2819 off += idx;
2820 len -= idx;
2821 }
2822 back = 0;
2823 }
2824 }
2825
Bram Moolenaar30613902019-12-01 22:11:18 +01002826 // Put the cursor back where it was
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 gui.row = old_row;
2828 gui.col = old_col;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002829 gui.highlight_mask = (int)old_hl_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
2831 return retval;
2832}
2833
2834 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002835gui_delete_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
2837 if (count <= 0)
2838 return;
2839
2840 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002841 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 gui_clear_block(row, gui.scroll_region_left,
2843 gui.scroll_region_bot, gui.scroll_region_right);
2844 else
2845 {
2846 gui_mch_delete_lines(row, count);
2847
Bram Moolenaar30613902019-12-01 22:11:18 +01002848 // If the cursor was in the deleted lines it's now gone. If the
2849 // cursor was in the scrolled lines adjust its position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 if (gui.cursor_row >= row
2851 && gui.cursor_col >= gui.scroll_region_left
2852 && gui.cursor_col <= gui.scroll_region_right)
2853 {
2854 if (gui.cursor_row < row + count)
2855 gui.cursor_is_valid = FALSE;
2856 else if (gui.cursor_row <= gui.scroll_region_bot)
2857 gui.cursor_row -= count;
2858 }
2859 }
2860}
2861
2862 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002863gui_insert_lines(int row, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
2865 if (count <= 0)
2866 return;
2867
2868 if (row + count > gui.scroll_region_bot)
Bram Moolenaar30613902019-12-01 22:11:18 +01002869 // Scrolled out of region, just blank the lines out
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 gui_clear_block(row, gui.scroll_region_left,
2871 gui.scroll_region_bot, gui.scroll_region_right);
2872 else
2873 {
2874 gui_mch_insert_lines(row, count);
2875
2876 if (gui.cursor_row >= gui.row
2877 && gui.cursor_col >= gui.scroll_region_left
2878 && gui.cursor_col <= gui.scroll_region_right)
2879 {
2880 if (gui.cursor_row <= gui.scroll_region_bot - count)
2881 gui.cursor_row += count;
2882 else if (gui.cursor_row <= gui.scroll_region_bot)
2883 gui.cursor_is_valid = FALSE;
2884 }
2885 }
2886}
2887
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002888#ifdef FEAT_TIMERS
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002889/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002890 * Passed to ui_wait_for_chars_or_timer(), ignoring extra arguments.
2891 */
2892 static int
2893gui_wait_for_chars_3(
2894 long wtime,
2895 int *interrupted UNUSED,
2896 int ignore_input UNUSED)
2897{
2898 return gui_mch_wait_for_chars(wtime);
2899}
Bram Moolenaar4ce46c22017-12-19 19:42:41 +01002900#endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002901
2902/*
Bram Moolenaar9472eec2017-06-05 13:31:56 +02002903 * Returns OK if a character was found to be available within the given time,
2904 * or FAIL otherwise.
2905 */
Bram Moolenaar975b5272016-03-15 23:10:59 +01002906 static int
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002907gui_wait_for_chars_or_timer(
2908 long wtime,
2909 int *interrupted UNUSED,
2910 int ignore_input UNUSED)
Bram Moolenaar975b5272016-03-15 23:10:59 +01002911{
2912#ifdef FEAT_TIMERS
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002913 return ui_wait_for_chars_or_timer(wtime, gui_wait_for_chars_3,
2914 interrupted, ignore_input);
Bram Moolenaar975b5272016-03-15 23:10:59 +01002915#else
2916 return gui_mch_wait_for_chars(wtime);
2917#endif
2918}
2919
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920/*
2921 * The main GUI input routine. Waits for a character from the keyboard.
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002922 * "wtime" == -1 Wait forever.
2923 * "wtime" == 0 Don't wait.
2924 * "wtime" > 0 Wait wtime milliseconds for a character.
2925 *
2926 * Returns the number of characters read or zero when timed out or interrupted.
2927 * "buf" may be NULL, in which case a non-zero number is returned if characters
2928 * are available.
2929 */
2930 static int
2931gui_wait_for_chars_buf(
2932 char_u *buf,
2933 int maxlen,
2934 long wtime, // don't use "time", MIPS cannot handle it
2935 int tb_change_cnt)
2936{
2937 int retval;
2938
2939#ifdef FEAT_MENU
2940 // If we're going to wait a bit, update the menus and mouse shape for the
2941 // current State.
2942 if (wtime != 0)
2943 gui_update_menus(0);
2944#endif
2945
2946 gui_mch_update();
2947 if (input_available()) // Got char, return immediately
2948 {
2949 if (buf != NULL && !typebuf_changed(tb_change_cnt))
2950 return read_from_input_buf(buf, (long)maxlen);
2951 return 0;
2952 }
2953 if (wtime == 0) // Don't wait for char
2954 return FAIL;
2955
2956 // Before waiting, flush any output to the screen.
2957 gui_mch_flush();
2958
2959 // Blink while waiting for a character.
2960 gui_mch_start_blink();
2961
2962 // Common function to loop until "wtime" is met, while handling timers and
2963 // other callbacks.
2964 retval = inchar_loop(buf, maxlen, wtime, tb_change_cnt,
2965 gui_wait_for_chars_or_timer, NULL);
2966
2967 gui_mch_stop_blink(TRUE);
2968
2969 return retval;
2970}
2971
2972/*
2973 * Wait for a character from the keyboard without actually reading it.
2974 * Also deals with timers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 * wtime == -1 Wait forever.
2976 * wtime == 0 Don't wait.
2977 * wtime > 0 Wait wtime milliseconds for a character.
2978 * Returns OK if a character was found to be available within the given time,
2979 * or FAIL otherwise.
2980 */
2981 int
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002982gui_wait_for_chars(long wtime, int tb_change_cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002984 return gui_wait_for_chars_buf(NULL, 0, wtime, tb_change_cnt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985}
2986
2987/*
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002988 * Equivalent of mch_inchar() for the GUI.
2989 */
2990 int
2991gui_inchar(
2992 char_u *buf,
2993 int maxlen,
Bram Moolenaar30613902019-12-01 22:11:18 +01002994 long wtime, // milli seconds
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002995 int tb_change_cnt)
2996{
Bram Moolenaare40b9d42019-01-27 16:55:47 +01002997 return gui_wait_for_chars_buf(buf, maxlen, wtime, tb_change_cnt);
Bram Moolenaarc9e649a2017-12-18 18:14:47 +01002998}
2999
3000/*
Bram Moolenaarb5dd4242007-05-06 12:28:24 +00003001 * Fill p[4] with mouse coordinates encoded for check_termcode().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 */
3003 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003004fill_mouse_coord(char_u *p, int col, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
3006 p[0] = (char_u)(col / 128 + ' ' + 1);
3007 p[1] = (char_u)(col % 128 + ' ' + 1);
3008 p[2] = (char_u)(row / 128 + ' ' + 1);
3009 p[3] = (char_u)(row % 128 + ' ' + 1);
3010}
3011
3012/*
3013 * Generic mouse support function. Add a mouse event to the input buffer with
3014 * the given properties.
3015 * button --- may be any of MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT,
3016 * MOUSE_X1, MOUSE_X2
3017 * MOUSE_DRAG, or MOUSE_RELEASE.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003018 * MOUSE_4 and MOUSE_5 are used for vertical scroll wheel,
3019 * MOUSE_6 and MOUSE_7 for horizontal scroll wheel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 * x, y --- Coordinates of mouse in pixels.
3021 * repeated_click --- TRUE if this click comes only a short time after a
3022 * previous click.
3023 * modifiers --- Bit field which may be any of the following modifiers
3024 * or'ed together: MOUSE_SHIFT | MOUSE_CTRL | MOUSE_ALT.
3025 * This function will ignore drag events where the mouse has not moved to a new
3026 * character.
3027 */
3028 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003029gui_send_mouse_event(
3030 int button,
3031 int x,
3032 int y,
3033 int repeated_click,
3034 int_u modifiers)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035{
3036 static int prev_row = 0, prev_col = 0;
3037 static int prev_button = -1;
3038 static int num_clicks = 1;
3039 char_u string[10];
3040 enum key_extra button_char;
3041 int row, col;
3042#ifdef FEAT_CLIPBOARD
3043 int checkfor;
3044 int did_clip = FALSE;
3045#endif
3046
3047 /*
3048 * Scrolling may happen at any time, also while a selection is present.
3049 */
3050 switch (button)
3051 {
3052 case MOUSE_X1:
3053 button_char = KE_X1MOUSE;
3054 goto button_set;
3055 case MOUSE_X2:
3056 button_char = KE_X2MOUSE;
3057 goto button_set;
3058 case MOUSE_4:
3059 button_char = KE_MOUSEDOWN;
3060 goto button_set;
3061 case MOUSE_5:
3062 button_char = KE_MOUSEUP;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003063 goto button_set;
3064 case MOUSE_6:
3065 button_char = KE_MOUSELEFT;
3066 goto button_set;
3067 case MOUSE_7:
3068 button_char = KE_MOUSERIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069button_set:
3070 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003071 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 if (hold_gui_events)
3073 return;
3074
3075 string[3] = CSI;
3076 string[4] = KS_EXTRA;
3077 string[5] = (int)button_char;
3078
Bram Moolenaar30613902019-12-01 22:11:18 +01003079 // Pass the pointer coordinates of the scroll event so that we
3080 // know which window to scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 row = gui_xy2colrow(x, y, &col);
3082 string[6] = (char_u)(col / 128 + ' ' + 1);
3083 string[7] = (char_u)(col % 128 + ' ' + 1);
3084 string[8] = (char_u)(row / 128 + ' ' + 1);
3085 string[9] = (char_u)(row % 128 + ' ' + 1);
3086
3087 if (modifiers == 0)
3088 add_to_input_buf(string + 3, 7);
3089 else
3090 {
3091 string[0] = CSI;
3092 string[1] = KS_MODIFIER;
3093 string[2] = 0;
3094 if (modifiers & MOUSE_SHIFT)
3095 string[2] |= MOD_MASK_SHIFT;
3096 if (modifiers & MOUSE_CTRL)
3097 string[2] |= MOD_MASK_CTRL;
3098 if (modifiers & MOUSE_ALT)
3099 string[2] |= MOD_MASK_ALT;
3100 add_to_input_buf(string, 10);
3101 }
3102 return;
3103 }
3104 }
3105
3106#ifdef FEAT_CLIPBOARD
Bram Moolenaar30613902019-12-01 22:11:18 +01003107 // If a clipboard selection is in progress, handle it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 if (clip_star.state == SELECT_IN_PROGRESS)
3109 {
3110 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y), repeated_click);
3111 return;
3112 }
3113
Bram Moolenaar30613902019-12-01 22:11:18 +01003114 // Determine which mouse settings to look for based on the current mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 switch (get_real_state())
3116 {
3117 case NORMAL_BUSY:
3118 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003119# ifdef FEAT_TERMINAL
3120 case TERMINAL:
3121# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 case NORMAL: checkfor = MOUSE_NORMAL; break;
3123 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003124 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 case REPLACE:
3126 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 case VREPLACE:
3128 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 case INSERT:
3130 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3131 case ASKMORE:
Bram Moolenaar30613902019-12-01 22:11:18 +01003132 case HITRETURN: // At the more- and hit-enter prompt pass the
3133 // mouse event for a click on or below the
3134 // message line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 if (Y_2_ROW(y) >= msg_row)
3136 checkfor = MOUSE_NORMAL;
3137 else
3138 checkfor = MOUSE_RETURN;
3139 break;
3140
3141 /*
3142 * On the command line, use the clipboard selection on all lines
3143 * but the command line. But not when pasting.
3144 */
3145 case CMDLINE:
3146 case CMDLINE+LANGMAP:
3147 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3148 checkfor = MOUSE_NONE;
3149 else
3150 checkfor = MOUSE_COMMAND;
3151 break;
3152
3153 default:
3154 checkfor = MOUSE_NONE;
3155 break;
3156 };
3157
3158 /*
3159 * Allow clipboard selection of text on the command line in "normal"
3160 * modes. Don't do this when dragging the status line, or extending a
3161 * Visual selection.
3162 */
3163 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003164 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 && button != MOUSE_DRAG
3166# ifdef FEAT_MOUSESHAPE
3167 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169# endif
3170 )
3171 checkfor = MOUSE_NONE;
3172
3173 /*
3174 * Use modeless selection when holding CTRL and SHIFT pressed.
3175 */
3176 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3177 checkfor = MOUSE_NONEF;
3178
3179 /*
3180 * In Ex mode, always use modeless selection.
3181 */
3182 if (exmode_active)
3183 checkfor = MOUSE_NONE;
3184
3185 /*
3186 * If the mouse settings say to not use the mouse, use the modeless
3187 * selection. But if Visual is active, assume that only the Visual area
3188 * will be selected.
3189 * Exception: On the command line, both the selection is used and a mouse
3190 * key is send.
3191 */
3192 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3193 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003194 // Don't do modeless selection in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3196 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
3198 /*
3199 * When 'mousemodel' is "popup", shift-left is translated to right.
3200 * But not when also using Ctrl.
3201 */
3202 if (mouse_model_popup() && button == MOUSE_LEFT
3203 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3204 {
3205 button = MOUSE_RIGHT;
3206 modifiers &= ~ MOUSE_SHIFT;
3207 }
3208
Bram Moolenaar30613902019-12-01 22:11:18 +01003209 // If the selection is done, allow the right button to extend it.
3210 // If the selection is cleared, allow the right button to start it
3211 // from the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 if (button == MOUSE_RIGHT)
3213 {
3214 if (clip_star.state == SELECT_CLEARED)
3215 {
3216 if (State & CMDLINE)
3217 {
3218 col = msg_col;
3219 row = msg_row;
3220 }
3221 else
3222 {
3223 col = curwin->w_wcol;
3224 row = curwin->w_wrow + W_WINROW(curwin);
3225 }
3226 clip_start_selection(col, row, FALSE);
3227 }
3228 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3229 repeated_click);
3230 did_clip = TRUE;
3231 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003232 // Allow the left button to start the selection
Bram Moolenaare60acc12011-05-10 16:41:25 +02003233 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 {
3235 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3236 did_clip = TRUE;
3237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238
Bram Moolenaar30613902019-12-01 22:11:18 +01003239 // Always allow pasting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 if (button != MOUSE_MIDDLE)
3241 {
3242 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3243 return;
3244 if (checkfor != MOUSE_COMMAND)
3245 button = MOUSE_LEFT;
3246 }
3247 repeated_click = FALSE;
3248 }
3249
3250 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003251 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252#endif
3253
Bram Moolenaar30613902019-12-01 22:11:18 +01003254 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 if (hold_gui_events)
3256 return;
3257
3258 row = gui_xy2colrow(x, y, &col);
3259
3260 /*
3261 * If we are dragging and the mouse hasn't moved far enough to be on a
3262 * different character, then don't send an event to vim.
3263 */
3264 if (button == MOUSE_DRAG)
3265 {
3266 if (row == prev_row && col == prev_col)
3267 return;
Bram Moolenaar30613902019-12-01 22:11:18 +01003268 // Dragging above the window, set "row" to -1 to cause a scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 if (y < 0)
3270 row = -1;
3271 }
3272
3273 /*
3274 * If topline has changed (window scrolled) since the last click, reset
3275 * repeated_click, because we don't want starting Visual mode when
3276 * clicking on a different character in the text.
3277 */
3278 if (curwin->w_topline != gui_prev_topline
3279#ifdef FEAT_DIFF
3280 || curwin->w_topfill != gui_prev_topfill
3281#endif
3282 )
3283 repeated_click = FALSE;
3284
Bram Moolenaar30613902019-12-01 22:11:18 +01003285 string[0] = CSI; // this sequence is recognized by check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 string[1] = KS_MOUSE;
3287 string[2] = KE_FILLER;
3288 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3289 {
3290 if (repeated_click)
3291 {
3292 /*
3293 * Handle multiple clicks. They only count if the mouse is still
3294 * pointing at the same character.
3295 */
3296 if (button != prev_button || row != prev_row || col != prev_col)
3297 num_clicks = 1;
3298 else if (++num_clicks > 4)
3299 num_clicks = 1;
3300 }
3301 else
3302 num_clicks = 1;
3303 prev_button = button;
3304 gui_prev_topline = curwin->w_topline;
3305#ifdef FEAT_DIFF
3306 gui_prev_topfill = curwin->w_topfill;
3307#endif
3308
3309 string[3] = (char_u)(button | 0x20);
3310 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3311 }
3312 else
3313 string[3] = (char_u)button;
3314
3315 string[3] |= modifiers;
3316 fill_mouse_coord(string + 4, col, row);
3317 add_to_input_buf(string, 8);
3318
3319 if (row < 0)
3320 prev_row = 0;
3321 else
3322 prev_row = row;
3323 prev_col = col;
3324
3325 /*
3326 * We need to make sure this is cleared since Athena doesn't tell us when
3327 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3328 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003329#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 gui.dragged_sb = SBAR_NONE;
3331#endif
3332}
3333
3334/*
3335 * Convert x and y coordinate to column and row in text window.
3336 * Corrects for multi-byte character.
3337 * returns column in "*colp" and row as return value;
3338 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003339 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003340gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
3342 int col = check_col(X_2_COL(x));
3343 int row = check_row(Y_2_ROW(y));
3344
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 return row;
3347}
3348
3349#if defined(FEAT_MENU) || defined(PROTO)
3350/*
3351 * Callback function for when a menu entry has been selected.
3352 */
3353 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003354gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003356 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
Bram Moolenaar30613902019-12-01 22:11:18 +01003358 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (hold_gui_events)
3360 return;
3361
3362 bytes[0] = CSI;
3363 bytes[1] = KS_MENU;
3364 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003365 add_to_input_buf(bytes, 3);
3366 add_long_to_buf((long_u)menu, bytes);
3367 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368}
3369#endif
3370
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003371static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003372
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373/*
3374 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003375 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 * in p_go.
3377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003379gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380{
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003381#ifdef FEAT_GUI_DARKTHEME
3382 static int prev_dark_theme = -1;
3383 int using_dark_theme = FALSE;
3384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385#ifdef FEAT_MENU
3386 static int prev_menu_is_active = -1;
3387#endif
3388#ifdef FEAT_TOOLBAR
3389 static int prev_toolbar = -1;
3390 int using_toolbar = FALSE;
3391#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003392#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003393 int using_tabline;
3394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395#ifdef FEAT_FOOTER
3396 static int prev_footer = -1;
3397 int using_footer = FALSE;
3398#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003399#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 static int prev_tearoff = -1;
3401 int using_tearoff = FALSE;
3402#endif
3403
3404 char_u *p;
3405 int i;
3406#ifdef FEAT_MENU
3407 int grey_old, grey_new;
3408 char_u *temp;
3409#endif
3410 win_T *wp;
3411 int need_set_size;
3412 int fix_size;
3413
3414#ifdef FEAT_MENU
3415 if (oldval != NULL && gui.in_use)
3416 {
3417 /*
3418 * Check if the menu's go from grey to non-grey or vise versa.
3419 */
3420 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3421 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3422 if (grey_old != grey_new)
3423 {
3424 temp = p_go;
3425 p_go = oldval;
3426 gui_update_menus(MENU_ALL_MODES);
3427 p_go = temp;
3428 }
3429 }
3430 gui.menu_is_active = FALSE;
3431#endif
3432
3433 for (i = 0; i < 3; i++)
3434 gui.which_scrollbars[i] = FALSE;
3435 for (p = p_go; *p; p++)
3436 switch (*p)
3437 {
3438 case GO_LEFT:
3439 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3440 break;
3441 case GO_RIGHT:
3442 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3443 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 case GO_VLEFT:
3445 if (win_hasvertsplit())
3446 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3447 break;
3448 case GO_VRIGHT:
3449 if (win_hasvertsplit())
3450 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3451 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 case GO_BOT:
3453 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3454 break;
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003455#ifdef FEAT_GUI_DARKTHEME
3456 case GO_DARKTHEME:
3457 using_dark_theme = TRUE;
3458 break;
3459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460#ifdef FEAT_MENU
3461 case GO_MENUS:
3462 gui.menu_is_active = TRUE;
3463 break;
3464#endif
3465 case GO_GREY:
Bram Moolenaar30613902019-12-01 22:11:18 +01003466 // make menu's have grey items, ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 break;
3468#ifdef FEAT_TOOLBAR
3469 case GO_TOOLBAR:
3470 using_toolbar = TRUE;
3471 break;
3472#endif
3473#ifdef FEAT_FOOTER
3474 case GO_FOOTER:
3475 using_footer = TRUE;
3476 break;
3477#endif
3478 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003479#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 using_tearoff = TRUE;
3481#endif
3482 break;
3483 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01003484 // Ignore options that are not supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 break;
3486 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003487
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (gui.in_use)
3489 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003490 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003492
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003493#ifdef FEAT_GUI_DARKTHEME
3494 if (using_dark_theme != prev_dark_theme)
3495 {
3496 gui_mch_set_dark_theme(using_dark_theme);
3497 prev_dark_theme = using_dark_theme;
3498 }
3499#endif
3500
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003501#ifdef FEAT_GUI_TABLINE
Bram Moolenaar30613902019-12-01 22:11:18 +01003502 // Update the GUI tab line, it may appear or disappear. This may
3503 // cause the non-GUI tab line to disappear or appear.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003504 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003505 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003506 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003507 // We don't want a resize event change "Rows" here, save and
3508 // restore it. Resizing is handled below.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003509 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003510 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003511 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003512 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003513 if (using_tabline)
3514 fix_size = TRUE;
3515 if (!gui_use_tabline())
Bram Moolenaar30613902019-12-01 22:11:18 +01003516 redraw_tabline = TRUE; // may draw non-GUI tab line
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003517 }
3518#endif
3519
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 for (i = 0; i < 3; i++)
3521 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003522 // The scrollbar needs to be updated when it is shown/unshown and
3523 // when switching tab pages. But the size only changes when it's
3524 // shown/unshown. Thus we need two places to remember whether a
3525 // scrollbar is there or not.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003526 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003527 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003528 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
3530 if (i == SBAR_BOTTOM)
3531 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3532 gui.which_scrollbars[i]);
3533 else
3534 {
3535 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003538 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3539 {
3540 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003541 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003542 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003543 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003544 if (gui.which_scrollbars[i])
3545 fix_size = TRUE;
3546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003548 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003549 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
3551
3552#ifdef FEAT_MENU
3553 if (gui.menu_is_active != prev_menu_is_active)
3554 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003555 // We don't want a resize event change "Rows" here, save and
3556 // restore it. Resizing is handled below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 i = Rows;
3558 gui_mch_enable_menu(gui.menu_is_active);
3559 Rows = i;
3560 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003561 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 if (gui.menu_is_active)
3563 fix_size = TRUE;
3564 }
3565#endif
3566
3567#ifdef FEAT_TOOLBAR
3568 if (using_toolbar != prev_toolbar)
3569 {
3570 gui_mch_show_toolbar(using_toolbar);
3571 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003572 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 if (using_toolbar)
3574 fix_size = TRUE;
3575 }
3576#endif
3577#ifdef FEAT_FOOTER
3578 if (using_footer != prev_footer)
3579 {
3580 gui_mch_enable_footer(using_footer);
3581 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003582 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 if (using_footer)
3584 fix_size = TRUE;
3585 }
3586#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003587#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 if (using_tearoff != prev_tearoff)
3589 {
3590 gui_mch_toggle_tearoffs(using_tearoff);
3591 prev_tearoff = using_tearoff;
3592 }
3593#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003594 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003595 {
3596#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003597 long prev_Columns = Columns;
3598 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003599#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003600 // Adjust the size of the window to make the text area keep the
3601 // same size and to avoid that part of our window is off-screen
3602 // and a scrollbar can't be used, for example.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003603 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003604
3605#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01003606 // GTK has the annoying habit of sending us resize events when
3607 // changing the window size ourselves. This mostly happens when
3608 // waiting for a character to arrive, quite unpredictably, and may
3609 // change Columns and Rows when we don't want it. Wait for a
3610 // character here to avoid this effect.
3611 // If you remove this, please test this command for resizing
3612 // effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3613 // Don't do this while starting up though.
3614 // Don't change Rows when adding menu/toolbar/tabline.
3615 // Don't change Columns when adding vertical toolbar.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003616 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003617 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003618 if ((need_set_size & RESIZE_VERT) == 0)
3619 Rows = prev_Rows;
3620 if ((need_set_size & RESIZE_HOR) == 0)
3621 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003622#endif
3623 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003624 // When the console tabline appears or disappears the window positions
3625 // change.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003626 if (firstwin->w_winrow != tabline_height())
Bram Moolenaar30613902019-12-01 22:11:18 +01003627 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629}
3630
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003631#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3632/*
3633 * Return TRUE if the GUI is taking care of the tabline.
3634 * It may still be hidden if 'showtabline' is zero.
3635 */
3636 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003637gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003638{
3639 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3640}
3641
3642/*
3643 * Return TRUE if the GUI is showing the tabline.
3644 * This uses 'showtabline'.
3645 */
3646 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003647gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003648{
3649 if (!gui_use_tabline()
3650 || p_stal == 0
3651 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3652 return FALSE;
3653 return TRUE;
3654}
3655
3656/*
3657 * Update the tabline.
3658 * This may display/undisplay the tabline and update the labels.
3659 */
3660 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003661gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003662{
3663 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003664 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003665
3666 if (!gui.starting && starting == 0)
3667 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003668 // Updating the tabline uses direct GUI commands, flush
3669 // outstanding instructions first. (esp. clear screen)
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003670 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003671
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003672 if (!showit != !shown)
3673 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003674 if (showit != 0)
3675 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003676
Bram Moolenaar30613902019-12-01 22:11:18 +01003677 // When the tabs change from hidden to shown or from shown to
3678 // hidden the size of the text area should remain the same.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003679 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003680 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003681 }
3682}
3683
3684/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003685 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003686 */
3687 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003688get_tabline_label(
3689 tabpage_T *tp,
Bram Moolenaar30613902019-12-01 22:11:18 +01003690 int tooltip) // TRUE: get tooltip
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003691{
3692 int modified = FALSE;
3693 char_u buf[40];
3694 int wincount;
3695 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003696 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003697
Bram Moolenaar30613902019-12-01 22:11:18 +01003698 // Use 'guitablabel' or 'guitabtooltip' if it's set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003699 opt = (tooltip ? &p_gtt : &p_gtl);
3700 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003701 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003702 int use_sandbox = FALSE;
3703 int save_called_emsg = called_emsg;
3704 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003705 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003706 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3707 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003708
3709 called_emsg = FALSE;
3710
3711 printer_page_num = tabpage_index(tp);
3712# ifdef FEAT_EVAL
3713 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003714 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003715# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003716 // It's almost as going to the tabpage, but without autocommands.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003717 curtab->tp_firstwin = firstwin;
3718 curtab->tp_lastwin = lastwin;
3719 curtab->tp_curwin = curwin;
3720 save_curtab = curtab;
3721 curtab = tp;
3722 topframe = curtab->tp_topframe;
3723 firstwin = curtab->tp_firstwin;
3724 lastwin = curtab->tp_lastwin;
3725 curwin = curtab->tp_curwin;
3726 curbuf = curwin->w_buffer;
3727
Bram Moolenaar30613902019-12-01 22:11:18 +01003728 // Can't use NameBuff directly, build_stl_str_hl() uses it.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003729 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003730 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003731 STRCPY(NameBuff, res);
3732
Bram Moolenaar30613902019-12-01 22:11:18 +01003733 // Back to the original curtab.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003734 curtab = save_curtab;
3735 topframe = curtab->tp_topframe;
3736 firstwin = curtab->tp_firstwin;
3737 lastwin = curtab->tp_lastwin;
3738 curwin = curtab->tp_curwin;
3739 curbuf = curwin->w_buffer;
3740
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003741 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003742 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003743 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003744 called_emsg |= save_called_emsg;
3745 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003746
Bram Moolenaar30613902019-12-01 22:11:18 +01003747 // If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3748 // use a default label.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003749 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003750 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003751 // Get the buffer name into NameBuff[] and shorten it.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003752 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003753 if (!tooltip)
3754 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003755
3756 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3757 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3758 if (bufIsChanged(wp->w_buffer))
3759 modified = TRUE;
3760 if (modified || wincount > 1)
3761 {
3762 if (wincount > 1)
3763 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3764 else
3765 buf[0] = NUL;
3766 if (modified)
3767 STRCAT(buf, "+");
3768 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003769 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003770 mch_memmove(NameBuff, buf, STRLEN(buf));
3771 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003772 }
3773}
3774
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003775/*
3776 * Send the event for clicking to select tab page "nr".
3777 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003778 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003779 */
3780 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003781send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003782{
3783 char_u string[3];
3784
3785 if (nr == tabpage_index(curtab))
3786 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003787
Bram Moolenaar30613902019-12-01 22:11:18 +01003788 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003789 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003790# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003791 || cmdwin_type != 0
3792# endif
3793 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003794 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003795 // Set it back to the current tab page.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003796 gui_mch_set_curtab(tabpage_index(curtab));
3797 return FALSE;
3798 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003799
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003800 string[0] = CSI;
3801 string[1] = KS_TABLINE;
3802 string[2] = KE_FILLER;
3803 add_to_input_buf(string, 3);
3804 string[0] = nr;
3805 add_to_input_buf_csi(string, 1);
3806 return TRUE;
3807}
3808
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003809/*
3810 * Send a tabline menu event
3811 */
3812 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003813send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003814{
3815 char_u string[3];
3816
Bram Moolenaar29547192018-12-11 20:39:19 +01003817 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003818 if (hold_gui_events)
3819 return;
3820
Bram Moolenaar29547192018-12-11 20:39:19 +01003821 // Cannot close the last tabpage.
3822 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3823 return;
3824
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003825 string[0] = CSI;
3826 string[1] = KS_TABMENU;
3827 string[2] = KE_FILLER;
3828 add_to_input_buf(string, 3);
3829 string[0] = tabidx;
3830 string[1] = (char_u)(long)event;
3831 add_to_input_buf_csi(string, 2);
3832}
3833
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835
3836/*
3837 * Scrollbar stuff:
3838 */
3839
Bram Moolenaare45828b2006-02-15 22:12:56 +00003840/*
3841 * Remove all scrollbars. Used before switching to another tab page.
3842 */
3843 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003844gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003845{
3846 int i;
3847 win_T *wp;
3848
3849 for (i = 0; i < 3; i++)
3850 {
3851 if (i == SBAR_BOTTOM)
3852 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3853 else
3854 {
3855 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003856 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003857 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003858 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003859 }
3860}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003863gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864{
3865 static int sbar_ident = 0;
3866
Bram Moolenaar30613902019-12-01 22:11:18 +01003867 sb->ident = sbar_ident++; // No check for too big, but would it happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 sb->wp = wp;
3869 sb->type = type;
3870 sb->value = 0;
3871#ifdef FEAT_GUI_ATHENA
3872 sb->pixval = 0;
3873#endif
3874 sb->size = 1;
3875 sb->max = 1;
3876 sb->top = 0;
3877 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 sb->status_height = 0;
3880 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3881}
3882
3883/*
3884 * Find the scrollbar with the given index.
3885 */
3886 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003887gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888{
3889 win_T *wp;
3890
3891 if (gui.bottom_sbar.ident == ident)
3892 return &gui.bottom_sbar;
3893 FOR_ALL_WINDOWS(wp)
3894 {
3895 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3896 return &wp->w_scrollbars[SBAR_LEFT];
3897 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3898 return &wp->w_scrollbars[SBAR_RIGHT];
3899 }
3900 return NULL;
3901}
3902
3903/*
3904 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3905 *
3906 * For Win32, Macintosh and GTK+ 2:
3907 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3908 * you stop dragging the scrollbar. We get here each time the scrollbar is
3909 * dragged another pixel, but as far as the rest of vim goes, it thinks
3910 * we're just hanging in the call to DispatchMessage() in
3911 * process_message(). The DispatchMessage() call that hangs was passed a
3912 * mouse button click event in the scrollbar window. -- webb.
3913 *
3914 * Solution: Do the scrolling right here. But only when allowed.
3915 * Ignore the scrollbars while executing an external command or when there
3916 * are still characters to be processed.
3917 */
3918 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003919gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 int sb_num;
3923#ifdef USE_ON_FLY_SCROLL
3924 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926# ifdef FEAT_DIFF
3927 int old_topfill = curwin->w_topfill;
3928# endif
3929#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003930 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 int byte_count;
3932#endif
3933
3934 if (sb == NULL)
3935 return;
3936
Bram Moolenaar30613902019-12-01 22:11:18 +01003937 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 if (hold_gui_events)
3939 return;
3940
3941#ifdef FEAT_CMDWIN
3942 if (cmdwin_type != 0 && sb->wp != curwin)
3943 return;
3944#endif
3945
3946 if (still_dragging)
3947 {
3948 if (sb->wp == NULL)
3949 gui.dragged_sb = SBAR_BOTTOM;
3950 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3951 gui.dragged_sb = SBAR_LEFT;
3952 else
3953 gui.dragged_sb = SBAR_RIGHT;
3954 gui.dragged_wp = sb->wp;
3955 }
3956 else
3957 {
3958 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003959#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01003960 // Keep the "dragged_wp" value until after the scrolling, for when the
3961 // mouse button is released. GTK2 doesn't send the button-up event.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 gui.dragged_wp = NULL;
3963#endif
3964 }
3965
Bram Moolenaar30613902019-12-01 22:11:18 +01003966 // Vertical sbar info is kept in the first sbar (the left one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 if (sb->wp != NULL)
3968 sb = &sb->wp->w_scrollbars[0];
3969
3970 /*
3971 * Check validity of value
3972 */
3973 if (value < 0)
3974 value = 0;
3975#ifdef SCROLL_PAST_END
3976 else if (value > sb->max)
3977 value = sb->max;
3978#else
3979 if (value > sb->max - sb->size + 1)
3980 value = sb->max - sb->size + 1;
3981#endif
3982
3983 sb->value = value;
3984
3985#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar30613902019-12-01 22:11:18 +01003986 // When not allowed to do the scrolling right now, return.
3987 // This also checked input_available(), but that causes the first click in
3988 // a scrollbar to be ignored when Vim doesn't have focus.
Bram Moolenaar67840782008-01-03 15:15:07 +00003989 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 return;
3991#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003992 // Disallow scrolling the current window when the completion popup menu is
3993 // visible.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003994 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3995 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996
3997#ifdef FEAT_RIGHTLEFT
3998 if (sb->wp == NULL && curwin->w_p_rl)
3999 {
4000 value = sb->max + 1 - sb->size - value;
4001 if (value < 0)
4002 value = 0;
4003 }
4004#endif
4005
Bram Moolenaar30613902019-12-01 22:11:18 +01004006 if (sb->wp != NULL) // vertical scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 {
4008 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4010 sb_num++;
4011 if (wp == NULL)
4012 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
4014#ifdef USE_ON_FLY_SCROLL
4015 current_scrollbar = sb_num;
4016 scrollbar_value = value;
4017 if (State & NORMAL)
4018 {
4019 gui_do_scroll();
4020 setcursor();
4021 }
4022 else if (State & INSERT)
4023 {
4024 ins_scroll();
4025 setcursor();
4026 }
4027 else if (State & CMDLINE)
4028 {
4029 if (msg_scrolled == 0)
4030 {
4031 gui_do_scroll();
4032 redrawcmdline();
4033 }
4034 }
4035# ifdef FEAT_FOLDING
Bram Moolenaar30613902019-12-01 22:11:18 +01004036 // Value may have been changed for closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 sb->value = sb->wp->w_topline - 1;
4038# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004039
Bram Moolenaar30613902019-12-01 22:11:18 +01004040 // When dragging one scrollbar and there is another one at the other
4041 // side move the thumb of that one too.
Bram Moolenaar371d5402006-03-20 21:47:49 +00004042 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4043 gui_mch_set_scrollbar_thumb(
4044 &sb->wp->w_scrollbars[
4045 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4046 ? SBAR_LEFT : SBAR_RIGHT],
4047 sb->value, sb->size, sb->max);
4048
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049#else
4050 bytes[0] = CSI;
4051 bytes[1] = KS_VER_SCROLLBAR;
4052 bytes[2] = KE_FILLER;
4053 bytes[3] = (char_u)sb_num;
4054 byte_count = 4;
4055#endif
4056 }
4057 else
4058 {
4059#ifdef USE_ON_FLY_SCROLL
4060 scrollbar_value = value;
4061
4062 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004063 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 else if (State & INSERT)
4065 ins_horscroll();
4066 else if (State & CMDLINE)
4067 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004068 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004070 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 redrawcmdline();
4072 }
4073 }
4074 if (old_leftcol != curwin->w_leftcol)
4075 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004076 updateWindow(curwin); // update window, status and cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 setcursor();
4078 }
4079#else
4080 bytes[0] = CSI;
4081 bytes[1] = KS_HOR_SCROLLBAR;
4082 bytes[2] = KE_FILLER;
4083 byte_count = 3;
4084#endif
4085 }
4086
4087#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 /*
4089 * synchronize other windows, as necessary according to 'scrollbind'
4090 */
4091 if (curwin->w_p_scb
4092 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4093 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004094# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 ))))
4098 {
4099 do_check_scrollbind(TRUE);
Bram Moolenaar30613902019-12-01 22:11:18 +01004100 // need to update the window right here
Bram Moolenaar29323592016-07-24 22:04:11 +02004101 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 if (wp->w_redr_type > 0)
4103 updateWindow(wp);
4104 setcursor();
4105 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004106 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004108 add_to_input_buf(bytes, byte_count);
4109 add_long_to_buf((long_u)value, bytes);
4110 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111#endif
4112}
4113
4114/*
4115 * Scrollbar stuff:
4116 */
4117
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004118/*
4119 * Called when something in the window layout has changed.
4120 */
4121 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004122gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004123{
4124 if (gui.in_use && starting == 0)
4125 {
4126 out_flush();
4127 gui_init_which_components(NULL);
4128 gui_update_scrollbars(TRUE);
4129 }
4130 need_mouse_correct = TRUE;
4131}
4132
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004134gui_update_scrollbars(
Bram Moolenaar30613902019-12-01 22:11:18 +01004135 int force) // Force all scrollbars to get updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136{
4137 win_T *wp;
4138 scrollbar_T *sb;
Bram Moolenaar30613902019-12-01 22:11:18 +01004139 long val, size, max; // need 32 bits here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 int which_sb;
4141 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
Bram Moolenaar30613902019-12-01 22:11:18 +01004144 // Update the horizontal scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 gui_update_horiz_scrollbar(force);
4146
Bram Moolenaar4f974752019-02-17 17:44:42 +01004147#ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01004148 // Return straight away if there is neither a left nor right scrollbar.
4149 // On MS-Windows this is required anyway for scrollwheel messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4151 return;
4152#endif
4153
4154 /*
4155 * Don't want to update a scrollbar while we're dragging it. But if we
4156 * have both a left and right scrollbar, and we drag one of them, we still
4157 * need to update the other one.
4158 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004159 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4160 && gui.which_scrollbars[SBAR_LEFT]
4161 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 {
4163 /*
4164 * If we have two scrollbars and one of them is being dragged, just
4165 * copy the scrollbar position from the dragged one to the other one.
4166 */
4167 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4168 if (gui.dragged_wp != NULL)
4169 gui_mch_set_scrollbar_thumb(
4170 &gui.dragged_wp->w_scrollbars[which_sb],
4171 gui.dragged_wp->w_scrollbars[0].value,
4172 gui.dragged_wp->w_scrollbars[0].size,
4173 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 }
4175
Bram Moolenaar30613902019-12-01 22:11:18 +01004176 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 ++hold_gui_events;
4178
Bram Moolenaar45a24952016-07-24 22:25:15 +02004179 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004181 if (wp->w_buffer == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 continue;
Bram Moolenaar30613902019-12-01 22:11:18 +01004183 // Skip a scrollbar that is being dragged.
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004184 if (!force && (gui.dragged_sb == SBAR_LEFT
4185 || gui.dragged_sb == SBAR_RIGHT)
4186 && gui.dragged_wp == wp)
4187 continue;
4188
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189#ifdef SCROLL_PAST_END
4190 max = wp->w_buffer->b_ml.ml_line_count - 1;
4191#else
4192 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4193#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004194 if (max < 0) // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 max = 0;
4196 val = wp->w_topline - 1;
4197 size = wp->w_height;
4198#ifdef SCROLL_PAST_END
Bram Moolenaar30613902019-12-01 22:11:18 +01004199 if (val > max) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 val = max;
4201#else
Bram Moolenaar30613902019-12-01 22:11:18 +01004202 if (size > max + 1) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 size = max + 1;
4204 if (val > max - size + 1)
4205 val = max - size + 1;
4206#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004207 if (val < 0) // minimal value is 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 val = 0;
4209
4210 /*
4211 * Scrollbar at index 0 (the left one) contains all the information.
4212 * It would be the same info for left and right so we just store it for
4213 * one of them.
4214 */
4215 sb = &wp->w_scrollbars[0];
4216
4217 /*
4218 * Note: no check for valid w_botline. If it's not valid the
4219 * scrollbars will be updated later anyway.
4220 */
4221 if (size < 1 || wp->w_botline - 2 > max)
4222 {
4223 /*
4224 * This can happen during changing files. Just don't update the
4225 * scrollbar for now.
4226 */
Bram Moolenaar30613902019-12-01 22:11:18 +01004227 sb->height = 0; // Force update next time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 if (gui.which_scrollbars[SBAR_LEFT])
4229 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4230 if (gui.which_scrollbars[SBAR_RIGHT])
4231 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4232 continue;
4233 }
4234 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 || sb->top != wp->w_winrow
4236 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004238 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004240 // Height, width or position of scrollbar has changed. For
4241 // vertical split: curwin changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 sb->top = wp->w_winrow;
4244 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246
Bram Moolenaar30613902019-12-01 22:11:18 +01004247 // Calculate height and position in pixels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 h = (sb->height + sb->status_height) * gui.char_height;
4249 y = sb->top * gui.char_height + gui.border_offset;
4250#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4251 if (gui.menu_is_active)
4252 y += gui.menu_height;
4253#endif
4254
4255#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4256 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4257# ifdef FEAT_GUI_ATHENA
4258 y += gui.toolbar_height;
4259# else
4260# ifdef FEAT_GUI_MSWIN
4261 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4262# endif
4263# endif
4264#endif
4265
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004266#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4267 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004268 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004269#endif
4270
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004273 // Height of top scrollbar includes width of top border
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 h += gui.border_offset;
4275 y -= gui.border_offset;
4276 }
4277 if (gui.which_scrollbars[SBAR_LEFT])
4278 {
4279 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4280 gui.left_sbar_x, y,
4281 gui.scrollbar_width, h);
4282 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4283 }
4284 if (gui.which_scrollbars[SBAR_RIGHT])
4285 {
4286 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4287 gui.right_sbar_x, y,
4288 gui.scrollbar_width, h);
4289 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4290 }
4291 }
4292
Bram Moolenaar30613902019-12-01 22:11:18 +01004293 // Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4294 // checking if the thumb moved at least a pixel. Only do this for
4295 // Athena, most other GUIs require the update anyway to make the
4296 // arrows work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297#ifdef FEAT_GUI_ATHENA
4298 if (max == 0)
4299 y = 0;
4300 else
4301 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4302 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4303#else
4304 if (force || sb->value != val || sb->size != size || sb->max != max)
4305#endif
4306 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004307 // Thumb of scrollbar has moved
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 sb->value = val;
4309#ifdef FEAT_GUI_ATHENA
4310 sb->pixval = y;
4311#endif
4312 sb->size = size;
4313 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004314 if (gui.which_scrollbars[SBAR_LEFT]
4315 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4317 val, size, max);
4318 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004319 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4321 val, size, max);
4322 }
4323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 --hold_gui_events;
4326}
4327
4328/*
4329 * Enable or disable a scrollbar.
4330 * Check for scrollbars for vertically split windows which are not enabled
4331 * sometimes.
4332 */
4333 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004334gui_do_scrollbar(
4335 win_T *wp,
Bram Moolenaar30613902019-12-01 22:11:18 +01004336 int which, // SBAR_LEFT or SBAR_RIGHT
4337 int enable) // TRUE to enable scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 int midcol = curwin->w_wincol + curwin->w_width / 2;
4340 int has_midcol = (wp->w_wincol <= midcol
4341 && wp->w_wincol + wp->w_width >= midcol);
4342
Bram Moolenaar30613902019-12-01 22:11:18 +01004343 // Only enable scrollbars that contain the middle column of the current
4344 // window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4346 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004347 // Scrollbars only on one side. Don't enable scrollbars that don't
4348 // contain the middle column of the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 if (!has_midcol)
4350 enable = FALSE;
4351 }
4352 else
4353 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004354 // Scrollbars on both sides. Don't enable scrollbars that neither
4355 // contain the middle column of the current window nor are on the far
4356 // side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 if (midcol > Columns / 2)
4358 {
4359 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4360 enable = FALSE;
4361 }
4362 else
4363 {
4364 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4365 : !has_midcol)
4366 enable = FALSE;
4367 }
4368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4370}
4371
4372/*
4373 * Scroll a window according to the values set in the globals current_scrollbar
4374 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4375 * or FALSE otherwise.
4376 */
4377 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004378gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379{
4380 win_T *wp, *save_wp;
4381 int i;
4382 long nlines;
4383 pos_T old_cursor;
4384 linenr_T old_topline;
4385#ifdef FEAT_DIFF
4386 int old_topfill;
4387#endif
4388
4389 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4390 if (wp == NULL)
4391 break;
4392 if (wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004393 // Couldn't find window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 return FALSE;
4395
4396 /*
4397 * Compute number of lines to scroll. If zero, nothing to do.
4398 */
4399 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4400 if (nlines == 0)
4401 return FALSE;
4402
4403 save_wp = curwin;
4404 old_topline = wp->w_topline;
4405#ifdef FEAT_DIFF
4406 old_topfill = wp->w_topfill;
4407#endif
4408 old_cursor = wp->w_cursor;
4409 curwin = wp;
4410 curbuf = wp->w_buffer;
4411 if (nlines < 0)
4412 scrolldown(-nlines, gui.dragged_wp == NULL);
4413 else
4414 scrollup(nlines, gui.dragged_wp == NULL);
Bram Moolenaar30613902019-12-01 22:11:18 +01004415 // Reset dragged_wp after using it. "dragged_sb" will have been reset for
4416 // the mouse-up event already, but we still want it to behave like when
4417 // dragging. But not the next click in an arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 if (gui.dragged_sb == SBAR_NONE)
4419 gui.dragged_wp = NULL;
4420
4421 if (old_topline != wp->w_topline
4422#ifdef FEAT_DIFF
4423 || old_topfill != wp->w_topfill
4424#endif
4425 )
4426 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004427 if (get_scrolloff_value() != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004429 cursor_correct(); // fix window for 'so'
4430 update_topline(); // avoid up/down jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
4432 if (old_cursor.lnum != wp->w_cursor.lnum)
4433 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 }
4436
Bram Moolenaar30613902019-12-01 22:11:18 +01004437 // Make sure wp->w_leftcol and wp->w_skipcol are correct.
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004438 validate_cursor();
4439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 curwin = save_wp;
4441 curbuf = save_wp->w_buffer;
4442
4443 /*
4444 * Don't call updateWindow() when nothing has changed (it will overwrite
4445 * the status line!).
4446 */
4447 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004448 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449#ifdef FEAT_DIFF
4450 || old_topfill != wp->w_topfill
4451#endif
4452 )
4453 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004454 int type = VALID;
4455
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004456 if (pum_visible())
4457 {
4458 type = NOT_VALID;
4459 wp->w_lines_valid = 0;
4460 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004461
Bram Moolenaar30613902019-12-01 22:11:18 +01004462 // Don't set must_redraw here, it may cause the popup menu to
4463 // disappear when losing focus after a scrollbar drag.
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004464 if (wp->w_redr_type < type)
4465 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004466 mch_disable_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01004467 updateWindow(wp); // update window, status line, and cmdline
Bram Moolenaara338adc2018-01-31 20:51:47 +01004468 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470
Bram Moolenaar30613902019-12-01 22:11:18 +01004471 // May need to redraw the popup menu.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004472 if (pum_visible())
4473 pum_redraw();
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004474
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004475 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476}
4477
4478
4479/*
4480 * Horizontal scrollbar stuff:
4481 */
4482
4483/*
4484 * Return length of line "lnum" for horizontal scrolling.
4485 */
4486 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004487scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488{
4489 char_u *p;
4490 colnr_T col;
4491 int w;
4492
4493 p = ml_get(lnum);
4494 col = 0;
4495 if (*p != NUL)
4496 for (;;)
4497 {
4498 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004499 MB_PTR_ADV(p);
Bram Moolenaar30613902019-12-01 22:11:18 +01004500 if (*p == NUL) // don't count the last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 break;
4502 col += w;
4503 }
4504 return col;
4505}
4506
Bram Moolenaar30613902019-12-01 22:11:18 +01004507// Remember which line is currently the longest, so that we don't have to
4508// search for it when scrolling horizontally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509static linenr_T longest_lnum = 0;
4510
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004511/*
4512 * Find longest visible line number. If this is not possible (or not desired,
4513 * by setting 'h' in "guioptions") then the current line number is returned.
4514 */
4515 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004516gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004517{
4518 linenr_T ret = 0;
4519
Bram Moolenaar30613902019-12-01 22:11:18 +01004520 // Calculate maximum for horizontal scrollbar. Check for reasonable
4521 // line numbers, topline and botline can be invalid when displaying is
4522 // postponed.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004523 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4524 && curwin->w_topline <= curwin->w_cursor.lnum
4525 && curwin->w_botline > curwin->w_cursor.lnum
4526 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4527 {
4528 linenr_T lnum;
4529 colnr_T n;
4530 long max = 0;
4531
Bram Moolenaar30613902019-12-01 22:11:18 +01004532 // Use maximum of all visible lines. Remember the lnum of the
4533 // longest line, closest to the cursor line. Used when scrolling
4534 // below.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004535 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4536 {
4537 n = scroll_line_len(lnum);
4538 if (n > (colnr_T)max)
4539 {
4540 max = n;
4541 ret = lnum;
4542 }
4543 else if (n == (colnr_T)max
4544 && abs((int)(lnum - curwin->w_cursor.lnum))
4545 < abs((int)(ret - curwin->w_cursor.lnum)))
4546 ret = lnum;
4547 }
4548 }
4549 else
Bram Moolenaar30613902019-12-01 22:11:18 +01004550 // Use cursor line only.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004551 ret = curwin->w_cursor.lnum;
4552
4553 return ret;
4554}
4555
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004557gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558{
Bram Moolenaar30613902019-12-01 22:11:18 +01004559 long value, size, max; // need 32 bit ints here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
4561 if (!gui.which_scrollbars[SBAR_BOTTOM])
4562 return;
4563
4564 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4565 return;
4566
4567 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4568 return;
4569
4570 /*
4571 * It is possible for the cursor to be invalid if we're in the middle of
4572 * something (like changing files). If so, don't do anything for now.
4573 */
4574 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4575 {
4576 gui.bottom_sbar.value = -1;
4577 return;
4578 }
4579
Bram Moolenaar02631462017-09-22 15:20:32 +02004580 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 if (curwin->w_p_wrap)
4582 {
4583 value = 0;
4584#ifdef SCROLL_PAST_END
4585 max = 0;
4586#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004587 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588#endif
4589 }
4590 else
4591 {
4592 value = curwin->w_leftcol;
4593
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004594 longest_lnum = gui_find_longest_lnum();
4595 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 if (virtual_active())
4598 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004599 // May move the cursor even further to the right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 if (curwin->w_virtcol >= (colnr_T)max)
4601 max = curwin->w_virtcol;
4602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
4604#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004605 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004607 // The line number isn't scrolled, thus there is less space when
4608 // 'number' or 'relativenumber' is set (also for 'foldcolumn').
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 size -= curwin_col_off();
4610#ifndef SCROLL_PAST_END
4611 max -= curwin_col_off();
4612#endif
4613 }
4614
4615#ifndef SCROLL_PAST_END
4616 if (value > max - size + 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01004617 value = max - size + 1; // limit the value to allowable range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618#endif
4619
4620#ifdef FEAT_RIGHTLEFT
4621 if (curwin->w_p_rl)
4622 {
4623 value = max + 1 - size - value;
4624 if (value < 0)
4625 {
4626 size += value;
4627 value = 0;
4628 }
4629 }
4630#endif
4631 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4632 && max == gui.bottom_sbar.max)
4633 return;
4634
4635 gui.bottom_sbar.value = value;
4636 gui.bottom_sbar.size = size;
4637 gui.bottom_sbar.max = max;
4638 gui.prev_wrap = curwin->w_p_wrap;
4639
4640 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4641}
4642
4643/*
4644 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4645 */
4646 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004647gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648{
Bram Moolenaar30613902019-12-01 22:11:18 +01004649 // no wrapping, no scrolling
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 if (curwin->w_p_wrap)
4651 return FALSE;
4652
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004653 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 return FALSE;
4655
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004656 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657
Bram Moolenaar30613902019-12-01 22:11:18 +01004658 // When the line of the cursor is too short, move the cursor to the
4659 // longest visible line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004661 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004662 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004664 if (compute_longest_lnum)
4665 {
4666 curwin->w_cursor.lnum = gui_find_longest_lnum();
4667 curwin->w_cursor.col = 0;
4668 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004669 // Do a sanity check on "longest_lnum", just in case.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004670 else if (longest_lnum >= curwin->w_topline
4671 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 {
4673 curwin->w_cursor.lnum = longest_lnum;
4674 curwin->w_cursor.col = 0;
4675 }
4676 }
4677
4678 return leftcol_changed();
4679}
4680
4681/*
4682 * Check that none of the colors are the same as the background color
4683 */
4684 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004685gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
4687 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4688 {
4689 gui_set_bg_color((char_u *)"White");
4690 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4691 gui_set_fg_color((char_u *)"Black");
4692 }
4693}
4694
Bram Moolenaar3918c952005-03-15 22:34:55 +00004695 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004696gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697{
4698 gui.norm_pixel = gui_get_color(name);
4699 hl_set_fg_color_name(vim_strsave(name));
4700}
4701
Bram Moolenaar3918c952005-03-15 22:34:55 +00004702 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004703gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
4705 gui.back_pixel = gui_get_color(name);
4706 hl_set_bg_color_name(vim_strsave(name));
4707}
4708
4709/*
4710 * Allocate a color by name.
4711 * Returns INVALCOLOR and gives an error message when failed.
4712 */
4713 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004714gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715{
4716 guicolor_T t;
4717
4718 if (*name == NUL)
4719 return INVALCOLOR;
4720 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004721
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004723#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 && gui.in_use
4725#endif
4726 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004727 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 return t;
4729}
4730
4731/*
4732 * Return the grey value of a color (range 0-255).
4733 */
4734 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004735gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004737 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004739 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004740 + (((rgb >> 8) & 0xff) * 587)
4741 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742}
4743
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004744 char_u *
4745gui_bg_default(void)
4746{
4747 if (gui_get_lightness(gui.back_pixel) < 127)
4748 return (char_u *)"dark";
4749 return (char_u *)"light";
4750}
4751
4752/*
4753 * Option initializations that can only be done after opening the GUI window.
4754 */
4755 void
4756init_gui_options(void)
4757{
Bram Moolenaar30613902019-12-01 22:11:18 +01004758 // Set the 'background' option according to the lightness of the
4759 // background color, unless the user has set it already.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004760 if (!option_was_set((char_u *)"bg") && STRCMP(p_bg, gui_bg_default()) != 0)
4761 {
4762 set_option_value((char_u *)"bg", 0L, gui_bg_default(), 0);
4763 highlight_changed();
4764 }
4765}
4766
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767#if defined(FEAT_GUI_X11) || defined(PROTO)
4768 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004769gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
4771 win_T *wp;
4772
Bram Moolenaar30613902019-12-01 22:11:18 +01004773 // Nothing to do if GUI hasn't started yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 if (!gui.in_use)
4775 return;
4776
4777 FOR_ALL_WINDOWS(wp)
4778 {
4779 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4780 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4781 }
4782 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4783}
4784#endif
4785
4786/*
4787 * Call this when focus has changed.
4788 */
4789 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004790gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791{
4792/*
4793 * Skip this code to avoid drawing the cursor when debugging and switching
4794 * between the debugger window and gvim.
4795 */
4796#if 1
4797 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004798 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
4800# ifdef FEAT_XIM
4801 xim_set_focus(in_focus);
4802# endif
4803
Bram Moolenaar30613902019-12-01 22:11:18 +01004804 // Put events in the input queue only when allowed.
4805 // ui_focus_change() isn't called directly, because it invokes
4806 // autocommands and that must not happen asynchronously.
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004807 if (!hold_gui_events)
4808 {
4809 char_u bytes[3];
4810
4811 bytes[0] = CSI;
4812 bytes[1] = KS_EXTRA;
4813 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4814 add_to_input_buf(bytes, 3);
4815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816#endif
4817}
4818
4819/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004820 * When mouse moved: apply 'mousefocus'.
4821 * Also updates the mouse pointer shape.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 */
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004823 static void
4824gui_mouse_focus(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825{
4826 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004827 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828
4829#ifdef FEAT_MOUSESHAPE
Bram Moolenaar30613902019-12-01 22:11:18 +01004830 // Get window pointer, and update mouse shape as well.
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004831 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832#endif
4833
Bram Moolenaar30613902019-12-01 22:11:18 +01004834 // Only handle this when 'mousefocus' set and ...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 if (p_mousef
Bram Moolenaar30613902019-12-01 22:11:18 +01004836 && !hold_gui_events // not holding events
4837 && (State & (NORMAL|INSERT))// Normal/Visual/Insert mode
4838 && State != HITRETURN // but not hit-return prompt
4839 && msg_scrolled == 0 // no scrolled message
4840 && !need_mouse_correct // not moving the pointer
4841 && gui.in_focus) // gvim in focus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004843 // Don't move the mouse when it's left or right of the Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if (x < 0 || x > Columns * gui.char_width)
4845 return;
4846#ifndef FEAT_MOUSESHAPE
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004847 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848#endif
4849 if (wp == curwin || wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004850 return; // still in the same old window, or none at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851
Bram Moolenaar30613902019-12-01 22:11:18 +01004852 // Ignore position in the tab pages line.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004853 if (Y_2_ROW(y) < tabline_height())
4854 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004855
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 /*
4857 * format a mouse click on status line input
4858 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004859 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4860 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 */
4862 if (finish_op)
4863 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004864 // abort the current operator first
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 st[0] = ESC;
4866 add_to_input_buf(st, 1);
4867 }
4868 st[0] = CSI;
4869 st[1] = KS_MOUSE;
4870 st[2] = KE_FILLER;
4871 st[3] = (char_u)MOUSE_LEFT;
4872 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004873 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 wp->w_height + W_WINROW(wp));
4875
4876 add_to_input_buf(st, 8);
4877 st[3] = (char_u)MOUSE_RELEASE;
4878 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004880 // Need to wake up the main loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 if (gtk_main_level() > 0)
4882 gtk_main_quit();
4883#endif
4884 }
4885}
4886
4887/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004888 * Called when the mouse moved (but not when dragging).
4889 */
4890 void
4891gui_mouse_moved(int x, int y)
4892{
4893 // Ignore this while still starting up.
4894 if (!gui.in_use || gui.starting)
4895 return;
4896
4897 // apply 'mousefocus' and pointer shape
4898 gui_mouse_focus(x, y);
4899
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004900#ifdef FEAT_PROP_POPUP
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004901 if (popup_visible)
4902 // Generate a mouse-moved event, so that the popup can perhaps be
4903 // closed, just like in the terminal.
4904 gui_send_mouse_event(MOUSE_DRAG, x, y, FALSE, 0);
4905#endif
4906}
4907
4908/*
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004909 * Get the window where the mouse pointer is on.
4910 * Returns NULL if not found.
4911 */
4912 win_T *
4913gui_mouse_window(mouse_find_T popup)
4914{
4915 int x, y;
4916
4917 if (!(gui.in_use && (p_mousef || popup == FIND_POPUP)))
4918 return NULL;
4919 gui_mch_getmouse(&x, &y);
4920
4921 // Only use the mouse when it's on the Vim window
4922 if (x >= 0 && x <= Columns * gui.char_width
4923 && y >= 0 && Y_2_ROW(y) >= tabline_height())
4924 return xy2win(x, y, popup);
4925 return NULL;
4926}
4927
4928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 * Called when mouse should be moved to window with focus.
4930 */
4931 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004932gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 win_T *wp = NULL;
4935
4936 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004937
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004938 wp = gui_mouse_window(IGNORE_POPUP);
Bram Moolenaar30613902019-12-01 22:11:18 +01004939 if (wp != curwin && wp != NULL) // If in other than current window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004941 validate_cline_row();
4942 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4943 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946}
4947
4948/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004949 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004950 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 static win_T *
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004953xy2win(int x, int y, mouse_find_T popup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 int row;
4956 int col;
4957 win_T *wp;
4958
4959 row = Y_2_ROW(y);
4960 col = X_2_COL(x);
Bram Moolenaar30613902019-12-01 22:11:18 +01004961 if (row < 0 || col < 0) // before first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 return NULL;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004963 wp = mouse_find_win(&row, &col, popup);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004964 if (wp == NULL)
4965 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004966#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 if (State == HITRETURN || State == ASKMORE)
4968 {
4969 if (Y_2_ROW(y) >= msg_row)
4970 update_mouseshape(SHAPE_IDX_MOREL);
4971 else
4972 update_mouseshape(SHAPE_IDX_MORE);
4973 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004974 else if (row > wp->w_height) // below status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004976 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004977 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004979 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004980 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 update_mouseshape(SHAPE_IDX_STATUS);
4982 else
4983 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004985 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986}
4987
4988/*
4989 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4990 * File names may be given to redefine the args list.
4991 */
4992 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004993ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994{
4995 char_u *arg = eap->arg;
4996
4997 /*
4998 * Check for "-f" argument: foreground, don't fork.
4999 * Also don't fork when started with "gvim -f".
5000 * Do fork when using "gui -b".
5001 */
5002 if (arg[0] == '-'
5003 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01005004 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
5006 gui.dofork = (arg[1] == 'b');
5007 eap->arg = skipwhite(eap->arg + 2);
5008 }
5009 if (!gui.in_use)
5010 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005011#if defined(VIMDLL) && !defined(EXPERIMENTAL_GUI_CMD)
5012 emsg(_(e_nogvim));
5013 return;
5014#else
Bram Moolenaar30613902019-12-01 22:11:18 +01005015 // Clear the command. Needed for when forking+exiting, to avoid part
5016 // of the argument ending up after the shell prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 msg_clr_eos_force();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005018# ifdef GUI_MAY_SPAWN
5019 if (!ends_excmd(*eap->arg))
5020 gui_start(eap->arg);
5021 else
5022# endif
5023 gui_start(NULL);
5024# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01005025 channel_gui_register_all();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005026# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
5029 if (!ends_excmd(*eap->arg))
5030 ex_next(eap);
5031}
5032
Bram Moolenaar4f974752019-02-17 17:44:42 +01005033#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
5034 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)) \
5035 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036/*
5037 * This is shared between Athena, Motif and GTK.
5038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039
5040/*
5041 * Callback function for do_in_runtimepath().
5042 */
5043 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005044gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005046 char_u *gfp_buffer = cookie;
5047
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 if (STRLEN(fname) >= MAXPATHL)
5049 *gfp_buffer = NUL;
5050 else
5051 STRCPY(gfp_buffer, fname);
5052}
5053
5054/*
5055 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5056 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5057 */
5058 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005059gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
5061 if (STRLEN(name) > MAXPATHL - 14)
5062 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005063 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005064 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005065 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 return FAIL;
5067 return OK;
5068}
5069
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005070# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071/*
5072 * Given the name of the "icon=" argument, try finding the bitmap file for the
5073 * icon. If it is an absolute path name, use it as it is. Otherwise append
5074 * "ext" and search for it in 'runtimepath'.
5075 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5076 * contains "name".
5077 */
5078 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005079gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080{
5081 char_u buf[MAXPATHL + 1];
5082
5083 expand_env(name, buffer, MAXPATHL);
5084 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5085 STRCPY(buffer, buf);
5086}
5087# endif
5088#endif
5089
Bram Moolenaar9372a112005-12-06 19:59:18 +00005090#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005092display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093{
5094 char_u *p;
5095
5096 if (isatty(2))
5097 fflush(stderr);
5098 else if (error_ga.ga_data != NULL)
5099 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005100 // avoid putting up a message box with blanks only
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5102 if (!isspace(*p))
5103 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005104 // Truncate a very long message, it will go off-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 if (STRLEN(p) > 2000)
5106 STRCPY(p + 2000 - 14, "...(truncated)");
5107 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005108 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 break;
5110 }
5111 ga_clear(&error_ga);
5112 }
5113}
5114#endif
5115
5116#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5117/*
5118 * Return TRUE if still starting up and there is no place to enter text.
5119 * For GTK and X11 we check if stderr is not a tty, which means we were
5120 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5121 * allow typing on stdin.
5122 */
5123 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005124no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125{
5126 return ((!gui.in_use || gui.starting)
5127# ifndef NO_CONSOLE
5128 && !isatty(0) && !isatty(2)
5129# endif
5130 );
5131}
5132#endif
5133
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005134#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005135 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005136 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137/*
5138 * Update the current window and the screen.
5139 */
5140 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005141gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005143# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005144 linenr_T conceal_old_cursor_line = 0;
5145 linenr_T conceal_new_cursor_line = 0;
5146 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005147# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 update_topline();
5150 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005151
Bram Moolenaar30613902019-12-01 22:11:18 +01005152 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005153 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005154# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005155 || popup_visible
5156# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005157# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005158 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005159# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005160 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005161 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005162 if (has_cursormoved())
5163 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005164#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005165 if (popup_visible)
5166 popup_check_cursor_pos();
5167#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005168# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005169 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005170 {
5171 conceal_old_cursor_line = last_cursormoved.lnum;
5172 conceal_new_cursor_line = curwin->w_cursor.lnum;
5173 conceal_update_lines = TRUE;
5174 }
5175# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005176 last_cursormoved = curwin->w_cursor;
5177 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005178
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005179# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005180 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005181 && (conceal_old_cursor_line != conceal_new_cursor_line
5182 || conceal_cursor_line(curwin)
5183 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005184 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005185 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005186 redrawWinline(curwin, conceal_old_cursor_line);
5187 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005188 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005189 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005190 }
5191# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005192 update_screen(0); // may need to update the screen
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005193 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005194 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195}
5196#endif
5197
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005198#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199/*
5200 * Get the text to use in a find/replace dialog. Uses the last search pattern
5201 * if the argument is empty.
5202 * Returns an allocated string.
5203 */
5204 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005205get_find_dialog_text(
5206 char_u *arg,
Bram Moolenaar30613902019-12-01 22:11:18 +01005207 int *wwordp, // return: TRUE if \< \> found
5208 int *mcasep) // return: TRUE if \C found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209{
5210 char_u *text;
5211
5212 if (*arg == NUL)
5213 text = last_search_pat();
5214 else
5215 text = arg;
5216 if (text != NULL)
5217 {
5218 text = vim_strsave(text);
5219 if (text != NULL)
5220 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005221 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 int i;
5223
Bram Moolenaar30613902019-12-01 22:11:18 +01005224 // Remove "\V"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5226 {
5227 mch_memmove(text, text + 2, (size_t)(len - 1));
5228 len -= 2;
5229 }
5230
Bram Moolenaar30613902019-12-01 22:11:18 +01005231 // Recognize "\c" and "\C" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5233 {
5234 *mcasep = (text[1] == 'C');
5235 mch_memmove(text, text + 2, (size_t)(len - 1));
5236 len -= 2;
5237 }
5238
Bram Moolenaar30613902019-12-01 22:11:18 +01005239 // Recognize "\<text\>" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 if (len >= 4
5241 && STRNCMP(text, "\\<", 2) == 0
5242 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5243 {
5244 *wwordp = TRUE;
5245 mch_memmove(text, text + 2, (size_t)(len - 4));
5246 text[len - 4] = NUL;
5247 }
5248
Bram Moolenaar30613902019-12-01 22:11:18 +01005249 // Recognize "\/" or "\?" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 for (i = 0; i + 1 < len; ++i)
5251 if (text[i] == '\\' && (text[i + 1] == '/'
5252 || text[i + 1] == '?'))
5253 {
5254 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5255 --len;
5256 }
5257 }
5258 }
5259 return text;
5260}
5261
5262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 * Handle the press of a button in the find-replace dialog.
5264 * Return TRUE when something was added to the input buffer.
5265 */
5266 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005267gui_do_findrepl(
Bram Moolenaar30613902019-12-01 22:11:18 +01005268 int flags, // one of FRD_REPLACE, FRD_FINDNEXT, etc.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005269 char_u *find_text,
5270 char_u *repl_text,
Bram Moolenaar30613902019-12-01 22:11:18 +01005271 int down) // Search downwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272{
5273 garray_T ga;
5274 int i;
5275 int type = (flags & FRD_TYPE_MASK);
5276 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005277 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005278 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005279 static int busy = FALSE;
5280
Bram Moolenaar30613902019-12-01 22:11:18 +01005281 // When the screen is being updated we should not change buffers and
5282 // windows structures, it may cause freed memory to be used. Also don't
5283 // do this recursively (pressing "Find" quickly several times.
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005284 if (updating_screen || busy)
5285 return FALSE;
5286
Bram Moolenaar30613902019-12-01 22:11:18 +01005287 // refuse replace when text cannot be changed
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005288 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5289 return FALSE;
5290
5291 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292
5293 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005294 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 ga_concat(&ga, (char_u *)"%s/");
5296
5297 ga_concat(&ga, (char_u *)"\\V");
5298 if (flags & FRD_MATCH_CASE)
5299 ga_concat(&ga, (char_u *)"\\C");
5300 else
5301 ga_concat(&ga, (char_u *)"\\c");
5302 if (flags & FRD_WHOLE_WORD)
5303 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar30613902019-12-01 22:11:18 +01005304 // escape slash and backslash
Bram Moolenaar518bc172018-05-13 17:05:30 +02005305 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5306 if (p != NULL)
5307 ga_concat(&ga, p);
5308 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 if (flags & FRD_WHOLE_WORD)
5310 ga_concat(&ga, (char_u *)"\\>");
5311
5312 if (type == FRD_REPLACEALL)
5313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar30613902019-12-01 22:11:18 +01005315 // escape slash and backslash
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005316 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5317 if (p != NULL)
5318 ga_concat(&ga, p);
5319 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005321 }
5322 ga_append(&ga, NUL);
5323
5324 if (type == FRD_REPLACE)
5325 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005326 // Do the replacement when the text at the cursor matches. Thus no
5327 // replacement is done if the cursor was moved!
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005328 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5329 regmatch.rm_ic = 0;
5330 if (regmatch.regprog != NULL)
5331 {
5332 p = ml_get_cursor();
5333 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5334 && regmatch.startp[0] == p)
5335 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005336 // Clear the command line to remove any old "No match"
5337 // error.
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005338 msg_end_prompt();
5339
5340 if (u_save_cursor() == OK)
5341 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005342 // A button was pressed thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005343 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005344
5345 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005346 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005347 ins_str(repl_text);
5348 }
5349 }
5350 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005351 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005352 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005353 }
5354 }
5355
5356 if (type == FRD_REPLACEALL)
5357 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005358 // A button was pressed, thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005359 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 do_cmdline_cmd(ga.ga_data);
5361 }
5362 else
5363 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005364 int searchflags = SEARCH_MSG + SEARCH_MARK;
5365
Bram Moolenaar30613902019-12-01 22:11:18 +01005366 // Search for the next match.
5367 // Don't skip text under cursor for single replace.
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005368 if (type == FRD_REPLACE)
5369 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005371 if (down)
5372 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02005373 (void)do_search(NULL, '/', ga.ga_data, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005374 }
5375 else
5376 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005377 // We need to escape '?' if and only if we are searching in the up
5378 // direction
Bram Moolenaar518bc172018-05-13 17:05:30 +02005379 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5380 if (p != NULL)
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02005381 (void)do_search(NULL, '?', p, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005382 vim_free(p);
5383 }
5384
Bram Moolenaar30613902019-12-01 22:11:18 +01005385 msg_scroll = i; // don't let an error message set msg_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 }
5387
Bram Moolenaar30613902019-12-01 22:11:18 +01005388 // Don't want to pass did_emsg to other code, it may cause disabling
5389 // syntax HL if we were busy redrawing.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005390 did_emsg = save_did_emsg;
5391
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 if (State & (NORMAL | INSERT))
5393 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005394 gui_update_screen(); // update the screen
5395 msg_didout = 0; // overwrite any message
5396 need_wait_return = FALSE; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 }
5398
5399 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005400 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 return (ga.ga_len > 0);
5402}
5403
5404#endif
5405
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005406#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407/*
5408 * Jump to the window at specified point (x, y).
5409 */
5410 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005411gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412{
5413 int row = Y_2_ROW(y);
5414 int col = X_2_COL(x);
5415 win_T *wp;
5416
5417 if (row >= 0 && col >= 0)
5418 {
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005419 wp = mouse_find_win(&row, &col, FAIL_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 if (wp != NULL && wp != curwin)
5421 win_goto(wp);
5422 }
5423}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
5425/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005426 * Function passed to handle_drop() for the actions to be done after the
5427 * argument list has been updated.
5428 */
5429 static void
5430drop_callback(void *cookie)
5431{
5432 char_u *p = cookie;
5433
Bram Moolenaar30613902019-12-01 22:11:18 +01005434 // If Shift held down, change to first file's directory. If the first
5435 // item is a directory, change to that directory (and let the explorer
5436 // plugin show the contents).
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005437 if (p != NULL)
5438 {
5439 if (mch_isdir(p))
5440 {
5441 if (mch_chdir((char *)p) == 0)
5442 shorten_fnames(TRUE);
5443 }
5444 else if (vim_chdirfile(p, "drop") == OK)
5445 shorten_fnames(TRUE);
5446 vim_free(p);
5447 }
5448
Bram Moolenaar30613902019-12-01 22:11:18 +01005449 // Update the screen display
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005450 update_screen(NOT_VALID);
5451# ifdef FEAT_MENU
5452 gui_update_menus(0);
5453# endif
5454#ifdef FEAT_TITLE
5455 maketitle();
5456#endif
5457 setcursor();
5458 out_flush_cursor(FALSE, FALSE);
5459}
5460
5461/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 * Process file drop. Mouse cursor position, key modifiers, name of files
5463 * and count of files are given. Argument "fnames[count]" has full pathnames
5464 * of dropped files, they will be freed in this function, and caller can't use
5465 * fnames after call this function.
5466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005468gui_handle_drop(
5469 int x UNUSED,
5470 int y UNUSED,
5471 int_u modifiers,
5472 char_u **fnames,
5473 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474{
5475 int i;
5476 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005477 static int entered = FALSE;
5478
5479 /*
5480 * This function is called by event handlers. Just in case we get a
5481 * second event before the first one is handled, ignore the second one.
5482 * Not sure if this can ever happen, just in case.
5483 */
5484 if (entered)
5485 return;
5486 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
5488 /*
5489 * When the cursor is at the command line, add the file names to the
5490 * command line, don't edit the files.
5491 */
5492 if (State & CMDLINE)
5493 {
5494 shorten_filenames(fnames, count);
5495 for (i = 0; i < count; ++i)
5496 {
5497 if (fnames[i] != NULL)
5498 {
5499 if (i > 0)
5500 add_to_input_buf((char_u*)" ", 1);
5501
Bram Moolenaar30613902019-12-01 22:11:18 +01005502 // We don't know what command is used thus we can't be sure
5503 // about which characters need to be escaped. Only escape the
5504 // most common ones.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505# ifdef BACKSLASH_IN_FILENAME
5506 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5507# else
5508 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5509# endif
5510 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005511 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 vim_free(p);
5513 vim_free(fnames[i]);
5514 }
5515 }
5516 vim_free(fnames);
5517 }
5518 else
5519 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005520 // Go to the window under mouse cursor, then shorten given "fnames" by
5521 // current window, because a window can have local current dir.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 shorten_filenames(fnames, count);
5524
Bram Moolenaar30613902019-12-01 22:11:18 +01005525 // If Shift held down, remember the first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 if ((modifiers & MOUSE_SHIFT) != 0)
5527 p = vim_strsave(fnames[0]);
5528 else
5529 p = NULL;
5530
Bram Moolenaar30613902019-12-01 22:11:18 +01005531 // Handle the drop, :edit or :split to get to the file. This also
5532 // frees fnames[]. Skip this if there is only one item, it's a
5533 // directory and Shift is held down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5535 && mch_isdir(fnames[0]))
5536 {
5537 vim_free(fnames[0]);
5538 vim_free(fnames);
5539 }
5540 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005541 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
5542 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005544
5545 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546}
5547#endif