blob: 8fd311474361da5cb2c49d538e2ad5ebc6c839f3 [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);
Bram Moolenaar0ce37332019-12-18 21:33:22 +01003111
3112 // A release event may still need to be sent if the position is equal.
3113 row = gui_xy2colrow(x, y, &col);
3114 if (button != MOUSE_RELEASE || row != prev_row || col != prev_col)
3115 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 }
3117
Bram Moolenaar30613902019-12-01 22:11:18 +01003118 // Determine which mouse settings to look for based on the current mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 switch (get_real_state())
3120 {
3121 case NORMAL_BUSY:
3122 case OP_PENDING:
Bram Moolenaarae147ab2017-11-11 17:09:09 +01003123# ifdef FEAT_TERMINAL
3124 case TERMINAL:
3125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 case NORMAL: checkfor = MOUSE_NORMAL; break;
3127 case VISUAL: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003128 case SELECTMODE: checkfor = MOUSE_VISUAL; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 case REPLACE:
3130 case REPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 case VREPLACE:
3132 case VREPLACE+LANGMAP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 case INSERT:
3134 case INSERT+LANGMAP: checkfor = MOUSE_INSERT; break;
3135 case ASKMORE:
Bram Moolenaar30613902019-12-01 22:11:18 +01003136 case HITRETURN: // At the more- and hit-enter prompt pass the
3137 // mouse event for a click on or below the
3138 // message line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 if (Y_2_ROW(y) >= msg_row)
3140 checkfor = MOUSE_NORMAL;
3141 else
3142 checkfor = MOUSE_RETURN;
3143 break;
3144
3145 /*
3146 * On the command line, use the clipboard selection on all lines
3147 * but the command line. But not when pasting.
3148 */
3149 case CMDLINE:
3150 case CMDLINE+LANGMAP:
3151 if (Y_2_ROW(y) < cmdline_row && button != MOUSE_MIDDLE)
3152 checkfor = MOUSE_NONE;
3153 else
3154 checkfor = MOUSE_COMMAND;
3155 break;
3156
3157 default:
3158 checkfor = MOUSE_NONE;
3159 break;
3160 };
3161
3162 /*
3163 * Allow clipboard selection of text on the command line in "normal"
3164 * modes. Don't do this when dragging the status line, or extending a
3165 * Visual selection.
3166 */
3167 if ((State == NORMAL || State == NORMAL_BUSY || (State & INSERT))
Bram Moolenaar4033c552017-09-16 20:54:51 +02003168 && Y_2_ROW(y) >= topframe->fr_height + firstwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 && button != MOUSE_DRAG
3170# ifdef FEAT_MOUSESHAPE
3171 && !drag_status_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 && !drag_sep_line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173# endif
3174 )
3175 checkfor = MOUSE_NONE;
3176
3177 /*
3178 * Use modeless selection when holding CTRL and SHIFT pressed.
3179 */
3180 if ((modifiers & MOUSE_CTRL) && (modifiers & MOUSE_SHIFT))
3181 checkfor = MOUSE_NONEF;
3182
3183 /*
3184 * In Ex mode, always use modeless selection.
3185 */
3186 if (exmode_active)
3187 checkfor = MOUSE_NONE;
3188
3189 /*
3190 * If the mouse settings say to not use the mouse, use the modeless
3191 * selection. But if Visual is active, assume that only the Visual area
3192 * will be selected.
3193 * Exception: On the command line, both the selection is used and a mouse
3194 * key is send.
3195 */
3196 if (!mouse_has(checkfor) || checkfor == MOUSE_COMMAND)
3197 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003198 // Don't do modeless selection in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 if (checkfor != MOUSE_NONEF && VIsual_active && (State & NORMAL))
3200 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201
3202 /*
3203 * When 'mousemodel' is "popup", shift-left is translated to right.
3204 * But not when also using Ctrl.
3205 */
3206 if (mouse_model_popup() && button == MOUSE_LEFT
3207 && (modifiers & MOUSE_SHIFT) && !(modifiers & MOUSE_CTRL))
3208 {
3209 button = MOUSE_RIGHT;
3210 modifiers &= ~ MOUSE_SHIFT;
3211 }
3212
Bram Moolenaar30613902019-12-01 22:11:18 +01003213 // If the selection is done, allow the right button to extend it.
3214 // If the selection is cleared, allow the right button to start it
3215 // from the cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 if (button == MOUSE_RIGHT)
3217 {
3218 if (clip_star.state == SELECT_CLEARED)
3219 {
3220 if (State & CMDLINE)
3221 {
3222 col = msg_col;
3223 row = msg_row;
3224 }
3225 else
3226 {
3227 col = curwin->w_wcol;
3228 row = curwin->w_wrow + W_WINROW(curwin);
3229 }
3230 clip_start_selection(col, row, FALSE);
3231 }
3232 clip_process_selection(button, X_2_COL(x), Y_2_ROW(y),
3233 repeated_click);
3234 did_clip = TRUE;
3235 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003236 // Allow the left button to start the selection
Bram Moolenaare60acc12011-05-10 16:41:25 +02003237 else if (button == MOUSE_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
3239 clip_start_selection(X_2_COL(x), Y_2_ROW(y), repeated_click);
3240 did_clip = TRUE;
3241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
Bram Moolenaar30613902019-12-01 22:11:18 +01003243 // Always allow pasting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 if (button != MOUSE_MIDDLE)
3245 {
3246 if (!mouse_has(checkfor) || button == MOUSE_RELEASE)
3247 return;
3248 if (checkfor != MOUSE_COMMAND)
3249 button = MOUSE_LEFT;
3250 }
3251 repeated_click = FALSE;
3252 }
3253
3254 if (clip_star.state != SELECT_CLEARED && !did_clip)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003255 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256#endif
3257
Bram Moolenaar30613902019-12-01 22:11:18 +01003258 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 if (hold_gui_events)
3260 return;
3261
3262 row = gui_xy2colrow(x, y, &col);
3263
3264 /*
3265 * If we are dragging and the mouse hasn't moved far enough to be on a
3266 * different character, then don't send an event to vim.
3267 */
3268 if (button == MOUSE_DRAG)
3269 {
3270 if (row == prev_row && col == prev_col)
3271 return;
Bram Moolenaar30613902019-12-01 22:11:18 +01003272 // Dragging above the window, set "row" to -1 to cause a scroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 if (y < 0)
3274 row = -1;
3275 }
3276
3277 /*
3278 * If topline has changed (window scrolled) since the last click, reset
3279 * repeated_click, because we don't want starting Visual mode when
3280 * clicking on a different character in the text.
3281 */
3282 if (curwin->w_topline != gui_prev_topline
3283#ifdef FEAT_DIFF
3284 || curwin->w_topfill != gui_prev_topfill
3285#endif
3286 )
3287 repeated_click = FALSE;
3288
Bram Moolenaar30613902019-12-01 22:11:18 +01003289 string[0] = CSI; // this sequence is recognized by check_termcode()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 string[1] = KS_MOUSE;
3291 string[2] = KE_FILLER;
3292 if (button != MOUSE_DRAG && button != MOUSE_RELEASE)
3293 {
3294 if (repeated_click)
3295 {
3296 /*
3297 * Handle multiple clicks. They only count if the mouse is still
3298 * pointing at the same character.
3299 */
3300 if (button != prev_button || row != prev_row || col != prev_col)
3301 num_clicks = 1;
3302 else if (++num_clicks > 4)
3303 num_clicks = 1;
3304 }
3305 else
3306 num_clicks = 1;
3307 prev_button = button;
3308 gui_prev_topline = curwin->w_topline;
3309#ifdef FEAT_DIFF
3310 gui_prev_topfill = curwin->w_topfill;
3311#endif
3312
3313 string[3] = (char_u)(button | 0x20);
3314 SET_NUM_MOUSE_CLICKS(string[3], num_clicks);
3315 }
3316 else
3317 string[3] = (char_u)button;
3318
3319 string[3] |= modifiers;
3320 fill_mouse_coord(string + 4, col, row);
3321 add_to_input_buf(string, 8);
3322
3323 if (row < 0)
3324 prev_row = 0;
3325 else
3326 prev_row = row;
3327 prev_col = col;
3328
3329 /*
3330 * We need to make sure this is cleared since Athena doesn't tell us when
3331 * he is done dragging. Neither does GTK+ 2 -- at least for now.
3332 */
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003333#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 gui.dragged_sb = SBAR_NONE;
3335#endif
3336}
3337
3338/*
3339 * Convert x and y coordinate to column and row in text window.
3340 * Corrects for multi-byte character.
3341 * returns column in "*colp" and row as return value;
3342 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003343 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003344gui_xy2colrow(int x, int y, int *colp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346 int col = check_col(X_2_COL(x));
3347 int row = check_row(Y_2_ROW(y));
3348
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 *colp = mb_fix_col(col, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 return row;
3351}
3352
3353#if defined(FEAT_MENU) || defined(PROTO)
3354/*
3355 * Callback function for when a menu entry has been selected.
3356 */
3357 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003358gui_menu_cb(vimmenu_T *menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003360 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361
Bram Moolenaar30613902019-12-01 22:11:18 +01003362 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 if (hold_gui_events)
3364 return;
3365
3366 bytes[0] = CSI;
3367 bytes[1] = KS_MENU;
3368 bytes[2] = KE_FILLER;
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003369 add_to_input_buf(bytes, 3);
3370 add_long_to_buf((long_u)menu, bytes);
3371 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372}
3373#endif
3374
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003375static int prev_which_scrollbars[3];
Bram Moolenaare45828b2006-02-15 22:12:56 +00003376
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377/*
3378 * Set which components are present.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003379 * If "oldval" is not NULL, "oldval" is the previous value, the new value is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 * in p_go.
3381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003383gui_init_which_components(char_u *oldval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384{
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003385#ifdef FEAT_GUI_DARKTHEME
3386 static int prev_dark_theme = -1;
3387 int using_dark_theme = FALSE;
3388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389#ifdef FEAT_MENU
3390 static int prev_menu_is_active = -1;
3391#endif
3392#ifdef FEAT_TOOLBAR
3393 static int prev_toolbar = -1;
3394 int using_toolbar = FALSE;
3395#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003396#ifdef FEAT_GUI_TABLINE
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003397 int using_tabline;
3398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399#ifdef FEAT_FOOTER
3400 static int prev_footer = -1;
3401 int using_footer = FALSE;
3402#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003403#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 static int prev_tearoff = -1;
3405 int using_tearoff = FALSE;
3406#endif
3407
3408 char_u *p;
3409 int i;
3410#ifdef FEAT_MENU
3411 int grey_old, grey_new;
3412 char_u *temp;
3413#endif
3414 win_T *wp;
3415 int need_set_size;
3416 int fix_size;
3417
3418#ifdef FEAT_MENU
3419 if (oldval != NULL && gui.in_use)
3420 {
3421 /*
3422 * Check if the menu's go from grey to non-grey or vise versa.
3423 */
3424 grey_old = (vim_strchr(oldval, GO_GREY) != NULL);
3425 grey_new = (vim_strchr(p_go, GO_GREY) != NULL);
3426 if (grey_old != grey_new)
3427 {
3428 temp = p_go;
3429 p_go = oldval;
3430 gui_update_menus(MENU_ALL_MODES);
3431 p_go = temp;
3432 }
3433 }
3434 gui.menu_is_active = FALSE;
3435#endif
3436
3437 for (i = 0; i < 3; i++)
3438 gui.which_scrollbars[i] = FALSE;
3439 for (p = p_go; *p; p++)
3440 switch (*p)
3441 {
3442 case GO_LEFT:
3443 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3444 break;
3445 case GO_RIGHT:
3446 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3447 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 case GO_VLEFT:
3449 if (win_hasvertsplit())
3450 gui.which_scrollbars[SBAR_LEFT] = TRUE;
3451 break;
3452 case GO_VRIGHT:
3453 if (win_hasvertsplit())
3454 gui.which_scrollbars[SBAR_RIGHT] = TRUE;
3455 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 case GO_BOT:
3457 gui.which_scrollbars[SBAR_BOTTOM] = TRUE;
3458 break;
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003459#ifdef FEAT_GUI_DARKTHEME
3460 case GO_DARKTHEME:
3461 using_dark_theme = TRUE;
3462 break;
3463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464#ifdef FEAT_MENU
3465 case GO_MENUS:
3466 gui.menu_is_active = TRUE;
3467 break;
3468#endif
3469 case GO_GREY:
Bram Moolenaar30613902019-12-01 22:11:18 +01003470 // make menu's have grey items, ignored here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 break;
3472#ifdef FEAT_TOOLBAR
3473 case GO_TOOLBAR:
3474 using_toolbar = TRUE;
3475 break;
3476#endif
3477#ifdef FEAT_FOOTER
3478 case GO_FOOTER:
3479 using_footer = TRUE;
3480 break;
3481#endif
3482 case GO_TEAROFF:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003483#if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 using_tearoff = TRUE;
3485#endif
3486 break;
3487 default:
Bram Moolenaar30613902019-12-01 22:11:18 +01003488 // Ignore options that are not supported
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 break;
3490 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003491
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 if (gui.in_use)
3493 {
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003494 need_set_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 fix_size = FALSE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003496
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +02003497#ifdef FEAT_GUI_DARKTHEME
3498 if (using_dark_theme != prev_dark_theme)
3499 {
3500 gui_mch_set_dark_theme(using_dark_theme);
3501 prev_dark_theme = using_dark_theme;
3502 }
3503#endif
3504
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003505#ifdef FEAT_GUI_TABLINE
Bram Moolenaar30613902019-12-01 22:11:18 +01003506 // Update the GUI tab line, it may appear or disappear. This may
3507 // cause the non-GUI tab line to disappear or appear.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003508 using_tabline = gui_has_tabline();
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003509 if (!gui_mch_showing_tabline() != !using_tabline)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003510 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003511 // We don't want a resize event change "Rows" here, save and
3512 // restore it. Resizing is handled below.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003513 i = Rows;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003514 gui_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003515 Rows = i;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003516 need_set_size |= RESIZE_VERT;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003517 if (using_tabline)
3518 fix_size = TRUE;
3519 if (!gui_use_tabline())
Bram Moolenaar30613902019-12-01 22:11:18 +01003520 redraw_tabline = TRUE; // may draw non-GUI tab line
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003521 }
3522#endif
3523
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 for (i = 0; i < 3; i++)
3525 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003526 // The scrollbar needs to be updated when it is shown/unshown and
3527 // when switching tab pages. But the size only changes when it's
3528 // shown/unshown. Thus we need two places to remember whether a
3529 // scrollbar is there or not.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003530 if (gui.which_scrollbars[i] != prev_which_scrollbars[i]
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003531 || gui.which_scrollbars[i]
Bram Moolenaar4033c552017-09-16 20:54:51 +02003532 != curtab->tp_prev_which_scrollbars[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 {
3534 if (i == SBAR_BOTTOM)
3535 gui_mch_enable_scrollbar(&gui.bottom_sbar,
3536 gui.which_scrollbars[i]);
3537 else
3538 {
3539 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 gui_do_scrollbar(wp, i, gui.which_scrollbars[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003542 if (gui.which_scrollbars[i] != prev_which_scrollbars[i])
3543 {
3544 if (i == SBAR_BOTTOM)
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003545 need_set_size |= RESIZE_VERT;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003546 else
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003547 need_set_size |= RESIZE_HOR;
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003548 if (gui.which_scrollbars[i])
3549 fix_size = TRUE;
3550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003552 curtab->tp_prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003553 prev_which_scrollbars[i] = gui.which_scrollbars[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555
3556#ifdef FEAT_MENU
3557 if (gui.menu_is_active != prev_menu_is_active)
3558 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003559 // We don't want a resize event change "Rows" here, save and
3560 // restore it. Resizing is handled below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 i = Rows;
3562 gui_mch_enable_menu(gui.menu_is_active);
3563 Rows = i;
3564 prev_menu_is_active = gui.menu_is_active;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003565 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 if (gui.menu_is_active)
3567 fix_size = TRUE;
3568 }
3569#endif
3570
3571#ifdef FEAT_TOOLBAR
3572 if (using_toolbar != prev_toolbar)
3573 {
3574 gui_mch_show_toolbar(using_toolbar);
3575 prev_toolbar = using_toolbar;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003576 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 if (using_toolbar)
3578 fix_size = TRUE;
3579 }
3580#endif
3581#ifdef FEAT_FOOTER
3582 if (using_footer != prev_footer)
3583 {
3584 gui_mch_enable_footer(using_footer);
3585 prev_footer = using_footer;
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003586 need_set_size |= RESIZE_VERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 if (using_footer)
3588 fix_size = TRUE;
3589 }
3590#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003591#if defined(FEAT_MENU) && !(defined(MSWIN) && !defined(FEAT_TEAROFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 if (using_tearoff != prev_tearoff)
3593 {
3594 gui_mch_toggle_tearoffs(using_tearoff);
3595 prev_tearoff = using_tearoff;
3596 }
3597#endif
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003598 if (need_set_size != 0)
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003599 {
3600#ifdef FEAT_GUI_GTK
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003601 long prev_Columns = Columns;
3602 long prev_Rows = Rows;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003603#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003604 // Adjust the size of the window to make the text area keep the
3605 // same size and to avoid that part of our window is off-screen
3606 // and a scrollbar can't be used, for example.
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003607 gui_set_shellsize(FALSE, fix_size, need_set_size);
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003608
3609#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01003610 // GTK has the annoying habit of sending us resize events when
3611 // changing the window size ourselves. This mostly happens when
3612 // waiting for a character to arrive, quite unpredictably, and may
3613 // change Columns and Rows when we don't want it. Wait for a
3614 // character here to avoid this effect.
3615 // If you remove this, please test this command for resizing
3616 // effects (with optional left scrollbar): ":vsp|q|vsp|q|vsp|q".
3617 // Don't do this while starting up though.
3618 // Don't change Rows when adding menu/toolbar/tabline.
3619 // Don't change Columns when adding vertical toolbar.
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003620 if (!gui.starting && need_set_size != (RESIZE_VERT | RESIZE_HOR))
Bram Moolenaar01a7b9d2005-05-27 20:16:24 +00003621 (void)char_avail();
Bram Moolenaar0133bba2008-12-03 17:50:45 +00003622 if ((need_set_size & RESIZE_VERT) == 0)
3623 Rows = prev_Rows;
3624 if ((need_set_size & RESIZE_HOR) == 0)
3625 Columns = prev_Columns;
Bram Moolenaarc1087e62005-05-20 21:22:17 +00003626#endif
3627 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003628 // When the console tabline appears or disappears the window positions
3629 // change.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003630 if (firstwin->w_winrow != tabline_height())
Bram Moolenaar30613902019-12-01 22:11:18 +01003631 shell_new_rows(); // recompute window positions and heights
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
3633}
3634
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003635#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3636/*
3637 * Return TRUE if the GUI is taking care of the tabline.
3638 * It may still be hidden if 'showtabline' is zero.
3639 */
3640 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003641gui_use_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003642{
3643 return gui.in_use && vim_strchr(p_go, GO_TABLINE) != NULL;
3644}
3645
3646/*
3647 * Return TRUE if the GUI is showing the tabline.
3648 * This uses 'showtabline'.
3649 */
3650 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003651gui_has_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003652{
3653 if (!gui_use_tabline()
3654 || p_stal == 0
3655 || (p_stal == 1 && first_tabpage->tp_next == NULL))
3656 return FALSE;
3657 return TRUE;
3658}
3659
3660/*
3661 * Update the tabline.
3662 * This may display/undisplay the tabline and update the labels.
3663 */
3664 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003665gui_update_tabline(void)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003666{
3667 int showit = gui_has_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003668 int shown = gui_mch_showing_tabline();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003669
3670 if (!gui.starting && starting == 0)
3671 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003672 // Updating the tabline uses direct GUI commands, flush
3673 // outstanding instructions first. (esp. clear screen)
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003674 out_flush();
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00003675
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003676 if (!showit != !shown)
3677 gui_mch_show_tabline(showit);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003678 if (showit != 0)
3679 gui_mch_update_tabline();
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003680
Bram Moolenaar30613902019-12-01 22:11:18 +01003681 // When the tabs change from hidden to shown or from shown to
3682 // hidden the size of the text area should remain the same.
Bram Moolenaar7b5f8322006-03-23 22:47:08 +00003683 if (!showit != !shown)
Bram Moolenaar2e2a2812006-03-27 20:55:21 +00003684 gui_set_shellsize(FALSE, showit, RESIZE_VERT);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003685 }
3686}
3687
3688/*
Bram Moolenaar57657d82006-04-21 22:12:41 +00003689 * Get the label or tooltip for tab page "tp" into NameBuff[].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003690 */
3691 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003692get_tabline_label(
3693 tabpage_T *tp,
Bram Moolenaar30613902019-12-01 22:11:18 +01003694 int tooltip) // TRUE: get tooltip
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003695{
3696 int modified = FALSE;
3697 char_u buf[40];
3698 int wincount;
3699 win_T *wp;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003700 char_u **opt;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003701
Bram Moolenaar30613902019-12-01 22:11:18 +01003702 // Use 'guitablabel' or 'guitabtooltip' if it's set.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003703 opt = (tooltip ? &p_gtt : &p_gtl);
3704 if (**opt != NUL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003705 {
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003706 int use_sandbox = FALSE;
3707 int save_called_emsg = called_emsg;
3708 char_u res[MAXPATHL];
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003709 tabpage_T *save_curtab;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003710 char_u *opt_name = (char_u *)(tooltip ? "guitabtooltip"
3711 : "guitablabel");
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003712
3713 called_emsg = FALSE;
3714
3715 printer_page_num = tabpage_index(tp);
3716# ifdef FEAT_EVAL
3717 set_vim_var_nr(VV_LNUM, printer_page_num);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003718 use_sandbox = was_set_insecurely(opt_name, 0);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003719# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003720 // It's almost as going to the tabpage, but without autocommands.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003721 curtab->tp_firstwin = firstwin;
3722 curtab->tp_lastwin = lastwin;
3723 curtab->tp_curwin = curwin;
3724 save_curtab = curtab;
3725 curtab = tp;
3726 topframe = curtab->tp_topframe;
3727 firstwin = curtab->tp_firstwin;
3728 lastwin = curtab->tp_lastwin;
3729 curwin = curtab->tp_curwin;
3730 curbuf = curwin->w_buffer;
3731
Bram Moolenaar30613902019-12-01 22:11:18 +01003732 // Can't use NameBuff directly, build_stl_str_hl() uses it.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003733 build_stl_str_hl(curwin, res, MAXPATHL, *opt, use_sandbox,
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003734 0, (int)Columns, NULL, NULL);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003735 STRCPY(NameBuff, res);
3736
Bram Moolenaar30613902019-12-01 22:11:18 +01003737 // Back to the original curtab.
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003738 curtab = save_curtab;
3739 topframe = curtab->tp_topframe;
3740 firstwin = curtab->tp_firstwin;
3741 lastwin = curtab->tp_lastwin;
3742 curwin = curtab->tp_curwin;
3743 curbuf = curwin->w_buffer;
3744
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003745 if (called_emsg)
Bram Moolenaar57657d82006-04-21 22:12:41 +00003746 set_string_option_direct(opt_name, -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003747 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003748 called_emsg |= save_called_emsg;
3749 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00003750
Bram Moolenaar30613902019-12-01 22:11:18 +01003751 // If 'guitablabel'/'guitabtooltip' is not set or the result is empty then
3752 // use a default label.
Bram Moolenaard68071d2006-05-02 22:08:30 +00003753 if (**opt == NUL || *NameBuff == NUL)
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003754 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003755 // Get the buffer name into NameBuff[] and shorten it.
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003756 get_trans_bufname(tp == curtab ? curbuf : tp->tp_curwin->w_buffer);
Bram Moolenaar57657d82006-04-21 22:12:41 +00003757 if (!tooltip)
3758 shorten_dir(NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003759
3760 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
3761 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
3762 if (bufIsChanged(wp->w_buffer))
3763 modified = TRUE;
3764 if (modified || wincount > 1)
3765 {
3766 if (wincount > 1)
3767 vim_snprintf((char *)buf, sizeof(buf), "%d", wincount);
3768 else
3769 buf[0] = NUL;
3770 if (modified)
3771 STRCAT(buf, "+");
3772 STRCAT(buf, " ");
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00003773 STRMOVE(NameBuff + STRLEN(buf), NameBuff);
Bram Moolenaarc542aef2006-02-25 21:47:41 +00003774 mch_memmove(NameBuff, buf, STRLEN(buf));
3775 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003776 }
3777}
3778
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003779/*
3780 * Send the event for clicking to select tab page "nr".
3781 * Returns TRUE if it was done, FALSE when skipped because we are already at
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003782 * that tab page or the cmdline window is open.
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003783 */
3784 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003785send_tabline_event(int nr)
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003786{
3787 char_u string[3];
3788
3789 if (nr == tabpage_index(curtab))
3790 return FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003791
Bram Moolenaar30613902019-12-01 22:11:18 +01003792 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003793 if (hold_gui_events
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003794# ifdef FEAT_CMDWIN
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003795 || cmdwin_type != 0
3796# endif
3797 )
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003798 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003799 // Set it back to the current tab page.
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003800 gui_mch_set_curtab(tabpage_index(curtab));
3801 return FALSE;
3802 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003803
Bram Moolenaar1cad2922006-02-27 00:00:52 +00003804 string[0] = CSI;
3805 string[1] = KS_TABLINE;
3806 string[2] = KE_FILLER;
3807 add_to_input_buf(string, 3);
3808 string[0] = nr;
3809 add_to_input_buf_csi(string, 1);
3810 return TRUE;
3811}
3812
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003813/*
3814 * Send a tabline menu event
3815 */
3816 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003817send_tabline_menu_event(int tabidx, int event)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003818{
3819 char_u string[3];
3820
Bram Moolenaar29547192018-12-11 20:39:19 +01003821 // Don't put events in the input queue now.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003822 if (hold_gui_events)
3823 return;
3824
Bram Moolenaar29547192018-12-11 20:39:19 +01003825 // Cannot close the last tabpage.
3826 if (event == TABLINE_MENU_CLOSE && first_tabpage->tp_next == NULL)
3827 return;
3828
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003829 string[0] = CSI;
3830 string[1] = KS_TABMENU;
3831 string[2] = KE_FILLER;
3832 add_to_input_buf(string, 3);
3833 string[0] = tabidx;
3834 string[1] = (char_u)(long)event;
3835 add_to_input_buf_csi(string, 2);
3836}
3837
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839
3840/*
3841 * Scrollbar stuff:
3842 */
3843
Bram Moolenaare45828b2006-02-15 22:12:56 +00003844/*
3845 * Remove all scrollbars. Used before switching to another tab page.
3846 */
3847 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003848gui_remove_scrollbars(void)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003849{
3850 int i;
3851 win_T *wp;
3852
3853 for (i = 0; i < 3; i++)
3854 {
3855 if (i == SBAR_BOTTOM)
3856 gui_mch_enable_scrollbar(&gui.bottom_sbar, FALSE);
3857 else
3858 {
3859 FOR_ALL_WINDOWS(wp)
Bram Moolenaare45828b2006-02-15 22:12:56 +00003860 gui_do_scrollbar(wp, i, FALSE);
Bram Moolenaare45828b2006-02-15 22:12:56 +00003861 }
Bram Moolenaar371d5402006-03-20 21:47:49 +00003862 curtab->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaare45828b2006-02-15 22:12:56 +00003863 }
3864}
Bram Moolenaare45828b2006-02-15 22:12:56 +00003865
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003867gui_create_scrollbar(scrollbar_T *sb, int type, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868{
3869 static int sbar_ident = 0;
3870
Bram Moolenaar30613902019-12-01 22:11:18 +01003871 sb->ident = sbar_ident++; // No check for too big, but would it happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 sb->wp = wp;
3873 sb->type = type;
3874 sb->value = 0;
3875#ifdef FEAT_GUI_ATHENA
3876 sb->pixval = 0;
3877#endif
3878 sb->size = 1;
3879 sb->max = 1;
3880 sb->top = 0;
3881 sb->height = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 sb->width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 sb->status_height = 0;
3884 gui_mch_create_scrollbar(sb, (wp == NULL) ? SBAR_HORIZ : SBAR_VERT);
3885}
3886
3887/*
3888 * Find the scrollbar with the given index.
3889 */
3890 scrollbar_T *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003891gui_find_scrollbar(long ident)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892{
3893 win_T *wp;
3894
3895 if (gui.bottom_sbar.ident == ident)
3896 return &gui.bottom_sbar;
3897 FOR_ALL_WINDOWS(wp)
3898 {
3899 if (wp->w_scrollbars[SBAR_LEFT].ident == ident)
3900 return &wp->w_scrollbars[SBAR_LEFT];
3901 if (wp->w_scrollbars[SBAR_RIGHT].ident == ident)
3902 return &wp->w_scrollbars[SBAR_RIGHT];
3903 }
3904 return NULL;
3905}
3906
3907/*
3908 * For most systems: Put a code in the input buffer for a dragged scrollbar.
3909 *
3910 * For Win32, Macintosh and GTK+ 2:
3911 * Scrollbars seem to grab focus and vim doesn't read the input queue until
3912 * you stop dragging the scrollbar. We get here each time the scrollbar is
3913 * dragged another pixel, but as far as the rest of vim goes, it thinks
3914 * we're just hanging in the call to DispatchMessage() in
3915 * process_message(). The DispatchMessage() call that hangs was passed a
3916 * mouse button click event in the scrollbar window. -- webb.
3917 *
3918 * Solution: Do the scrolling right here. But only when allowed.
3919 * Ignore the scrollbars while executing an external command or when there
3920 * are still characters to be processed.
3921 */
3922 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003923gui_drag_scrollbar(scrollbar_T *sb, long value, int still_dragging)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 int sb_num;
3927#ifdef USE_ON_FLY_SCROLL
3928 colnr_T old_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 linenr_T old_topline = curwin->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930# ifdef FEAT_DIFF
3931 int old_topfill = curwin->w_topfill;
3932# endif
3933#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00003934 char_u bytes[sizeof(long_u)];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 int byte_count;
3936#endif
3937
3938 if (sb == NULL)
3939 return;
3940
Bram Moolenaar30613902019-12-01 22:11:18 +01003941 // Don't put events in the input queue now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if (hold_gui_events)
3943 return;
3944
3945#ifdef FEAT_CMDWIN
3946 if (cmdwin_type != 0 && sb->wp != curwin)
3947 return;
3948#endif
3949
3950 if (still_dragging)
3951 {
3952 if (sb->wp == NULL)
3953 gui.dragged_sb = SBAR_BOTTOM;
3954 else if (sb == &sb->wp->w_scrollbars[SBAR_LEFT])
3955 gui.dragged_sb = SBAR_LEFT;
3956 else
3957 gui.dragged_sb = SBAR_RIGHT;
3958 gui.dragged_wp = sb->wp;
3959 }
3960 else
3961 {
3962 gui.dragged_sb = SBAR_NONE;
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02003963#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01003964 // Keep the "dragged_wp" value until after the scrolling, for when the
3965 // mouse button is released. GTK2 doesn't send the button-up event.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 gui.dragged_wp = NULL;
3967#endif
3968 }
3969
Bram Moolenaar30613902019-12-01 22:11:18 +01003970 // Vertical sbar info is kept in the first sbar (the left one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 if (sb->wp != NULL)
3972 sb = &sb->wp->w_scrollbars[0];
3973
3974 /*
3975 * Check validity of value
3976 */
3977 if (value < 0)
3978 value = 0;
3979#ifdef SCROLL_PAST_END
3980 else if (value > sb->max)
3981 value = sb->max;
3982#else
3983 if (value > sb->max - sb->size + 1)
3984 value = sb->max - sb->size + 1;
3985#endif
3986
3987 sb->value = value;
3988
3989#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar30613902019-12-01 22:11:18 +01003990 // When not allowed to do the scrolling right now, return.
3991 // This also checked input_available(), but that causes the first click in
3992 // a scrollbar to be ignored when Vim doesn't have focus.
Bram Moolenaar67840782008-01-03 15:15:07 +00003993 if (dont_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 return;
3995#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01003996 // Disallow scrolling the current window when the completion popup menu is
3997 // visible.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00003998 if ((sb->wp == NULL || sb->wp == curwin) && pum_visible())
3999 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
4001#ifdef FEAT_RIGHTLEFT
4002 if (sb->wp == NULL && curwin->w_p_rl)
4003 {
4004 value = sb->max + 1 - sb->size - value;
4005 if (value < 0)
4006 value = 0;
4007 }
4008#endif
4009
Bram Moolenaar30613902019-12-01 22:11:18 +01004010 if (sb->wp != NULL) // vertical scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
4012 sb_num = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 for (wp = firstwin; wp != sb->wp && wp != NULL; wp = wp->w_next)
4014 sb_num++;
4015 if (wp == NULL)
4016 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
4018#ifdef USE_ON_FLY_SCROLL
4019 current_scrollbar = sb_num;
4020 scrollbar_value = value;
4021 if (State & NORMAL)
4022 {
4023 gui_do_scroll();
4024 setcursor();
4025 }
4026 else if (State & INSERT)
4027 {
4028 ins_scroll();
4029 setcursor();
4030 }
4031 else if (State & CMDLINE)
4032 {
4033 if (msg_scrolled == 0)
4034 {
4035 gui_do_scroll();
4036 redrawcmdline();
4037 }
4038 }
4039# ifdef FEAT_FOLDING
Bram Moolenaar30613902019-12-01 22:11:18 +01004040 // Value may have been changed for closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 sb->value = sb->wp->w_topline - 1;
4042# endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00004043
Bram Moolenaar30613902019-12-01 22:11:18 +01004044 // When dragging one scrollbar and there is another one at the other
4045 // side move the thumb of that one too.
Bram Moolenaar371d5402006-03-20 21:47:49 +00004046 if (gui.which_scrollbars[SBAR_RIGHT] && gui.which_scrollbars[SBAR_LEFT])
4047 gui_mch_set_scrollbar_thumb(
4048 &sb->wp->w_scrollbars[
4049 sb == &sb->wp->w_scrollbars[SBAR_RIGHT]
4050 ? SBAR_LEFT : SBAR_RIGHT],
4051 sb->value, sb->size, sb->max);
4052
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053#else
4054 bytes[0] = CSI;
4055 bytes[1] = KS_VER_SCROLLBAR;
4056 bytes[2] = KE_FILLER;
4057 bytes[3] = (char_u)sb_num;
4058 byte_count = 4;
4059#endif
4060 }
4061 else
4062 {
4063#ifdef USE_ON_FLY_SCROLL
4064 scrollbar_value = value;
4065
4066 if (State & NORMAL)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004067 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 else if (State & INSERT)
4069 ins_horscroll();
4070 else if (State & CMDLINE)
4071 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004072 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004074 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 redrawcmdline();
4076 }
4077 }
4078 if (old_leftcol != curwin->w_leftcol)
4079 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004080 updateWindow(curwin); // update window, status and cmdline
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 setcursor();
4082 }
4083#else
4084 bytes[0] = CSI;
4085 bytes[1] = KS_HOR_SCROLLBAR;
4086 bytes[2] = KE_FILLER;
4087 byte_count = 3;
4088#endif
4089 }
4090
4091#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 /*
4093 * synchronize other windows, as necessary according to 'scrollbind'
4094 */
4095 if (curwin->w_p_scb
4096 && ((sb->wp == NULL && curwin->w_leftcol != old_leftcol)
4097 || (sb->wp == curwin && (curwin->w_topline != old_topline
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004098# ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 || curwin->w_topfill != old_topfill
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01004100# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 ))))
4102 {
4103 do_check_scrollbind(TRUE);
Bram Moolenaar30613902019-12-01 22:11:18 +01004104 // need to update the window right here
Bram Moolenaar29323592016-07-24 22:04:11 +02004105 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 if (wp->w_redr_type > 0)
4107 updateWindow(wp);
4108 setcursor();
4109 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01004110 out_flush_cursor(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111#else
Bram Moolenaar110bc6b2006-02-10 23:13:40 +00004112 add_to_input_buf(bytes, byte_count);
4113 add_long_to_buf((long_u)value, bytes);
4114 add_to_input_buf_csi(bytes, sizeof(long_u));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115#endif
4116}
4117
4118/*
4119 * Scrollbar stuff:
4120 */
4121
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004122/*
4123 * Called when something in the window layout has changed.
4124 */
4125 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004126gui_may_update_scrollbars(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004127{
4128 if (gui.in_use && starting == 0)
4129 {
4130 out_flush();
4131 gui_init_which_components(NULL);
4132 gui_update_scrollbars(TRUE);
4133 }
4134 need_mouse_correct = TRUE;
4135}
4136
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004138gui_update_scrollbars(
Bram Moolenaar30613902019-12-01 22:11:18 +01004139 int force) // Force all scrollbars to get updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140{
4141 win_T *wp;
4142 scrollbar_T *sb;
Bram Moolenaar30613902019-12-01 22:11:18 +01004143 long val, size, max; // need 32 bits here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 int which_sb;
4145 int h, y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 static win_T *prev_curwin = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
Bram Moolenaar30613902019-12-01 22:11:18 +01004148 // Update the horizontal scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 gui_update_horiz_scrollbar(force);
4150
Bram Moolenaar4f974752019-02-17 17:44:42 +01004151#ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01004152 // Return straight away if there is neither a left nor right scrollbar.
4153 // On MS-Windows this is required anyway for scrollwheel messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 if (!gui.which_scrollbars[SBAR_LEFT] && !gui.which_scrollbars[SBAR_RIGHT])
4155 return;
4156#endif
4157
4158 /*
4159 * Don't want to update a scrollbar while we're dragging it. But if we
4160 * have both a left and right scrollbar, and we drag one of them, we still
4161 * need to update the other one.
4162 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004163 if (!force && (gui.dragged_sb == SBAR_LEFT || gui.dragged_sb == SBAR_RIGHT)
4164 && gui.which_scrollbars[SBAR_LEFT]
4165 && gui.which_scrollbars[SBAR_RIGHT])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 {
4167 /*
4168 * If we have two scrollbars and one of them is being dragged, just
4169 * copy the scrollbar position from the dragged one to the other one.
4170 */
4171 which_sb = SBAR_LEFT + SBAR_RIGHT - gui.dragged_sb;
4172 if (gui.dragged_wp != NULL)
4173 gui_mch_set_scrollbar_thumb(
4174 &gui.dragged_wp->w_scrollbars[which_sb],
4175 gui.dragged_wp->w_scrollbars[0].value,
4176 gui.dragged_wp->w_scrollbars[0].size,
4177 gui.dragged_wp->w_scrollbars[0].max);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179
Bram Moolenaar30613902019-12-01 22:11:18 +01004180 // avoid that moving components around generates events
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 ++hold_gui_events;
4182
Bram Moolenaar45a24952016-07-24 22:25:15 +02004183 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004185 if (wp->w_buffer == NULL) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 continue;
Bram Moolenaar30613902019-12-01 22:11:18 +01004187 // Skip a scrollbar that is being dragged.
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004188 if (!force && (gui.dragged_sb == SBAR_LEFT
4189 || gui.dragged_sb == SBAR_RIGHT)
4190 && gui.dragged_wp == wp)
4191 continue;
4192
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193#ifdef SCROLL_PAST_END
4194 max = wp->w_buffer->b_ml.ml_line_count - 1;
4195#else
4196 max = wp->w_buffer->b_ml.ml_line_count + wp->w_height - 2;
4197#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004198 if (max < 0) // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 max = 0;
4200 val = wp->w_topline - 1;
4201 size = wp->w_height;
4202#ifdef SCROLL_PAST_END
Bram Moolenaar30613902019-12-01 22:11:18 +01004203 if (val > max) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 val = max;
4205#else
Bram Moolenaar30613902019-12-01 22:11:18 +01004206 if (size > max + 1) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 size = max + 1;
4208 if (val > max - size + 1)
4209 val = max - size + 1;
4210#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004211 if (val < 0) // minimal value is 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 val = 0;
4213
4214 /*
4215 * Scrollbar at index 0 (the left one) contains all the information.
4216 * It would be the same info for left and right so we just store it for
4217 * one of them.
4218 */
4219 sb = &wp->w_scrollbars[0];
4220
4221 /*
4222 * Note: no check for valid w_botline. If it's not valid the
4223 * scrollbars will be updated later anyway.
4224 */
4225 if (size < 1 || wp->w_botline - 2 > max)
4226 {
4227 /*
4228 * This can happen during changing files. Just don't update the
4229 * scrollbar for now.
4230 */
Bram Moolenaar30613902019-12-01 22:11:18 +01004231 sb->height = 0; // Force update next time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 if (gui.which_scrollbars[SBAR_LEFT])
4233 gui_do_scrollbar(wp, SBAR_LEFT, FALSE);
4234 if (gui.which_scrollbars[SBAR_RIGHT])
4235 gui_do_scrollbar(wp, SBAR_RIGHT, FALSE);
4236 continue;
4237 }
4238 if (force || sb->height != wp->w_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 || sb->top != wp->w_winrow
4240 || sb->status_height != wp->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 || sb->width != wp->w_width
Bram Moolenaar4033c552017-09-16 20:54:51 +02004242 || prev_curwin != curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004244 // Height, width or position of scrollbar has changed. For
4245 // vertical split: curwin changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 sb->height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 sb->top = wp->w_winrow;
4248 sb->status_height = wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 sb->width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
Bram Moolenaar30613902019-12-01 22:11:18 +01004251 // Calculate height and position in pixels
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 h = (sb->height + sb->status_height) * gui.char_height;
4253 y = sb->top * gui.char_height + gui.border_offset;
4254#if defined(FEAT_MENU) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_PHOTON)
4255 if (gui.menu_is_active)
4256 y += gui.menu_height;
4257#endif
4258
4259#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_ATHENA))
4260 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
4261# ifdef FEAT_GUI_ATHENA
4262 y += gui.toolbar_height;
4263# else
4264# ifdef FEAT_GUI_MSWIN
4265 y += TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
4266# endif
4267# endif
4268#endif
4269
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004270#if defined(FEAT_GUI_TABLINE) && defined(FEAT_GUI_MSWIN)
4271 if (gui_has_tabline())
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004272 y += gui.tabline_height;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004273#endif
4274
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 if (wp->w_winrow == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004277 // Height of top scrollbar includes width of top border
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 h += gui.border_offset;
4279 y -= gui.border_offset;
4280 }
4281 if (gui.which_scrollbars[SBAR_LEFT])
4282 {
4283 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_LEFT],
4284 gui.left_sbar_x, y,
4285 gui.scrollbar_width, h);
4286 gui_do_scrollbar(wp, SBAR_LEFT, TRUE);
4287 }
4288 if (gui.which_scrollbars[SBAR_RIGHT])
4289 {
4290 gui_mch_set_scrollbar_pos(&wp->w_scrollbars[SBAR_RIGHT],
4291 gui.right_sbar_x, y,
4292 gui.scrollbar_width, h);
4293 gui_do_scrollbar(wp, SBAR_RIGHT, TRUE);
4294 }
4295 }
4296
Bram Moolenaar30613902019-12-01 22:11:18 +01004297 // Reduce the number of calls to gui_mch_set_scrollbar_thumb() by
4298 // checking if the thumb moved at least a pixel. Only do this for
4299 // Athena, most other GUIs require the update anyway to make the
4300 // arrows work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301#ifdef FEAT_GUI_ATHENA
4302 if (max == 0)
4303 y = 0;
4304 else
4305 y = (val * (sb->height + 2) * gui.char_height + max / 2) / max;
4306 if (force || sb->pixval != y || sb->size != size || sb->max != max)
4307#else
4308 if (force || sb->value != val || sb->size != size || sb->max != max)
4309#endif
4310 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004311 // Thumb of scrollbar has moved
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 sb->value = val;
4313#ifdef FEAT_GUI_ATHENA
4314 sb->pixval = y;
4315#endif
4316 sb->size = size;
4317 sb->max = max;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004318 if (gui.which_scrollbars[SBAR_LEFT]
4319 && (gui.dragged_sb != SBAR_LEFT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_LEFT],
4321 val, size, max);
4322 if (gui.which_scrollbars[SBAR_RIGHT]
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004323 && (gui.dragged_sb != SBAR_RIGHT || gui.dragged_wp != wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 gui_mch_set_scrollbar_thumb(&wp->w_scrollbars[SBAR_RIGHT],
4325 val, size, max);
4326 }
4327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 prev_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 --hold_gui_events;
4330}
4331
4332/*
4333 * Enable or disable a scrollbar.
4334 * Check for scrollbars for vertically split windows which are not enabled
4335 * sometimes.
4336 */
4337 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004338gui_do_scrollbar(
4339 win_T *wp,
Bram Moolenaar30613902019-12-01 22:11:18 +01004340 int which, // SBAR_LEFT or SBAR_RIGHT
4341 int enable) // TRUE to enable scrollbar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 int midcol = curwin->w_wincol + curwin->w_width / 2;
4344 int has_midcol = (wp->w_wincol <= midcol
4345 && wp->w_wincol + wp->w_width >= midcol);
4346
Bram Moolenaar30613902019-12-01 22:11:18 +01004347 // Only enable scrollbars that contain the middle column of the current
4348 // window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 if (gui.which_scrollbars[SBAR_RIGHT] != gui.which_scrollbars[SBAR_LEFT])
4350 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004351 // Scrollbars only on one side. Don't enable scrollbars that don't
4352 // contain the middle column of the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 if (!has_midcol)
4354 enable = FALSE;
4355 }
4356 else
4357 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004358 // Scrollbars on both sides. Don't enable scrollbars that neither
4359 // contain the middle column of the current window nor are on the far
4360 // side.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 if (midcol > Columns / 2)
4362 {
4363 if (which == SBAR_LEFT ? wp->w_wincol != 0 : !has_midcol)
4364 enable = FALSE;
4365 }
4366 else
4367 {
4368 if (which == SBAR_RIGHT ? wp->w_wincol + wp->w_width != Columns
4369 : !has_midcol)
4370 enable = FALSE;
4371 }
4372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 gui_mch_enable_scrollbar(&wp->w_scrollbars[which], enable);
4374}
4375
4376/*
4377 * Scroll a window according to the values set in the globals current_scrollbar
4378 * and scrollbar_value. Return TRUE if the cursor in the current window moved
4379 * or FALSE otherwise.
4380 */
4381 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004382gui_do_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383{
4384 win_T *wp, *save_wp;
4385 int i;
4386 long nlines;
4387 pos_T old_cursor;
4388 linenr_T old_topline;
4389#ifdef FEAT_DIFF
4390 int old_topfill;
4391#endif
4392
4393 for (wp = firstwin, i = 0; i < current_scrollbar; wp = W_NEXT(wp), i++)
4394 if (wp == NULL)
4395 break;
4396 if (wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004397 // Couldn't find window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 return FALSE;
4399
4400 /*
4401 * Compute number of lines to scroll. If zero, nothing to do.
4402 */
4403 nlines = (long)scrollbar_value + 1 - (long)wp->w_topline;
4404 if (nlines == 0)
4405 return FALSE;
4406
4407 save_wp = curwin;
4408 old_topline = wp->w_topline;
4409#ifdef FEAT_DIFF
4410 old_topfill = wp->w_topfill;
4411#endif
4412 old_cursor = wp->w_cursor;
4413 curwin = wp;
4414 curbuf = wp->w_buffer;
4415 if (nlines < 0)
4416 scrolldown(-nlines, gui.dragged_wp == NULL);
4417 else
4418 scrollup(nlines, gui.dragged_wp == NULL);
Bram Moolenaar30613902019-12-01 22:11:18 +01004419 // Reset dragged_wp after using it. "dragged_sb" will have been reset for
4420 // the mouse-up event already, but we still want it to behave like when
4421 // dragging. But not the next click in an arrow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 if (gui.dragged_sb == SBAR_NONE)
4423 gui.dragged_wp = NULL;
4424
4425 if (old_topline != wp->w_topline
4426#ifdef FEAT_DIFF
4427 || old_topfill != wp->w_topfill
4428#endif
4429 )
4430 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01004431 if (get_scrolloff_value() != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004433 cursor_correct(); // fix window for 'so'
4434 update_topline(); // avoid up/down jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 }
4436 if (old_cursor.lnum != wp->w_cursor.lnum)
4437 coladvance(wp->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 wp->w_scbind_pos = wp->w_topline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440
Bram Moolenaar30613902019-12-01 22:11:18 +01004441 // Make sure wp->w_leftcol and wp->w_skipcol are correct.
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004442 validate_cursor();
4443
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 curwin = save_wp;
4445 curbuf = save_wp->w_buffer;
4446
4447 /*
4448 * Don't call updateWindow() when nothing has changed (it will overwrite
4449 * the status line!).
4450 */
4451 if (old_topline != wp->w_topline
Bram Moolenaar578b49e2005-09-10 19:22:57 +00004452 || wp->w_redr_type != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453#ifdef FEAT_DIFF
4454 || old_topfill != wp->w_topfill
4455#endif
4456 )
4457 {
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004458 int type = VALID;
4459
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004460 if (pum_visible())
4461 {
4462 type = NOT_VALID;
4463 wp->w_lines_valid = 0;
4464 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004465
Bram Moolenaar30613902019-12-01 22:11:18 +01004466 // Don't set must_redraw here, it may cause the popup menu to
4467 // disappear when losing focus after a scrollbar drag.
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00004468 if (wp->w_redr_type < type)
4469 wp->w_redr_type = type;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004470 mch_disable_flush();
Bram Moolenaar30613902019-12-01 22:11:18 +01004471 updateWindow(wp); // update window, status line, and cmdline
Bram Moolenaara338adc2018-01-31 20:51:47 +01004472 mch_enable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474
Bram Moolenaar30613902019-12-01 22:11:18 +01004475 // May need to redraw the popup menu.
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004476 if (pum_visible())
4477 pum_redraw();
Bram Moolenaar05bb82f2006-09-10 19:39:25 +00004478
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004479 return (wp == curwin && !EQUAL_POS(curwin->w_cursor, old_cursor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480}
4481
4482
4483/*
4484 * Horizontal scrollbar stuff:
4485 */
4486
4487/*
4488 * Return length of line "lnum" for horizontal scrolling.
4489 */
4490 static colnr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004491scroll_line_len(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492{
4493 char_u *p;
4494 colnr_T col;
4495 int w;
4496
4497 p = ml_get(lnum);
4498 col = 0;
4499 if (*p != NUL)
4500 for (;;)
4501 {
4502 w = chartabsize(p, col);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004503 MB_PTR_ADV(p);
Bram Moolenaar30613902019-12-01 22:11:18 +01004504 if (*p == NUL) // don't count the last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 break;
4506 col += w;
4507 }
4508 return col;
4509}
4510
Bram Moolenaar30613902019-12-01 22:11:18 +01004511// Remember which line is currently the longest, so that we don't have to
4512// search for it when scrolling horizontally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513static linenr_T longest_lnum = 0;
4514
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004515/*
4516 * Find longest visible line number. If this is not possible (or not desired,
4517 * by setting 'h' in "guioptions") then the current line number is returned.
4518 */
4519 static linenr_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004520gui_find_longest_lnum(void)
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004521{
4522 linenr_T ret = 0;
4523
Bram Moolenaar30613902019-12-01 22:11:18 +01004524 // Calculate maximum for horizontal scrollbar. Check for reasonable
4525 // line numbers, topline and botline can be invalid when displaying is
4526 // postponed.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004527 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
4528 && curwin->w_topline <= curwin->w_cursor.lnum
4529 && curwin->w_botline > curwin->w_cursor.lnum
4530 && curwin->w_botline <= curbuf->b_ml.ml_line_count + 1)
4531 {
4532 linenr_T lnum;
4533 colnr_T n;
4534 long max = 0;
4535
Bram Moolenaar30613902019-12-01 22:11:18 +01004536 // Use maximum of all visible lines. Remember the lnum of the
4537 // longest line, closest to the cursor line. Used when scrolling
4538 // below.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004539 for (lnum = curwin->w_topline; lnum < curwin->w_botline; ++lnum)
4540 {
4541 n = scroll_line_len(lnum);
4542 if (n > (colnr_T)max)
4543 {
4544 max = n;
4545 ret = lnum;
4546 }
4547 else if (n == (colnr_T)max
4548 && abs((int)(lnum - curwin->w_cursor.lnum))
4549 < abs((int)(ret - curwin->w_cursor.lnum)))
4550 ret = lnum;
4551 }
4552 }
4553 else
Bram Moolenaar30613902019-12-01 22:11:18 +01004554 // Use cursor line only.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004555 ret = curwin->w_cursor.lnum;
4556
4557 return ret;
4558}
4559
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004561gui_update_horiz_scrollbar(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562{
Bram Moolenaar30613902019-12-01 22:11:18 +01004563 long value, size, max; // need 32 bit ints here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
4565 if (!gui.which_scrollbars[SBAR_BOTTOM])
4566 return;
4567
4568 if (!force && gui.dragged_sb == SBAR_BOTTOM)
4569 return;
4570
4571 if (!force && curwin->w_p_wrap && gui.prev_wrap)
4572 return;
4573
4574 /*
4575 * It is possible for the cursor to be invalid if we're in the middle of
4576 * something (like changing files). If so, don't do anything for now.
4577 */
4578 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4579 {
4580 gui.bottom_sbar.value = -1;
4581 return;
4582 }
4583
Bram Moolenaar02631462017-09-22 15:20:32 +02004584 size = curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 if (curwin->w_p_wrap)
4586 {
4587 value = 0;
4588#ifdef SCROLL_PAST_END
4589 max = 0;
4590#else
Bram Moolenaar02631462017-09-22 15:20:32 +02004591 max = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592#endif
4593 }
4594 else
4595 {
4596 value = curwin->w_leftcol;
4597
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004598 longest_lnum = gui_find_longest_lnum();
4599 max = scroll_line_len(longest_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 if (virtual_active())
4602 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004603 // May move the cursor even further to the right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (curwin->w_virtcol >= (colnr_T)max)
4605 max = curwin->w_virtcol;
4606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607
4608#ifndef SCROLL_PAST_END
Bram Moolenaar02631462017-09-22 15:20:32 +02004609 max += curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610#endif
Bram Moolenaar30613902019-12-01 22:11:18 +01004611 // The line number isn't scrolled, thus there is less space when
4612 // 'number' or 'relativenumber' is set (also for 'foldcolumn').
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 size -= curwin_col_off();
4614#ifndef SCROLL_PAST_END
4615 max -= curwin_col_off();
4616#endif
4617 }
4618
4619#ifndef SCROLL_PAST_END
4620 if (value > max - size + 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01004621 value = max - size + 1; // limit the value to allowable range
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622#endif
4623
4624#ifdef FEAT_RIGHTLEFT
4625 if (curwin->w_p_rl)
4626 {
4627 value = max + 1 - size - value;
4628 if (value < 0)
4629 {
4630 size += value;
4631 value = 0;
4632 }
4633 }
4634#endif
4635 if (!force && value == gui.bottom_sbar.value && size == gui.bottom_sbar.size
4636 && max == gui.bottom_sbar.max)
4637 return;
4638
4639 gui.bottom_sbar.value = value;
4640 gui.bottom_sbar.size = size;
4641 gui.bottom_sbar.max = max;
4642 gui.prev_wrap = curwin->w_p_wrap;
4643
4644 gui_mch_set_scrollbar_thumb(&gui.bottom_sbar, value, size, max);
4645}
4646
4647/*
4648 * Do a horizontal scroll. Return TRUE if the cursor moved, FALSE otherwise.
4649 */
4650 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004651gui_do_horiz_scroll(long_u leftcol, int compute_longest_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652{
Bram Moolenaar30613902019-12-01 22:11:18 +01004653 // no wrapping, no scrolling
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 if (curwin->w_p_wrap)
4655 return FALSE;
4656
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004657 if (curwin->w_leftcol == (colnr_T)leftcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 return FALSE;
4659
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004660 curwin->w_leftcol = (colnr_T)leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
Bram Moolenaar30613902019-12-01 22:11:18 +01004662 // When the line of the cursor is too short, move the cursor to the
4663 // longest visible line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 if (vim_strchr(p_go, GO_HORSCROLL) == NULL
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004665 && !virtual_active()
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004666 && (colnr_T)leftcol > scroll_line_len(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004668 if (compute_longest_lnum)
4669 {
4670 curwin->w_cursor.lnum = gui_find_longest_lnum();
4671 curwin->w_cursor.col = 0;
4672 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004673 // Do a sanity check on "longest_lnum", just in case.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004674 else if (longest_lnum >= curwin->w_topline
4675 && longest_lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 {
4677 curwin->w_cursor.lnum = longest_lnum;
4678 curwin->w_cursor.col = 0;
4679 }
4680 }
4681
4682 return leftcol_changed();
4683}
4684
4685/*
4686 * Check that none of the colors are the same as the background color
4687 */
4688 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004689gui_check_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
4691 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4692 {
4693 gui_set_bg_color((char_u *)"White");
4694 if (gui.norm_pixel == gui.back_pixel || gui.norm_pixel == INVALCOLOR)
4695 gui_set_fg_color((char_u *)"Black");
4696 }
4697}
4698
Bram Moolenaar3918c952005-03-15 22:34:55 +00004699 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004700gui_set_fg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
4702 gui.norm_pixel = gui_get_color(name);
4703 hl_set_fg_color_name(vim_strsave(name));
4704}
4705
Bram Moolenaar3918c952005-03-15 22:34:55 +00004706 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004707gui_set_bg_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708{
4709 gui.back_pixel = gui_get_color(name);
4710 hl_set_bg_color_name(vim_strsave(name));
4711}
4712
4713/*
4714 * Allocate a color by name.
4715 * Returns INVALCOLOR and gives an error message when failed.
4716 */
4717 guicolor_T
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004718gui_get_color(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719{
4720 guicolor_T t;
4721
4722 if (*name == NUL)
4723 return INVALCOLOR;
4724 t = gui_mch_get_color(name);
Bram Moolenaar843ee412004-06-30 16:16:41 +00004725
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 if (t == INVALCOLOR
Bram Moolenaar9372a112005-12-06 19:59:18 +00004727#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 && gui.in_use
4729#endif
4730 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004731 semsg(_("E254: Cannot allocate color %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 return t;
4733}
4734
4735/*
4736 * Return the grey value of a color (range 0-255).
4737 */
4738 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004739gui_get_lightness(guicolor_T pixel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02004741 long_u rgb = (long_u)gui_mch_get_rgb(pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004743 return (int)( (((rgb >> 16) & 0xff) * 299)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004744 + (((rgb >> 8) & 0xff) * 587)
4745 + ((rgb & 0xff) * 114)) / 1000;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746}
4747
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004748 char_u *
4749gui_bg_default(void)
4750{
4751 if (gui_get_lightness(gui.back_pixel) < 127)
4752 return (char_u *)"dark";
4753 return (char_u *)"light";
4754}
4755
4756/*
4757 * Option initializations that can only be done after opening the GUI window.
4758 */
4759 void
4760init_gui_options(void)
4761{
Bram Moolenaar30613902019-12-01 22:11:18 +01004762 // Set the 'background' option according to the lightness of the
4763 // background color, unless the user has set it already.
Bram Moolenaar7bae0b12019-11-21 22:14:18 +01004764 if (!option_was_set((char_u *)"bg") && STRCMP(p_bg, gui_bg_default()) != 0)
4765 {
4766 set_option_value((char_u *)"bg", 0L, gui_bg_default(), 0);
4767 highlight_changed();
4768 }
4769}
4770
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771#if defined(FEAT_GUI_X11) || defined(PROTO)
4772 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004773gui_new_scrollbar_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774{
4775 win_T *wp;
4776
Bram Moolenaar30613902019-12-01 22:11:18 +01004777 // Nothing to do if GUI hasn't started yet.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 if (!gui.in_use)
4779 return;
4780
4781 FOR_ALL_WINDOWS(wp)
4782 {
4783 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_LEFT]));
4784 gui_mch_set_scrollbar_colors(&(wp->w_scrollbars[SBAR_RIGHT]));
4785 }
4786 gui_mch_set_scrollbar_colors(&gui.bottom_sbar);
4787}
4788#endif
4789
4790/*
4791 * Call this when focus has changed.
4792 */
4793 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004794gui_focus_change(int in_focus)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795{
4796/*
4797 * Skip this code to avoid drawing the cursor when debugging and switching
4798 * between the debugger window and gvim.
4799 */
4800#if 1
4801 gui.in_focus = in_focus;
Bram Moolenaara338adc2018-01-31 20:51:47 +01004802 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803
4804# ifdef FEAT_XIM
4805 xim_set_focus(in_focus);
4806# endif
4807
Bram Moolenaar30613902019-12-01 22:11:18 +01004808 // Put events in the input queue only when allowed.
4809 // ui_focus_change() isn't called directly, because it invokes
4810 // autocommands and that must not happen asynchronously.
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00004811 if (!hold_gui_events)
4812 {
4813 char_u bytes[3];
4814
4815 bytes[0] = CSI;
4816 bytes[1] = KS_EXTRA;
4817 bytes[2] = in_focus ? (int)KE_FOCUSGAINED : (int)KE_FOCUSLOST;
4818 add_to_input_buf(bytes, 3);
4819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820#endif
4821}
4822
4823/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004824 * When mouse moved: apply 'mousefocus'.
4825 * Also updates the mouse pointer shape.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 */
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004827 static void
4828gui_mouse_focus(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829{
4830 win_T *wp;
Bram Moolenaar7b240602006-06-20 18:39:51 +00004831 char_u st[8];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832
4833#ifdef FEAT_MOUSESHAPE
Bram Moolenaar30613902019-12-01 22:11:18 +01004834 // Get window pointer, and update mouse shape as well.
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004835 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836#endif
4837
Bram Moolenaar30613902019-12-01 22:11:18 +01004838 // Only handle this when 'mousefocus' set and ...
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 if (p_mousef
Bram Moolenaar30613902019-12-01 22:11:18 +01004840 && !hold_gui_events // not holding events
4841 && (State & (NORMAL|INSERT))// Normal/Visual/Insert mode
4842 && State != HITRETURN // but not hit-return prompt
4843 && msg_scrolled == 0 // no scrolled message
4844 && !need_mouse_correct // not moving the pointer
4845 && gui.in_focus) // gvim in focus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004847 // Don't move the mouse when it's left or right of the Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 if (x < 0 || x > Columns * gui.char_width)
4849 return;
4850#ifndef FEAT_MOUSESHAPE
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004851 wp = xy2win(x, y, IGNORE_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852#endif
4853 if (wp == curwin || wp == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +01004854 return; // still in the same old window, or none at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
Bram Moolenaar30613902019-12-01 22:11:18 +01004856 // Ignore position in the tab pages line.
Bram Moolenaar9c102382006-05-03 21:26:49 +00004857 if (Y_2_ROW(y) < tabline_height())
4858 return;
Bram Moolenaar9c102382006-05-03 21:26:49 +00004859
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 /*
4861 * format a mouse click on status line input
4862 * ala gui_send_mouse_event(0, x, y, 0, 0);
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004863 * Trick: Use a column number -1, so that get_pseudo_mouse_code() will
4864 * generate a K_LEFTMOUSE_NM key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 */
4866 if (finish_op)
4867 {
Bram Moolenaar30613902019-12-01 22:11:18 +01004868 // abort the current operator first
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 st[0] = ESC;
4870 add_to_input_buf(st, 1);
4871 }
4872 st[0] = CSI;
4873 st[1] = KS_MOUSE;
4874 st[2] = KE_FILLER;
4875 st[3] = (char_u)MOUSE_LEFT;
4876 fill_mouse_coord(st + 4,
Bram Moolenaar41bfd302005-04-24 21:59:46 +00004877 wp->w_wincol == 0 ? -1 : wp->w_wincol + MOUSE_COLOFF,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 wp->w_height + W_WINROW(wp));
4879
4880 add_to_input_buf(st, 8);
4881 st[3] = (char_u)MOUSE_RELEASE;
4882 add_to_input_buf(st, 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883#ifdef FEAT_GUI_GTK
Bram Moolenaar30613902019-12-01 22:11:18 +01004884 // Need to wake up the main loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 if (gtk_main_level() > 0)
4886 gtk_main_quit();
4887#endif
4888 }
4889}
4890
4891/*
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004892 * Called when the mouse moved (but not when dragging).
4893 */
4894 void
4895gui_mouse_moved(int x, int y)
4896{
4897 // Ignore this while still starting up.
4898 if (!gui.in_use || gui.starting)
4899 return;
4900
4901 // apply 'mousefocus' and pointer shape
4902 gui_mouse_focus(x, y);
4903
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004904#ifdef FEAT_PROP_POPUP
Bram Moolenaar49fe95f2019-07-08 21:57:30 +02004905 if (popup_visible)
4906 // Generate a mouse-moved event, so that the popup can perhaps be
4907 // closed, just like in the terminal.
4908 gui_send_mouse_event(MOUSE_DRAG, x, y, FALSE, 0);
4909#endif
4910}
4911
4912/*
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004913 * Get the window where the mouse pointer is on.
4914 * Returns NULL if not found.
4915 */
4916 win_T *
4917gui_mouse_window(mouse_find_T popup)
4918{
4919 int x, y;
4920
4921 if (!(gui.in_use && (p_mousef || popup == FIND_POPUP)))
4922 return NULL;
4923 gui_mch_getmouse(&x, &y);
4924
4925 // Only use the mouse when it's on the Vim window
4926 if (x >= 0 && x <= Columns * gui.char_width
4927 && y >= 0 && Y_2_ROW(y) >= tabline_height())
4928 return xy2win(x, y, popup);
4929 return NULL;
4930}
4931
4932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 * Called when mouse should be moved to window with focus.
4934 */
4935 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004936gui_mouse_correct(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 win_T *wp = NULL;
4939
4940 need_mouse_correct = FALSE;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004941
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004942 wp = gui_mouse_window(IGNORE_POPUP);
Bram Moolenaar30613902019-12-01 22:11:18 +01004943 if (wp != curwin && wp != NULL) // If in other than current window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 {
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00004945 validate_cline_row();
4946 gui_mch_setmouse((int)W_ENDCOL(curwin) * gui.char_width - 3,
4947 (W_WINROW(curwin) + curwin->w_wrow) * gui.char_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 + (gui.char_height) / 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
4950}
4951
4952/*
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004953 * Find window where the mouse pointer "x" / "y" coordinate is in.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004954 * As a side effect update the shape of the mouse pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 static win_T *
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004957xy2win(int x, int y, mouse_find_T popup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 int row;
4960 int col;
4961 win_T *wp;
4962
4963 row = Y_2_ROW(y);
4964 col = X_2_COL(x);
Bram Moolenaar30613902019-12-01 22:11:18 +01004965 if (row < 0 || col < 0) // before first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 return NULL;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004967 wp = mouse_find_win(&row, &col, popup);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004968 if (wp == NULL)
4969 return NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004970#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 if (State == HITRETURN || State == ASKMORE)
4972 {
4973 if (Y_2_ROW(y) >= msg_row)
4974 update_mouseshape(SHAPE_IDX_MOREL);
4975 else
4976 update_mouseshape(SHAPE_IDX_MORE);
4977 }
Bram Moolenaar30613902019-12-01 22:11:18 +01004978 else if (row > wp->w_height) // below status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 update_mouseshape(SHAPE_IDX_CLINE);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004980 else if (!(State & CMDLINE) && wp->w_vsep_width > 0 && col == wp->w_width
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004981 && (row != wp->w_height || !stl_connected(wp)) && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 update_mouseshape(SHAPE_IDX_VSEP);
Bram Moolenaare0de17d2017-09-24 16:24:34 +02004983 else if (!(State & CMDLINE) && wp->w_status_height > 0
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004984 && row == wp->w_height && msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 update_mouseshape(SHAPE_IDX_STATUS);
4986 else
4987 update_mouseshape(-2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02004989 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990}
4991
4992/*
4993 * ":gui" and ":gvim": Change from the terminal version to the GUI version.
4994 * File names may be given to redefine the args list.
4995 */
4996 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004997ex_gui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998{
4999 char_u *arg = eap->arg;
5000
5001 /*
5002 * Check for "-f" argument: foreground, don't fork.
5003 * Also don't fork when started with "gvim -f".
5004 * Do fork when using "gui -b".
5005 */
5006 if (arg[0] == '-'
5007 && (arg[1] == 'f' || arg[1] == 'b')
Bram Moolenaar1c465442017-03-12 20:10:05 +01005008 && (arg[2] == NUL || VIM_ISWHITE(arg[2])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 {
5010 gui.dofork = (arg[1] == 'b');
5011 eap->arg = skipwhite(eap->arg + 2);
5012 }
5013 if (!gui.in_use)
5014 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005015#if defined(VIMDLL) && !defined(EXPERIMENTAL_GUI_CMD)
5016 emsg(_(e_nogvim));
5017 return;
5018#else
Bram Moolenaar30613902019-12-01 22:11:18 +01005019 // Clear the command. Needed for when forking+exiting, to avoid part
5020 // of the argument ending up after the shell prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 msg_clr_eos_force();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005022# ifdef GUI_MAY_SPAWN
5023 if (!ends_excmd(*eap->arg))
5024 gui_start(eap->arg);
5025 else
5026# endif
5027 gui_start(NULL);
5028# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard04a0202016-01-26 23:30:18 +01005029 channel_gui_register_all();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005030# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02005031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 if (!ends_excmd(*eap->arg))
5034 ex_next(eap);
5035}
5036
Bram Moolenaar4f974752019-02-17 17:44:42 +01005037#if ((defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
5038 || defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_PHOTON)) \
5039 && defined(FEAT_TOOLBAR)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040/*
5041 * This is shared between Athena, Motif and GTK.
5042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043
5044/*
5045 * Callback function for do_in_runtimepath().
5046 */
5047 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005048gfp_setname(char_u *fname, void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005050 char_u *gfp_buffer = cookie;
5051
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (STRLEN(fname) >= MAXPATHL)
5053 *gfp_buffer = NUL;
5054 else
5055 STRCPY(gfp_buffer, fname);
5056}
5057
5058/*
5059 * Find the path of bitmap "name" with extension "ext" in 'runtimepath'.
5060 * Return FAIL for failure and OK if buffer[MAXPATHL] contains the result.
5061 */
5062 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005063gui_find_bitmap(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064{
5065 if (STRLEN(name) > MAXPATHL - 14)
5066 return FAIL;
Bram Moolenaar051b7822005-05-19 21:00:46 +00005067 vim_snprintf((char *)buffer, MAXPATHL, "bitmaps/%s.%s", name, ext);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01005068 if (do_in_runtimepath(buffer, 0, gfp_setname, buffer) == FAIL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005069 || *buffer == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 return FAIL;
5071 return OK;
5072}
5073
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02005074# if !defined(FEAT_GUI_GTK) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075/*
5076 * Given the name of the "icon=" argument, try finding the bitmap file for the
5077 * icon. If it is an absolute path name, use it as it is. Otherwise append
5078 * "ext" and search for it in 'runtimepath'.
5079 * The result is put in "buffer[MAXPATHL]". If something fails "buffer"
5080 * contains "name".
5081 */
5082 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005083gui_find_iconfile(char_u *name, char_u *buffer, char *ext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
5085 char_u buf[MAXPATHL + 1];
5086
5087 expand_env(name, buffer, MAXPATHL);
5088 if (!mch_isFullName(buffer) && gui_find_bitmap(buffer, buf, ext) == OK)
5089 STRCPY(buffer, buf);
5090}
5091# endif
5092#endif
5093
Bram Moolenaar9372a112005-12-06 19:59:18 +00005094#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005096display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097{
5098 char_u *p;
5099
5100 if (isatty(2))
5101 fflush(stderr);
5102 else if (error_ga.ga_data != NULL)
5103 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005104 // avoid putting up a message box with blanks only
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 for (p = (char_u *)error_ga.ga_data; *p != NUL; ++p)
5106 if (!isspace(*p))
5107 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005108 // Truncate a very long message, it will go off-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 if (STRLEN(p) > 2000)
5110 STRCPY(p + 2000 - 14, "...(truncated)");
5111 (void)do_dialog(VIM_ERROR, (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +01005112 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 break;
5114 }
5115 ga_clear(&error_ga);
5116 }
5117}
5118#endif
5119
5120#if defined(NO_CONSOLE_INPUT) || defined(PROTO)
5121/*
5122 * Return TRUE if still starting up and there is no place to enter text.
5123 * For GTK and X11 we check if stderr is not a tty, which means we were
5124 * (probably) started from the desktop. Also check stdin, "vim >& file" does
5125 * allow typing on stdin.
5126 */
5127 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005128no_console_input(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129{
5130 return ((!gui.in_use || gui.starting)
5131# ifndef NO_CONSOLE
5132 && !isatty(0) && !isatty(2)
5133# endif
5134 );
5135}
5136#endif
5137
Bram Moolenaarbb1969b2019-01-17 15:45:25 +01005138#if defined(FIND_REPLACE_DIALOG) \
Bram Moolenaarda68cf32006-10-10 15:35:57 +00005139 || defined(NEED_GUI_UPDATE_SCREEN) \
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005140 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141/*
5142 * Update the current window and the screen.
5143 */
5144 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005145gui_update_screen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005147# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005148 linenr_T conceal_old_cursor_line = 0;
5149 linenr_T conceal_new_cursor_line = 0;
5150 int conceal_update_lines = FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005151# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005152
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 update_topline();
5154 validate_cursor();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005155
Bram Moolenaar30613902019-12-01 22:11:18 +01005156 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005157 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005158# ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005159 || popup_visible
5160# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005161# ifdef FEAT_CONCEAL
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005162 || curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005163# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005164 ) && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaarec80df72008-05-07 19:46:51 +00005165 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005166 if (has_cursormoved())
5167 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005168#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02005169 if (popup_visible)
5170 popup_check_cursor_pos();
5171#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005172# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005173 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005174 {
5175 conceal_old_cursor_line = last_cursormoved.lnum;
5176 conceal_new_cursor_line = curwin->w_cursor.lnum;
5177 conceal_update_lines = TRUE;
5178 }
5179# endif
Bram Moolenaarec80df72008-05-07 19:46:51 +00005180 last_cursormoved = curwin->w_cursor;
5181 }
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005182
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005183# ifdef FEAT_CONCEAL
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005184 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005185 && (conceal_old_cursor_line != conceal_new_cursor_line
5186 || conceal_cursor_line(curwin)
5187 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005188 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005189 if (conceal_old_cursor_line != conceal_new_cursor_line)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005190 redrawWinline(curwin, conceal_old_cursor_line);
5191 redrawWinline(curwin, conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005192 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005193 need_cursor_line_redraw = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005194 }
5195# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01005196 update_screen(0); // may need to update the screen
Bram Moolenaar535d5b62019-01-11 20:45:36 +01005197 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01005198 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199}
5200#endif
5201
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005202#if defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203/*
5204 * Get the text to use in a find/replace dialog. Uses the last search pattern
5205 * if the argument is empty.
5206 * Returns an allocated string.
5207 */
5208 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005209get_find_dialog_text(
5210 char_u *arg,
Bram Moolenaar30613902019-12-01 22:11:18 +01005211 int *wwordp, // return: TRUE if \< \> found
5212 int *mcasep) // return: TRUE if \C found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213{
5214 char_u *text;
5215
5216 if (*arg == NUL)
5217 text = last_search_pat();
5218 else
5219 text = arg;
5220 if (text != NULL)
5221 {
5222 text = vim_strsave(text);
5223 if (text != NULL)
5224 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005225 int len = (int)STRLEN(text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 int i;
5227
Bram Moolenaar30613902019-12-01 22:11:18 +01005228 // Remove "\V"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 if (len >= 2 && STRNCMP(text, "\\V", 2) == 0)
5230 {
5231 mch_memmove(text, text + 2, (size_t)(len - 1));
5232 len -= 2;
5233 }
5234
Bram Moolenaar30613902019-12-01 22:11:18 +01005235 // Recognize "\c" and "\C" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 if (len >= 2 && *text == '\\' && (text[1] == 'c' || text[1] == 'C'))
5237 {
5238 *mcasep = (text[1] == 'C');
5239 mch_memmove(text, text + 2, (size_t)(len - 1));
5240 len -= 2;
5241 }
5242
Bram Moolenaar30613902019-12-01 22:11:18 +01005243 // Recognize "\<text\>" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 if (len >= 4
5245 && STRNCMP(text, "\\<", 2) == 0
5246 && STRNCMP(text + len - 2, "\\>", 2) == 0)
5247 {
5248 *wwordp = TRUE;
5249 mch_memmove(text, text + 2, (size_t)(len - 4));
5250 text[len - 4] = NUL;
5251 }
5252
Bram Moolenaar30613902019-12-01 22:11:18 +01005253 // Recognize "\/" or "\?" and remove.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 for (i = 0; i + 1 < len; ++i)
5255 if (text[i] == '\\' && (text[i + 1] == '/'
5256 || text[i + 1] == '?'))
5257 {
5258 mch_memmove(text + i, text + i + 1, (size_t)(len - i));
5259 --len;
5260 }
5261 }
5262 }
5263 return text;
5264}
5265
5266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 * Handle the press of a button in the find-replace dialog.
5268 * Return TRUE when something was added to the input buffer.
5269 */
5270 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005271gui_do_findrepl(
Bram Moolenaar30613902019-12-01 22:11:18 +01005272 int flags, // one of FRD_REPLACE, FRD_FINDNEXT, etc.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005273 char_u *find_text,
5274 char_u *repl_text,
Bram Moolenaar30613902019-12-01 22:11:18 +01005275 int down) // Search downwards.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276{
5277 garray_T ga;
5278 int i;
5279 int type = (flags & FRD_TYPE_MASK);
5280 char_u *p;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005281 regmatch_T regmatch;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005282 int save_did_emsg = did_emsg;
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005283 static int busy = FALSE;
5284
Bram Moolenaar30613902019-12-01 22:11:18 +01005285 // When the screen is being updated we should not change buffers and
5286 // windows structures, it may cause freed memory to be used. Also don't
5287 // do this recursively (pressing "Find" quickly several times.
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005288 if (updating_screen || busy)
5289 return FALSE;
5290
Bram Moolenaar30613902019-12-01 22:11:18 +01005291 // refuse replace when text cannot be changed
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005292 if ((type == FRD_REPLACE || type == FRD_REPLACEALL) && text_locked())
5293 return FALSE;
5294
5295 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
5297 ga_init2(&ga, 1, 100);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005298 if (type == FRD_REPLACEALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 ga_concat(&ga, (char_u *)"%s/");
5300
5301 ga_concat(&ga, (char_u *)"\\V");
5302 if (flags & FRD_MATCH_CASE)
5303 ga_concat(&ga, (char_u *)"\\C");
5304 else
5305 ga_concat(&ga, (char_u *)"\\c");
5306 if (flags & FRD_WHOLE_WORD)
5307 ga_concat(&ga, (char_u *)"\\<");
Bram Moolenaar30613902019-12-01 22:11:18 +01005308 // escape slash and backslash
Bram Moolenaar518bc172018-05-13 17:05:30 +02005309 p = vim_strsave_escaped(find_text, (char_u *)"/\\");
5310 if (p != NULL)
5311 ga_concat(&ga, p);
5312 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 if (flags & FRD_WHOLE_WORD)
5314 ga_concat(&ga, (char_u *)"\\>");
5315
5316 if (type == FRD_REPLACEALL)
5317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 ga_concat(&ga, (char_u *)"/");
Bram Moolenaar30613902019-12-01 22:11:18 +01005319 // escape slash and backslash
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005320 p = vim_strsave_escaped(repl_text, (char_u *)"/\\");
5321 if (p != NULL)
5322 ga_concat(&ga, p);
5323 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 ga_concat(&ga, (char_u *)"/g");
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005325 }
5326 ga_append(&ga, NUL);
5327
5328 if (type == FRD_REPLACE)
5329 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005330 // Do the replacement when the text at the cursor matches. Thus no
5331 // replacement is done if the cursor was moved!
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005332 regmatch.regprog = vim_regcomp(ga.ga_data, RE_MAGIC + RE_STRING);
5333 regmatch.rm_ic = 0;
5334 if (regmatch.regprog != NULL)
5335 {
5336 p = ml_get_cursor();
5337 if (vim_regexec_nl(&regmatch, p, (colnr_T)0)
5338 && regmatch.startp[0] == p)
5339 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005340 // Clear the command line to remove any old "No match"
5341 // error.
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005342 msg_end_prompt();
5343
5344 if (u_save_cursor() == OK)
5345 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005346 // A button was pressed thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005347 u_sync(FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005348
5349 del_bytes((long)(regmatch.endp[0] - regmatch.startp[0]),
Bram Moolenaard35f9712005-12-18 22:02:33 +00005350 FALSE, FALSE);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005351 ins_str(repl_text);
5352 }
5353 }
5354 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005355 msg(_("No match at cursor, finding next"));
Bram Moolenaar473de612013-06-08 18:19:48 +02005356 vim_regfree(regmatch.regprog);
Bram Moolenaar7171abe2004-10-11 10:06:20 +00005357 }
5358 }
5359
5360 if (type == FRD_REPLACEALL)
5361 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005362 // A button was pressed, thus undo should be synced.
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005363 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 do_cmdline_cmd(ga.ga_data);
5365 }
5366 else
5367 {
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005368 int searchflags = SEARCH_MSG + SEARCH_MARK;
5369
Bram Moolenaar30613902019-12-01 22:11:18 +01005370 // Search for the next match.
5371 // Don't skip text under cursor for single replace.
Bram Moolenaarbee666f2016-06-14 20:39:42 +02005372 if (type == FRD_REPLACE)
5373 searchflags += SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 i = msg_scroll;
Bram Moolenaar518bc172018-05-13 17:05:30 +02005375 if (down)
5376 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02005377 (void)do_search(NULL, '/', ga.ga_data, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005378 }
5379 else
5380 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005381 // We need to escape '?' if and only if we are searching in the up
5382 // direction
Bram Moolenaar518bc172018-05-13 17:05:30 +02005383 p = vim_strsave_escaped(ga.ga_data, (char_u *)"?");
5384 if (p != NULL)
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02005385 (void)do_search(NULL, '?', p, 1L, searchflags, NULL);
Bram Moolenaar518bc172018-05-13 17:05:30 +02005386 vim_free(p);
5387 }
5388
Bram Moolenaar30613902019-12-01 22:11:18 +01005389 msg_scroll = i; // don't let an error message set msg_scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 }
5391
Bram Moolenaar30613902019-12-01 22:11:18 +01005392 // Don't want to pass did_emsg to other code, it may cause disabling
5393 // syntax HL if we were busy redrawing.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005394 did_emsg = save_did_emsg;
5395
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 if (State & (NORMAL | INSERT))
5397 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005398 gui_update_screen(); // update the screen
5399 msg_didout = 0; // overwrite any message
5400 need_wait_return = FALSE; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 }
5402
5403 vim_free(ga.ga_data);
Bram Moolenaar9f8650c2009-07-29 09:11:15 +00005404 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 return (ga.ga_len > 0);
5406}
5407
5408#endif
5409
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005410#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411/*
5412 * Jump to the window at specified point (x, y).
5413 */
5414 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005415gui_wingoto_xy(int x, int y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
5417 int row = Y_2_ROW(y);
5418 int col = X_2_COL(x);
5419 win_T *wp;
5420
5421 if (row >= 0 && col >= 0)
5422 {
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005423 wp = mouse_find_win(&row, &col, FAIL_POPUP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 if (wp != NULL && wp != curwin)
5425 win_goto(wp);
5426 }
5427}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428
5429/*
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005430 * Function passed to handle_drop() for the actions to be done after the
5431 * argument list has been updated.
5432 */
5433 static void
5434drop_callback(void *cookie)
5435{
5436 char_u *p = cookie;
5437
Bram Moolenaar30613902019-12-01 22:11:18 +01005438 // If Shift held down, change to first file's directory. If the first
5439 // item is a directory, change to that directory (and let the explorer
5440 // plugin show the contents).
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005441 if (p != NULL)
5442 {
5443 if (mch_isdir(p))
5444 {
5445 if (mch_chdir((char *)p) == 0)
5446 shorten_fnames(TRUE);
5447 }
5448 else if (vim_chdirfile(p, "drop") == OK)
5449 shorten_fnames(TRUE);
5450 vim_free(p);
5451 }
5452
Bram Moolenaar30613902019-12-01 22:11:18 +01005453 // Update the screen display
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005454 update_screen(NOT_VALID);
5455# ifdef FEAT_MENU
5456 gui_update_menus(0);
5457# endif
5458#ifdef FEAT_TITLE
5459 maketitle();
5460#endif
5461 setcursor();
5462 out_flush_cursor(FALSE, FALSE);
5463}
5464
5465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 * Process file drop. Mouse cursor position, key modifiers, name of files
5467 * and count of files are given. Argument "fnames[count]" has full pathnames
5468 * of dropped files, they will be freed in this function, and caller can't use
5469 * fnames after call this function.
5470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01005472gui_handle_drop(
5473 int x UNUSED,
5474 int y UNUSED,
5475 int_u modifiers,
5476 char_u **fnames,
5477 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478{
5479 int i;
5480 char_u *p;
Bram Moolenaarc236c162008-07-13 17:41:49 +00005481 static int entered = FALSE;
5482
5483 /*
5484 * This function is called by event handlers. Just in case we get a
5485 * second event before the first one is handled, ignore the second one.
5486 * Not sure if this can ever happen, just in case.
5487 */
5488 if (entered)
5489 return;
5490 entered = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491
5492 /*
5493 * When the cursor is at the command line, add the file names to the
5494 * command line, don't edit the files.
5495 */
5496 if (State & CMDLINE)
5497 {
5498 shorten_filenames(fnames, count);
5499 for (i = 0; i < count; ++i)
5500 {
5501 if (fnames[i] != NULL)
5502 {
5503 if (i > 0)
5504 add_to_input_buf((char_u*)" ", 1);
5505
Bram Moolenaar30613902019-12-01 22:11:18 +01005506 // We don't know what command is used thus we can't be sure
5507 // about which characters need to be escaped. Only escape the
5508 // most common ones.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509# ifdef BACKSLASH_IN_FILENAME
5510 p = vim_strsave_escaped(fnames[i], (char_u *)" \t\"|");
5511# else
5512 p = vim_strsave_escaped(fnames[i], (char_u *)"\\ \t\"|");
5513# endif
5514 if (p != NULL)
Bram Moolenaar70c2a632007-08-15 18:08:50 +00005515 add_to_input_buf_csi(p, (int)STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 vim_free(p);
5517 vim_free(fnames[i]);
5518 }
5519 }
5520 vim_free(fnames);
5521 }
5522 else
5523 {
Bram Moolenaar30613902019-12-01 22:11:18 +01005524 // Go to the window under mouse cursor, then shorten given "fnames" by
5525 // current window, because a window can have local current dir.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 gui_wingoto_xy(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 shorten_filenames(fnames, count);
5528
Bram Moolenaar30613902019-12-01 22:11:18 +01005529 // If Shift held down, remember the first item.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 if ((modifiers & MOUSE_SHIFT) != 0)
5531 p = vim_strsave(fnames[0]);
5532 else
5533 p = NULL;
5534
Bram Moolenaar30613902019-12-01 22:11:18 +01005535 // Handle the drop, :edit or :split to get to the file. This also
5536 // frees fnames[]. Skip this if there is only one item, it's a
5537 // directory and Shift is held down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 if (count == 1 && (modifiers & MOUSE_SHIFT) != 0
5539 && mch_isdir(fnames[0]))
5540 {
5541 vim_free(fnames[0]);
5542 vim_free(fnames);
5543 }
5544 else
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005545 handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
5546 drop_callback, (void *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00005548
5549 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550}
5551#endif