7
|
1 /* vi:set ts=8 sts=4 sw=4:
|
|
2 *
|
|
3 * if_sniff.c Interface between Vim and SNiFF+
|
|
4 *
|
|
5 * $Id$
|
|
6 *
|
|
7 * See README.txt for an overview of the Vim source code.
|
|
8 */
|
|
9
|
|
10 #include "vim.h"
|
|
11
|
|
12 #ifdef WIN32
|
|
13 # include <stdio.h>
|
|
14 # include <fcntl.h>
|
714
|
15 # include "vimio.h"
|
7
|
16 # include <process.h>
|
|
17 # include <string.h>
|
|
18 # include <assert.h>
|
|
19 #else
|
|
20 # ifdef FEAT_GUI_X11
|
|
21 # include "gui_x11.pro"
|
|
22 # endif
|
|
23 # include "os_unixx.h"
|
|
24 #endif
|
|
25
|
|
26 static int sniffemacs_pid;
|
|
27
|
|
28 int fd_from_sniff;
|
|
29 int sniff_connected = 0;
|
|
30 int sniff_request_waiting = 0;
|
|
31 int want_sniff_request = 0;
|
|
32
|
|
33 #define MAX_REQUEST_LEN 512
|
|
34
|
|
35 #define NEED_SYMBOL 2
|
|
36 #define EMPTY_SYMBOL 4
|
|
37 #define NEED_FILE 8
|
|
38 #define SILENT 16
|
|
39 #define DISCONNECT 32
|
|
40 #define CONNECT 64
|
|
41
|
|
42 #define RQ_NONE 0
|
|
43 #define RQ_SIMPLE 1
|
|
44 #define RQ_CONTEXT NEED_FILE + NEED_SYMBOL
|
|
45 #define RQ_SCONTEXT NEED_FILE + NEED_SYMBOL + EMPTY_SYMBOL
|
|
46 #define RQ_NOSYMBOL NEED_FILE
|
|
47 #define RQ_SILENT RQ_NOSYMBOL + SILENT
|
|
48 #define RQ_CONNECT RQ_NONE + CONNECT
|
|
49 #define RQ_DISCONNECT RQ_SIMPLE + DISCONNECT
|
|
50
|
|
51 struct sn_cmd
|
|
52 {
|
|
53 char *cmd_name;
|
|
54 char cmd_code;
|
|
55 char *cmd_msg;
|
|
56 int cmd_type;
|
|
57 };
|
|
58
|
|
59 struct sn_cmd_list
|
|
60 {
|
|
61 struct sn_cmd* sniff_cmd;
|
|
62 struct sn_cmd_list* next_cmd;
|
|
63 };
|
|
64
|
|
65 static struct sn_cmd sniff_cmds[] =
|
|
66 {
|
|
67 { "toggle", 'e', N_("Toggle implementation/definition"),RQ_SCONTEXT },
|
|
68 { "superclass", 's', N_("Show base class of"), RQ_CONTEXT },
|
|
69 { "overridden", 'm', N_("Show overridden member function"),RQ_SCONTEXT },
|
|
70 { "retrieve-file", 'r', N_("Retrieve from file"), RQ_CONTEXT },
|
|
71 { "retrieve-project",'p', N_("Retrieve from project"), RQ_CONTEXT },
|
|
72 { "retrieve-all-projects",
|
|
73 'P', N_("Retrieve from all projects"), RQ_CONTEXT },
|
|
74 { "retrieve-next", 'R', N_("Retrieve"), RQ_CONTEXT },
|
|
75 { "goto-symbol", 'g', N_("Show source of"), RQ_CONTEXT },
|
|
76 { "find-symbol", 'f', N_("Find symbol"), RQ_CONTEXT },
|
|
77 { "browse-class", 'w', N_("Browse class"), RQ_CONTEXT },
|
|
78 { "hierarchy", 't', N_("Show class in hierarchy"), RQ_CONTEXT },
|
|
79 { "restr-hier", 'T', N_("Show class in restricted hierarchy"),RQ_CONTEXT },
|
|
80 { "xref-to", 'x', N_("Xref refers to"), RQ_CONTEXT },
|
|
81 { "xref-by", 'X', N_("Xref referred by"), RQ_CONTEXT },
|
|
82 { "xref-has", 'c', N_("Xref has a"), RQ_CONTEXT },
|
|
83 { "xref-used-by", 'C', N_("Xref used by"), RQ_CONTEXT },
|
|
84 { "show-docu", 'd', N_("Show docu of"), RQ_CONTEXT },
|
|
85 { "gen-docu", 'D', N_("Generate docu for"), RQ_CONTEXT },
|
|
86 { "connect", 'y', NULL, RQ_CONNECT },
|
|
87 { "disconnect", 'q', NULL, RQ_DISCONNECT },
|
|
88 { "font-info", 'z', NULL, RQ_SILENT },
|
|
89 { "update", 'u', NULL, RQ_SILENT },
|
|
90 { NULL, '\0', NULL, 0}
|
|
91 };
|
|
92
|
|
93
|
|
94 static char *SniffEmacs[2] = {"sniffemacs", (char *)NULL}; /* Yes, Emacs! */
|
|
95 static int fd_to_sniff;
|
|
96 static int sniff_will_disconnect = 0;
|
|
97 static char msg_sniff_disconnect[] = N_("Cannot connect to SNiFF+. Check environment (sniffemacs must be found in $PATH).\n");
|
|
98 static char sniff_rq_sep[] = " ";
|
|
99 static struct sn_cmd_list *sniff_cmd_ext = NULL;
|
|
100
|
|
101 /* Initializing vim commands
|
|
102 * executed each time vim connects to Sniff
|
|
103 */
|
|
104 static char *init_cmds[]= {
|
|
105 "augroup sniff",
|
|
106 "autocmd BufWritePost * sniff update",
|
|
107 "autocmd BufReadPost * sniff font-info",
|
|
108 "autocmd VimLeave * sniff disconnect",
|
|
109 "augroup END",
|
|
110
|
|
111 "let g:sniff_connected = 1",
|
|
112
|
|
113 "if ! exists('g:sniff_mappings_sourced')|"
|
|
114 "if ! exists('g:sniff_mappings')|"
|
|
115 "if exists('$SNIFF_DIR4')|"
|
|
116 "let g:sniff_mappings='$SNIFF_DIR4/config/integrations/vim/sniff.vim'|"
|
|
117 "else|"
|
|
118 "let g:sniff_mappings='$SNIFF_DIR/config/sniff.vim'|"
|
|
119 "endif|"
|
|
120 "endif|"
|
|
121 "let g:sniff_mappings=expand(g:sniff_mappings)|"
|
|
122 "if filereadable(g:sniff_mappings)|"
|
|
123 "execute 'source' g:sniff_mappings|"
|
|
124 "let g:sniff_mappings_sourced=1|"
|
|
125 "endif|"
|
|
126 "endif",
|
|
127
|
|
128 NULL
|
|
129 };
|
|
130
|
|
131 /*-------- Function Prototypes ----------------------------------*/
|
|
132
|
|
133 static int ConnectToSniffEmacs __ARGS((void));
|
|
134 static void sniff_connect __ARGS((void));
|
|
135 static void HandleSniffRequest __ARGS((char* buffer));
|
|
136 static int get_request __ARGS((int fd, char *buf, int maxlen));
|
|
137 static void WriteToSniff __ARGS((char *str));
|
|
138 static void SendRequest __ARGS((struct sn_cmd *command, char* symbol));
|
|
139 static void vi_msg __ARGS((char *));
|
|
140 static void vi_error_msg __ARGS((char *));
|
|
141 static char *vi_symbol_under_cursor __ARGS((void));
|
|
142 static void vi_open_file __ARGS((char *));
|
|
143 static char *vi_buffer_name __ARGS((void));
|
|
144 static buf_T *vi_find_buffer __ARGS((char *));
|
|
145 static void vi_exec_cmd __ARGS((char *));
|
|
146 static void vi_set_cursor_pos __ARGS((long char_nr));
|
|
147 static long vi_cursor_pos __ARGS((void));
|
|
148
|
|
149 /* debug trace */
|
|
150 #if 0
|
|
151 static FILE* _tracefile = NULL;
|
|
152 #define SNIFF_TRACE_OPEN(file) if (!_tracefile) _tracefile = fopen(file, "w")
|
|
153 #define SNIFF_TRACE(msg) fprintf(_tracefile, msg); fflush(_tracefile);
|
|
154 #define SNIFF_TRACE1(msg, arg) fprintf(_tracefile, msg,arg); fflush(_tracefile);
|
|
155 #define SNIFF_TRACE_CLOSE fclose(_tracefile); _tracefile=NULL;
|
|
156 #else
|
|
157 #define SNIFF_TRACE_OPEN(file)
|
|
158 #define SNIFF_TRACE(msg)
|
|
159 #define SNIFF_TRACE1(msg, arg)
|
|
160 #define SNIFF_TRACE_CLOSE
|
|
161 #endif
|
|
162
|
|
163 /*-------- Windows Only Declarations -----------------------------*/
|
|
164 #ifdef WIN32
|
|
165
|
|
166 static int sniff_request_processed=1;
|
|
167 static HANDLE sniffemacs_handle=NULL;
|
|
168 static HANDLE readthread_handle=NULL;
|
|
169 static HANDLE handle_to_sniff=NULL;
|
|
170 static HANDLE handle_from_sniff=NULL;
|
|
171
|
|
172 struct sniffBufNode
|
|
173 {
|
|
174 struct sniffBufNode *next;
|
|
175 int bufLen;
|
|
176 char buf[MAX_REQUEST_LEN];
|
|
177 };
|
|
178 static struct sniffBufNode *sniffBufStart=NULL;
|
|
179 static struct sniffBufNode *sniffBufEnd=NULL;
|
|
180 static HANDLE hBufferMutex=NULL;
|
|
181
|
|
182 # ifdef FEAT_GUI_W32
|
|
183 extern HWND s_hwnd; /* gvim's Window handle */
|
|
184 # endif
|
|
185 /*
|
|
186 * some helper functions for Windows port only
|
|
187 */
|
|
188
|
|
189 static HANDLE
|
|
190 ExecuteDetachedProgram(char *szBinary, char *szCmdLine,
|
|
191 HANDLE hStdInput, HANDLE hStdOutput)
|
|
192 {
|
|
193 BOOL bResult;
|
|
194 DWORD nError;
|
|
195 PROCESS_INFORMATION aProcessInformation;
|
|
196 PROCESS_INFORMATION *pProcessInformation= &aProcessInformation;
|
|
197 STARTUPINFO aStartupInfo;
|
|
198 STARTUPINFO *pStartupInfo= &aStartupInfo;
|
|
199 DWORD dwCreationFlags= 0;
|
|
200 char szPath[512];
|
|
201 HINSTANCE hResult;
|
|
202
|
|
203 hResult = FindExecutable(szBinary, ".", szPath);
|
|
204 if ((int)hResult <= 32)
|
|
205 {
|
|
206 /* can't find the exe file */
|
|
207 return NULL;
|
|
208 }
|
|
209
|
|
210 ZeroMemory(pStartupInfo, sizeof(*pStartupInfo));
|
|
211 pStartupInfo->dwFlags= STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
|
|
212 pStartupInfo->hStdInput = hStdInput;
|
|
213 pStartupInfo->hStdOutput = hStdOutput;
|
|
214 pStartupInfo->wShowWindow= SW_HIDE;
|
|
215 pStartupInfo->cb = sizeof(STARTUPINFO);
|
|
216
|
|
217 bResult= CreateProcess(
|
|
218 szPath,
|
|
219 szCmdLine,
|
|
220 NULL, /* security attr for process */
|
|
221 NULL, /* security attr for primary thread */
|
|
222 TRUE, /* DO inherit stdin and stdout */
|
|
223 dwCreationFlags, /* creation flags */
|
|
224 NULL, /* environment */
|
|
225 ".", /* current directory */
|
|
226 pStartupInfo, /* startup info: NULL crashes */
|
|
227 pProcessInformation /* process information: NULL crashes */
|
|
228 );
|
|
229 nError= GetLastError();
|
|
230 if (bResult)
|
|
231 {
|
|
232 CloseHandle(pProcessInformation->hThread);
|
|
233 CloseHandle(hStdInput);
|
|
234 CloseHandle(hStdOutput);
|
|
235 return(pProcessInformation->hProcess);
|
|
236 }
|
|
237 else
|
|
238 return(NULL);
|
|
239 }
|
|
240
|
|
241 /*
|
|
242 * write to the internal Thread / Thread communications buffer.
|
|
243 * Return TRUE if successful, FALSE else.
|
|
244 */
|
|
245 static BOOL
|
|
246 writeToBuffer(char *msg, int len)
|
|
247 {
|
|
248 DWORD dwWaitResult; /* Request ownership of mutex. */
|
|
249 struct sniffBufNode *bn;
|
|
250 int bnSize;
|
|
251
|
|
252 SNIFF_TRACE1("writeToBuffer %d\n", len);
|
|
253 bnSize = sizeof(struct sniffBufNode) - MAX_REQUEST_LEN + len + 1;
|
|
254 if (bnSize < 128) bnSize = 128; /* minimum length to avoid fragmentation */
|
|
255 bn = (struct sniffBufNode *)malloc(bnSize);
|
|
256 if (!bn)
|
|
257 return FALSE;
|
|
258
|
|
259 memcpy(bn->buf, msg, len);
|
|
260 bn->buf[len]='\0'; /* terminate CString for added safety */
|
|
261 bn->next = NULL;
|
|
262 bn->bufLen = len;
|
|
263 /* now, acquire a Mutex for adding the string to our linked list */
|
|
264 dwWaitResult = WaitForSingleObject(
|
|
265 hBufferMutex, /* handle of mutex */
|
|
266 1000L); /* one-second time-out interval */
|
|
267 if (dwWaitResult == WAIT_OBJECT_0)
|
|
268 {
|
|
269 /* The thread got mutex ownership. */
|
|
270 if (sniffBufEnd)
|
|
271 {
|
|
272 sniffBufEnd->next = bn;
|
|
273 sniffBufEnd = bn;
|
|
274 }
|
|
275 else
|
|
276 sniffBufStart = sniffBufEnd = bn;
|
|
277 /* Release ownership of the mutex object. */
|
|
278 if (! ReleaseMutex(hBufferMutex))
|
|
279 {
|
|
280 /* Deal with error. */
|
|
281 }
|
|
282 return TRUE;
|
|
283 }
|
|
284
|
|
285 /* Cannot get mutex ownership due to time-out or mutex object abandoned. */
|
|
286 free(bn);
|
|
287 return FALSE;
|
|
288 }
|
|
289
|
|
290 /*
|
|
291 * read from the internal Thread / Thread communications buffer.
|
|
292 * Return TRUE if successful, FALSE else.
|
|
293 */
|
|
294 static int
|
|
295 ReadFromBuffer(char *buf, int maxlen)
|
|
296 {
|
|
297 DWORD dwWaitResult; /* Request ownership of mutex. */
|
|
298 int theLen;
|
|
299 struct sniffBufNode *bn;
|
|
300
|
|
301 dwWaitResult = WaitForSingleObject(
|
|
302 hBufferMutex, /* handle of mutex */
|
|
303 1000L); /* one-second time-out interval */
|
|
304 if (dwWaitResult == WAIT_OBJECT_0)
|
|
305 {
|
|
306 if (!sniffBufStart)
|
|
307 {
|
|
308 /* all pending Requests Processed */
|
|
309 theLen = 0;
|
|
310 }
|
|
311 else
|
|
312 {
|
|
313 bn = sniffBufStart;
|
|
314 theLen = bn->bufLen;
|
|
315 SNIFF_TRACE1("ReadFromBuffer %d\n", theLen);
|
|
316 if (theLen >= maxlen)
|
|
317 {
|
|
318 /* notify the user of buffer overflow? */
|
|
319 theLen = maxlen-1;
|
|
320 }
|
|
321 memcpy(buf, bn->buf, theLen);
|
|
322 buf[theLen] = '\0';
|
|
323 if (! (sniffBufStart = bn->next))
|
|
324 {
|
|
325 sniffBufEnd = NULL;
|
|
326 sniff_request_processed = 1;
|
|
327 }
|
|
328 free(bn);
|
|
329 }
|
|
330 if (! ReleaseMutex(hBufferMutex))
|
|
331 {
|
|
332 /* Deal with error. */
|
|
333 }
|
|
334 return theLen;
|
|
335 }
|
|
336
|
|
337 /* Cannot get mutex ownership due to time-out or mutex object abandoned. */
|
|
338 return -1;
|
|
339 }
|
|
340
|
|
341 /* on Win32, a separate Thread reads the input pipe. get_request is not needed here. */
|
|
342 static void __cdecl
|
|
343 SniffEmacsReadThread(void *dummy)
|
|
344 {
|
|
345 static char ReadThreadBuffer[MAX_REQUEST_LEN];
|
|
346 int ReadThreadLen=0;
|
|
347 int result=0;
|
|
348 int msgLen=0;
|
|
349 char *msgStart, *msgCur;
|
|
350
|
|
351 SNIFF_TRACE("begin thread\n");
|
|
352 /* Read from the pipe to SniffEmacs */
|
|
353 while (sniff_connected)
|
|
354 {
|
|
355 if (!ReadFile(handle_from_sniff,
|
|
356 ReadThreadBuffer + ReadThreadLen, /* acknowledge rest in buffer */
|
|
357 MAX_REQUEST_LEN - ReadThreadLen,
|
|
358 &result,
|
|
359 NULL))
|
|
360 {
|
|
361 DWORD err = GetLastError();
|
|
362 result = -1;
|
|
363 }
|
|
364
|
|
365 if (result < 0)
|
|
366 {
|
|
367 /* probably sniffemacs died... log the Error? */
|
|
368 sniff_disconnect(1);
|
|
369 }
|
|
370 else if (result > 0)
|
|
371 {
|
|
372 ReadThreadLen += result-1; /* total length of valid chars */
|
|
373 for(msgCur=msgStart=ReadThreadBuffer; ReadThreadLen > 0; msgCur++, ReadThreadLen--)
|
|
374 {
|
|
375 if (*msgCur == '\0' || *msgCur == '\r' || *msgCur == '\n')
|
|
376 {
|
|
377 msgLen = msgCur-msgStart; /* don't add the CR/LF chars */
|
|
378 if (msgLen > 0)
|
|
379 writeToBuffer(msgStart, msgLen);
|
|
380 msgStart = msgCur + 1; /* over-read single CR/LF chars */
|
|
381 }
|
|
382 }
|
|
383
|
|
384 /* move incomplete message to beginning of buffer */
|
|
385 ReadThreadLen = msgCur - msgStart;
|
|
386 if (ReadThreadLen > 0)
|
|
387 mch_memmove(ReadThreadBuffer, msgStart, ReadThreadLen);
|
|
388
|
|
389 if (sniff_request_processed)
|
|
390 {
|
|
391 /* notify others that new data has arrived */
|
|
392 sniff_request_processed = 0;
|
|
393 sniff_request_waiting = 1;
|
|
394 #ifdef FEAT_GUI_W32
|
|
395 PostMessage(s_hwnd, WM_USER, (WPARAM)0, (LPARAM)0);
|
|
396 #endif
|
|
397 }
|
|
398 }
|
|
399 }
|
|
400 SNIFF_TRACE("end thread\n");
|
|
401 }
|
|
402 #endif /* WIN32 */
|
|
403 /*-------- End of Windows Only Declarations ------------------------*/
|
|
404
|
|
405
|
|
406 /* ProcessSniffRequests
|
|
407 * Function that should be called from outside
|
|
408 * to process the waiting sniff requests
|
|
409 */
|
|
410 void
|
|
411 ProcessSniffRequests()
|
|
412 {
|
|
413 static char buf[MAX_REQUEST_LEN];
|
|
414 int len;
|
|
415
|
|
416 while (sniff_connected)
|
|
417 {
|
|
418 #ifdef WIN32
|
|
419 len = ReadFromBuffer(buf, sizeof(buf));
|
|
420 #else
|
|
421 len = get_request(fd_from_sniff, buf, sizeof(buf));
|
|
422 #endif
|
|
423 if (len < 0)
|
|
424 {
|
|
425 vi_error_msg(_("E274: Sniff: Error during read. Disconnected"));
|
|
426 sniff_disconnect(1);
|
|
427 break;
|
|
428 }
|
|
429 else if (len > 0)
|
|
430 HandleSniffRequest( buf );
|
|
431 else
|
|
432 break;
|
|
433 }
|
|
434
|
|
435 if (sniff_will_disconnect) /* Now the last msg has been processed */
|
|
436 sniff_disconnect(1);
|
|
437 }
|
|
438
|
|
439 static struct sn_cmd *
|
|
440 find_sniff_cmd(cmd)
|
|
441 char *cmd;
|
|
442 {
|
|
443 struct sn_cmd *sniff_cmd = NULL;
|
|
444 int i;
|
|
445 for(i=0; sniff_cmds[i].cmd_name; i++)
|
|
446 {
|
|
447 if (!strcmp(cmd, sniff_cmds[i].cmd_name))
|
|
448 {
|
|
449 sniff_cmd = &sniff_cmds[i];
|
|
450 break;
|
|
451 }
|
|
452 }
|
|
453 if (!sniff_cmd)
|
|
454 {
|
|
455 struct sn_cmd_list *list = sniff_cmd_ext;
|
|
456 while(list)
|
|
457 {
|
|
458 if (!strcmp(cmd, list->sniff_cmd->cmd_name))
|
|
459 {
|
|
460 sniff_cmd = list->sniff_cmd;
|
|
461 break;
|
|
462 }
|
|
463 list = list->next_cmd;
|
|
464 }
|
|
465 }
|
|
466 return sniff_cmd;
|
|
467 }
|
|
468
|
|
469 static int
|
|
470 add_sniff_cmd(cmd, def, msg)
|
|
471 char *cmd;
|
|
472 char *def;
|
|
473 char *msg;
|
|
474 {
|
|
475 int rc = 0;
|
|
476 if (def != NULL && def[0] != NUL && find_sniff_cmd(cmd) == NULL)
|
|
477 {
|
|
478 struct sn_cmd_list *list = sniff_cmd_ext;
|
|
479 struct sn_cmd *sniff_cmd = (struct sn_cmd*)malloc(sizeof(struct sn_cmd));
|
|
480 struct sn_cmd_list *cmd_node = (struct sn_cmd_list*)malloc(sizeof(struct sn_cmd_list));
|
|
481 int rq_type = 0;
|
|
482
|
|
483 /* unescape message text */
|
|
484 char *p = msg;
|
|
485 char *end = p+strlen(msg);
|
|
486 while(*p)
|
|
487 {
|
|
488 if (*p == '\\')
|
|
489 mch_memmove(p,p+1,end-p);
|
|
490 p++;
|
|
491 }
|
|
492 SNIFF_TRACE1("request name = %s\n",cmd);
|
|
493 SNIFF_TRACE1("request def = %s\n",def);
|
|
494 SNIFF_TRACE1("request msg = %s\n",msg);
|
|
495
|
|
496 while(list && list->next_cmd)
|
|
497 list = list->next_cmd;
|
|
498 if (!list)
|
|
499 sniff_cmd_ext = cmd_node;
|
|
500 else
|
|
501 list->next_cmd = cmd_node;
|
|
502
|
|
503 sniff_cmd->cmd_name = cmd;
|
|
504 sniff_cmd->cmd_code = def[0];
|
|
505 sniff_cmd->cmd_msg = msg;
|
|
506 switch(def[1])
|
|
507 {
|
|
508 case 'f':
|
|
509 rq_type = RQ_NOSYMBOL;
|
|
510 break;
|
|
511 case 's':
|
|
512 rq_type = RQ_CONTEXT;
|
|
513 break;
|
|
514 case 'S':
|
|
515 rq_type = RQ_SCONTEXT;
|
|
516 break;
|
|
517 default:
|
|
518 rq_type = RQ_SIMPLE;
|
|
519 break;
|
|
520 }
|
|
521 sniff_cmd->cmd_type = rq_type;
|
|
522 cmd_node->sniff_cmd = sniff_cmd;
|
|
523 cmd_node->next_cmd = NULL;
|
|
524 rc = 1;
|
|
525 }
|
|
526 return rc;
|
|
527 }
|
|
528
|
|
529 /* ex_sniff
|
|
530 * Handle ":sniff" command
|
|
531 */
|
|
532 void
|
|
533 ex_sniff(eap)
|
|
534 exarg_T *eap;
|
|
535 {
|
|
536 char_u *arg = eap->arg;
|
|
537 char_u *symbol = NULL;
|
|
538 char_u *cmd = NULL;
|
|
539
|
|
540 SNIFF_TRACE_OPEN("if_sniff.log");
|
|
541 if (ends_excmd(*arg)) /* no request: print available commands */
|
|
542 {
|
|
543 int i;
|
|
544 msg_start();
|
|
545 msg_outtrans_attr((char_u *)"-- SNiFF+ commands --", hl_attr(HLF_T));
|
|
546 for(i=0; sniff_cmds[i].cmd_name; i++)
|
|
547 {
|
|
548 msg_putchar('\n');
|
|
549 msg_outtrans((char_u *)":sniff ");
|
|
550 msg_outtrans((char_u *)sniff_cmds[i].cmd_name);
|
|
551 }
|
|
552 msg_putchar('\n');
|
|
553 msg_outtrans((char_u *)_("SNiFF+ is currently "));
|
|
554 if (!sniff_connected)
|
|
555 msg_outtrans((char_u *)_("not "));
|
|
556 msg_outtrans((char_u *)_("connected"));
|
|
557 msg_end();
|
|
558 }
|
|
559 else /* extract command name and symbol if present */
|
|
560 {
|
|
561 symbol = skiptowhite(arg);
|
|
562 cmd = vim_strnsave(arg, (int)(symbol-arg));
|
|
563 symbol = skipwhite(symbol);
|
|
564 if (ends_excmd(*symbol))
|
|
565 symbol = NULL;
|
|
566 if (!strcmp((char *)cmd, "addcmd"))
|
|
567 {
|
|
568 char_u *def = skiptowhite(symbol);
|
|
569 char_u *name = vim_strnsave(symbol, (int)(def-symbol));
|
|
570 char_u *msg;
|
|
571 def = skipwhite(def);
|
|
572 msg = skiptowhite(def);
|
|
573 def = vim_strnsave(def, (int)(msg-def));
|
|
574 msg = skipwhite(msg);
|
|
575 if (ends_excmd(*msg))
|
|
576 msg = vim_strsave(name);
|
|
577 else
|
|
578 msg = vim_strnsave(msg, (int)(skiptowhite_esc(msg)-msg));
|
|
579 if (!add_sniff_cmd((char*)name, (char*)def, (char*)msg))
|
|
580 {
|
|
581 vim_free(msg);
|
|
582 vim_free(def);
|
|
583 vim_free(name);
|
|
584 }
|
|
585 }
|
|
586 else
|
|
587 {
|
|
588 struct sn_cmd* sniff_cmd = find_sniff_cmd((char*)cmd);
|
|
589 if (sniff_cmd)
|
|
590 SendRequest(sniff_cmd, (char *)symbol);
|
|
591 else
|
|
592 EMSG2(_("E275: Unknown SNiFF+ request: %s"), cmd);
|
|
593 }
|
|
594 vim_free(cmd);
|
|
595 }
|
|
596 }
|
|
597
|
|
598
|
|
599 static void
|
|
600 sniff_connect()
|
|
601 {
|
|
602 if (sniff_connected)
|
|
603 return;
|
|
604 if (ConnectToSniffEmacs())
|
|
605 vi_error_msg(_("E276: Error connecting to SNiFF+"));
|
|
606 else
|
|
607 {
|
|
608 int i;
|
|
609
|
|
610 for (i = 0; init_cmds[i]; i++)
|
|
611 vi_exec_cmd(init_cmds[i]);
|
|
612 }
|
|
613 }
|
|
614
|
|
615 void
|
|
616 sniff_disconnect(immediately)
|
|
617 int immediately;
|
|
618 {
|
|
619 if (!sniff_connected)
|
|
620 return;
|
|
621 if (immediately)
|
|
622 {
|
|
623 vi_exec_cmd("augroup sniff");
|
|
624 vi_exec_cmd("au!");
|
|
625 vi_exec_cmd("augroup END");
|
|
626 vi_exec_cmd("unlet g:sniff_connected");
|
|
627 sniff_connected = 0;
|
|
628 want_sniff_request = 0;
|
|
629 sniff_will_disconnect = 0;
|
|
630 #ifdef FEAT_GUI
|
|
631 if (gui.in_use)
|
|
632 gui_mch_wait_for_chars(0L);
|
|
633 #endif
|
|
634 #ifdef WIN32
|
|
635 while(sniffBufStart != NULL)
|
|
636 {
|
|
637 struct sniffBufNode *node = sniffBufStart;
|
|
638 sniffBufStart = sniffBufStart->next;
|
|
639 free(node);
|
|
640 }
|
|
641 sniffBufStart = sniffBufEnd = NULL;
|
|
642 sniff_request_processed = 1;
|
|
643 CloseHandle(handle_to_sniff);
|
|
644 CloseHandle(handle_from_sniff);
|
|
645 WaitForSingleObject(sniffemacs_handle, 1000L);
|
|
646 CloseHandle(sniffemacs_handle);
|
|
647 sniffemacs_handle = NULL;
|
|
648 WaitForSingleObject(readthread_handle, 1000L);
|
|
649 readthread_handle = NULL;
|
|
650 CloseHandle(hBufferMutex);
|
|
651 hBufferMutex = NULL;
|
|
652 SNIFF_TRACE_CLOSE;
|
|
653 #else
|
|
654 close(fd_to_sniff);
|
|
655 close(fd_from_sniff);
|
|
656 wait(NULL);
|
|
657 #endif
|
|
658 }
|
|
659 else
|
|
660 {
|
|
661 #ifdef WIN32
|
|
662 _sleep(2);
|
|
663 if (!sniff_request_processed)
|
|
664 ProcessSniffRequests();
|
|
665 #else
|
|
666 sleep(2); /* Incoming msg could disturb edit */
|
|
667 #endif
|
|
668 sniff_will_disconnect = 1; /* We expect disconnect msg in 2 secs */
|
|
669 }
|
|
670 }
|
|
671
|
|
672
|
|
673 /* ConnectToSniffEmacs
|
|
674 * Connect to Sniff: returns 1 on error
|
|
675 */
|
|
676 static int
|
|
677 ConnectToSniffEmacs()
|
|
678 {
|
|
679 #ifdef WIN32 /* Windows Version of the Code */
|
|
680 HANDLE ToSniffEmacs[2], FromSniffEmacs[2];
|
|
681 SECURITY_ATTRIBUTES sa;
|
|
682
|
|
683 sa.nLength = sizeof(sa);
|
|
684 sa.lpSecurityDescriptor = NULL;
|
|
685 sa.bInheritHandle = TRUE;
|
|
686
|
|
687 if (! CreatePipe(&ToSniffEmacs[0], &ToSniffEmacs[1], &sa, 0))
|
|
688 return 1;
|
|
689 if (! CreatePipe(&FromSniffEmacs[0], &FromSniffEmacs[1], &sa, 0))
|
|
690 return 1;
|
|
691
|
|
692 sniffemacs_handle = ExecuteDetachedProgram(SniffEmacs[0], SniffEmacs[0],
|
|
693 ToSniffEmacs[0], FromSniffEmacs[1]);
|
|
694
|
|
695 if (sniffemacs_handle)
|
|
696 {
|
|
697 handle_to_sniff = ToSniffEmacs[1];
|
|
698 handle_from_sniff = FromSniffEmacs[0];
|
|
699 sniff_connected = 1;
|
|
700 hBufferMutex = CreateMutex(
|
|
701 NULL, /* no security attributes */
|
|
702 FALSE, /* initially not owned */
|
|
703 "SniffReadBufferMutex"); /* name of mutex */
|
|
704 if (hBufferMutex == NULL)
|
|
705 {
|
|
706 /* Check for error. */
|
|
707 }
|
|
708 readthread_handle = (HANDLE)_beginthread(SniffEmacsReadThread, 0, NULL);
|
|
709 return 0;
|
|
710 }
|
|
711 else
|
|
712 {
|
|
713 /* error in spawn() */
|
|
714 return 1;
|
|
715 }
|
|
716
|
|
717 #else /* UNIX Version of the Code */
|
|
718 int ToSniffEmacs[2], FromSniffEmacs[2];
|
|
719
|
|
720 pipe(ToSniffEmacs);
|
|
721 pipe(FromSniffEmacs);
|
|
722
|
|
723 /* fork */
|
|
724 if ((sniffemacs_pid=fork()) == 0)
|
|
725 {
|
|
726 /* child */
|
|
727
|
|
728 /* prepare communication pipes */
|
|
729 close(ToSniffEmacs[1]);
|
|
730 close(FromSniffEmacs[0]);
|
|
731
|
|
732 dup2(ToSniffEmacs[0],fileno(stdin)); /* write to ToSniffEmacs[1] */
|
|
733 dup2(FromSniffEmacs[1],fileno(stdout));/* read from FromSniffEmacs[0] */
|
|
734
|
|
735 close(ToSniffEmacs[0]);
|
|
736 close(FromSniffEmacs[1]);
|
|
737
|
|
738 /* start sniffemacs */
|
|
739 execvp (SniffEmacs[0], SniffEmacs);
|
|
740 {
|
|
741 /* FILE *out = fdopen(FromSniffEmacs[1], "w"); */
|
|
742 sleep(1);
|
|
743 fputs(_(msg_sniff_disconnect), stdout);
|
|
744 fflush(stdout);
|
|
745 sleep(3);
|
|
746 #ifdef FEAT_GUI
|
|
747 if (gui.in_use)
|
|
748 gui_exit(1);
|
|
749 #endif
|
|
750 exit(1);
|
|
751 }
|
|
752 return 1;
|
|
753 }
|
|
754 else if (sniffemacs_pid > 0)
|
|
755 {
|
|
756 /* parent process */
|
|
757 close(ToSniffEmacs[0]);
|
|
758 fd_to_sniff = ToSniffEmacs[1];
|
|
759 close(FromSniffEmacs[1]);
|
|
760 fd_from_sniff = FromSniffEmacs[0];
|
|
761 sniff_connected = 1;
|
|
762 return 0;
|
|
763 }
|
|
764 else /* error in fork() */
|
|
765 return 1;
|
|
766 #endif /* UNIX Version of the Code */
|
|
767 }
|
|
768
|
|
769
|
|
770 /* HandleSniffRequest
|
|
771 * Handle one request from SNiFF+
|
|
772 */
|
|
773 static void
|
|
774 HandleSniffRequest(buffer)
|
|
775 char *buffer;
|
|
776 {
|
|
777 char VICommand[MAX_REQUEST_LEN];
|
|
778 char command;
|
|
779 char *arguments;
|
|
780 char *token;
|
|
781 char *argv[3];
|
|
782 int argc = 0;
|
|
783 buf_T *buf;
|
|
784
|
|
785 const char *SetTab = "set tabstop=%d";
|
|
786 const char *SelectBuf = "buf %s";
|
|
787 const char *DeleteBuf = "bd %s";
|
|
788 const char *UnloadBuf = "bun %s";
|
|
789 const char *GotoLine = "%d";
|
|
790
|
|
791 command = buffer[0];
|
|
792 arguments = &buffer[1];
|
|
793 token = strtok(arguments, sniff_rq_sep);
|
|
794 while(argc <3)
|
|
795 {
|
|
796 if (token)
|
|
797 {
|
|
798 argv[argc] = (char*)vim_strsave((char_u *)token);
|
|
799 token = strtok(0, sniff_rq_sep);
|
|
800 }
|
|
801 else
|
|
802 argv[argc] = strdup("");
|
|
803 argc++;
|
|
804 }
|
|
805
|
|
806 switch (command)
|
|
807 {
|
|
808 case 'o' : /* visit file at char pos */
|
|
809 case 'O' : /* visit file at line number */
|
|
810 {
|
|
811 char *file = argv[0];
|
|
812 int position = atoi(argv[1]);
|
|
813
|
|
814 buf = vi_find_buffer(file);
|
|
815 setpcmark(); /* insert current pos in jump list [mark.c]*/
|
|
816 if (!buf)
|
|
817 vi_open_file(file);
|
|
818 else if (buf!=curbuf)
|
|
819 {
|
426
|
820 vim_snprintf(VICommand, sizeof(VICommand),
|
|
821 (char *)SelectBuf, file);
|
7
|
822 vi_exec_cmd(VICommand);
|
|
823 }
|
|
824 if (command == 'o')
|
|
825 vi_set_cursor_pos((long)position);
|
|
826 else
|
|
827 {
|
426
|
828 vim_snprintf(VICommand, sizeof(VICommand),
|
|
829 (char *)GotoLine, (int)position);
|
7
|
830 vi_exec_cmd(VICommand);
|
|
831 }
|
|
832 checkpcmark(); /* [mark.c] */
|
|
833 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32)
|
|
834 if (gui.in_use && !gui.in_focus) /* Raise Vim Window */
|
|
835 {
|
|
836 # ifdef FEAT_GUI_W32
|
|
837 SetForegroundWindow(s_hwnd);
|
|
838 # else
|
|
839 extern Widget vimShell;
|
|
840
|
|
841 XSetInputFocus(gui.dpy, XtWindow(vimShell), RevertToNone,
|
|
842 CurrentTime);
|
|
843 XRaiseWindow(gui.dpy, XtWindow(vimShell));
|
|
844 # endif
|
|
845 }
|
|
846 #endif
|
|
847 break;
|
|
848 }
|
|
849 case 'p' : /* path of file has changed */
|
|
850 /* when changing from shared to private WS (checkout) */
|
|
851 {
|
|
852 char *file = argv[0];
|
|
853 char *new_path = argv[1];
|
|
854
|
|
855 buf = vi_find_buffer(file);
|
|
856 if (buf && !buf->b_changed) /* delete buffer only if not modified */
|
|
857 {
|
426
|
858 vim_snprintf(VICommand, sizeof(VICommand),
|
|
859 (char *)DeleteBuf, file);
|
7
|
860 vi_exec_cmd(VICommand);
|
|
861 }
|
|
862 vi_open_file(new_path);
|
|
863 break;
|
|
864 }
|
|
865 case 'w' : /* writability has changed */
|
|
866 /* Sniff sends request twice,
|
|
867 * but only the last one is the right one */
|
|
868 {
|
|
869 char *file = argv[0];
|
|
870 int writable = atoi(argv[1]);
|
|
871
|
|
872 buf = vi_find_buffer(file);
|
|
873 if (buf)
|
|
874 {
|
|
875 buf->b_p_ro = !writable;
|
|
876 if (buf != curbuf)
|
|
877 {
|
|
878 buf->b_flags |= BF_CHECK_RO + BF_NEVERLOADED;
|
|
879 if (writable && !buf->b_changed)
|
|
880 {
|
426
|
881 vim_snprintf(VICommand, sizeof(VICommand),
|
|
882 (char *)UnloadBuf, file);
|
7
|
883 vi_exec_cmd(VICommand);
|
|
884 }
|
|
885 }
|
|
886 else if (writable && !buf->b_changed)
|
|
887 {
|
|
888 vi_exec_cmd("e");
|
|
889 }
|
|
890 }
|
|
891 break;
|
|
892 }
|
|
893 case 'h' : /* highlight info */
|
|
894 break; /* not implemented */
|
|
895
|
|
896 case 't' : /* Set tab width */
|
|
897 {
|
|
898 int tab_width = atoi(argv[1]);
|
|
899
|
|
900 if (tab_width > 0 && tab_width <= 16)
|
|
901 {
|
426
|
902 vim_snprintf(VICommand, sizeof(VICommand),
|
|
903 (char *)SetTab, tab_width);
|
7
|
904 vi_exec_cmd(VICommand);
|
|
905 }
|
|
906 break;
|
|
907 }
|
|
908 case '|':
|
|
909 {
|
|
910 /* change the request separator */
|
|
911 sniff_rq_sep[0] = arguments[0];
|
|
912 /* echo the request */
|
|
913 WriteToSniff(buffer);
|
|
914 break;
|
|
915 }
|
|
916 case 'A' : /* Warning/Info msg */
|
|
917 vi_msg(arguments);
|
426
|
918 if (!strncmp(arguments, "Disconnected", 12))
|
7
|
919 sniff_disconnect(1); /* unexpected disconnection */
|
|
920 break;
|
|
921 case 'a' : /* Error msg */
|
|
922 vi_error_msg(arguments);
|
426
|
923 if (!strncmp(arguments, "Cannot connect", 14))
|
7
|
924 sniff_disconnect(1);
|
|
925 break;
|
|
926
|
|
927 default :
|
|
928 break;
|
|
929 }
|
|
930 while(argc)
|
|
931 vim_free(argv[--argc]);
|
|
932 }
|
|
933
|
|
934
|
|
935 #ifndef WIN32
|
|
936 /* get_request
|
|
937 * read string from fd up to next newline (excluding the nl),
|
|
938 * returns length of string
|
|
939 * 0 if no data available or no complete line
|
|
940 * <0 on error
|
|
941 */
|
|
942 static int
|
|
943 get_request(fd, buf, maxlen)
|
|
944 int fd;
|
|
945 char *buf;
|
|
946 int maxlen;
|
|
947 {
|
|
948 static char inbuf[1024];
|
|
949 static int pos = 0, bytes = 0;
|
|
950 int len;
|
|
951 #ifdef HAVE_SELECT
|
|
952 struct timeval tval;
|
|
953 fd_set rfds;
|
|
954
|
|
955 FD_ZERO(&rfds);
|
|
956 FD_SET(fd, &rfds);
|
|
957 tval.tv_sec = 0;
|
|
958 tval.tv_usec = 0;
|
|
959 #else
|
|
960 struct pollfd fds;
|
|
961
|
|
962 fds.fd = fd;
|
|
963 fds.events = POLLIN;
|
|
964 #endif
|
|
965
|
|
966 for (len = 0; len < maxlen; len++)
|
|
967 {
|
|
968 if (pos >= bytes) /* end of buffer reached? */
|
|
969 {
|
|
970 #ifdef HAVE_SELECT
|
|
971 if (select(fd + 1, &rfds, NULL, NULL, &tval) > 0)
|
|
972 #else
|
|
973 if (poll(&fds, 1, 0) > 0)
|
|
974 #endif
|
|
975 {
|
|
976 pos = 0;
|
|
977 bytes = read(fd, inbuf, sizeof(inbuf));
|
|
978 if (bytes <= 0)
|
|
979 return bytes;
|
|
980 }
|
|
981 else
|
|
982 {
|
|
983 pos = pos-len;
|
|
984 buf[0] = '\0';
|
|
985 return 0;
|
|
986 }
|
|
987 }
|
|
988 if ((buf[len] = inbuf[pos++]) =='\n')
|
|
989 break;
|
|
990 }
|
|
991 buf[len] = '\0';
|
|
992 return len;
|
|
993 }
|
|
994 #endif /* WIN32 */
|
|
995
|
|
996
|
|
997 static void
|
|
998 SendRequest(command, symbol)
|
|
999 struct sn_cmd *command;
|
|
1000 char *symbol;
|
|
1001 {
|
|
1002 int cmd_type = command->cmd_type;
|
|
1003 static char cmdstr[MAX_REQUEST_LEN];
|
|
1004 static char msgtxt[MAX_REQUEST_LEN];
|
|
1005 char *buffer_name = NULL;
|
|
1006
|
|
1007 if (cmd_type == RQ_CONNECT)
|
|
1008 {
|
|
1009 sniff_connect();
|
|
1010 return;
|
|
1011 }
|
|
1012 if (!sniff_connected && !(cmd_type & SILENT))
|
|
1013 {
|
|
1014 vi_error_msg(_("E278: SNiFF+ not connected"));
|
|
1015 return;
|
|
1016 }
|
|
1017
|
|
1018 if (cmd_type & NEED_FILE)
|
|
1019 {
|
|
1020 if (!curbuf->b_sniff)
|
|
1021 {
|
|
1022 if (!(cmd_type & SILENT))
|
|
1023 vi_error_msg(_("E279: Not a SNiFF+ buffer"));
|
|
1024 return;
|
|
1025 }
|
|
1026 buffer_name = vi_buffer_name();
|
|
1027 if (buffer_name == NULL)
|
|
1028 return;
|
|
1029 if (cmd_type & NEED_SYMBOL)
|
|
1030 {
|
|
1031 if (cmd_type & EMPTY_SYMBOL)
|
|
1032 symbol = " ";
|
|
1033 else if (!symbol && !(symbol = vi_symbol_under_cursor()))
|
|
1034 return; /* error msg already displayed */
|
|
1035 }
|
|
1036
|
|
1037 if (symbol)
|
272
|
1038 vim_snprintf(cmdstr, sizeof(cmdstr), "%c%s%s%ld%s%s\n",
|
7
|
1039 command->cmd_code,
|
|
1040 buffer_name,
|
|
1041 sniff_rq_sep,
|
|
1042 vi_cursor_pos(),
|
|
1043 sniff_rq_sep,
|
|
1044 symbol
|
|
1045 );
|
|
1046 else
|
272
|
1047 vim_snprintf(cmdstr, sizeof(cmdstr), "%c%s\n",
|
|
1048 command->cmd_code, buffer_name);
|
7
|
1049 }
|
|
1050 else /* simple request */
|
|
1051 {
|
|
1052 cmdstr[0] = command->cmd_code;
|
|
1053 cmdstr[1] = '\n';
|
|
1054 cmdstr[2] = '\0';
|
|
1055 }
|
|
1056 if (command->cmd_msg && !(cmd_type & SILENT))
|
|
1057 {
|
|
1058 if ((cmd_type & NEED_SYMBOL) && !(cmd_type & EMPTY_SYMBOL))
|
|
1059 {
|
272
|
1060 vim_snprintf(msgtxt, sizeof(msgtxt), "%s: %s",
|
|
1061 _(command->cmd_msg), symbol);
|
7
|
1062 vi_msg(msgtxt);
|
|
1063 }
|
|
1064 else
|
|
1065 vi_msg(_(command->cmd_msg));
|
|
1066 }
|
|
1067 WriteToSniff(cmdstr);
|
|
1068 if (cmd_type & DISCONNECT)
|
|
1069 sniff_disconnect(0);
|
|
1070 }
|
|
1071
|
|
1072
|
|
1073
|
|
1074 static void
|
|
1075 WriteToSniff(str)
|
|
1076 char *str;
|
|
1077 {
|
|
1078 int bytes;
|
|
1079 #ifdef WIN32
|
|
1080 if (! WriteFile(handle_to_sniff, str, strlen(str), &bytes, NULL))
|
|
1081 {
|
|
1082 DWORD err=GetLastError();
|
|
1083 bytes = -1;
|
|
1084 }
|
|
1085 #else
|
|
1086 bytes = write(fd_to_sniff, str, strlen(str));
|
|
1087 #endif
|
|
1088 if (bytes<0)
|
|
1089 {
|
|
1090 vi_msg(_("Sniff: Error during write. Disconnected"));
|
|
1091 sniff_disconnect(1);
|
|
1092 }
|
|
1093 }
|
|
1094
|
|
1095 /*-------- vim helping functions --------------------------------*/
|
|
1096
|
|
1097 static void
|
|
1098 vi_msg(str)
|
|
1099 char *str;
|
|
1100 {
|
|
1101 if (str != NULL && *str != NUL)
|
|
1102 MSG((char_u *)str);
|
|
1103 }
|
|
1104
|
|
1105 static void
|
|
1106 vi_error_msg(str)
|
|
1107 char *str;
|
|
1108 {
|
|
1109 if (str != NULL && *str != NUL)
|
|
1110 EMSG((char_u *)str);
|
|
1111 }
|
|
1112
|
|
1113 static void
|
|
1114 vi_open_file(fname)
|
|
1115 char *fname;
|
|
1116 {
|
|
1117 ++no_wait_return;
|
|
1118 do_ecmd(0, (char_u *)fname, NULL, NULL, ECMD_ONE, ECMD_HIDE+ECMD_OLDBUF);
|
|
1119 curbuf->b_sniff = TRUE;
|
|
1120 --no_wait_return; /* [ex_docmd.c] */
|
|
1121 }
|
|
1122
|
|
1123 static buf_T *
|
|
1124 vi_find_buffer(fname)
|
|
1125 char *fname;
|
|
1126 { /* derived from buflist_findname() [buffer.c] */
|
|
1127 buf_T *buf;
|
|
1128
|
|
1129 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
|
|
1130 if (buf->b_sfname != NULL && fnamecmp(fname, buf->b_sfname) == 0)
|
|
1131 return (buf);
|
|
1132 return NULL;
|
|
1133 }
|
|
1134
|
|
1135
|
|
1136 static char *
|
|
1137 vi_symbol_under_cursor()
|
|
1138 {
|
|
1139 int len;
|
|
1140 char *symbolp;
|
|
1141 char *p;
|
|
1142 static char sniff_symbol[256];
|
|
1143
|
|
1144 len = find_ident_under_cursor((char_u **)&symbolp, FIND_IDENT);
|
|
1145 /* [normal.c] */
|
|
1146 if (len <= 0)
|
|
1147 return NULL;
|
|
1148 for (p=sniff_symbol; len; len--)
|
|
1149 *p++ = *symbolp++;
|
|
1150 *p = '\0';
|
|
1151 return sniff_symbol;
|
|
1152 }
|
|
1153
|
|
1154
|
|
1155 static char *
|
|
1156 vi_buffer_name()
|
|
1157 {
|
|
1158 return (char *)curbuf->b_sfname;
|
|
1159 }
|
|
1160
|
|
1161 static void
|
|
1162 vi_exec_cmd(vicmd)
|
|
1163 char *vicmd;
|
|
1164 {
|
|
1165 do_cmdline_cmd((char_u *)vicmd); /* [ex_docmd.c] */
|
|
1166 }
|
|
1167
|
|
1168 /*
|
|
1169 * Set cursor on character position
|
|
1170 * derived from cursor_pos_info() [buffer.c]
|
|
1171 */
|
|
1172 static void
|
|
1173 vi_set_cursor_pos(char_pos)
|
|
1174 long char_pos;
|
|
1175 {
|
|
1176 linenr_T lnum;
|
|
1177 long char_count = 1; /* first position = 1 */
|
|
1178 int line_size;
|
|
1179 int eol_size;
|
|
1180
|
|
1181 if (char_pos == 0)
|
|
1182 {
|
|
1183 char_pos = 1;
|
|
1184 }
|
|
1185 if (get_fileformat(curbuf) == EOL_DOS)
|
|
1186 eol_size = 2;
|
|
1187 else
|
|
1188 eol_size = 1;
|
|
1189 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
|
|
1190 {
|
|
1191 line_size = STRLEN(ml_get(lnum)) + eol_size;
|
|
1192 if (char_count+line_size > char_pos) break;
|
|
1193 char_count += line_size;
|
|
1194 }
|
|
1195 curwin->w_cursor.lnum = lnum;
|
|
1196 curwin->w_cursor.col = char_pos - char_count;
|
|
1197 }
|
|
1198
|
|
1199 static long
|
|
1200 vi_cursor_pos()
|
|
1201 {
|
|
1202 linenr_T lnum;
|
|
1203 long char_count=1; /* sniff starts with pos 1 */
|
|
1204 int line_size;
|
|
1205 int eol_size;
|
|
1206
|
|
1207 if (curbuf->b_p_tx)
|
|
1208 eol_size = 2;
|
|
1209 else
|
|
1210 eol_size = 1;
|
|
1211 for (lnum = 1; lnum < curwin->w_cursor.lnum; ++lnum)
|
|
1212 {
|
|
1213 line_size = STRLEN(ml_get(lnum)) + eol_size;
|
|
1214 char_count += line_size;
|
|
1215 }
|
|
1216 return char_count + curwin->w_cursor.col;
|
|
1217 }
|