Mercurial > vim
annotate src/ui.c @ 5100:18b43970fb7a v7.3.1293
updated for version 7.3.1293
Problem: Put in empty buffer cannot be undone.
Solution: Save one more line for undo. (Ozaki)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 03 Jul 2013 14:19:54 +0200 |
parents | 5e0b6a9282df |
children | c3a82208e143 |
rev | line source |
---|---|
7 | 1 /* vi:set ts=8 sts=4 sw=4: |
2 * | |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * | |
5 * Do ":help uganda" in Vim to read copying and usage conditions. | |
6 * Do ":help credits" in Vim to see a list of people who contributed. | |
7 * See README.txt for an overview of the Vim source code. | |
8 */ | |
9 | |
10 /* | |
11 * ui.c: functions that handle the user interface. | |
12 * 1. Keyboard input stuff, and a bit of windowing stuff. These are called | |
13 * before the machine specific stuff (mch_*) so that we can call the GUI | |
14 * stuff instead if the GUI is running. | |
15 * 2. Clipboard stuff. | |
16 * 3. Input buffer stuff. | |
17 */ | |
18 | |
19 #include "vim.h" | |
20 | |
21 void | |
22 ui_write(s, len) | |
23 char_u *s; | |
24 int len; | |
25 { | |
26 #ifdef FEAT_GUI | |
27 if (gui.in_use && !gui.dying && !gui.starting) | |
28 { | |
29 gui_write(s, len); | |
30 if (p_wd) | |
31 gui_wait_for_chars(p_wd); | |
32 return; | |
33 } | |
34 #endif | |
35 #ifndef NO_CONSOLE | |
36 /* Don't output anything in silent mode ("ex -s") unless 'verbose' set */ | |
37 if (!(silent_mode && p_verbose == 0)) | |
38 { | |
39 #ifdef FEAT_MBYTE | |
40 char_u *tofree = NULL; | |
41 | |
42 if (output_conv.vc_type != CONV_NONE) | |
43 { | |
44 /* Convert characters from 'encoding' to 'termencoding'. */ | |
45 tofree = string_convert(&output_conv, s, &len); | |
46 if (tofree != NULL) | |
47 s = tofree; | |
48 } | |
49 #endif | |
50 | |
51 mch_write(s, len); | |
52 | |
53 #ifdef FEAT_MBYTE | |
54 if (output_conv.vc_type != CONV_NONE) | |
55 vim_free(tofree); | |
56 #endif | |
57 } | |
58 #endif | |
59 } | |
60 | |
2935 | 61 #if defined(UNIX) || defined(VMS) || defined(PROTO) || defined(WIN3264) |
7 | 62 /* |
63 * When executing an external program, there may be some typed characters that | |
64 * are not consumed by it. Give them back to ui_inchar() and they are stored | |
65 * here for the next call. | |
66 */ | |
67 static char_u *ta_str = NULL; | |
68 static int ta_off; /* offset for next char to use when ta_str != NULL */ | |
69 static int ta_len; /* length of ta_str when it's not NULL*/ | |
70 | |
71 void | |
72 ui_inchar_undo(s, len) | |
73 char_u *s; | |
74 int len; | |
75 { | |
76 char_u *new; | |
77 int newlen; | |
78 | |
79 newlen = len; | |
80 if (ta_str != NULL) | |
81 newlen += ta_len - ta_off; | |
82 new = alloc(newlen); | |
83 if (new != NULL) | |
84 { | |
85 if (ta_str != NULL) | |
86 { | |
87 mch_memmove(new, ta_str + ta_off, (size_t)(ta_len - ta_off)); | |
88 mch_memmove(new + ta_len - ta_off, s, (size_t)len); | |
89 vim_free(ta_str); | |
90 } | |
91 else | |
92 mch_memmove(new, s, (size_t)len); | |
93 ta_str = new; | |
94 ta_len = newlen; | |
95 ta_off = 0; | |
96 } | |
97 } | |
98 #endif | |
99 | |
100 /* | |
3877 | 101 * ui_inchar(): low level input function. |
7 | 102 * Get characters from the keyboard. |
103 * Return the number of characters that are available. | |
104 * If "wtime" == 0 do not wait for characters. | |
105 * If "wtime" == -1 wait forever for characters. | |
106 * If "wtime" > 0 wait "wtime" milliseconds for a character. | |
107 * | |
108 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into | |
109 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received | |
110 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is NULL | |
111 * otherwise. | |
112 */ | |
113 int | |
114 ui_inchar(buf, maxlen, wtime, tb_change_cnt) | |
115 char_u *buf; | |
116 int maxlen; | |
117 long wtime; /* don't use "time", MIPS cannot handle it */ | |
118 int tb_change_cnt; | |
119 { | |
120 int retval = 0; | |
121 | |
122 #if defined(FEAT_GUI) && (defined(UNIX) || defined(VMS)) | |
123 /* | |
124 * Use the typeahead if there is any. | |
125 */ | |
126 if (ta_str != NULL) | |
127 { | |
128 if (maxlen >= ta_len - ta_off) | |
129 { | |
130 mch_memmove(buf, ta_str + ta_off, (size_t)ta_len); | |
131 vim_free(ta_str); | |
132 ta_str = NULL; | |
133 return ta_len; | |
134 } | |
135 mch_memmove(buf, ta_str + ta_off, (size_t)maxlen); | |
136 ta_off += maxlen; | |
137 return maxlen; | |
138 } | |
139 #endif | |
140 | |
170 | 141 #ifdef FEAT_PROFILE |
791 | 142 if (do_profiling == PROF_YES && wtime != 0) |
170 | 143 prof_inchar_enter(); |
144 #endif | |
145 | |
7 | 146 #ifdef NO_CONSOLE_INPUT |
147 /* Don't wait for character input when the window hasn't been opened yet. | |
148 * Do try reading, this works when redirecting stdin from a file. | |
149 * Must return something, otherwise we'll loop forever. If we run into | |
150 * this very often we probably got stuck, exit Vim. */ | |
151 if (no_console_input()) | |
152 { | |
153 static int count = 0; | |
154 | |
155 # ifndef NO_CONSOLE | |
228 | 156 retval = mch_inchar(buf, maxlen, (wtime >= 0 && wtime < 10) |
157 ? 10L : wtime, tb_change_cnt); | |
158 if (retval > 0 || typebuf_changed(tb_change_cnt) || wtime >= 0) | |
170 | 159 goto theend; |
7 | 160 # endif |
161 if (wtime == -1 && ++count == 1000) | |
162 read_error_exit(); | |
163 buf[0] = CAR; | |
170 | 164 retval = 1; |
165 goto theend; | |
7 | 166 } |
167 #endif | |
168 | |
1086 | 169 /* If we are going to wait for some time or block... */ |
170 if (wtime == -1 || wtime > 100L) | |
171 { | |
172 /* ... allow signals to kill us. */ | |
173 (void)vim_handle_signal(SIGNAL_UNBLOCK); | |
174 | |
175 /* ... there is no need for CTRL-C to interrupt something, don't let | |
176 * it set got_int when it was mapped. */ | |
177 if (mapped_ctrl_c) | |
178 ctrl_c_interrupts = FALSE; | |
179 } | |
7 | 180 |
181 #ifdef FEAT_GUI | |
182 if (gui.in_use) | |
183 { | |
184 if (gui_wait_for_chars(wtime) && !typebuf_changed(tb_change_cnt)) | |
185 retval = read_from_input_buf(buf, (long)maxlen); | |
186 } | |
187 #endif | |
188 #ifndef NO_CONSOLE | |
189 # ifdef FEAT_GUI | |
190 else | |
191 # endif | |
36 | 192 { |
7 | 193 retval = mch_inchar(buf, maxlen, wtime, tb_change_cnt); |
36 | 194 } |
7 | 195 #endif |
196 | |
1086 | 197 if (wtime == -1 || wtime > 100L) |
198 /* block SIGHUP et al. */ | |
199 (void)vim_handle_signal(SIGNAL_BLOCK); | |
200 | |
7 | 201 ctrl_c_interrupts = TRUE; |
202 | |
170 | 203 #ifdef NO_CONSOLE_INPUT |
204 theend: | |
205 #endif | |
206 #ifdef FEAT_PROFILE | |
791 | 207 if (do_profiling == PROF_YES && wtime != 0) |
170 | 208 prof_inchar_exit(); |
209 #endif | |
7 | 210 return retval; |
211 } | |
212 | |
213 /* | |
214 * return non-zero if a character is available | |
215 */ | |
216 int | |
217 ui_char_avail() | |
218 { | |
219 #ifdef FEAT_GUI | |
220 if (gui.in_use) | |
221 { | |
222 gui_mch_update(); | |
223 return input_available(); | |
224 } | |
225 #endif | |
226 #ifndef NO_CONSOLE | |
227 # ifdef NO_CONSOLE_INPUT | |
228 if (no_console_input()) | |
229 return 0; | |
230 # endif | |
231 return mch_char_avail(); | |
232 #else | |
233 return 0; | |
234 #endif | |
235 } | |
236 | |
237 /* | |
238 * Delay for the given number of milliseconds. If ignoreinput is FALSE then we | |
239 * cancel the delay if a key is hit. | |
240 */ | |
241 void | |
242 ui_delay(msec, ignoreinput) | |
243 long msec; | |
244 int ignoreinput; | |
245 { | |
246 #ifdef FEAT_GUI | |
247 if (gui.in_use && !ignoreinput) | |
248 gui_wait_for_chars(msec); | |
249 else | |
250 #endif | |
251 mch_delay(msec, ignoreinput); | |
252 } | |
253 | |
254 /* | |
255 * If the machine has job control, use it to suspend the program, | |
256 * otherwise fake it by starting a new shell. | |
257 * When running the GUI iconify the window. | |
258 */ | |
259 void | |
260 ui_suspend() | |
261 { | |
262 #ifdef FEAT_GUI | |
263 if (gui.in_use) | |
264 { | |
265 gui_mch_iconify(); | |
266 return; | |
267 } | |
268 #endif | |
269 mch_suspend(); | |
270 } | |
271 | |
272 #if !defined(UNIX) || !defined(SIGTSTP) || defined(PROTO) || defined(__BEOS__) | |
273 /* | |
274 * When the OS can't really suspend, call this function to start a shell. | |
275 * This is never called in the GUI. | |
276 */ | |
277 void | |
278 suspend_shell() | |
279 { | |
280 if (*p_sh == NUL) | |
281 EMSG(_(e_shellempty)); | |
282 else | |
283 { | |
284 MSG_PUTS(_("new shell started\n")); | |
285 do_shell(NULL, 0); | |
286 } | |
287 } | |
288 #endif | |
289 | |
290 /* | |
291 * Try to get the current Vim shell size. Put the result in Rows and Columns. | |
292 * Use the new sizes as defaults for 'columns' and 'lines'. | |
293 * Return OK when size could be determined, FAIL otherwise. | |
294 */ | |
295 int | |
296 ui_get_shellsize() | |
297 { | |
298 int retval; | |
299 | |
300 #ifdef FEAT_GUI | |
301 if (gui.in_use) | |
302 retval = gui_get_shellsize(); | |
303 else | |
304 #endif | |
305 retval = mch_get_shellsize(); | |
306 | |
307 check_shellsize(); | |
308 | |
309 /* adjust the default for 'lines' and 'columns' */ | |
310 if (retval == OK) | |
311 { | |
312 set_number_default("lines", Rows); | |
313 set_number_default("columns", Columns); | |
314 } | |
315 return retval; | |
316 } | |
317 | |
318 /* | |
319 * Set the size of the Vim shell according to Rows and Columns, if possible. | |
320 * The gui_set_shellsize() or mch_set_shellsize() function will try to set the | |
321 * new size. If this is not possible, it will adjust Rows and Columns. | |
322 */ | |
323 void | |
324 ui_set_shellsize(mustset) | |
1883 | 325 int mustset UNUSED; /* set by the user */ |
7 | 326 { |
327 #ifdef FEAT_GUI | |
328 if (gui.in_use) | |
329 gui_set_shellsize(mustset, | |
330 # ifdef WIN3264 | |
331 TRUE | |
332 # else | |
333 FALSE | |
334 # endif | |
814 | 335 , RESIZE_BOTH); |
7 | 336 else |
337 #endif | |
338 mch_set_shellsize(); | |
339 } | |
340 | |
341 /* | |
342 * Called when Rows and/or Columns changed. Adjust scroll region and mouse | |
343 * region. | |
344 */ | |
345 void | |
346 ui_new_shellsize() | |
347 { | |
348 if (full_screen && !exiting) | |
349 { | |
350 #ifdef FEAT_GUI | |
351 if (gui.in_use) | |
352 gui_new_shellsize(); | |
353 else | |
354 #endif | |
355 mch_new_shellsize(); | |
356 } | |
357 } | |
358 | |
359 void | |
360 ui_breakcheck() | |
361 { | |
362 #ifdef FEAT_GUI | |
363 if (gui.in_use) | |
364 gui_mch_update(); | |
365 else | |
366 #endif | |
367 mch_breakcheck(); | |
368 } | |
369 | |
370 /***************************************************************************** | |
371 * Functions for copying and pasting text between applications. | |
372 * This is always included in a GUI version, but may also be included when the | |
373 * clipboard and mouse is available to a terminal version such as xterm. | |
374 * Note: there are some more functions in ops.c that handle selection stuff. | |
375 * | |
376 * Also note that the majority of functions here deal with the X 'primary' | |
377 * (visible - for Visual mode use) selection, and only that. There are no | |
378 * versions of these for the 'clipboard' selection, as Visual mode has no use | |
379 * for them. | |
380 */ | |
381 | |
382 #if defined(FEAT_CLIPBOARD) || defined(PROTO) | |
383 | |
3674 | 384 static void clip_copy_selection __ARGS((VimClipboard *clip)); |
385 | |
7 | 386 /* |
387 * Selection stuff using Visual mode, for cutting and pasting text to other | |
388 * windows. | |
389 */ | |
390 | |
391 /* | |
392 * Call this to initialise the clipboard. Pass it FALSE if the clipboard code | |
393 * is included, but the clipboard can not be used, or TRUE if the clipboard can | |
394 * be used. Eg unix may call this with FALSE, then call it again with TRUE if | |
395 * the GUI starts. | |
396 */ | |
397 void | |
398 clip_init(can_use) | |
399 int can_use; | |
400 { | |
401 VimClipboard *cb; | |
402 | |
403 cb = &clip_star; | |
404 for (;;) | |
405 { | |
406 cb->available = can_use; | |
407 cb->owned = FALSE; | |
408 cb->start.lnum = 0; | |
409 cb->start.col = 0; | |
410 cb->end.lnum = 0; | |
411 cb->end.col = 0; | |
412 cb->state = SELECT_CLEARED; | |
413 | |
414 if (cb == &clip_plus) | |
415 break; | |
416 cb = &clip_plus; | |
417 } | |
418 } | |
419 | |
420 /* | |
421 * Check whether the VIsual area has changed, and if so try to become the owner | |
422 * of the selection, and free any old converted selection we may still have | |
423 * lying around. If the VIsual mode has ended, make a copy of what was | |
424 * selected so we can still give it to others. Will probably have to make sure | |
425 * this is called whenever VIsual mode is ended. | |
426 */ | |
427 void | |
3674 | 428 clip_update_selection(clip) |
429 VimClipboard *clip; | |
7 | 430 { |
3674 | 431 pos_T start, end; |
7 | 432 |
433 /* If visual mode is only due to a redo command ("."), then ignore it */ | |
434 if (!redo_VIsual_busy && VIsual_active && (State & NORMAL)) | |
435 { | |
436 if (lt(VIsual, curwin->w_cursor)) | |
437 { | |
438 start = VIsual; | |
439 end = curwin->w_cursor; | |
440 #ifdef FEAT_MBYTE | |
441 if (has_mbyte) | |
474 | 442 end.col += (*mb_ptr2len)(ml_get_cursor()) - 1; |
7 | 443 #endif |
444 } | |
445 else | |
446 { | |
447 start = curwin->w_cursor; | |
448 end = VIsual; | |
449 } | |
3674 | 450 if (!equalpos(clip->start, start) |
451 || !equalpos(clip->end, end) | |
452 || clip->vmode != VIsual_mode) | |
7 | 453 { |
3674 | 454 clip_clear_selection(clip); |
455 clip->start = start; | |
456 clip->end = end; | |
457 clip->vmode = VIsual_mode; | |
458 clip_free_selection(clip); | |
459 clip_own_selection(clip); | |
460 clip_gen_set_selection(clip); | |
7 | 461 } |
462 } | |
463 } | |
464 | |
465 void | |
466 clip_own_selection(cbd) | |
467 VimClipboard *cbd; | |
468 { | |
469 /* | |
470 * Also want to check somehow that we are reading from the keyboard rather | |
471 * than a mapping etc. | |
472 */ | |
2270
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
473 #ifdef FEAT_X11 |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
474 /* Always own the selection, we might have lost it without being |
2586 | 475 * notified, e.g. during a ":sh" command. */ |
2270
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
476 if (cbd->available) |
7 | 477 { |
2270
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
478 int was_owned = cbd->owned; |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
479 |
7 | 480 cbd->owned = (clip_gen_own_selection(cbd) == OK); |
3674 | 481 if (!was_owned && (cbd == &clip_star || cbd == &clip_plus)) |
7 | 482 { |
620 | 483 /* May have to show a different kind of highlighting for the |
484 * selected area. There is no specific redraw command for this, | |
485 * just redraw all windows on the current buffer. */ | |
7 | 486 if (cbd->owned |
791 | 487 && (get_real_state() == VISUAL |
488 || get_real_state() == SELECTMODE) | |
3674 | 489 && (cbd == &clip_star ? clip_isautosel_star() |
490 : clip_isautosel_plus()) | |
7 | 491 && hl_attr(HLF_V) != hl_attr(HLF_VNC)) |
492 redraw_curbuf_later(INVERTED_ALL); | |
493 } | |
2270
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
494 } |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
495 #else |
3877 | 496 /* Only own the clipboard when we didn't own it yet. */ |
2270
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
497 if (!cbd->owned && cbd->available) |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
498 cbd->owned = (clip_gen_own_selection(cbd) == OK); |
7 | 499 #endif |
500 } | |
501 | |
502 void | |
503 clip_lose_selection(cbd) | |
504 VimClipboard *cbd; | |
505 { | |
506 #ifdef FEAT_X11 | |
507 int was_owned = cbd->owned; | |
508 #endif | |
3674 | 509 int visual_selection = FALSE; |
510 | |
511 if (cbd == &clip_star || cbd == &clip_plus) | |
512 visual_selection = TRUE; | |
7 | 513 |
514 clip_free_selection(cbd); | |
515 cbd->owned = FALSE; | |
516 if (visual_selection) | |
3674 | 517 clip_clear_selection(cbd); |
7 | 518 clip_gen_lose_selection(cbd); |
519 #ifdef FEAT_X11 | |
520 if (visual_selection) | |
521 { | |
522 /* May have to show a different kind of highlighting for the selected | |
523 * area. There is no specific redraw command for this, just redraw all | |
524 * windows on the current buffer. */ | |
525 if (was_owned | |
791 | 526 && (get_real_state() == VISUAL |
527 || get_real_state() == SELECTMODE) | |
3674 | 528 && (cbd == &clip_star ? |
529 clip_isautosel_star() : clip_isautosel_plus()) | |
7 | 530 && hl_attr(HLF_V) != hl_attr(HLF_VNC)) |
531 { | |
532 update_curbuf(INVERTED_ALL); | |
533 setcursor(); | |
534 cursor_on(); | |
535 out_flush(); | |
536 # ifdef FEAT_GUI | |
537 if (gui.in_use) | |
538 gui_update_cursor(TRUE, FALSE); | |
539 # endif | |
540 } | |
541 } | |
542 #endif | |
543 } | |
544 | |
3674 | 545 static void |
546 clip_copy_selection(clip) | |
547 VimClipboard *clip; | |
7 | 548 { |
3674 | 549 if (VIsual_active && (State & NORMAL) && clip->available) |
7 | 550 { |
3674 | 551 clip_update_selection(clip); |
552 clip_free_selection(clip); | |
553 clip_own_selection(clip); | |
554 if (clip->owned) | |
555 clip_get_selection(clip); | |
556 clip_gen_set_selection(clip); | |
7 | 557 } |
558 } | |
559 | |
560 /* | |
561 * Called when Visual mode is ended: update the selection. | |
562 */ | |
563 void | |
564 clip_auto_select() | |
565 { | |
3674 | 566 if (clip_isautosel_star()) |
567 clip_copy_selection(&clip_star); | |
568 if (clip_isautosel_plus()) | |
569 clip_copy_selection(&clip_plus); | |
7 | 570 } |
571 | |
572 /* | |
3674 | 573 * Return TRUE if automatic selection of Visual area is desired for the * |
574 * register. | |
7 | 575 */ |
576 int | |
3674 | 577 clip_isautosel_star() |
7 | 578 { |
579 return ( | |
580 #ifdef FEAT_GUI | |
581 gui.in_use ? (vim_strchr(p_go, GO_ASEL) != NULL) : | |
582 #endif | |
3674 | 583 clip_autoselect_star); |
584 } | |
585 | |
586 /* | |
587 * Return TRUE if automatic selection of Visual area is desired for the + | |
588 * register. | |
589 */ | |
590 int | |
591 clip_isautosel_plus() | |
592 { | |
593 return ( | |
594 #ifdef FEAT_GUI | |
595 gui.in_use ? (vim_strchr(p_go, GO_ASELPLUS) != NULL) : | |
596 #endif | |
597 clip_autoselect_plus); | |
7 | 598 } |
599 | |
600 | |
601 /* | |
602 * Stuff for general mouse selection, without using Visual mode. | |
603 */ | |
604 | |
605 static int clip_compare_pos __ARGS((int row1, int col1, int row2, int col2)); | |
606 static void clip_invert_area __ARGS((int, int, int, int, int how)); | |
607 static void clip_invert_rectangle __ARGS((int row, int col, int height, int width, int invert)); | |
608 static void clip_get_word_boundaries __ARGS((VimClipboard *, int, int)); | |
609 static int clip_get_line_end __ARGS((int)); | |
610 static void clip_update_modeless_selection __ARGS((VimClipboard *, int, int, | |
611 int, int)); | |
612 | |
613 /* flags for clip_invert_area() */ | |
614 #define CLIP_CLEAR 1 | |
615 #define CLIP_SET 2 | |
616 #define CLIP_TOGGLE 3 | |
617 | |
618 /* | |
619 * Start, continue or end a modeless selection. Used when editing the | |
620 * command-line and in the cmdline window. | |
621 */ | |
622 void | |
623 clip_modeless(button, is_click, is_drag) | |
624 int button; | |
625 int is_click; | |
626 int is_drag; | |
627 { | |
628 int repeat; | |
629 | |
630 repeat = ((clip_star.mode == SELECT_MODE_CHAR | |
631 || clip_star.mode == SELECT_MODE_LINE) | |
632 && (mod_mask & MOD_MASK_2CLICK)) | |
633 || (clip_star.mode == SELECT_MODE_WORD | |
634 && (mod_mask & MOD_MASK_3CLICK)); | |
635 if (is_click && button == MOUSE_RIGHT) | |
636 { | |
637 /* Right mouse button: If there was no selection, start one. | |
638 * Otherwise extend the existing selection. */ | |
639 if (clip_star.state == SELECT_CLEARED) | |
640 clip_start_selection(mouse_col, mouse_row, FALSE); | |
641 clip_process_selection(button, mouse_col, mouse_row, repeat); | |
642 } | |
643 else if (is_click) | |
644 clip_start_selection(mouse_col, mouse_row, repeat); | |
645 else if (is_drag) | |
646 { | |
647 /* Don't try extending a selection if there isn't one. Happens when | |
648 * button-down is in the cmdline and them moving mouse upwards. */ | |
649 if (clip_star.state != SELECT_CLEARED) | |
650 clip_process_selection(button, mouse_col, mouse_row, repeat); | |
651 } | |
652 else /* release */ | |
653 clip_process_selection(MOUSE_RELEASE, mouse_col, mouse_row, FALSE); | |
654 } | |
655 | |
656 /* | |
657 * Compare two screen positions ala strcmp() | |
658 */ | |
659 static int | |
660 clip_compare_pos(row1, col1, row2, col2) | |
661 int row1; | |
662 int col1; | |
663 int row2; | |
664 int col2; | |
665 { | |
666 if (row1 > row2) return(1); | |
667 if (row1 < row2) return(-1); | |
668 if (col1 > col2) return(1); | |
669 if (col1 < col2) return(-1); | |
670 return(0); | |
671 } | |
672 | |
673 /* | |
674 * Start the selection | |
675 */ | |
676 void | |
677 clip_start_selection(col, row, repeated_click) | |
678 int col; | |
679 int row; | |
680 int repeated_click; | |
681 { | |
682 VimClipboard *cb = &clip_star; | |
683 | |
684 if (cb->state == SELECT_DONE) | |
3674 | 685 clip_clear_selection(cb); |
7 | 686 |
687 row = check_row(row); | |
688 col = check_col(col); | |
689 #ifdef FEAT_MBYTE | |
690 col = mb_fix_col(col, row); | |
691 #endif | |
692 | |
693 cb->start.lnum = row; | |
694 cb->start.col = col; | |
695 cb->end = cb->start; | |
696 cb->origin_row = (short_u)cb->start.lnum; | |
697 cb->state = SELECT_IN_PROGRESS; | |
698 | |
699 if (repeated_click) | |
700 { | |
701 if (++cb->mode > SELECT_MODE_LINE) | |
702 cb->mode = SELECT_MODE_CHAR; | |
703 } | |
704 else | |
705 cb->mode = SELECT_MODE_CHAR; | |
706 | |
707 #ifdef FEAT_GUI | |
708 /* clear the cursor until the selection is made */ | |
709 if (gui.in_use) | |
710 gui_undraw_cursor(); | |
711 #endif | |
712 | |
713 switch (cb->mode) | |
714 { | |
715 case SELECT_MODE_CHAR: | |
716 cb->origin_start_col = cb->start.col; | |
717 cb->word_end_col = clip_get_line_end((int)cb->start.lnum); | |
718 break; | |
719 | |
720 case SELECT_MODE_WORD: | |
721 clip_get_word_boundaries(cb, (int)cb->start.lnum, cb->start.col); | |
722 cb->origin_start_col = cb->word_start_col; | |
723 cb->origin_end_col = cb->word_end_col; | |
724 | |
725 clip_invert_area((int)cb->start.lnum, cb->word_start_col, | |
726 (int)cb->end.lnum, cb->word_end_col, CLIP_SET); | |
727 cb->start.col = cb->word_start_col; | |
728 cb->end.col = cb->word_end_col; | |
729 break; | |
730 | |
731 case SELECT_MODE_LINE: | |
732 clip_invert_area((int)cb->start.lnum, 0, (int)cb->start.lnum, | |
733 (int)Columns, CLIP_SET); | |
734 cb->start.col = 0; | |
735 cb->end.col = Columns; | |
736 break; | |
737 } | |
738 | |
739 cb->prev = cb->start; | |
740 | |
741 #ifdef DEBUG_SELECTION | |
742 printf("Selection started at (%u,%u)\n", cb->start.lnum, cb->start.col); | |
743 #endif | |
744 } | |
745 | |
746 /* | |
747 * Continue processing the selection | |
748 */ | |
749 void | |
750 clip_process_selection(button, col, row, repeated_click) | |
751 int button; | |
752 int col; | |
753 int row; | |
754 int_u repeated_click; | |
755 { | |
756 VimClipboard *cb = &clip_star; | |
757 int diff; | |
758 int slen = 1; /* cursor shape width */ | |
759 | |
760 if (button == MOUSE_RELEASE) | |
761 { | |
762 /* Check to make sure we have something selected */ | |
763 if (cb->start.lnum == cb->end.lnum && cb->start.col == cb->end.col) | |
764 { | |
765 #ifdef FEAT_GUI | |
766 if (gui.in_use) | |
767 gui_update_cursor(FALSE, FALSE); | |
768 #endif | |
769 cb->state = SELECT_CLEARED; | |
770 return; | |
771 } | |
772 | |
773 #ifdef DEBUG_SELECTION | |
774 printf("Selection ended: (%u,%u) to (%u,%u)\n", cb->start.lnum, | |
775 cb->start.col, cb->end.lnum, cb->end.col); | |
776 #endif | |
3674 | 777 if (clip_isautosel_star() |
7 | 778 || ( |
779 #ifdef FEAT_GUI | |
780 gui.in_use ? (vim_strchr(p_go, GO_ASELML) != NULL) : | |
781 #endif | |
782 clip_autoselectml)) | |
783 clip_copy_modeless_selection(FALSE); | |
784 #ifdef FEAT_GUI | |
785 if (gui.in_use) | |
786 gui_update_cursor(FALSE, FALSE); | |
787 #endif | |
788 | |
789 cb->state = SELECT_DONE; | |
790 return; | |
791 } | |
792 | |
793 row = check_row(row); | |
794 col = check_col(col); | |
795 #ifdef FEAT_MBYTE | |
796 col = mb_fix_col(col, row); | |
797 #endif | |
798 | |
799 if (col == (int)cb->prev.col && row == cb->prev.lnum && !repeated_click) | |
800 return; | |
801 | |
802 /* | |
803 * When extending the selection with the right mouse button, swap the | |
804 * start and end if the position is before half the selection | |
805 */ | |
806 if (cb->state == SELECT_DONE && button == MOUSE_RIGHT) | |
807 { | |
808 /* | |
809 * If the click is before the start, or the click is inside the | |
810 * selection and the start is the closest side, set the origin to the | |
811 * end of the selection. | |
812 */ | |
813 if (clip_compare_pos(row, col, (int)cb->start.lnum, cb->start.col) < 0 | |
814 || (clip_compare_pos(row, col, | |
815 (int)cb->end.lnum, cb->end.col) < 0 | |
816 && (((cb->start.lnum == cb->end.lnum | |
817 && cb->end.col - col > col - cb->start.col)) | |
818 || ((diff = (cb->end.lnum - row) - | |
819 (row - cb->start.lnum)) > 0 | |
820 || (diff == 0 && col < (int)(cb->start.col + | |
821 cb->end.col) / 2))))) | |
822 { | |
823 cb->origin_row = (short_u)cb->end.lnum; | |
824 cb->origin_start_col = cb->end.col - 1; | |
825 cb->origin_end_col = cb->end.col; | |
826 } | |
827 else | |
828 { | |
829 cb->origin_row = (short_u)cb->start.lnum; | |
830 cb->origin_start_col = cb->start.col; | |
831 cb->origin_end_col = cb->start.col; | |
832 } | |
833 if (cb->mode == SELECT_MODE_WORD && !repeated_click) | |
834 cb->mode = SELECT_MODE_CHAR; | |
835 } | |
836 | |
837 /* set state, for when using the right mouse button */ | |
838 cb->state = SELECT_IN_PROGRESS; | |
839 | |
840 #ifdef DEBUG_SELECTION | |
841 printf("Selection extending to (%d,%d)\n", row, col); | |
842 #endif | |
843 | |
844 if (repeated_click && ++cb->mode > SELECT_MODE_LINE) | |
845 cb->mode = SELECT_MODE_CHAR; | |
846 | |
847 switch (cb->mode) | |
848 { | |
849 case SELECT_MODE_CHAR: | |
850 /* If we're on a different line, find where the line ends */ | |
851 if (row != cb->prev.lnum) | |
852 cb->word_end_col = clip_get_line_end(row); | |
853 | |
854 /* See if we are before or after the origin of the selection */ | |
855 if (clip_compare_pos(row, col, cb->origin_row, | |
856 cb->origin_start_col) >= 0) | |
857 { | |
858 if (col >= (int)cb->word_end_col) | |
859 clip_update_modeless_selection(cb, cb->origin_row, | |
860 cb->origin_start_col, row, (int)Columns); | |
861 else | |
862 { | |
863 #ifdef FEAT_MBYTE | |
864 if (has_mbyte && mb_lefthalve(row, col)) | |
865 slen = 2; | |
866 #endif | |
867 clip_update_modeless_selection(cb, cb->origin_row, | |
868 cb->origin_start_col, row, col + slen); | |
869 } | |
870 } | |
871 else | |
872 { | |
873 #ifdef FEAT_MBYTE | |
874 if (has_mbyte | |
875 && mb_lefthalve(cb->origin_row, cb->origin_start_col)) | |
876 slen = 2; | |
877 #endif | |
878 if (col >= (int)cb->word_end_col) | |
879 clip_update_modeless_selection(cb, row, cb->word_end_col, | |
880 cb->origin_row, cb->origin_start_col + slen); | |
881 else | |
882 clip_update_modeless_selection(cb, row, col, | |
883 cb->origin_row, cb->origin_start_col + slen); | |
884 } | |
885 break; | |
886 | |
887 case SELECT_MODE_WORD: | |
888 /* If we are still within the same word, do nothing */ | |
889 if (row == cb->prev.lnum && col >= (int)cb->word_start_col | |
890 && col < (int)cb->word_end_col && !repeated_click) | |
891 return; | |
892 | |
893 /* Get new word boundaries */ | |
894 clip_get_word_boundaries(cb, row, col); | |
895 | |
896 /* Handle being after the origin point of selection */ | |
897 if (clip_compare_pos(row, col, cb->origin_row, | |
898 cb->origin_start_col) >= 0) | |
899 clip_update_modeless_selection(cb, cb->origin_row, | |
900 cb->origin_start_col, row, cb->word_end_col); | |
901 else | |
902 clip_update_modeless_selection(cb, row, cb->word_start_col, | |
903 cb->origin_row, cb->origin_end_col); | |
904 break; | |
905 | |
906 case SELECT_MODE_LINE: | |
907 if (row == cb->prev.lnum && !repeated_click) | |
908 return; | |
909 | |
910 if (clip_compare_pos(row, col, cb->origin_row, | |
911 cb->origin_start_col) >= 0) | |
912 clip_update_modeless_selection(cb, cb->origin_row, 0, row, | |
913 (int)Columns); | |
914 else | |
915 clip_update_modeless_selection(cb, row, 0, cb->origin_row, | |
916 (int)Columns); | |
917 break; | |
918 } | |
919 | |
920 cb->prev.lnum = row; | |
921 cb->prev.col = col; | |
922 | |
923 #ifdef DEBUG_SELECTION | |
924 printf("Selection is: (%u,%u) to (%u,%u)\n", cb->start.lnum, | |
925 cb->start.col, cb->end.lnum, cb->end.col); | |
926 #endif | |
927 } | |
928 | |
929 # if defined(FEAT_GUI) || defined(PROTO) | |
930 /* | |
931 * Redraw part of the selection if character at "row,col" is inside of it. | |
932 * Only used for the GUI. | |
933 */ | |
934 void | |
935 clip_may_redraw_selection(row, col, len) | |
936 int row, col; | |
937 int len; | |
938 { | |
939 int start = col; | |
940 int end = col + len; | |
941 | |
942 if (clip_star.state != SELECT_CLEARED | |
943 && row >= clip_star.start.lnum | |
944 && row <= clip_star.end.lnum) | |
945 { | |
946 if (row == clip_star.start.lnum && start < (int)clip_star.start.col) | |
947 start = clip_star.start.col; | |
948 if (row == clip_star.end.lnum && end > (int)clip_star.end.col) | |
949 end = clip_star.end.col; | |
950 if (end > start) | |
951 clip_invert_area(row, start, row, end, 0); | |
952 } | |
953 } | |
954 # endif | |
955 | |
956 /* | |
957 * Called from outside to clear selected region from the display | |
958 */ | |
959 void | |
3674 | 960 clip_clear_selection(cbd) |
961 VimClipboard *cbd; | |
7 | 962 { |
963 | |
3674 | 964 if (cbd->state == SELECT_CLEARED) |
7 | 965 return; |
966 | |
3674 | 967 clip_invert_area((int)cbd->start.lnum, cbd->start.col, (int)cbd->end.lnum, |
968 cbd->end.col, CLIP_CLEAR); | |
969 cbd->state = SELECT_CLEARED; | |
7 | 970 } |
971 | |
972 /* | |
973 * Clear the selection if any lines from "row1" to "row2" are inside of it. | |
974 */ | |
975 void | |
976 clip_may_clear_selection(row1, row2) | |
977 int row1, row2; | |
978 { | |
979 if (clip_star.state == SELECT_DONE | |
980 && row2 >= clip_star.start.lnum | |
981 && row1 <= clip_star.end.lnum) | |
3674 | 982 clip_clear_selection(&clip_star); |
7 | 983 } |
984 | |
985 /* | |
986 * Called before the screen is scrolled up or down. Adjusts the line numbers | |
987 * of the selection. Call with big number when clearing the screen. | |
988 */ | |
989 void | |
990 clip_scroll_selection(rows) | |
991 int rows; /* negative for scroll down */ | |
992 { | |
993 int lnum; | |
994 | |
995 if (clip_star.state == SELECT_CLEARED) | |
996 return; | |
997 | |
998 lnum = clip_star.start.lnum - rows; | |
999 if (lnum <= 0) | |
1000 clip_star.start.lnum = 0; | |
1001 else if (lnum >= screen_Rows) /* scrolled off of the screen */ | |
1002 clip_star.state = SELECT_CLEARED; | |
1003 else | |
1004 clip_star.start.lnum = lnum; | |
1005 | |
1006 lnum = clip_star.end.lnum - rows; | |
1007 if (lnum < 0) /* scrolled off of the screen */ | |
1008 clip_star.state = SELECT_CLEARED; | |
1009 else if (lnum >= screen_Rows) | |
1010 clip_star.end.lnum = screen_Rows - 1; | |
1011 else | |
1012 clip_star.end.lnum = lnum; | |
1013 } | |
1014 | |
1015 /* | |
1016 * Invert a region of the display between a starting and ending row and column | |
1017 * Values for "how": | |
1018 * CLIP_CLEAR: undo inversion | |
1019 * CLIP_SET: set inversion | |
1020 * CLIP_TOGGLE: set inversion if pos1 < pos2, undo inversion otherwise. | |
1021 * 0: invert (GUI only). | |
1022 */ | |
1023 static void | |
1024 clip_invert_area(row1, col1, row2, col2, how) | |
1025 int row1; | |
1026 int col1; | |
1027 int row2; | |
1028 int col2; | |
1029 int how; | |
1030 { | |
1031 int invert = FALSE; | |
1032 | |
1033 if (how == CLIP_SET) | |
1034 invert = TRUE; | |
1035 | |
1036 /* Swap the from and to positions so the from is always before */ | |
1037 if (clip_compare_pos(row1, col1, row2, col2) > 0) | |
1038 { | |
1039 int tmp_row, tmp_col; | |
1040 | |
1041 tmp_row = row1; | |
1042 tmp_col = col1; | |
1043 row1 = row2; | |
1044 col1 = col2; | |
1045 row2 = tmp_row; | |
1046 col2 = tmp_col; | |
1047 } | |
1048 else if (how == CLIP_TOGGLE) | |
1049 invert = TRUE; | |
1050 | |
1051 /* If all on the same line, do it the easy way */ | |
1052 if (row1 == row2) | |
1053 { | |
1054 clip_invert_rectangle(row1, col1, 1, col2 - col1, invert); | |
1055 } | |
1056 else | |
1057 { | |
1058 /* Handle a piece of the first line */ | |
1059 if (col1 > 0) | |
1060 { | |
1061 clip_invert_rectangle(row1, col1, 1, (int)Columns - col1, invert); | |
1062 row1++; | |
1063 } | |
1064 | |
1065 /* Handle a piece of the last line */ | |
1066 if (col2 < Columns - 1) | |
1067 { | |
1068 clip_invert_rectangle(row2, 0, 1, col2, invert); | |
1069 row2--; | |
1070 } | |
1071 | |
1072 /* Handle the rectangle thats left */ | |
1073 if (row2 >= row1) | |
1074 clip_invert_rectangle(row1, 0, row2 - row1 + 1, (int)Columns, | |
1075 invert); | |
1076 } | |
1077 } | |
1078 | |
1079 /* | |
1080 * Invert or un-invert a rectangle of the screen. | |
1081 * "invert" is true if the result is inverted. | |
1082 */ | |
1083 static void | |
1084 clip_invert_rectangle(row, col, height, width, invert) | |
1085 int row; | |
1086 int col; | |
1087 int height; | |
1088 int width; | |
1089 int invert; | |
1090 { | |
1091 #ifdef FEAT_GUI | |
1092 if (gui.in_use) | |
1093 gui_mch_invert_rectangle(row, col, height, width); | |
1094 else | |
1095 #endif | |
1096 screen_draw_rectangle(row, col, height, width, invert); | |
1097 } | |
1098 | |
1099 /* | |
1100 * Copy the currently selected area into the '*' register so it will be | |
1101 * available for pasting. | |
1102 * When "both" is TRUE also copy to the '+' register. | |
1103 */ | |
1104 void | |
1105 clip_copy_modeless_selection(both) | |
1883 | 1106 int both UNUSED; |
7 | 1107 { |
1108 char_u *buffer; | |
1109 char_u *bufp; | |
1110 int row; | |
1111 int start_col; | |
1112 int end_col; | |
1113 int line_end_col; | |
1114 int add_newline_flag = FALSE; | |
1115 int len; | |
1116 #ifdef FEAT_MBYTE | |
1117 char_u *p; | |
1118 #endif | |
1119 int row1 = clip_star.start.lnum; | |
1120 int col1 = clip_star.start.col; | |
1121 int row2 = clip_star.end.lnum; | |
1122 int col2 = clip_star.end.col; | |
1123 | |
534 | 1124 /* Can't use ScreenLines unless initialized */ |
1125 if (ScreenLines == NULL) | |
1126 return; | |
1127 | |
7 | 1128 /* |
1129 * Make sure row1 <= row2, and if row1 == row2 that col1 <= col2. | |
1130 */ | |
1131 if (row1 > row2) | |
1132 { | |
1133 row = row1; row1 = row2; row2 = row; | |
1134 row = col1; col1 = col2; col2 = row; | |
1135 } | |
1136 else if (row1 == row2 && col1 > col2) | |
1137 { | |
1138 row = col1; col1 = col2; col2 = row; | |
1139 } | |
1140 #ifdef FEAT_MBYTE | |
1141 /* correct starting point for being on right halve of double-wide char */ | |
1142 p = ScreenLines + LineOffset[row1]; | |
1143 if (enc_dbcs != 0) | |
1144 col1 -= (*mb_head_off)(p, p + col1); | |
1145 else if (enc_utf8 && p[col1] == 0) | |
1146 --col1; | |
1147 #endif | |
1148 | |
1149 /* Create a temporary buffer for storing the text */ | |
1150 len = (row2 - row1 + 1) * Columns + 1; | |
1151 #ifdef FEAT_MBYTE | |
1152 if (enc_dbcs != 0) | |
1153 len *= 2; /* max. 2 bytes per display cell */ | |
1154 else if (enc_utf8) | |
714 | 1155 len *= MB_MAXBYTES; |
7 | 1156 #endif |
1157 buffer = lalloc((long_u)len, TRUE); | |
1158 if (buffer == NULL) /* out of memory */ | |
1159 return; | |
1160 | |
1161 /* Process each row in the selection */ | |
1162 for (bufp = buffer, row = row1; row <= row2; row++) | |
1163 { | |
1164 if (row == row1) | |
1165 start_col = col1; | |
1166 else | |
1167 start_col = 0; | |
1168 | |
1169 if (row == row2) | |
1170 end_col = col2; | |
1171 else | |
1172 end_col = Columns; | |
1173 | |
1174 line_end_col = clip_get_line_end(row); | |
1175 | |
1176 /* See if we need to nuke some trailing whitespace */ | |
1177 if (end_col >= Columns && (row < row2 || end_col > line_end_col)) | |
1178 { | |
1179 /* Get rid of trailing whitespace */ | |
1180 end_col = line_end_col; | |
1181 if (end_col < start_col) | |
1182 end_col = start_col; | |
1183 | |
1184 /* If the last line extended to the end, add an extra newline */ | |
1185 if (row == row2) | |
1186 add_newline_flag = TRUE; | |
1187 } | |
1188 | |
1189 /* If after the first row, we need to always add a newline */ | |
1190 if (row > row1 && !LineWraps[row - 1]) | |
1191 *bufp++ = NL; | |
1192 | |
1193 if (row < screen_Rows && end_col <= screen_Columns) | |
1194 { | |
1195 #ifdef FEAT_MBYTE | |
1196 if (enc_dbcs != 0) | |
1197 { | |
944 | 1198 int i; |
1199 | |
7 | 1200 p = ScreenLines + LineOffset[row]; |
1201 for (i = start_col; i < end_col; ++i) | |
1202 if (enc_dbcs == DBCS_JPNU && p[i] == 0x8e) | |
1203 { | |
1204 /* single-width double-byte char */ | |
1205 *bufp++ = 0x8e; | |
1206 *bufp++ = ScreenLines2[LineOffset[row] + i]; | |
1207 } | |
1208 else | |
1209 { | |
1210 *bufp++ = p[i]; | |
1211 if (MB_BYTE2LEN(p[i]) == 2) | |
1212 *bufp++ = p[++i]; | |
1213 } | |
1214 } | |
1215 else if (enc_utf8) | |
1216 { | |
1217 int off; | |
714 | 1218 int i; |
761 | 1219 int ci; |
7 | 1220 |
1221 off = LineOffset[row]; | |
1222 for (i = start_col; i < end_col; ++i) | |
1223 { | |
1224 /* The base character is either in ScreenLinesUC[] or | |
1225 * ScreenLines[]. */ | |
1226 if (ScreenLinesUC[off + i] == 0) | |
1227 *bufp++ = ScreenLines[off + i]; | |
1228 else | |
1229 { | |
1230 bufp += utf_char2bytes(ScreenLinesUC[off + i], bufp); | |
761 | 1231 for (ci = 0; ci < Screen_mco; ++ci) |
7 | 1232 { |
714 | 1233 /* Add a composing character. */ |
761 | 1234 if (ScreenLinesC[ci][off + i] == 0) |
714 | 1235 break; |
761 | 1236 bufp += utf_char2bytes(ScreenLinesC[ci][off + i], |
7 | 1237 bufp); |
1238 } | |
1239 } | |
1240 /* Skip right halve of double-wide character. */ | |
1241 if (ScreenLines[off + i + 1] == 0) | |
1242 ++i; | |
1243 } | |
1244 } | |
1245 else | |
1246 #endif | |
1247 { | |
1248 STRNCPY(bufp, ScreenLines + LineOffset[row] + start_col, | |
1249 end_col - start_col); | |
1250 bufp += end_col - start_col; | |
1251 } | |
1252 } | |
1253 } | |
1254 | |
1255 /* Add a newline at the end if the selection ended there */ | |
1256 if (add_newline_flag) | |
1257 *bufp++ = NL; | |
1258 | |
1259 /* First cleanup any old selection and become the owner. */ | |
1260 clip_free_selection(&clip_star); | |
1261 clip_own_selection(&clip_star); | |
1262 | |
1263 /* Yank the text into the '*' register. */ | |
1264 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_star); | |
1265 | |
1266 /* Make the register contents available to the outside world. */ | |
1267 clip_gen_set_selection(&clip_star); | |
1268 | |
1269 #ifdef FEAT_X11 | |
1270 if (both) | |
1271 { | |
1272 /* Do the same for the '+' register. */ | |
1273 clip_free_selection(&clip_plus); | |
1274 clip_own_selection(&clip_plus); | |
1275 clip_yank_selection(MCHAR, buffer, (long)(bufp - buffer), &clip_plus); | |
1276 clip_gen_set_selection(&clip_plus); | |
1277 } | |
1278 #endif | |
1279 vim_free(buffer); | |
1280 } | |
1281 | |
1282 /* | |
1283 * Find the starting and ending positions of the word at the given row and | |
1284 * column. Only white-separated words are recognized here. | |
1285 */ | |
1286 #define CHAR_CLASS(c) (c <= ' ' ? ' ' : vim_iswordc(c)) | |
1287 | |
1288 static void | |
1289 clip_get_word_boundaries(cb, row, col) | |
1290 VimClipboard *cb; | |
1291 int row; | |
1292 int col; | |
1293 { | |
1294 int start_class; | |
1295 int temp_col; | |
1296 char_u *p; | |
1297 #ifdef FEAT_MBYTE | |
1298 int mboff; | |
1299 #endif | |
1300 | |
534 | 1301 if (row >= screen_Rows || col >= screen_Columns || ScreenLines == NULL) |
7 | 1302 return; |
1303 | |
1304 p = ScreenLines + LineOffset[row]; | |
1305 #ifdef FEAT_MBYTE | |
1306 /* Correct for starting in the right halve of a double-wide char */ | |
1307 if (enc_dbcs != 0) | |
1308 col -= dbcs_screen_head_off(p, p + col); | |
1309 else if (enc_utf8 && p[col] == 0) | |
1310 --col; | |
1311 #endif | |
1312 start_class = CHAR_CLASS(p[col]); | |
1313 | |
1314 temp_col = col; | |
1315 for ( ; temp_col > 0; temp_col--) | |
1316 #ifdef FEAT_MBYTE | |
1317 if (enc_dbcs != 0 | |
1318 && (mboff = dbcs_screen_head_off(p, p + temp_col - 1)) > 0) | |
1319 temp_col -= mboff; | |
1320 else | |
1321 #endif | |
1322 if (CHAR_CLASS(p[temp_col - 1]) != start_class | |
1323 #ifdef FEAT_MBYTE | |
1324 && !(enc_utf8 && p[temp_col - 1] == 0) | |
1325 #endif | |
1326 ) | |
1327 break; | |
1328 cb->word_start_col = temp_col; | |
1329 | |
1330 temp_col = col; | |
1331 for ( ; temp_col < screen_Columns; temp_col++) | |
1332 #ifdef FEAT_MBYTE | |
1333 if (enc_dbcs != 0 && dbcs_ptr2cells(p + temp_col) == 2) | |
1334 ++temp_col; | |
1335 else | |
1336 #endif | |
1337 if (CHAR_CLASS(p[temp_col]) != start_class | |
1338 #ifdef FEAT_MBYTE | |
1339 && !(enc_utf8 && p[temp_col] == 0) | |
1340 #endif | |
1341 ) | |
1342 break; | |
1343 cb->word_end_col = temp_col; | |
1344 } | |
1345 | |
1346 /* | |
1347 * Find the column position for the last non-whitespace character on the given | |
1348 * line. | |
1349 */ | |
1350 static int | |
1351 clip_get_line_end(row) | |
1352 int row; | |
1353 { | |
1354 int i; | |
1355 | |
534 | 1356 if (row >= screen_Rows || ScreenLines == NULL) |
7 | 1357 return 0; |
1358 for (i = screen_Columns; i > 0; i--) | |
1359 if (ScreenLines[LineOffset[row] + i - 1] != ' ') | |
1360 break; | |
1361 return i; | |
1362 } | |
1363 | |
1364 /* | |
1365 * Update the currently selected region by adding and/or subtracting from the | |
1366 * beginning or end and inverting the changed area(s). | |
1367 */ | |
1368 static void | |
1369 clip_update_modeless_selection(cb, row1, col1, row2, col2) | |
1370 VimClipboard *cb; | |
1371 int row1; | |
1372 int col1; | |
1373 int row2; | |
1374 int col2; | |
1375 { | |
1376 /* See if we changed at the beginning of the selection */ | |
1377 if (row1 != cb->start.lnum || col1 != (int)cb->start.col) | |
1378 { | |
1379 clip_invert_area(row1, col1, (int)cb->start.lnum, cb->start.col, | |
1380 CLIP_TOGGLE); | |
1381 cb->start.lnum = row1; | |
1382 cb->start.col = col1; | |
1383 } | |
1384 | |
1385 /* See if we changed at the end of the selection */ | |
1386 if (row2 != cb->end.lnum || col2 != (int)cb->end.col) | |
1387 { | |
1388 clip_invert_area((int)cb->end.lnum, cb->end.col, row2, col2, | |
1389 CLIP_TOGGLE); | |
1390 cb->end.lnum = row2; | |
1391 cb->end.col = col2; | |
1392 } | |
1393 } | |
1394 | |
1395 int | |
1396 clip_gen_own_selection(cbd) | |
1397 VimClipboard *cbd; | |
1398 { | |
1399 #ifdef FEAT_XCLIPBOARD | |
1400 # ifdef FEAT_GUI | |
1401 if (gui.in_use) | |
1402 return clip_mch_own_selection(cbd); | |
1403 else | |
1404 # endif | |
1405 return clip_xterm_own_selection(cbd); | |
1406 #else | |
1407 return clip_mch_own_selection(cbd); | |
1408 #endif | |
1409 } | |
1410 | |
1411 void | |
1412 clip_gen_lose_selection(cbd) | |
1413 VimClipboard *cbd; | |
1414 { | |
1415 #ifdef FEAT_XCLIPBOARD | |
1416 # ifdef FEAT_GUI | |
1417 if (gui.in_use) | |
1418 clip_mch_lose_selection(cbd); | |
1419 else | |
1420 # endif | |
1421 clip_xterm_lose_selection(cbd); | |
1422 #else | |
1423 clip_mch_lose_selection(cbd); | |
1424 #endif | |
1425 } | |
1426 | |
1427 void | |
1428 clip_gen_set_selection(cbd) | |
1429 VimClipboard *cbd; | |
1430 { | |
1431 #ifdef FEAT_XCLIPBOARD | |
1432 # ifdef FEAT_GUI | |
1433 if (gui.in_use) | |
1434 clip_mch_set_selection(cbd); | |
1435 else | |
1436 # endif | |
1437 clip_xterm_set_selection(cbd); | |
1438 #else | |
1439 clip_mch_set_selection(cbd); | |
1440 #endif | |
1441 } | |
1442 | |
1443 void | |
1444 clip_gen_request_selection(cbd) | |
1445 VimClipboard *cbd; | |
1446 { | |
1447 #ifdef FEAT_XCLIPBOARD | |
1448 # ifdef FEAT_GUI | |
1449 if (gui.in_use) | |
1450 clip_mch_request_selection(cbd); | |
1451 else | |
1452 # endif | |
1453 clip_xterm_request_selection(cbd); | |
1454 #else | |
1455 clip_mch_request_selection(cbd); | |
1456 #endif | |
1457 } | |
1458 | |
4209 | 1459 int |
1460 clip_gen_owner_exists(cbd) | |
4272 | 1461 VimClipboard *cbd UNUSED; |
4209 | 1462 { |
1463 #ifdef FEAT_XCLIPBOARD | |
1464 # ifdef FEAT_GUI_GTK | |
1465 if (gui.in_use) | |
1466 return clip_gtk_owner_exists(cbd); | |
1467 else | |
1468 # endif | |
1469 return clip_x11_owner_exists(cbd); | |
1470 #endif | |
1471 return TRUE; | |
1472 } | |
1473 | |
7 | 1474 #endif /* FEAT_CLIPBOARD */ |
1475 | |
1476 /***************************************************************************** | |
1477 * Functions that handle the input buffer. | |
1478 * This is used for any GUI version, and the unix terminal version. | |
1479 * | |
1480 * For Unix, the input characters are buffered to be able to check for a | |
1481 * CTRL-C. This should be done with signals, but I don't know how to do that | |
1482 * in a portable way for a tty in RAW mode. | |
1483 * | |
1484 * For the client-server code in the console the received keys are put in the | |
1485 * input buffer. | |
1486 */ | |
1487 | |
1488 #if defined(USE_INPUT_BUF) || defined(PROTO) | |
1489 | |
1490 /* | |
1491 * Internal typeahead buffer. Includes extra space for long key code | |
1492 * descriptions which would otherwise overflow. The buffer is considered full | |
1493 * when only this extra space (or part of it) remains. | |
1494 */ | |
1495 #if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \ | |
1496 || defined(FEAT_CLIENTSERVER) | |
1497 /* | |
1498 * Sun WorkShop and NetBeans stuff debugger commands into the input buffer. | |
1499 * This requires a larger buffer... | |
1500 * (Madsen) Go with this for remote input as well ... | |
1501 */ | |
1502 # define INBUFLEN 4096 | |
1503 #else | |
1504 # define INBUFLEN 250 | |
1505 #endif | |
1506 | |
1507 static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN]; | |
1508 static int inbufcount = 0; /* number of chars in inbuf[] */ | |
1509 | |
1510 /* | |
1511 * vim_is_input_buf_full(), vim_is_input_buf_empty(), add_to_input_buf(), and | |
1512 * trash_input_buf() are functions for manipulating the input buffer. These | |
1513 * are used by the gui_* calls when a GUI is used to handle keyboard input. | |
1514 */ | |
1515 | |
1516 int | |
1517 vim_is_input_buf_full() | |
1518 { | |
1519 return (inbufcount >= INBUFLEN); | |
1520 } | |
1521 | |
1522 int | |
1523 vim_is_input_buf_empty() | |
1524 { | |
1525 return (inbufcount == 0); | |
1526 } | |
1527 | |
1528 #if defined(FEAT_OLE) || defined(PROTO) | |
1529 int | |
1530 vim_free_in_input_buf() | |
1531 { | |
1532 return (INBUFLEN - inbufcount); | |
1533 } | |
1534 #endif | |
1535 | |
575 | 1536 #if defined(FEAT_GUI_GTK) || defined(PROTO) |
7 | 1537 int |
1538 vim_used_in_input_buf() | |
1539 { | |
1540 return inbufcount; | |
1541 } | |
1542 #endif | |
1543 | |
1544 #if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO) | |
1545 /* | |
1546 * Return the current contents of the input buffer and make it empty. | |
1547 * The returned pointer must be passed to set_input_buf() later. | |
1548 */ | |
1549 char_u * | |
1550 get_input_buf() | |
1551 { | |
1552 garray_T *gap; | |
1553 | |
1554 /* We use a growarray to store the data pointer and the length. */ | |
1555 gap = (garray_T *)alloc((unsigned)sizeof(garray_T)); | |
1556 if (gap != NULL) | |
1557 { | |
1558 /* Add one to avoid a zero size. */ | |
1559 gap->ga_data = alloc((unsigned)inbufcount + 1); | |
1560 if (gap->ga_data != NULL) | |
1561 mch_memmove(gap->ga_data, inbuf, (size_t)inbufcount); | |
1562 gap->ga_len = inbufcount; | |
1563 } | |
1564 trash_input_buf(); | |
1565 return (char_u *)gap; | |
1566 } | |
1567 | |
1568 /* | |
1569 * Restore the input buffer with a pointer returned from get_input_buf(). | |
1570 * The allocated memory is freed, this only works once! | |
1571 */ | |
1572 void | |
1573 set_input_buf(p) | |
1574 char_u *p; | |
1575 { | |
1576 garray_T *gap = (garray_T *)p; | |
1577 | |
1578 if (gap != NULL) | |
1579 { | |
1580 if (gap->ga_data != NULL) | |
1581 { | |
1582 mch_memmove(inbuf, gap->ga_data, gap->ga_len); | |
1583 inbufcount = gap->ga_len; | |
1584 vim_free(gap->ga_data); | |
1585 } | |
1586 vim_free(gap); | |
1587 } | |
1588 } | |
1589 #endif | |
1590 | |
1621 | 1591 #if defined(FEAT_GUI) \ |
1592 || defined(FEAT_MOUSE_GPM) || defined(FEAT_SYSMOUSE) \ | |
7 | 1593 || defined(FEAT_XCLIPBOARD) || defined(VMS) \ |
666 | 1594 || defined(FEAT_SNIFF) || defined(FEAT_CLIENTSERVER) \ |
1595 || defined(PROTO) | |
7 | 1596 /* |
1597 * Add the given bytes to the input buffer | |
1598 * Special keys start with CSI. A real CSI must have been translated to | |
1599 * CSI KS_EXTRA KE_CSI. K_SPECIAL doesn't require translation. | |
1600 */ | |
1601 void | |
1602 add_to_input_buf(s, len) | |
1603 char_u *s; | |
1604 int len; | |
1605 { | |
1606 if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN) | |
1607 return; /* Shouldn't ever happen! */ | |
1608 | |
1609 #ifdef FEAT_HANGULIN | |
1610 if ((State & (INSERT|CMDLINE)) && hangul_input_state_get()) | |
1611 if ((len = hangul_input_process(s, len)) == 0) | |
1612 return; | |
1613 #endif | |
1614 | |
1615 while (len--) | |
1616 inbuf[inbufcount++] = *s++; | |
1617 } | |
1618 #endif | |
1619 | |
1364 | 1620 #if ((defined(FEAT_XIM) || defined(FEAT_DND)) && defined(FEAT_GUI_GTK)) \ |
1621 || defined(FEAT_GUI_MSWIN) \ | |
1622 || defined(FEAT_GUI_MAC) \ | |
7 | 1623 || (defined(FEAT_MBYTE) && defined(FEAT_MBYTE_IME)) \ |
666 | 1624 || (defined(FEAT_GUI) && (!defined(USE_ON_FLY_SCROLL) \ |
1625 || defined(FEAT_MENU))) \ | |
7 | 1626 || defined(PROTO) |
1627 /* | |
1628 * Add "str[len]" to the input buffer while escaping CSI bytes. | |
1629 */ | |
1630 void | |
1631 add_to_input_buf_csi(char_u *str, int len) | |
1632 { | |
1633 int i; | |
1634 char_u buf[2]; | |
1635 | |
1636 for (i = 0; i < len; ++i) | |
1637 { | |
1638 add_to_input_buf(str + i, 1); | |
1639 if (str[i] == CSI) | |
1640 { | |
1641 /* Turn CSI into K_CSI. */ | |
1642 buf[0] = KS_EXTRA; | |
1643 buf[1] = (int)KE_CSI; | |
1644 add_to_input_buf(buf, 2); | |
1645 } | |
1646 } | |
1647 } | |
1648 #endif | |
1649 | |
1650 #if defined(FEAT_HANGULIN) || defined(PROTO) | |
1651 void | |
2896 | 1652 push_raw_key(s, len) |
7 | 1653 char_u *s; |
1654 int len; | |
1655 { | |
1656 while (len--) | |
1657 inbuf[inbufcount++] = *s++; | |
1658 } | |
1659 #endif | |
1660 | |
1661 #if defined(FEAT_GUI) || defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) \ | |
1662 || defined(PROTO) | |
1663 /* Remove everything from the input buffer. Called when ^C is found */ | |
1664 void | |
1665 trash_input_buf() | |
1666 { | |
1667 inbufcount = 0; | |
1668 } | |
1669 #endif | |
1670 | |
1671 /* | |
1672 * Read as much data from the input buffer as possible up to maxlen, and store | |
1673 * it in buf. | |
1674 * Note: this function used to be Read() in unix.c | |
1675 */ | |
1676 int | |
1677 read_from_input_buf(buf, maxlen) | |
1678 char_u *buf; | |
1679 long maxlen; | |
1680 { | |
1681 if (inbufcount == 0) /* if the buffer is empty, fill it */ | |
1682 fill_input_buf(TRUE); | |
1683 if (maxlen > inbufcount) | |
1684 maxlen = inbufcount; | |
1685 mch_memmove(buf, inbuf, (size_t)maxlen); | |
1686 inbufcount -= maxlen; | |
1687 if (inbufcount) | |
1688 mch_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount); | |
1689 return (int)maxlen; | |
1690 } | |
1691 | |
1692 void | |
1693 fill_input_buf(exit_on_error) | |
1883 | 1694 int exit_on_error UNUSED; |
7 | 1695 { |
1696 #if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX) | |
1697 int len; | |
1698 int try; | |
1699 static int did_read_something = FALSE; | |
1700 # ifdef FEAT_MBYTE | |
1701 static char_u *rest = NULL; /* unconverted rest of previous read */ | |
1702 static int restlen = 0; | |
1703 int unconverted; | |
1704 # endif | |
1705 #endif | |
1706 | |
1707 #ifdef FEAT_GUI | |
294 | 1708 if (gui.in_use |
1709 # ifdef NO_CONSOLE_INPUT | |
1710 /* Don't use the GUI input when the window hasn't been opened yet. | |
1711 * We get here from ui_inchar() when we should try reading from stdin. */ | |
1712 && !no_console_input() | |
1713 # endif | |
1714 ) | |
7 | 1715 { |
1716 gui_mch_update(); | |
1717 return; | |
1718 } | |
1719 #endif | |
1720 #if defined(UNIX) || defined(OS2) || defined(VMS) || defined(MACOS_X_UNIX) | |
1721 if (vim_is_input_buf_full()) | |
1722 return; | |
1723 /* | |
1724 * Fill_input_buf() is only called when we really need a character. | |
1725 * If we can't get any, but there is some in the buffer, just return. | |
1726 * If we can't get any, and there isn't any in the buffer, we give up and | |
1727 * exit Vim. | |
1728 */ | |
1729 # ifdef __BEOS__ | |
1730 /* | |
1731 * On the BeBox version (for now), all input is secretly performed within | |
1732 * beos_select() which is called from RealWaitForChar(). | |
1733 */ | |
1734 while (!vim_is_input_buf_full() && RealWaitForChar(read_cmd_fd, 0, NULL)) | |
1735 ; | |
1736 len = inbufcount; | |
1737 inbufcount = 0; | |
1738 # else | |
1739 | |
1740 # ifdef FEAT_SNIFF | |
1741 if (sniff_request_waiting) | |
1742 { | |
1743 add_to_input_buf((char_u *)"\233sniff",6); /* results in K_SNIFF */ | |
1744 sniff_request_waiting = 0; | |
1745 want_sniff_request = 0; | |
1746 return; | |
1747 } | |
1748 # endif | |
1749 | |
1750 # ifdef FEAT_MBYTE | |
1751 if (rest != NULL) | |
1752 { | |
1753 /* Use remainder of previous call, starts with an invalid character | |
1754 * that may become valid when reading more. */ | |
1755 if (restlen > INBUFLEN - inbufcount) | |
1756 unconverted = INBUFLEN - inbufcount; | |
1757 else | |
1758 unconverted = restlen; | |
1759 mch_memmove(inbuf + inbufcount, rest, unconverted); | |
1760 if (unconverted == restlen) | |
1761 { | |
1762 vim_free(rest); | |
1763 rest = NULL; | |
1764 } | |
1765 else | |
1766 { | |
1767 restlen -= unconverted; | |
1768 mch_memmove(rest, rest + unconverted, restlen); | |
1769 } | |
1770 inbufcount += unconverted; | |
1771 } | |
1772 else | |
1773 unconverted = 0; | |
1774 #endif | |
1775 | |
1776 len = 0; /* to avoid gcc warning */ | |
1777 for (try = 0; try < 100; ++try) | |
1778 { | |
1779 # ifdef VMS | |
1780 len = vms_read( | |
1781 # else | |
1782 len = read(read_cmd_fd, | |
1783 # endif | |
1784 (char *)inbuf + inbufcount, (size_t)((INBUFLEN - inbufcount) | |
1785 # ifdef FEAT_MBYTE | |
1786 / input_conv.vc_factor | |
1787 # endif | |
1788 )); | |
1789 # if 0 | |
1790 ) /* avoid syntax highlight error */ | |
1791 # endif | |
169 | 1792 |
7 | 1793 if (len > 0 || got_int) |
1794 break; | |
1795 /* | |
1796 * If reading stdin results in an error, continue reading stderr. | |
1797 * This helps when using "foo | xargs vim". | |
1798 */ | |
1799 if (!did_read_something && !isatty(read_cmd_fd) && read_cmd_fd == 0) | |
1800 { | |
1801 int m = cur_tmode; | |
1802 | |
1803 /* We probably set the wrong file descriptor to raw mode. Switch | |
1804 * back to cooked mode, use another descriptor and set the mode to | |
1805 * what it was. */ | |
1806 settmode(TMODE_COOK); | |
1807 #ifdef HAVE_DUP | |
1808 /* Use stderr for stdin, also works for shell commands. */ | |
1809 close(0); | |
1757 | 1810 ignored = dup(2); |
7 | 1811 #else |
1812 read_cmd_fd = 2; /* read from stderr instead of stdin */ | |
1813 #endif | |
1814 settmode(m); | |
1815 } | |
1816 if (!exit_on_error) | |
1817 return; | |
1818 } | |
1819 # endif | |
1820 if (len <= 0 && !got_int) | |
1821 read_error_exit(); | |
1822 if (len > 0) | |
1823 did_read_something = TRUE; | |
1824 if (got_int) | |
1825 { | |
1826 /* Interrupted, pretend a CTRL-C was typed. */ | |
1827 inbuf[0] = 3; | |
1828 inbufcount = 1; | |
1829 } | |
1830 else | |
1831 { | |
1832 # ifdef FEAT_MBYTE | |
1833 /* | |
1834 * May perform conversion on the input characters. | |
1835 * Include the unconverted rest of the previous call. | |
1836 * If there is an incomplete char at the end it is kept for the next | |
1837 * time, reading more bytes should make conversion possible. | |
1838 * Don't do this in the unlikely event that the input buffer is too | |
1839 * small ("rest" still contains more bytes). | |
1840 */ | |
1841 if (input_conv.vc_type != CONV_NONE) | |
1842 { | |
1843 inbufcount -= unconverted; | |
1844 len = convert_input_safe(inbuf + inbufcount, | |
1845 len + unconverted, INBUFLEN - inbufcount, | |
1846 rest == NULL ? &rest : NULL, &restlen); | |
1847 } | |
1848 # endif | |
1849 while (len-- > 0) | |
1850 { | |
1851 /* | |
1852 * if a CTRL-C was typed, remove it from the buffer and set got_int | |
1853 */ | |
1854 if (inbuf[inbufcount] == 3 && ctrl_c_interrupts) | |
1855 { | |
1856 /* remove everything typed before the CTRL-C */ | |
1857 mch_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1)); | |
1858 inbufcount = 0; | |
1859 got_int = TRUE; | |
1860 } | |
1861 ++inbufcount; | |
1862 } | |
1863 } | |
1864 #endif /* UNIX or OS2 or VMS*/ | |
1865 } | |
1866 #endif /* defined(UNIX) || defined(FEAT_GUI) || defined(OS2) || defined(VMS) */ | |
1867 | |
1868 /* | |
1869 * Exit because of an input read error. | |
1870 */ | |
1871 void | |
1872 read_error_exit() | |
1873 { | |
1874 if (silent_mode) /* Normal way to exit for "ex -s" */ | |
1875 getout(0); | |
1876 STRCPY(IObuff, _("Vim: Error reading input, exiting...\n")); | |
1877 preserve_exit(); | |
1878 } | |
1879 | |
1880 #if defined(CURSOR_SHAPE) || defined(PROTO) | |
1881 /* | |
1882 * May update the shape of the cursor. | |
1883 */ | |
1884 void | |
1885 ui_cursor_shape() | |
1886 { | |
1887 # ifdef FEAT_GUI | |
1888 if (gui.in_use) | |
1889 gui_update_cursor_later(); | |
36 | 1890 else |
7 | 1891 # endif |
36 | 1892 term_cursor_shape(); |
1893 | |
7 | 1894 # ifdef MCH_CURSOR_SHAPE |
1895 mch_update_cursor(); | |
1896 # endif | |
2378
85b7dc8da5eb
Add the 'concealcursor' option to decide when the cursor line is to be
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
1897 |
85b7dc8da5eb
Add the 'concealcursor' option to decide when the cursor line is to be
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
1898 # ifdef FEAT_CONCEAL |
2428
33148c37f3c9
Changes for VMS. Mostly by Zoltan Arpadffy.
Bram Moolenaar <bram@vim.org>
parents:
2378
diff
changeset
|
1899 conceal_check_cursur_line(); |
2378
85b7dc8da5eb
Add the 'concealcursor' option to decide when the cursor line is to be
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
1900 # endif |
7 | 1901 } |
1902 #endif | |
1903 | |
1904 #if defined(FEAT_CLIPBOARD) || defined(FEAT_GUI) || defined(FEAT_RIGHTLEFT) \ | |
1671 | 1905 || defined(FEAT_MBYTE) || defined(PROTO) |
7 | 1906 /* |
1907 * Check bounds for column number | |
1908 */ | |
1909 int | |
1910 check_col(col) | |
1911 int col; | |
1912 { | |
1913 if (col < 0) | |
1914 return 0; | |
1915 if (col >= (int)screen_Columns) | |
1916 return (int)screen_Columns - 1; | |
1917 return col; | |
1918 } | |
1919 | |
1920 /* | |
1921 * Check bounds for row number | |
1922 */ | |
1923 int | |
1924 check_row(row) | |
1925 int row; | |
1926 { | |
1927 if (row < 0) | |
1928 return 0; | |
1929 if (row >= (int)screen_Rows) | |
1930 return (int)screen_Rows - 1; | |
1931 return row; | |
1932 } | |
1933 #endif | |
1934 | |
1935 /* | |
1936 * Stuff for the X clipboard. Shared between VMS and Unix. | |
1937 */ | |
1938 | |
1939 #if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) || defined(PROTO) | |
1940 # include <X11/Xatom.h> | |
1941 # include <X11/Intrinsic.h> | |
1942 | |
1943 /* | |
1944 * Open the application context (if it hasn't been opened yet). | |
1945 * Used for Motif and Athena GUI and the xterm clipboard. | |
1946 */ | |
1947 void | |
1948 open_app_context() | |
1949 { | |
1950 if (app_context == NULL) | |
1951 { | |
1952 XtToolkitInitialize(); | |
1953 app_context = XtCreateApplicationContext(); | |
1954 } | |
1955 } | |
1956 | |
1957 static Atom vim_atom; /* Vim's own special selection format */ | |
1958 #ifdef FEAT_MBYTE | |
1959 static Atom vimenc_atom; /* Vim's extended selection format */ | |
3346 | 1960 static Atom utf8_atom; |
7 | 1961 #endif |
1962 static Atom compound_text_atom; | |
1963 static Atom text_atom; | |
1964 static Atom targets_atom; | |
2270
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1965 static Atom timestamp_atom; /* Used to get a timestamp */ |
7 | 1966 |
1967 void | |
1968 x11_setup_atoms(dpy) | |
1969 Display *dpy; | |
1970 { | |
1971 vim_atom = XInternAtom(dpy, VIM_ATOM_NAME, False); | |
1972 #ifdef FEAT_MBYTE | |
1973 vimenc_atom = XInternAtom(dpy, VIMENC_ATOM_NAME,False); | |
3346 | 1974 utf8_atom = XInternAtom(dpy, "UTF8_STRING", False); |
7 | 1975 #endif |
1976 compound_text_atom = XInternAtom(dpy, "COMPOUND_TEXT", False); | |
1977 text_atom = XInternAtom(dpy, "TEXT", False); | |
2270
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1978 targets_atom = XInternAtom(dpy, "TARGETS", False); |
7 | 1979 clip_star.sel_atom = XA_PRIMARY; |
2270
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1980 clip_plus.sel_atom = XInternAtom(dpy, "CLIPBOARD", False); |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1981 timestamp_atom = XInternAtom(dpy, "TIMESTAMP", False); |
7 | 1982 } |
1983 | |
1984 /* | |
1985 * X Selection stuff, for cutting and pasting text to other windows. | |
1986 */ | |
1987 | |
2270
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1988 static Boolean clip_x11_convert_selection_cb __ARGS((Widget, Atom *, Atom *, Atom *, XtPointer *, long_u *, int *)); |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1989 static void clip_x11_lose_ownership_cb __ARGS((Widget, Atom *)); |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1990 static void clip_x11_timestamp_cb __ARGS((Widget w, XtPointer n, XEvent *event, Boolean *cont)); |
2586 | 1991 static void clip_x11_request_selection_cb __ARGS((Widget, XtPointer, Atom *, Atom *, XtPointer, long_u *, int *)); |
2270
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1992 |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1993 /* |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1994 * Property callback to get a timestamp for XtOwnSelection. |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1995 */ |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1996 static void |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1997 clip_x11_timestamp_cb(w, n, event, cont) |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1998 Widget w; |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
1999 XtPointer n UNUSED; |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2000 XEvent *event; |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2001 Boolean *cont UNUSED; |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2002 { |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2003 Atom actual_type; |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2004 int format; |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2005 unsigned long nitems, bytes_after; |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2006 unsigned char *prop=NULL; |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2007 XPropertyEvent *xproperty=&event->xproperty; |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2008 |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2009 /* Must be a property notify, state can't be Delete (True), has to be |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2010 * one of the supported selection types. */ |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2011 if (event->type != PropertyNotify || xproperty->state |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2012 || (xproperty->atom != clip_star.sel_atom |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2013 && xproperty->atom != clip_plus.sel_atom)) |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2014 return; |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2015 |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2016 if (XGetWindowProperty(xproperty->display, xproperty->window, |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2017 xproperty->atom, 0, 0, False, timestamp_atom, &actual_type, &format, |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2018 &nitems, &bytes_after, &prop)) |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2019 return; |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2020 |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2021 if (prop) |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2022 XFree(prop); |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2023 |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2024 /* Make sure the property type is "TIMESTAMP" and it's 32 bits. */ |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2025 if (actual_type != timestamp_atom || format != 32) |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2026 return; |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2027 |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2028 /* Get the selection, using the event timestamp. */ |
2586 | 2029 if (XtOwnSelection(w, xproperty->atom, xproperty->time, |
2030 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb, | |
2031 NULL) == OK) | |
2032 { | |
2033 /* Set the "owned" flag now, there may have been a call to | |
2034 * lose_ownership_cb in between. */ | |
2035 if (xproperty->atom == clip_plus.sel_atom) | |
2036 clip_plus.owned = TRUE; | |
2037 else | |
2038 clip_star.owned = TRUE; | |
2039 } | |
2270
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2040 } |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2041 |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2042 void |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2043 x11_setup_selection(w) |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2044 Widget w; |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2045 { |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2046 XtAddEventHandler(w, PropertyChangeMask, False, |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2047 /*(XtEventHandler)*/clip_x11_timestamp_cb, (XtPointer)NULL); |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2048 } |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2049 |
7 | 2050 static void |
2051 clip_x11_request_selection_cb(w, success, sel_atom, type, value, length, | |
2052 format) | |
1883 | 2053 Widget w UNUSED; |
7 | 2054 XtPointer success; |
2055 Atom *sel_atom; | |
2056 Atom *type; | |
2057 XtPointer value; | |
2058 long_u *length; | |
2059 int *format; | |
2060 { | |
2896 | 2061 int motion_type = MAUTO; |
7 | 2062 long_u len; |
2063 char_u *p; | |
2064 char **text_list = NULL; | |
2065 VimClipboard *cbd; | |
2066 #ifdef FEAT_MBYTE | |
2067 char_u *tmpbuf = NULL; | |
2068 #endif | |
2069 | |
2070 if (*sel_atom == clip_plus.sel_atom) | |
2071 cbd = &clip_plus; | |
2072 else | |
2073 cbd = &clip_star; | |
2074 | |
2075 if (value == NULL || *length == 0) | |
2076 { | |
1719 | 2077 clip_free_selection(cbd); /* nothing received, clear register */ |
7 | 2078 *(int *)success = FALSE; |
2079 return; | |
2080 } | |
2081 p = (char_u *)value; | |
2082 len = *length; | |
2083 if (*type == vim_atom) | |
2084 { | |
2085 motion_type = *p++; | |
2086 len--; | |
2087 } | |
2088 | |
2089 #ifdef FEAT_MBYTE | |
2090 else if (*type == vimenc_atom) | |
2091 { | |
2092 char_u *enc; | |
2093 vimconv_T conv; | |
2094 int convlen; | |
2095 | |
2096 motion_type = *p++; | |
2097 --len; | |
2098 | |
2099 enc = p; | |
2100 p += STRLEN(p) + 1; | |
2101 len -= p - enc; | |
2102 | |
2103 /* If the encoding of the text is different from 'encoding', attempt | |
2104 * converting it. */ | |
2105 conv.vc_type = CONV_NONE; | |
2106 convert_setup(&conv, enc, p_enc); | |
2107 if (conv.vc_type != CONV_NONE) | |
2108 { | |
2109 convlen = len; /* Need to use an int here. */ | |
2110 tmpbuf = string_convert(&conv, p, &convlen); | |
2111 len = convlen; | |
2112 if (tmpbuf != NULL) | |
2113 p = tmpbuf; | |
2114 convert_setup(&conv, NULL, NULL); | |
2115 } | |
2116 } | |
2117 #endif | |
2118 | |
3346 | 2119 else if (*type == compound_text_atom |
2120 #ifdef FEAT_MBYTE | |
2121 || *type == utf8_atom | |
2122 #endif | |
2123 || ( | |
7 | 2124 #ifdef FEAT_MBYTE |
2125 enc_dbcs != 0 && | |
2126 #endif | |
2127 *type == text_atom)) | |
2128 { | |
2129 XTextProperty text_prop; | |
2130 int n_text = 0; | |
2131 int status; | |
2132 | |
2133 text_prop.value = (unsigned char *)value; | |
2134 text_prop.encoding = *type; | |
2135 text_prop.format = *format; | |
1719 | 2136 text_prop.nitems = len; |
4272 | 2137 #if defined(FEAT_MBYTE) && defined(X_HAVE_UTF8_STRING) |
4201 | 2138 if (*type == utf8_atom) |
2139 status = Xutf8TextPropertyToTextList(X_DISPLAY, &text_prop, | |
2140 &text_list, &n_text); | |
2141 else | |
2142 #endif | |
2143 status = XmbTextPropertyToTextList(X_DISPLAY, &text_prop, | |
7 | 2144 &text_list, &n_text); |
2145 if (status != Success || n_text < 1) | |
2146 { | |
2147 *(int *)success = FALSE; | |
2148 return; | |
2149 } | |
2150 p = (char_u *)text_list[0]; | |
2151 len = STRLEN(p); | |
2152 } | |
2153 clip_yank_selection(motion_type, p, (long)len, cbd); | |
2154 | |
2155 if (text_list != NULL) | |
2156 XFreeStringList(text_list); | |
2157 #ifdef FEAT_MBYTE | |
2158 vim_free(tmpbuf); | |
2159 #endif | |
2160 XtFree((char *)value); | |
2161 *(int *)success = TRUE; | |
2162 } | |
2163 | |
2164 void | |
2165 clip_x11_request_selection(myShell, dpy, cbd) | |
2166 Widget myShell; | |
2167 Display *dpy; | |
2168 VimClipboard *cbd; | |
2169 { | |
2170 XEvent event; | |
2171 Atom type; | |
2172 static int success; | |
2173 int i; | |
1715 | 2174 time_t start_time; |
2175 int timed_out = FALSE; | |
7 | 2176 |
2177 for (i = | |
2178 #ifdef FEAT_MBYTE | |
2179 0 | |
2180 #else | |
2181 1 | |
2182 #endif | |
3346 | 2183 ; i < 6; i++) |
7 | 2184 { |
2185 switch (i) | |
2186 { | |
2187 #ifdef FEAT_MBYTE | |
2188 case 0: type = vimenc_atom; break; | |
2189 #endif | |
2190 case 1: type = vim_atom; break; | |
3346 | 2191 #ifdef FEAT_MBYTE |
2192 case 2: type = utf8_atom; break; | |
2193 #endif | |
2194 case 3: type = compound_text_atom; break; | |
2195 case 4: type = text_atom; break; | |
7 | 2196 default: type = XA_STRING; |
2197 } | |
3346 | 2198 #ifdef FEAT_MBYTE |
4272 | 2199 if (type == utf8_atom |
2200 # if defined(X_HAVE_UTF8_STRING) | |
2201 && !enc_utf8 | |
2202 # endif | |
2203 ) | |
2204 /* Only request utf-8 when 'encoding' is utf8 and | |
2205 * Xutf8TextPropertyToTextList is available. */ | |
3346 | 2206 continue; |
2207 #endif | |
1719 | 2208 success = MAYBE; |
7 | 2209 XtGetSelectionValue(myShell, cbd->sel_atom, type, |
2210 clip_x11_request_selection_cb, (XtPointer)&success, CurrentTime); | |
2211 | |
2212 /* Make sure the request for the selection goes out before waiting for | |
2213 * a response. */ | |
2214 XFlush(dpy); | |
2215 | |
2216 /* | |
2217 * Wait for result of selection request, otherwise if we type more | |
2218 * characters, then they will appear before the one that requested the | |
2219 * paste! Don't worry, we will catch up with any other events later. | |
2220 */ | |
1715 | 2221 start_time = time(NULL); |
1719 | 2222 while (success == MAYBE) |
7 | 2223 { |
1719 | 2224 if (XCheckTypedEvent(dpy, SelectionNotify, &event) |
2225 || XCheckTypedEvent(dpy, SelectionRequest, &event) | |
2226 || XCheckTypedEvent(dpy, PropertyNotify, &event)) | |
1715 | 2227 { |
1719 | 2228 /* This is where clip_x11_request_selection_cb() should be |
2229 * called. It may actually happen a bit later, so we loop | |
2230 * until "success" changes. | |
2231 * We may get a SelectionRequest here and if we don't handle | |
2232 * it we hang. KDE klipper does this, for example. | |
2233 * We need to handle a PropertyNotify for large selections. */ | |
1715 | 2234 XtDispatchEvent(&event); |
1719 | 2235 continue; |
1715 | 2236 } |
7 | 2237 |
1715 | 2238 /* Time out after 2 to 3 seconds to avoid that we hang when the |
2239 * other process doesn't respond. Note that the SelectionNotify | |
2240 * event may still come later when the selection owner comes back | |
1719 | 2241 * to life and the text gets inserted unexpectedly. Don't know |
2242 * why that happens or how to avoid that :-(. */ | |
1715 | 2243 if (time(NULL) > start_time + 2) |
2244 { | |
2245 timed_out = TRUE; | |
2246 break; | |
2247 } | |
2248 | |
7 | 2249 /* Do we need this? Probably not. */ |
2250 XSync(dpy, False); | |
2251 | |
1715 | 2252 /* Wait for 1 msec to avoid that we eat up all CPU time. */ |
2253 ui_delay(1L, TRUE); | |
7 | 2254 } |
2255 | |
1719 | 2256 if (success == TRUE) |
7 | 2257 return; |
1715 | 2258 |
2259 /* don't do a retry with another type after timing out, otherwise we | |
2260 * hang for 15 seconds. */ | |
2261 if (timed_out) | |
2262 break; | |
7 | 2263 } |
2264 | |
2265 /* Final fallback position - use the X CUT_BUFFER0 store */ | |
1924 | 2266 yank_cut_buffer0(dpy, cbd); |
7 | 2267 } |
2268 | |
2269 static Boolean | |
2270 clip_x11_convert_selection_cb(w, sel_atom, target, type, value, length, format) | |
1883 | 2271 Widget w UNUSED; |
7 | 2272 Atom *sel_atom; |
2273 Atom *target; | |
2274 Atom *type; | |
2275 XtPointer *value; | |
2276 long_u *length; | |
2277 int *format; | |
2278 { | |
2279 char_u *string; | |
2280 char_u *result; | |
2281 int motion_type; | |
2282 VimClipboard *cbd; | |
2283 int i; | |
2284 | |
2285 if (*sel_atom == clip_plus.sel_atom) | |
2286 cbd = &clip_plus; | |
2287 else | |
2288 cbd = &clip_star; | |
2289 | |
2290 if (!cbd->owned) | |
2291 return False; /* Shouldn't ever happen */ | |
2292 | |
2293 /* requestor wants to know what target types we support */ | |
2294 if (*target == targets_atom) | |
2295 { | |
2296 Atom *array; | |
2297 | |
3346 | 2298 if ((array = (Atom *)XtMalloc((unsigned)(sizeof(Atom) * 7))) == NULL) |
7 | 2299 return False; |
2300 *value = (XtPointer)array; | |
2301 i = 0; | |
2302 array[i++] = targets_atom; | |
2303 #ifdef FEAT_MBYTE | |
2304 array[i++] = vimenc_atom; | |
2305 #endif | |
2306 array[i++] = vim_atom; | |
3346 | 2307 #ifdef FEAT_MBYTE |
2308 if (enc_utf8) | |
2309 array[i++] = utf8_atom; | |
2310 #endif | |
2311 array[i++] = XA_STRING; | |
7 | 2312 array[i++] = text_atom; |
2313 array[i++] = compound_text_atom; | |
3346 | 2314 |
7 | 2315 *type = XA_ATOM; |
2316 /* This used to be: *format = sizeof(Atom) * 8; but that caused | |
2317 * crashes on 64 bit machines. (Peter Derr) */ | |
2318 *format = 32; | |
2319 *length = i; | |
2320 return True; | |
2321 } | |
2322 | |
2323 if ( *target != XA_STRING | |
2324 #ifdef FEAT_MBYTE | |
2325 && *target != vimenc_atom | |
3346 | 2326 && *target != utf8_atom |
7 | 2327 #endif |
2328 && *target != vim_atom | |
2329 && *target != text_atom | |
2330 && *target != compound_text_atom) | |
2331 return False; | |
2332 | |
2333 clip_get_selection(cbd); | |
2334 motion_type = clip_convert_selection(&string, length, cbd); | |
2335 if (motion_type < 0) | |
2336 return False; | |
2337 | |
2338 /* For our own format, the first byte contains the motion type */ | |
2339 if (*target == vim_atom) | |
2340 (*length)++; | |
2341 | |
2342 #ifdef FEAT_MBYTE | |
2343 /* Our own format with encoding: motion 'encoding' NUL text */ | |
2344 if (*target == vimenc_atom) | |
2345 *length += STRLEN(p_enc) + 2; | |
2346 #endif | |
2347 | |
2348 *value = XtMalloc((Cardinal)*length); | |
2349 result = (char_u *)*value; | |
2350 if (result == NULL) | |
2351 { | |
2352 vim_free(string); | |
2353 return False; | |
2354 } | |
2355 | |
3346 | 2356 if (*target == XA_STRING |
2357 #ifdef FEAT_MBYTE | |
2358 || (*target == utf8_atom && enc_utf8) | |
2359 #endif | |
2360 ) | |
7 | 2361 { |
2362 mch_memmove(result, string, (size_t)(*length)); | |
3346 | 2363 *type = *target; |
7 | 2364 } |
3346 | 2365 else if (*target == compound_text_atom || *target == text_atom) |
7 | 2366 { |
2367 XTextProperty text_prop; | |
2368 char *string_nt = (char *)alloc((unsigned)*length + 1); | |
5037
5e0b6a9282df
updated for version 7.3.1262
Bram Moolenaar <bram@vim.org>
parents:
4272
diff
changeset
|
2369 int conv_result; |
7 | 2370 |
2371 /* create NUL terminated string which XmbTextListToTextProperty wants */ | |
2372 mch_memmove(string_nt, string, (size_t)*length); | |
2373 string_nt[*length] = NUL; | |
5037
5e0b6a9282df
updated for version 7.3.1262
Bram Moolenaar <bram@vim.org>
parents:
4272
diff
changeset
|
2374 conv_result = XmbTextListToTextProperty(X_DISPLAY, (char **)&string_nt, |
5e0b6a9282df
updated for version 7.3.1262
Bram Moolenaar <bram@vim.org>
parents:
4272
diff
changeset
|
2375 1, XCompoundTextStyle, &text_prop); |
7 | 2376 vim_free(string_nt); |
2377 XtFree(*value); /* replace with COMPOUND text */ | |
5037
5e0b6a9282df
updated for version 7.3.1262
Bram Moolenaar <bram@vim.org>
parents:
4272
diff
changeset
|
2378 if (conv_result != Success) |
5e0b6a9282df
updated for version 7.3.1262
Bram Moolenaar <bram@vim.org>
parents:
4272
diff
changeset
|
2379 { |
5e0b6a9282df
updated for version 7.3.1262
Bram Moolenaar <bram@vim.org>
parents:
4272
diff
changeset
|
2380 vim_free(string); |
5e0b6a9282df
updated for version 7.3.1262
Bram Moolenaar <bram@vim.org>
parents:
4272
diff
changeset
|
2381 return False; |
5e0b6a9282df
updated for version 7.3.1262
Bram Moolenaar <bram@vim.org>
parents:
4272
diff
changeset
|
2382 } |
7 | 2383 *value = (XtPointer)(text_prop.value); /* from plain text */ |
2384 *length = text_prop.nitems; | |
2385 *type = compound_text_atom; | |
2386 } | |
2387 | |
2388 #ifdef FEAT_MBYTE | |
2389 else if (*target == vimenc_atom) | |
2390 { | |
2391 int l = STRLEN(p_enc); | |
2392 | |
2393 result[0] = motion_type; | |
2394 STRCPY(result + 1, p_enc); | |
2395 mch_memmove(result + l + 2, string, (size_t)(*length - l - 2)); | |
2396 *type = vimenc_atom; | |
2397 } | |
2398 #endif | |
2399 | |
2400 else | |
2401 { | |
2402 result[0] = motion_type; | |
2403 mch_memmove(result + 1, string, (size_t)(*length - 1)); | |
2404 *type = vim_atom; | |
2405 } | |
2406 *format = 8; /* 8 bits per char */ | |
2407 vim_free(string); | |
2408 return True; | |
2409 } | |
2410 | |
2411 static void | |
2412 clip_x11_lose_ownership_cb(w, sel_atom) | |
1883 | 2413 Widget w UNUSED; |
7 | 2414 Atom *sel_atom; |
2415 { | |
2416 if (*sel_atom == clip_plus.sel_atom) | |
2417 clip_lose_selection(&clip_plus); | |
2418 else | |
2419 clip_lose_selection(&clip_star); | |
2420 } | |
2421 | |
2422 void | |
2423 clip_x11_lose_selection(myShell, cbd) | |
2586 | 2424 Widget myShell; |
7 | 2425 VimClipboard *cbd; |
2426 { | |
4209 | 2427 XtDisownSelection(myShell, cbd->sel_atom, |
2428 XtLastTimestampProcessed(XtDisplay(myShell))); | |
7 | 2429 } |
2430 | |
2431 int | |
2432 clip_x11_own_selection(myShell, cbd) | |
2586 | 2433 Widget myShell; |
7 | 2434 VimClipboard *cbd; |
2435 { | |
2586 | 2436 /* When using the GUI we have proper timestamps, use the one of the last |
2437 * event. When in the console we don't get events (the terminal gets | |
2438 * them), Get the time by a zero-length append, clip_x11_timestamp_cb will | |
2439 * be called with the current timestamp. */ | |
2440 #ifdef FEAT_GUI | |
2441 if (gui.in_use) | |
2442 { | |
2443 if (XtOwnSelection(myShell, cbd->sel_atom, | |
2444 XtLastTimestampProcessed(XtDisplay(myShell)), | |
2445 clip_x11_convert_selection_cb, clip_x11_lose_ownership_cb, | |
2446 NULL) == False) | |
3312 | 2447 return FAIL; |
2586 | 2448 } |
2449 else | |
2450 #endif | |
2451 { | |
2452 if (!XChangeProperty(XtDisplay(myShell), XtWindow(myShell), | |
2453 cbd->sel_atom, timestamp_atom, 32, PropModeAppend, NULL, 0)) | |
3312 | 2454 return FAIL; |
2586 | 2455 } |
2270
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2456 /* Flush is required in a terminal as nothing else is doing it. */ |
917fff7bc09d
Fixes for time in clipboard request. Also fix ownership. (David Fries)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
2457 XFlush(XtDisplay(myShell)); |
7 | 2458 return OK; |
2459 } | |
2460 | |
2461 /* | |
2462 * Send the current selection to the clipboard. Do nothing for X because we | |
2463 * will fill in the selection only when requested by another app. | |
2464 */ | |
2465 void | |
2466 clip_x11_set_selection(cbd) | |
1883 | 2467 VimClipboard *cbd UNUSED; |
7 | 2468 { |
2469 } | |
4209 | 2470 |
2471 int | |
2472 clip_x11_owner_exists(cbd) | |
2473 VimClipboard *cbd; | |
2474 { | |
2475 return XGetSelectionOwner(X_DISPLAY, cbd->sel_atom) != None; | |
2476 } | |
7 | 2477 #endif |
2478 | |
1924 | 2479 #if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11) \ |
2480 || defined(FEAT_GUI_GTK) || defined(PROTO) | |
2481 /* | |
2482 * Get the contents of the X CUT_BUFFER0 and put it in "cbd". | |
2483 */ | |
2484 void | |
2485 yank_cut_buffer0(dpy, cbd) | |
2486 Display *dpy; | |
2487 VimClipboard *cbd; | |
2488 { | |
2489 int nbytes = 0; | |
2490 char_u *buffer = (char_u *)XFetchBuffer(dpy, &nbytes, 0); | |
2491 | |
2492 if (nbytes > 0) | |
2493 { | |
2494 #ifdef FEAT_MBYTE | |
2495 int done = FALSE; | |
2496 | |
2497 /* CUT_BUFFER0 is supposed to be always latin1. Convert to 'enc' when | |
2498 * using a multi-byte encoding. Conversion between two 8-bit | |
2499 * character sets usually fails and the text might actually be in | |
2500 * 'enc' anyway. */ | |
2501 if (has_mbyte) | |
2502 { | |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
1961
diff
changeset
|
2503 char_u *conv_buf; |
1924 | 2504 vimconv_T vc; |
2505 | |
2506 vc.vc_type = CONV_NONE; | |
2507 if (convert_setup(&vc, (char_u *)"latin1", p_enc) == OK) | |
2508 { | |
2509 conv_buf = string_convert(&vc, buffer, &nbytes); | |
2510 if (conv_buf != NULL) | |
2511 { | |
2512 clip_yank_selection(MCHAR, conv_buf, (long)nbytes, cbd); | |
2513 vim_free(conv_buf); | |
2514 done = TRUE; | |
2515 } | |
2516 convert_setup(&vc, NULL, NULL); | |
2517 } | |
2518 } | |
2519 if (!done) /* use the text without conversion */ | |
2520 #endif | |
2521 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd); | |
2522 XFree((void *)buffer); | |
2523 if (p_verbose > 0) | |
2524 { | |
2525 verbose_enter(); | |
2526 verb_msg((char_u *)_("Used CUT_BUFFER0 instead of empty selection")); | |
2527 verbose_leave(); | |
2528 } | |
2529 } | |
2530 } | |
2531 #endif | |
2532 | |
7 | 2533 #if defined(FEAT_MOUSE) || defined(PROTO) |
2534 | |
2535 /* | |
2536 * Move the cursor to the specified row and column on the screen. | |
1226 | 2537 * Change current window if necessary. Returns an integer with the |
7 | 2538 * CURSOR_MOVED bit set if the cursor has moved or unset otherwise. |
2539 * | |
2540 * The MOUSE_FOLD_CLOSE bit is set when clicked on the '-' in a fold column. | |
2541 * The MOUSE_FOLD_OPEN bit is set when clicked on the '+' in a fold column. | |
2542 * | |
2543 * If flags has MOUSE_FOCUS, then the current window will not be changed, and | |
2544 * if the mouse is outside the window then the text will scroll, or if the | |
2545 * mouse was previously on a status line, then the status line may be dragged. | |
2546 * | |
2547 * If flags has MOUSE_MAY_VIS, then VIsual mode will be started before the | |
2548 * cursor is moved unless the cursor was on a status line. | |
2549 * This function returns one of IN_UNKNOWN, IN_BUFFER, IN_STATUS_LINE or | |
2550 * IN_SEP_LINE depending on where the cursor was clicked. | |
2551 * | |
2552 * If flags has MOUSE_MAY_STOP_VIS, then Visual mode will be stopped, unless | |
2553 * the mouse is on the status line of the same window. | |
2554 * | |
2555 * If flags has MOUSE_DID_MOVE, nothing is done if the mouse didn't move since | |
2556 * the last call. | |
2557 * | |
2558 * If flags has MOUSE_SETPOS, nothing is done, only the current position is | |
2559 * remembered. | |
2560 */ | |
2561 int | |
2562 jump_to_mouse(flags, inclusive, which_button) | |
2563 int flags; | |
2564 int *inclusive; /* used for inclusive operator, can be NULL */ | |
2565 int which_button; /* MOUSE_LEFT, MOUSE_RIGHT, MOUSE_MIDDLE */ | |
2566 { | |
2567 static int on_status_line = 0; /* #lines below bottom of window */ | |
2568 #ifdef FEAT_VERTSPLIT | |
2569 static int on_sep_line = 0; /* on separator right of window */ | |
2570 #endif | |
2571 static int prev_row = -1; | |
2572 static int prev_col = -1; | |
2573 static win_T *dragwin = NULL; /* window being dragged */ | |
2574 static int did_drag = FALSE; /* drag was noticed */ | |
2575 | |
2576 win_T *wp, *old_curwin; | |
2577 pos_T old_cursor; | |
2578 int count; | |
2579 int first; | |
2580 int row = mouse_row; | |
2581 int col = mouse_col; | |
2582 #ifdef FEAT_FOLDING | |
2583 int mouse_char; | |
2584 #endif | |
2585 | |
2586 mouse_past_bottom = FALSE; | |
2587 mouse_past_eol = FALSE; | |
2588 | |
2589 if (flags & MOUSE_RELEASED) | |
2590 { | |
2591 /* On button release we may change window focus if positioned on a | |
2592 * status line and no dragging happened. */ | |
2593 if (dragwin != NULL && !did_drag) | |
2594 flags &= ~(MOUSE_FOCUS | MOUSE_DID_MOVE); | |
2595 dragwin = NULL; | |
2596 did_drag = FALSE; | |
2597 } | |
2598 | |
2599 if ((flags & MOUSE_DID_MOVE) | |
2600 && prev_row == mouse_row | |
2601 && prev_col == mouse_col) | |
2602 { | |
2603 retnomove: | |
1226 | 2604 /* before moving the cursor for a left click which is NOT in a status |
7 | 2605 * line, stop Visual mode */ |
2606 if (on_status_line) | |
2607 return IN_STATUS_LINE; | |
2608 #ifdef FEAT_VERTSPLIT | |
2609 if (on_sep_line) | |
2610 return IN_SEP_LINE; | |
2611 #endif | |
2612 #ifdef FEAT_VISUAL | |
2613 if (flags & MOUSE_MAY_STOP_VIS) | |
2614 { | |
2615 end_visual_mode(); | |
2616 redraw_curbuf_later(INVERTED); /* delete the inversion */ | |
2617 } | |
2618 #endif | |
2619 #if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD) | |
2620 /* Continue a modeless selection in another window. */ | |
2621 if (cmdwin_type != 0 && row < W_WINROW(curwin)) | |
2622 return IN_OTHER_WIN; | |
2623 #endif | |
2624 return IN_BUFFER; | |
2625 } | |
2626 | |
2627 prev_row = mouse_row; | |
2628 prev_col = mouse_col; | |
2629 | |
2630 if (flags & MOUSE_SETPOS) | |
2631 goto retnomove; /* ugly goto... */ | |
2632 | |
2633 #ifdef FEAT_FOLDING | |
2634 /* Remember the character under the mouse, it might be a '-' or '+' in the | |
2635 * fold column. */ | |
534 | 2636 if (row >= 0 && row < Rows && col >= 0 && col <= Columns |
2637 && ScreenLines != NULL) | |
7 | 2638 mouse_char = ScreenLines[LineOffset[row] + col]; |
2639 else | |
2640 mouse_char = ' '; | |
2641 #endif | |
2642 | |
2643 old_curwin = curwin; | |
2644 old_cursor = curwin->w_cursor; | |
2645 | |
2646 if (!(flags & MOUSE_FOCUS)) | |
2647 { | |
2648 if (row < 0 || col < 0) /* check if it makes sense */ | |
2649 return IN_UNKNOWN; | |
2650 | |
2651 #ifdef FEAT_WINDOWS | |
2652 /* find the window where the row is in */ | |
2653 wp = mouse_find_win(&row, &col); | |
2654 #else | |
2655 wp = firstwin; | |
2656 #endif | |
2657 dragwin = NULL; | |
2658 /* | |
2659 * winpos and height may change in win_enter()! | |
2660 */ | |
2661 if (row >= wp->w_height) /* In (or below) status line */ | |
2662 { | |
2663 on_status_line = row - wp->w_height + 1; | |
2664 dragwin = wp; | |
2665 } | |
2666 else | |
2667 on_status_line = 0; | |
2668 #ifdef FEAT_VERTSPLIT | |
2669 if (col >= wp->w_width) /* In separator line */ | |
2670 { | |
2671 on_sep_line = col - wp->w_width + 1; | |
2672 dragwin = wp; | |
2673 } | |
2674 else | |
2675 on_sep_line = 0; | |
2676 | |
2677 /* The rightmost character of the status line might be a vertical | |
2678 * separator character if there is no connecting window to the right. */ | |
2679 if (on_status_line && on_sep_line) | |
2680 { | |
2681 if (stl_connected(wp)) | |
2682 on_sep_line = 0; | |
2683 else | |
2684 on_status_line = 0; | |
2685 } | |
2686 #endif | |
2687 | |
2688 #ifdef FEAT_VISUAL | |
2689 /* Before jumping to another buffer, or moving the cursor for a left | |
2690 * click, stop Visual mode. */ | |
2691 if (VIsual_active | |
2692 && (wp->w_buffer != curwin->w_buffer | |
2693 || (!on_status_line | |
2694 # ifdef FEAT_VERTSPLIT | |
2695 && !on_sep_line | |
2696 # endif | |
2697 # ifdef FEAT_FOLDING | |
2698 && ( | |
2699 # ifdef FEAT_RIGHTLEFT | |
2700 wp->w_p_rl ? col < W_WIDTH(wp) - wp->w_p_fdc : | |
2701 # endif | |
2702 col >= wp->w_p_fdc | |
2703 # ifdef FEAT_CMDWIN | |
2704 + (cmdwin_type == 0 && wp == curwin ? 0 : 1) | |
2705 # endif | |
2706 ) | |
2707 # endif | |
2708 && (flags & MOUSE_MAY_STOP_VIS)))) | |
2709 { | |
2710 end_visual_mode(); | |
2711 redraw_curbuf_later(INVERTED); /* delete the inversion */ | |
2712 } | |
2713 #endif | |
2714 #ifdef FEAT_CMDWIN | |
2715 if (cmdwin_type != 0 && wp != curwin) | |
2716 { | |
2717 /* A click outside the command-line window: Use modeless | |
2102
907cf09fbb32
updated for version 7.2.385
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
2718 * selection if possible. Allow dragging the status lines. */ |
7 | 2719 # ifdef FEAT_VERTSPLIT |
2720 on_sep_line = 0; | |
2721 # endif | |
2722 # ifdef FEAT_CLIPBOARD | |
2723 if (on_status_line) | |
2724 return IN_STATUS_LINE; | |
2725 return IN_OTHER_WIN; | |
2726 # else | |
2727 row = 0; | |
2728 col += wp->w_wincol; | |
2729 wp = curwin; | |
2730 # endif | |
2731 } | |
2732 #endif | |
2733 #ifdef FEAT_WINDOWS | |
2734 /* Only change window focus when not clicking on or dragging the | |
2735 * status line. Do change focus when releasing the mouse button | |
2736 * (MOUSE_FOCUS was set above if we dragged first). */ | |
2737 if (dragwin == NULL || (flags & MOUSE_RELEASED)) | |
2738 win_enter(wp, TRUE); /* can make wp invalid! */ | |
2739 # ifdef CHECK_DOUBLE_CLICK | |
2740 /* set topline, to be able to check for double click ourselves */ | |
2741 if (curwin != old_curwin) | |
2742 set_mouse_topline(curwin); | |
2743 # endif | |
2744 #endif | |
2745 if (on_status_line) /* In (or below) status line */ | |
2746 { | |
2747 /* Don't use start_arrow() if we're in the same window */ | |
2748 if (curwin == old_curwin) | |
2749 return IN_STATUS_LINE; | |
2750 else | |
2751 return IN_STATUS_LINE | CURSOR_MOVED; | |
2752 } | |
2753 #ifdef FEAT_VERTSPLIT | |
2754 if (on_sep_line) /* In (or below) status line */ | |
2755 { | |
2756 /* Don't use start_arrow() if we're in the same window */ | |
2757 if (curwin == old_curwin) | |
2758 return IN_SEP_LINE; | |
2759 else | |
2760 return IN_SEP_LINE | CURSOR_MOVED; | |
2761 } | |
2762 #endif | |
2763 | |
2764 curwin->w_cursor.lnum = curwin->w_topline; | |
2765 #ifdef FEAT_GUI | |
2766 /* remember topline, needed for double click */ | |
2767 gui_prev_topline = curwin->w_topline; | |
2768 # ifdef FEAT_DIFF | |
2769 gui_prev_topfill = curwin->w_topfill; | |
2770 # endif | |
2771 #endif | |
2772 } | |
2773 else if (on_status_line && which_button == MOUSE_LEFT) | |
2774 { | |
2775 #ifdef FEAT_WINDOWS | |
2776 if (dragwin != NULL) | |
2777 { | |
2778 /* Drag the status line */ | |
2779 count = row - dragwin->w_winrow - dragwin->w_height + 1 | |
2780 - on_status_line; | |
2781 win_drag_status_line(dragwin, count); | |
2782 did_drag |= count; | |
2783 } | |
2784 #endif | |
2785 return IN_STATUS_LINE; /* Cursor didn't move */ | |
2786 } | |
2787 #ifdef FEAT_VERTSPLIT | |
2788 else if (on_sep_line && which_button == MOUSE_LEFT) | |
2789 { | |
2790 if (dragwin != NULL) | |
2791 { | |
2792 /* Drag the separator column */ | |
2793 count = col - dragwin->w_wincol - dragwin->w_width + 1 | |
2794 - on_sep_line; | |
2795 win_drag_vsep_line(dragwin, count); | |
2796 did_drag |= count; | |
2797 } | |
2798 return IN_SEP_LINE; /* Cursor didn't move */ | |
2799 } | |
2800 #endif | |
2801 else /* keep_window_focus must be TRUE */ | |
2802 { | |
2803 #ifdef FEAT_VISUAL | |
2804 /* before moving the cursor for a left click, stop Visual mode */ | |
2805 if (flags & MOUSE_MAY_STOP_VIS) | |
2806 { | |
2807 end_visual_mode(); | |
2808 redraw_curbuf_later(INVERTED); /* delete the inversion */ | |
2809 } | |
2810 #endif | |
2811 | |
2812 #if defined(FEAT_CMDWIN) && defined(FEAT_CLIPBOARD) | |
2813 /* Continue a modeless selection in another window. */ | |
2814 if (cmdwin_type != 0 && row < W_WINROW(curwin)) | |
2815 return IN_OTHER_WIN; | |
2816 #endif | |
2817 | |
2818 row -= W_WINROW(curwin); | |
2819 #ifdef FEAT_VERTSPLIT | |
2820 col -= W_WINCOL(curwin); | |
2821 #endif | |
2822 | |
2823 /* | |
2824 * When clicking beyond the end of the window, scroll the screen. | |
2825 * Scroll by however many rows outside the window we are. | |
2826 */ | |
2827 if (row < 0) | |
2828 { | |
2829 count = 0; | |
2830 for (first = TRUE; curwin->w_topline > 1; ) | |
2831 { | |
2832 #ifdef FEAT_DIFF | |
2833 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)) | |
2834 ++count; | |
2835 else | |
2836 #endif | |
2837 count += plines(curwin->w_topline - 1); | |
2838 if (!first && count > -row) | |
2839 break; | |
2840 first = FALSE; | |
2841 #ifdef FEAT_FOLDING | |
2842 hasFolding(curwin->w_topline, &curwin->w_topline, NULL); | |
2843 #endif | |
2844 #ifdef FEAT_DIFF | |
2845 if (curwin->w_topfill < diff_check(curwin, curwin->w_topline)) | |
2846 ++curwin->w_topfill; | |
2847 else | |
2848 #endif | |
2849 { | |
2850 --curwin->w_topline; | |
2851 #ifdef FEAT_DIFF | |
2852 curwin->w_topfill = 0; | |
2853 #endif | |
2854 } | |
2855 } | |
2856 #ifdef FEAT_DIFF | |
2857 check_topfill(curwin, FALSE); | |
2858 #endif | |
2859 curwin->w_valid &= | |
2860 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP); | |
2861 redraw_later(VALID); | |
2862 row = 0; | |
2863 } | |
2864 else if (row >= curwin->w_height) | |
2865 { | |
2866 count = 0; | |
2867 for (first = TRUE; curwin->w_topline < curbuf->b_ml.ml_line_count; ) | |
2868 { | |
2869 #ifdef FEAT_DIFF | |
2870 if (curwin->w_topfill > 0) | |
2871 ++count; | |
2872 else | |
2873 #endif | |
2874 count += plines(curwin->w_topline); | |
2875 if (!first && count > row - curwin->w_height + 1) | |
2876 break; | |
2877 first = FALSE; | |
2878 #ifdef FEAT_FOLDING | |
2879 if (hasFolding(curwin->w_topline, NULL, &curwin->w_topline) | |
2880 && curwin->w_topline == curbuf->b_ml.ml_line_count) | |
2881 break; | |
2882 #endif | |
2883 #ifdef FEAT_DIFF | |
2884 if (curwin->w_topfill > 0) | |
2885 --curwin->w_topfill; | |
2886 else | |
2887 #endif | |
2888 { | |
2889 ++curwin->w_topline; | |
2890 #ifdef FEAT_DIFF | |
2891 curwin->w_topfill = | |
2892 diff_check_fill(curwin, curwin->w_topline); | |
2893 #endif | |
2894 } | |
2895 } | |
2896 #ifdef FEAT_DIFF | |
2897 check_topfill(curwin, FALSE); | |
2898 #endif | |
2899 redraw_later(VALID); | |
2900 curwin->w_valid &= | |
2901 ~(VALID_WROW|VALID_CROW|VALID_BOTLINE|VALID_BOTLINE_AP); | |
2902 row = curwin->w_height - 1; | |
2903 } | |
2904 else if (row == 0) | |
2905 { | |
2906 /* When dragging the mouse, while the text has been scrolled up as | |
2907 * far as it goes, moving the mouse in the top line should scroll | |
2908 * the text down (done later when recomputing w_topline). */ | |
1164 | 2909 if (mouse_dragging > 0 |
7 | 2910 && curwin->w_cursor.lnum |
2911 == curwin->w_buffer->b_ml.ml_line_count | |
2912 && curwin->w_cursor.lnum == curwin->w_topline) | |
2913 curwin->w_valid &= ~(VALID_TOPLINE); | |
2914 } | |
2915 } | |
2916 | |
2917 #ifdef FEAT_FOLDING | |
2918 /* Check for position outside of the fold column. */ | |
2919 if ( | |
2920 # ifdef FEAT_RIGHTLEFT | |
2921 curwin->w_p_rl ? col < W_WIDTH(curwin) - curwin->w_p_fdc : | |
2922 # endif | |
2923 col >= curwin->w_p_fdc | |
2924 # ifdef FEAT_CMDWIN | |
2925 + (cmdwin_type == 0 ? 0 : 1) | |
2926 # endif | |
2927 ) | |
2928 mouse_char = ' '; | |
2929 #endif | |
2930 | |
2931 /* compute the position in the buffer line from the posn on the screen */ | |
2932 if (mouse_comp_pos(curwin, &row, &col, &curwin->w_cursor.lnum)) | |
2933 mouse_past_bottom = TRUE; | |
2934 | |
2935 #ifdef FEAT_VISUAL | |
2936 /* Start Visual mode before coladvance(), for when 'sel' != "old" */ | |
2937 if ((flags & MOUSE_MAY_VIS) && !VIsual_active) | |
2938 { | |
2939 check_visual_highlight(); | |
2940 VIsual = old_cursor; | |
2941 VIsual_active = TRUE; | |
2942 VIsual_reselect = TRUE; | |
2943 /* if 'selectmode' contains "mouse", start Select mode */ | |
2944 may_start_select('o'); | |
2945 setmouse(); | |
642 | 2946 if (p_smd && msg_silent == 0) |
7 | 2947 redraw_cmdline = TRUE; /* show visual mode later */ |
2948 } | |
2949 #endif | |
2950 | |
2951 curwin->w_curswant = col; | |
2952 curwin->w_set_curswant = FALSE; /* May still have been TRUE */ | |
2953 if (coladvance(col) == FAIL) /* Mouse click beyond end of line */ | |
2954 { | |
2955 if (inclusive != NULL) | |
2956 *inclusive = TRUE; | |
2957 mouse_past_eol = TRUE; | |
2958 } | |
2959 else if (inclusive != NULL) | |
2960 *inclusive = FALSE; | |
2961 | |
2962 count = IN_BUFFER; | |
2963 if (curwin != old_curwin || curwin->w_cursor.lnum != old_cursor.lnum | |
2964 || curwin->w_cursor.col != old_cursor.col) | |
2965 count |= CURSOR_MOVED; /* Cursor has moved */ | |
2966 | |
2967 #ifdef FEAT_FOLDING | |
2968 if (mouse_char == '+') | |
2969 count |= MOUSE_FOLD_OPEN; | |
2970 else if (mouse_char != ' ') | |
2971 count |= MOUSE_FOLD_CLOSE; | |
2972 #endif | |
2973 | |
2974 return count; | |
2975 } | |
2976 | |
2977 /* | |
2978 * Compute the position in the buffer line from the posn on the screen in | |
2979 * window "win". | |
2980 * Returns TRUE if the position is below the last line. | |
2981 */ | |
2982 int | |
2983 mouse_comp_pos(win, rowp, colp, lnump) | |
2984 win_T *win; | |
2985 int *rowp; | |
2986 int *colp; | |
2987 linenr_T *lnump; | |
2988 { | |
2989 int col = *colp; | |
2990 int row = *rowp; | |
2991 linenr_T lnum; | |
2992 int retval = FALSE; | |
2993 int off; | |
2994 int count; | |
2995 | |
2996 #ifdef FEAT_RIGHTLEFT | |
2997 if (win->w_p_rl) | |
2998 col = W_WIDTH(win) - 1 - col; | |
2999 #endif | |
3000 | |
3001 lnum = win->w_topline; | |
3002 | |
3003 while (row > 0) | |
3004 { | |
3005 #ifdef FEAT_DIFF | |
3006 /* Don't include filler lines in "count" */ | |
237 | 3007 if (win->w_p_diff |
3008 # ifdef FEAT_FOLDING | |
3009 && !hasFoldingWin(win, lnum, NULL, NULL, TRUE, NULL) | |
3010 # endif | |
3011 ) | |
7 | 3012 { |
3013 if (lnum == win->w_topline) | |
3014 row -= win->w_topfill; | |
3015 else | |
3016 row -= diff_check_fill(win, lnum); | |
3017 count = plines_win_nofill(win, lnum, TRUE); | |
3018 } | |
3019 else | |
3020 #endif | |
3021 count = plines_win(win, lnum, TRUE); | |
3022 if (count > row) | |
3023 break; /* Position is in this buffer line. */ | |
3024 #ifdef FEAT_FOLDING | |
3025 (void)hasFoldingWin(win, lnum, NULL, &lnum, TRUE, NULL); | |
3026 #endif | |
3027 if (lnum == win->w_buffer->b_ml.ml_line_count) | |
3028 { | |
3029 retval = TRUE; | |
3030 break; /* past end of file */ | |
3031 } | |
3032 row -= count; | |
3033 ++lnum; | |
3034 } | |
3035 | |
3036 if (!retval) | |
3037 { | |
3038 /* Compute the column without wrapping. */ | |
3039 off = win_col_off(win) - win_col_off2(win); | |
3040 if (col < off) | |
3041 col = off; | |
3042 col += row * (W_WIDTH(win) - off); | |
3043 /* add skip column (for long wrapping line) */ | |
3044 col += win->w_skipcol; | |
3045 } | |
3046 | |
3047 if (!win->w_p_wrap) | |
3048 col += win->w_leftcol; | |
3049 | |
3050 /* skip line number and fold column in front of the line */ | |
3051 col -= win_col_off(win); | |
3052 if (col < 0) | |
3053 { | |
3054 #ifdef FEAT_NETBEANS_INTG | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2270
diff
changeset
|
3055 netbeans_gutter_click(lnum); |
7 | 3056 #endif |
3057 col = 0; | |
3058 } | |
3059 | |
3060 *colp = col; | |
3061 *rowp = row; | |
3062 *lnump = lnum; | |
3063 return retval; | |
3064 } | |
3065 | |
3066 #if defined(FEAT_WINDOWS) || defined(PROTO) | |
3067 /* | |
3068 * Find the window at screen position "*rowp" and "*colp". The positions are | |
3069 * updated to become relative to the top-left of the window. | |
3070 */ | |
3071 win_T * | |
3072 mouse_find_win(rowp, colp) | |
3073 int *rowp; | |
1883 | 3074 int *colp UNUSED; |
7 | 3075 { |
3076 frame_T *fp; | |
3077 | |
3078 fp = topframe; | |
667 | 3079 *rowp -= firstwin->w_winrow; |
7 | 3080 for (;;) |
3081 { | |
3082 if (fp->fr_layout == FR_LEAF) | |
3083 break; | |
3084 #ifdef FEAT_VERTSPLIT | |
3085 if (fp->fr_layout == FR_ROW) | |
3086 { | |
3087 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next) | |
3088 { | |
3089 if (*colp < fp->fr_width) | |
3090 break; | |
3091 *colp -= fp->fr_width; | |
3092 } | |
3093 } | |
3094 #endif | |
3095 else /* fr_layout == FR_COL */ | |
3096 { | |
3097 for (fp = fp->fr_child; fp->fr_next != NULL; fp = fp->fr_next) | |
3098 { | |
3099 if (*rowp < fp->fr_height) | |
3100 break; | |
3101 *rowp -= fp->fr_height; | |
3102 } | |
3103 } | |
3104 } | |
3105 return fp->fr_win; | |
3106 } | |
3107 #endif | |
3108 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3109 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MAC) \ |
7 | 3110 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \ |
3111 || defined(FEAT_GUI_PHOTON) || defined(PROTO) | |
3112 /* | |
3113 * Translate window coordinates to buffer position without any side effects | |
3114 */ | |
3115 int | |
3116 get_fpos_of_mouse(mpos) | |
3117 pos_T *mpos; | |
3118 { | |
3119 win_T *wp; | |
3120 int row = mouse_row; | |
3121 int col = mouse_col; | |
3122 | |
3123 if (row < 0 || col < 0) /* check if it makes sense */ | |
3124 return IN_UNKNOWN; | |
3125 | |
3126 #ifdef FEAT_WINDOWS | |
3127 /* find the window where the row is in */ | |
3128 wp = mouse_find_win(&row, &col); | |
3129 #else | |
3130 wp = firstwin; | |
3131 #endif | |
3132 /* | |
3133 * winpos and height may change in win_enter()! | |
3134 */ | |
3135 if (row >= wp->w_height) /* In (or below) status line */ | |
3136 return IN_STATUS_LINE; | |
3137 #ifdef FEAT_VERTSPLIT | |
3138 if (col >= wp->w_width) /* In vertical separator line */ | |
3139 return IN_SEP_LINE; | |
3140 #endif | |
3141 | |
3142 if (wp != curwin) | |
3143 return IN_UNKNOWN; | |
3144 | |
3145 /* compute the position in the buffer line from the posn on the screen */ | |
3146 if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum)) | |
3147 return IN_STATUS_LINE; /* past bottom */ | |
3148 | |
3149 mpos->col = vcol2col(wp, mpos->lnum, col); | |
3150 | |
3151 if (mpos->col > 0) | |
3152 --mpos->col; | |
2445
04dae202d316
Fixes for coverity warnings.
Bram Moolenaar <bram@vim.org>
parents:
2428
diff
changeset
|
3153 #ifdef FEAT_VIRTUALEDIT |
04dae202d316
Fixes for coverity warnings.
Bram Moolenaar <bram@vim.org>
parents:
2428
diff
changeset
|
3154 mpos->coladd = 0; |
04dae202d316
Fixes for coverity warnings.
Bram Moolenaar <bram@vim.org>
parents:
2428
diff
changeset
|
3155 #endif |
7 | 3156 return IN_BUFFER; |
3157 } | |
3158 | |
3159 /* | |
3160 * Convert a virtual (screen) column to a character column. | |
3161 * The first column is one. | |
3162 */ | |
3163 int | |
3164 vcol2col(wp, lnum, vcol) | |
3165 win_T *wp; | |
3166 linenr_T lnum; | |
3167 int vcol; | |
3168 { | |
3169 /* try to advance to the specified column */ | |
3170 int count = 0; | |
3171 char_u *ptr; | |
1961 | 3172 char_u *start; |
7 | 3173 |
1961 | 3174 start = ptr = ml_get_buf(wp->w_buffer, lnum, FALSE); |
3877 | 3175 while (count < vcol && *ptr != NUL) |
7 | 3176 { |
3177 count += win_lbr_chartabsize(wp, ptr, count, NULL); | |
39 | 3178 mb_ptr_adv(ptr); |
7 | 3179 } |
1961 | 3180 return (int)(ptr - start); |
7 | 3181 } |
3182 #endif | |
3183 | |
3184 #endif /* FEAT_MOUSE */ | |
3185 | |
3186 #if defined(FEAT_GUI) || defined(WIN3264) || defined(PROTO) | |
3187 /* | |
3188 * Called when focus changed. Used for the GUI or for systems where this can | |
3189 * be done in the console (Win32). | |
3190 */ | |
3191 void | |
3192 ui_focus_change(in_focus) | |
3193 int in_focus; /* TRUE if focus gained. */ | |
3194 { | |
3195 static time_t last_time = (time_t)0; | |
3196 int need_redraw = FALSE; | |
3197 | |
3198 /* When activated: Check if any file was modified outside of Vim. | |
3199 * Only do this when not done within the last two seconds (could get | |
3200 * several events in a row). */ | |
3201 if (in_focus && last_time + 2 < time(NULL)) | |
3202 { | |
3203 need_redraw = check_timestamps( | |
3204 # ifdef FEAT_GUI | |
3205 gui.in_use | |
3206 # else | |
3207 FALSE | |
3208 # endif | |
3209 ); | |
3210 last_time = time(NULL); | |
3211 } | |
3212 | |
3213 #ifdef FEAT_AUTOCMD | |
3214 /* | |
3215 * Fire the focus gained/lost autocommand. | |
3216 */ | |
3217 need_redraw |= apply_autocmds(in_focus ? EVENT_FOCUSGAINED | |
3218 : EVENT_FOCUSLOST, NULL, NULL, FALSE, curbuf); | |
3219 #endif | |
3220 | |
3221 if (need_redraw) | |
3222 { | |
3223 /* Something was executed, make sure the cursor is put back where it | |
3224 * belongs. */ | |
3225 need_wait_return = FALSE; | |
3226 | |
3227 if (State & CMDLINE) | |
3228 redrawcmdline(); | |
3229 else if (State == HITRETURN || State == SETWSIZE || State == ASKMORE | |
3230 || State == EXTERNCMD || State == CONFIRM || exmode_active) | |
3231 repeat_message(); | |
3232 else if ((State & NORMAL) || (State & INSERT)) | |
3233 { | |
3234 if (must_redraw != 0) | |
3235 update_screen(0); | |
3236 setcursor(); | |
3237 } | |
3238 cursor_on(); /* redrawing may have switched it off */ | |
3239 out_flush(); | |
3240 # ifdef FEAT_GUI | |
3241 if (gui.in_use) | |
3242 { | |
3243 gui_update_cursor(FALSE, TRUE); | |
3244 gui_update_scrollbars(FALSE); | |
3245 } | |
3246 # endif | |
3247 } | |
3248 #ifdef FEAT_TITLE | |
3249 /* File may have been changed from 'readonly' to 'noreadonly' */ | |
3250 if (need_maketitle) | |
3251 maketitle(); | |
3252 #endif | |
3253 } | |
3254 #endif | |
3255 | |
3256 #if defined(USE_IM_CONTROL) || defined(PROTO) | |
3257 /* | |
3258 * Save current Input Method status to specified place. | |
3259 */ | |
3260 void | |
3261 im_save_status(psave) | |
3262 long *psave; | |
3263 { | |
3264 /* Don't save when 'imdisable' is set or "xic" is NULL, IM is always | |
3265 * disabled then (but might start later). | |
3266 * Also don't save when inside a mapping, vgetc_im_active has not been set | |
3267 * then. | |
3268 * And don't save when the keys were stuffed (e.g., for a "." command). | |
3269 * And don't save when the GUI is running but our window doesn't have | |
3270 * input focus (e.g., when a find dialog is open). */ | |
3271 if (!p_imdisable && KeyTyped && !KeyStuffed | |
3272 # ifdef FEAT_XIM | |
3273 && xic != NULL | |
3274 # endif | |
3275 # ifdef FEAT_GUI | |
3276 && (!gui.in_use || gui.in_focus) | |
3277 # endif | |
3278 ) | |
3279 { | |
3280 /* Do save when IM is on, or IM is off and saved status is on. */ | |
3281 if (vgetc_im_active) | |
3282 *psave = B_IMODE_IM; | |
3283 else if (*psave == B_IMODE_IM) | |
3284 *psave = B_IMODE_NONE; | |
3285 } | |
3286 } | |
3287 #endif |