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