annotate src/filepath.c @ 17978:8f4cc259ed7a v8.1.1985

patch 8.1.1985: code for dealing with paths is spread out Commit: https://github.com/vim/vim/commit/26262f87770d3a1a68b09a70152d75c2e2ae186f Author: Bram Moolenaar <Bram@vim.org> Date: Wed Sep 4 20:59:15 2019 +0200 patch 8.1.1985: code for dealing with paths is spread out Problem: Code for dealing with paths is spread out. Solution: Move path related functions from misc1.c to filepath.c. Remove NO_EXPANDPATH.
author Bram Moolenaar <Bram@vim.org>
date Wed, 04 Sep 2019 21:00:04 +0200
parents 46f95606b9ec
children 5c8906f653f5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4 noet:
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2 *
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4 *
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
8 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
9
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11 * filepath.c: dealing with file names ant paths.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
12 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
14 #include "vim.h"
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
15
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
16 #ifdef MSWIN
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
17 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
18 * Functions for ":8" filename modifier: get 8.3 version of a filename.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
19 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
20
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
21 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
22 * Get the short path (8.3) for the filename in "fnamep".
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
23 * Only works for a valid file name.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
24 * When the path gets longer "fnamep" is changed and the allocated buffer
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
25 * is put in "bufp".
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
26 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
27 * Returns OK on success, FAIL on failure.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
28 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
29 static int
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
30 get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
31 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
32 int l, len;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
33 char_u *newbuf;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
34
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35 len = *fnamelen;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
36 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
37 if (l > len - 1)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
39 // If that doesn't work (not enough space), then save the string
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
40 // and try again with a new buffer big enough.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41 newbuf = vim_strnsave(*fnamep, l);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42 if (newbuf == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43 return FAIL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45 vim_free(*bufp);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
46 *fnamep = *bufp = newbuf;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
47
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
48 // Really should always succeed, as the buffer is big enough.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
49 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
51
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 *fnamelen = l;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 return OK;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
55
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
57 * Get the short path (8.3) for the filename in "fname". The converted
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58 * path is returned in "bufp".
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
59 *
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
60 * Some of the directories specified in "fname" may not exist. This function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
61 * will shorten the existing directories at the beginning of the path and then
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
62 * append the remaining non-existing path.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63 *
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 * fname - Pointer to the filename to shorten. On return, contains the
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
65 * pointer to the shortened pathname
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
66 * bufp - Pointer to an allocated buffer for the filename.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
67 * fnamelen - Length of the filename pointed to by fname
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
68 *
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
69 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
70 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
71 static int
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
72 shortpath_for_invalid_fname(
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
73 char_u **fname,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
74 char_u **bufp,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
75 int *fnamelen)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
76 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
77 char_u *short_fname, *save_fname, *pbuf_unused;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
78 char_u *endp, *save_endp;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
79 char_u ch;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
80 int old_len, len;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
81 int new_len, sfx_len;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
82 int retval = OK;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
83
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
84 // Make a copy
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
85 old_len = *fnamelen;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
86 save_fname = vim_strnsave(*fname, old_len);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
87 pbuf_unused = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
88 short_fname = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
89
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
90 endp = save_fname + old_len - 1; // Find the end of the copy
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
91 save_endp = endp;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
92
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
93 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
94 * Try shortening the supplied path till it succeeds by removing one
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
95 * directory at a time from the tail of the path.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
96 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
97 len = 0;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
98 for (;;)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
99 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
100 // go back one path-separator
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
101 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
102 --endp;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
103 if (endp <= save_fname)
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
104 break; // processed the complete path
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
105
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
106 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
107 * Replace the path separator with a NUL and try to shorten the
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
108 * resulting path.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
109 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
110 ch = *endp;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
111 *endp = 0;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
112 short_fname = save_fname;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
113 len = (int)STRLEN(short_fname) + 1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
114 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
115 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
116 retval = FAIL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
117 goto theend;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
118 }
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
119 *endp = ch; // preserve the string
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
120
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
121 if (len > 0)
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
122 break; // successfully shortened the path
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
123
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
124 // failed to shorten the path. Skip the path separator
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
125 --endp;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
126 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
127
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
128 if (len > 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
129 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
130 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
131 * Succeeded in shortening the path. Now concatenate the shortened
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
132 * path with the remaining path at the tail.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
133 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
134
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
135 /* Compute the length of the new path. */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
136 sfx_len = (int)(save_endp - endp) + 1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
137 new_len = len + sfx_len;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
138
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
139 *fnamelen = new_len;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
140 vim_free(*bufp);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
141 if (new_len > old_len)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
142 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
143 // There is not enough space in the currently allocated string,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
144 // copy it to a buffer big enough.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
145 *fname = *bufp = vim_strnsave(short_fname, new_len);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
146 if (*fname == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
147 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
148 retval = FAIL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
149 goto theend;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
150 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
151 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
152 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
153 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
154 // Transfer short_fname to the main buffer (it's big enough),
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
155 // unless get_short_pathname() did its work in-place.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
156 *fname = *bufp = save_fname;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
157 if (short_fname != save_fname)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
158 vim_strncpy(save_fname, short_fname, len);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
159 save_fname = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
160 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
161
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
162 // concat the not-shortened part of the path
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
163 vim_strncpy(*fname + len, endp, sfx_len);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
164 (*fname)[new_len] = NUL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
165 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
166
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
167 theend:
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
168 vim_free(pbuf_unused);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
169 vim_free(save_fname);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
170
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
171 return retval;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
172 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
173
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
174 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
175 * Get a pathname for a partial path.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176 * Returns OK for success, FAIL for failure.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
177 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
178 static int
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179 shortpath_for_partial(
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
180 char_u **fnamep,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
181 char_u **bufp,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
182 int *fnamelen)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
183 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
184 int sepcount, len, tflen;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
185 char_u *p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
186 char_u *pbuf, *tfname;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
187 int hasTilde;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
188
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
189 // Count up the path separators from the RHS.. so we know which part
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
190 // of the path to return.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
191 sepcount = 0;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
192 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
193 if (vim_ispathsep(*p))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
194 ++sepcount;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
195
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
196 // Need full path first (use expand_env() to remove a "~/")
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
197 hasTilde = (**fnamep == '~');
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
198 if (hasTilde)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
199 pbuf = tfname = expand_env_save(*fnamep);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
200 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
201 pbuf = tfname = FullName_save(*fnamep, FALSE);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
202
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
203 len = tflen = (int)STRLEN(tfname);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
204
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
205 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
206 return FAIL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
207
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
208 if (len == 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
209 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
210 // Don't have a valid filename, so shorten the rest of the
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
211 // path if we can. This CAN give us invalid 8.3 filenames, but
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
212 // there's not a lot of point in guessing what it might be.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
213 len = tflen;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
214 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
215 return FAIL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
216 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
217
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
218 // Count the paths backward to find the beginning of the desired string.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
219 for (p = tfname + len - 1; p >= tfname; --p)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
220 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
221 if (has_mbyte)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
222 p -= mb_head_off(tfname, p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
223 if (vim_ispathsep(*p))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
224 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
225 if (sepcount == 0 || (hasTilde && sepcount == 1))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
226 break;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
227 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
228 sepcount --;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
229 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
230 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
231 if (hasTilde)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
232 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
233 --p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
234 if (p >= tfname)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
235 *p = '~';
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
236 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
237 return FAIL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
238 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
239 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
240 ++p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
241
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
242 // Copy in the string - p indexes into tfname - allocated at pbuf
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
243 vim_free(*bufp);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
244 *fnamelen = (int)STRLEN(p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
245 *bufp = pbuf;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
246 *fnamep = p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
247
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
248 return OK;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
249 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
250 #endif // MSWIN
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
251
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
252 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
253 * Adjust a filename, according to a string of modifiers.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
254 * *fnamep must be NUL terminated when called. When returning, the length is
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
255 * determined by *fnamelen.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
256 * Returns VALID_ flags or -1 for failure.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
257 * When there is an error, *fnamep is set to NULL.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
258 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
259 int
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
260 modify_fname(
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
261 char_u *src, // string with modifiers
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
262 int tilde_file, // "~" is a file name, not $HOME
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
263 int *usedlen, // characters after src that are used
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
264 char_u **fnamep, // file name so far
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
265 char_u **bufp, // buffer for allocated file name or NULL
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
266 int *fnamelen) // length of fnamep
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
267 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
268 int valid = 0;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
269 char_u *tail;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
270 char_u *s, *p, *pbuf;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
271 char_u dirname[MAXPATHL];
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
272 int c;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
273 int has_fullname = 0;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
274 #ifdef MSWIN
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
275 char_u *fname_start = *fnamep;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
276 int has_shortname = 0;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
277 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
278
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
279 repeat:
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
280 // ":p" - full path/file_name
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
281 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
282 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
283 has_fullname = 1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
284
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
285 valid |= VALID_PATH;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
286 *usedlen += 2;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
287
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
288 // Expand "~/path" for all systems and "~user/path" for Unix and VMS
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
289 if ((*fnamep)[0] == '~'
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
290 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
291 && ((*fnamep)[1] == '/'
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
292 # ifdef BACKSLASH_IN_FILENAME
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
293 || (*fnamep)[1] == '\\'
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
294 # endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
295 || (*fnamep)[1] == NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
296 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
297 && !(tilde_file && (*fnamep)[1] == NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
298 )
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
299 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
300 *fnamep = expand_env_save(*fnamep);
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
301 vim_free(*bufp); // free any allocated file name
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
302 *bufp = *fnamep;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
303 if (*fnamep == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
304 return -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
305 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
306
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
307 // When "/." or "/.." is used: force expansion to get rid of it.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
308 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
309 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
310 if (vim_ispathsep(*p)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
311 && p[1] == '.'
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
312 && (p[2] == NUL
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
313 || vim_ispathsep(p[2])
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
314 || (p[2] == '.'
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
315 && (p[3] == NUL || vim_ispathsep(p[3])))))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
316 break;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
317 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
318
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
319 // FullName_save() is slow, don't use it when not needed.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
320 if (*p != NUL || !vim_isAbsName(*fnamep))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
321 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
322 *fnamep = FullName_save(*fnamep, *p != NUL);
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
323 vim_free(*bufp); // free any allocated file name
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
324 *bufp = *fnamep;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
325 if (*fnamep == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
326 return -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
327 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
328
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
329 #ifdef MSWIN
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
330 # if _WIN32_WINNT >= 0x0500
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
331 if (vim_strchr(*fnamep, '~') != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
332 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
333 // Expand 8.3 filename to full path. Needed to make sure the same
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
334 // file does not have two different names.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
335 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
336 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
337 WCHAR buf[_MAX_PATH];
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
338
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
339 if (wfname != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
340 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
341 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
342 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
343 char_u *p = utf16_to_enc(buf, NULL);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
344
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
345 if (p != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
346 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
347 vim_free(*bufp); // free any allocated file name
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
348 *bufp = *fnamep = p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
349 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
350 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
351 vim_free(wfname);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
352 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
353 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
354 # endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
355 #endif
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
356 // Append a path separator to a directory.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
357 if (mch_isdir(*fnamep))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
358 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
359 // Make room for one or two extra characters.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
360 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
361 vim_free(*bufp); // free any allocated file name
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
362 *bufp = *fnamep;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
363 if (*fnamep == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
364 return -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
365 add_pathsep(*fnamep);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
366 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
367 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
368
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
369 // ":." - path relative to the current directory
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
370 // ":~" - path relative to the home directory
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
371 // ":8" - shortname path - postponed till after
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
372 while (src[*usedlen] == ':'
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
373 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
374 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
375 *usedlen += 2;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
376 if (c == '8')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
377 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
378 #ifdef MSWIN
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
379 has_shortname = 1; // Postpone this.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
380 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
381 continue;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
382 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
383 pbuf = NULL;
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
384 // Need full path first (use expand_env() to remove a "~/")
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
385 if (!has_fullname)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
386 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
387 if (c == '.' && **fnamep == '~')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
388 p = pbuf = expand_env_save(*fnamep);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
389 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
390 p = pbuf = FullName_save(*fnamep, FALSE);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
391 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
392 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
393 p = *fnamep;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
394
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
395 has_fullname = 0;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
396
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
397 if (p != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
398 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
399 if (c == '.')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
400 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
401 mch_dirname(dirname, MAXPATHL);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
402 s = shorten_fname(p, dirname);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
403 if (s != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
404 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
405 *fnamep = s;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
406 if (pbuf != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
407 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
408 vim_free(*bufp); // free any allocated file name
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
409 *bufp = pbuf;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
410 pbuf = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
411 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
412 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
413 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
414 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
415 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
416 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
417 // Only replace it when it starts with '~'
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
418 if (*dirname == '~')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
419 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
420 s = vim_strsave(dirname);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
421 if (s != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
422 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
423 *fnamep = s;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
424 vim_free(*bufp);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
425 *bufp = s;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
426 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
427 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
428 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
429 vim_free(pbuf);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
430 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
431 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
432
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
433 tail = gettail(*fnamep);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
434 *fnamelen = (int)STRLEN(*fnamep);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
435
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
436 // ":h" - head, remove "/file_name", can be repeated
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
437 // Don't remove the first "/" or "c:\"
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
438 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
439 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
440 valid |= VALID_HEAD;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
441 *usedlen += 2;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
442 s = get_past_head(*fnamep);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
443 while (tail > s && after_pathsep(s, tail))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
444 MB_PTR_BACK(*fnamep, tail);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
445 *fnamelen = (int)(tail - *fnamep);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
446 #ifdef VMS
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
447 if (*fnamelen > 0)
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
448 *fnamelen += 1; // the path separator is part of the path
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
449 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
450 if (*fnamelen == 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
451 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
452 // Result is empty. Turn it into "." to make ":cd %:h" work.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
453 p = vim_strsave((char_u *)".");
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
454 if (p == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
455 return -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
456 vim_free(*bufp);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
457 *bufp = *fnamep = tail = p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
458 *fnamelen = 1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
459 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
460 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
461 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
462 while (tail > s && !after_pathsep(s, tail))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
463 MB_PTR_BACK(*fnamep, tail);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
464 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
465 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
466
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
467 // ":8" - shortname
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
468 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
469 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
470 *usedlen += 2;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
471 #ifdef MSWIN
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
472 has_shortname = 1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
473 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
474 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
475
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
476 #ifdef MSWIN
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
477 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
478 * Handle ":8" after we have done 'heads' and before we do 'tails'.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
479 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
480 if (has_shortname)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
481 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
482 // Copy the string if it is shortened by :h and when it wasn't copied
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
483 // yet, because we are going to change it in place. Avoids changing
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
484 // the buffer name for "%:8".
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
485 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
486 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
487 p = vim_strnsave(*fnamep, *fnamelen);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
488 if (p == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
489 return -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
490 vim_free(*bufp);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
491 *bufp = *fnamep = p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
492 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
493
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
494 // Split into two implementations - makes it easier. First is where
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
495 // there isn't a full name already, second is where there is.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
496 if (!has_fullname && !vim_isAbsName(*fnamep))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
497 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
498 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
499 return -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
500 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
501 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
502 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
503 int l = *fnamelen;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
504
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
505 // Simple case, already have the full-name.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
506 // Nearly always shorter, so try first time.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
507 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
508 return -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
509
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
510 if (l == 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
511 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
512 // Couldn't find the filename, search the paths.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
513 l = *fnamelen;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
514 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
515 return -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
516 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
517 *fnamelen = l;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
518 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
519 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
520 #endif // MSWIN
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
521
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
522 // ":t" - tail, just the basename
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
523 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
524 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
525 *usedlen += 2;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
526 *fnamelen -= (int)(tail - *fnamep);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
527 *fnamep = tail;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
528 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
529
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
530 // ":e" - extension, can be repeated
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
531 // ":r" - root, without extension, can be repeated
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
532 while (src[*usedlen] == ':'
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
533 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
534 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
535 // find a '.' in the tail:
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
536 // - for second :e: before the current fname
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
537 // - otherwise: The last '.'
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
538 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
539 s = *fnamep - 2;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
540 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
541 s = *fnamep + *fnamelen - 1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
542 for ( ; s > tail; --s)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
543 if (s[0] == '.')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
544 break;
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
545 if (src[*usedlen + 1] == 'e') // :e
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
546 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
547 if (s > tail)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
548 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
549 *fnamelen += (int)(*fnamep - (s + 1));
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
550 *fnamep = s + 1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
551 #ifdef VMS
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
552 // cut version from the extension
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
553 s = *fnamep + *fnamelen - 1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
554 for ( ; s > *fnamep; --s)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
555 if (s[0] == ';')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
556 break;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
557 if (s > *fnamep)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
558 *fnamelen = s - *fnamep;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
559 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
560 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
561 else if (*fnamep <= tail)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
562 *fnamelen = 0;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
563 }
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
564 else // :r
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
565 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
566 if (s > tail) // remove one extension
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
567 *fnamelen = (int)(s - *fnamep);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
568 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
569 *usedlen += 2;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
570 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
571
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
572 // ":s?pat?foo?" - substitute
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
573 // ":gs?pat?foo?" - global substitute
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
574 if (src[*usedlen] == ':'
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
575 && (src[*usedlen + 1] == 's'
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
576 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
577 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
578 char_u *str;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
579 char_u *pat;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
580 char_u *sub;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
581 int sep;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
582 char_u *flags;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
583 int didit = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
584
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
585 flags = (char_u *)"";
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
586 s = src + *usedlen + 2;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
587 if (src[*usedlen + 1] == 'g')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
588 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
589 flags = (char_u *)"g";
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
590 ++s;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
591 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
592
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
593 sep = *s++;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
594 if (sep)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
595 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
596 // find end of pattern
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
597 p = vim_strchr(s, sep);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
598 if (p != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
599 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
600 pat = vim_strnsave(s, (int)(p - s));
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
601 if (pat != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
602 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
603 s = p + 1;
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
604 // find end of substitution
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
605 p = vim_strchr(s, sep);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
606 if (p != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
607 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
608 sub = vim_strnsave(s, (int)(p - s));
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
609 str = vim_strnsave(*fnamep, *fnamelen);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
610 if (sub != NULL && str != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
611 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
612 *usedlen = (int)(p + 1 - src);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
613 s = do_string_sub(str, pat, sub, NULL, flags);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
614 if (s != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
615 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
616 *fnamep = s;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
617 *fnamelen = (int)STRLEN(s);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
618 vim_free(*bufp);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
619 *bufp = s;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
620 didit = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
621 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
622 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
623 vim_free(sub);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
624 vim_free(str);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
625 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
626 vim_free(pat);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
627 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
628 }
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
629 // after using ":s", repeat all the modifiers
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
630 if (didit)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
631 goto repeat;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
632 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
633 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
634
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
635 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
636 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
637 // vim_strsave_shellescape() needs a NUL terminated string.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
638 c = (*fnamep)[*fnamelen];
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
639 if (c != NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
640 (*fnamep)[*fnamelen] = NUL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
641 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
642 if (c != NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
643 (*fnamep)[*fnamelen] = c;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
644 if (p == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
645 return -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
646 vim_free(*bufp);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
647 *bufp = *fnamep = p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
648 *fnamelen = (int)STRLEN(p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
649 *usedlen += 2;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
650 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
651
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
652 return valid;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
653 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
654
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
655 #if defined(FEAT_EVAL) || defined(PROTO)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
656
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
657 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
658 * "chdir(dir)" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
659 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
660 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
661 f_chdir(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
662 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
663 char_u *cwd;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
664 cdscope_T scope = CDSCOPE_GLOBAL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
665
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
666 rettv->v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
667 rettv->vval.v_string = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
668
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
669 if (argvars[0].v_type != VAR_STRING)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
670 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
671
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
672 // Return the current directory
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
673 cwd = alloc(MAXPATHL);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
674 if (cwd != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
675 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
676 if (mch_dirname(cwd, MAXPATHL) != FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
677 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
678 #ifdef BACKSLASH_IN_FILENAME
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
679 slash_adjust(cwd);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
680 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
681 rettv->vval.v_string = vim_strsave(cwd);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
682 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
683 vim_free(cwd);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
684 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
685
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
686 if (curwin->w_localdir != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
687 scope = CDSCOPE_WINDOW;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
688 else if (curtab->tp_localdir != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
689 scope = CDSCOPE_TABPAGE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
690
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
691 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
692 // Directory change failed
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
693 VIM_CLEAR(rettv->vval.v_string);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
694 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
695
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
696 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
697 * "delete()" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
698 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
699 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
700 f_delete(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
701 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
702 char_u nbuf[NUMBUFLEN];
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
703 char_u *name;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
704 char_u *flags;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
705
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
706 rettv->vval.v_number = -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
707 if (check_restricted() || check_secure())
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
708 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
709
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
710 name = tv_get_string(&argvars[0]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
711 if (name == NULL || *name == NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
712 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
713 emsg(_(e_invarg));
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
714 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
715 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
716
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
717 if (argvars[1].v_type != VAR_UNKNOWN)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
718 flags = tv_get_string_buf(&argvars[1], nbuf);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
719 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
720 flags = (char_u *)"";
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
721
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
722 if (*flags == NUL)
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
723 // delete a file
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
724 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
725 else if (STRCMP(flags, "d") == 0)
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
726 // delete an empty directory
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
727 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
728 else if (STRCMP(flags, "rf") == 0)
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
729 // delete a directory recursively
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
730 rettv->vval.v_number = delete_recursive(name);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
731 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
732 semsg(_(e_invexpr2), flags);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
733 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
734
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
735 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
736 * "executable()" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
737 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
738 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
739 f_executable(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
740 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
741 char_u *name = tv_get_string(&argvars[0]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
742
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
743 // Check in $PATH and also check directly if there is a directory name.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
744 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
745 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
746
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
747 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
748 * "exepath()" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
749 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
750 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
751 f_exepath(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
752 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
753 char_u *p = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
754
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
755 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
756 rettv->v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
757 rettv->vval.v_string = p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
758 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
759
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
760 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
761 * "filereadable()" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
762 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
763 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
764 f_filereadable(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
765 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
766 int fd;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
767 char_u *p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
768 int n;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
769
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
770 #ifndef O_NONBLOCK
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
771 # define O_NONBLOCK 0
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
772 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
773 p = tv_get_string(&argvars[0]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
774 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
775 O_RDONLY | O_NONBLOCK, 0)) >= 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
776 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
777 n = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
778 close(fd);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
779 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
780 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
781 n = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
782
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
783 rettv->vval.v_number = n;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
784 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
785
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
786 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
787 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
788 * rights to write into.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
789 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
790 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
791 f_filewritable(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
792 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
793 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
794 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
795
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
796 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
797 findfilendir(
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
798 typval_T *argvars UNUSED,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
799 typval_T *rettv,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
800 int find_what UNUSED)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
801 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
802 #ifdef FEAT_SEARCHPATH
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
803 char_u *fname;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
804 char_u *fresult = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
805 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
806 char_u *p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
807 char_u pathbuf[NUMBUFLEN];
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
808 int count = 1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
809 int first = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
810 int error = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
811 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
812
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
813 rettv->vval.v_string = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
814 rettv->v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
815
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
816 #ifdef FEAT_SEARCHPATH
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
817 fname = tv_get_string(&argvars[0]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
818
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
819 if (argvars[1].v_type != VAR_UNKNOWN)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
820 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
821 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
822 if (p == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
823 error = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
824 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
825 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
826 if (*p != NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
827 path = p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
828
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
829 if (argvars[2].v_type != VAR_UNKNOWN)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
830 count = (int)tv_get_number_chk(&argvars[2], &error);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
831 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
832 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
833
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
834 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
835 error = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
836
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
837 if (*fname != NUL && !error)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
838 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
839 do
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
840 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
841 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
842 vim_free(fresult);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
843 fresult = find_file_in_path_option(first ? fname : NULL,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
844 first ? (int)STRLEN(fname) : 0,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
845 0, first, path,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
846 find_what,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
847 curbuf->b_ffname,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
848 find_what == FINDFILE_DIR
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
849 ? (char_u *)"" : curbuf->b_p_sua);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
850 first = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
851
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
852 if (fresult != NULL && rettv->v_type == VAR_LIST)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
853 list_append_string(rettv->vval.v_list, fresult, -1);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
854
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
855 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
856 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
857
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
858 if (rettv->v_type == VAR_STRING)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
859 rettv->vval.v_string = fresult;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
860 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
861 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
862
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
863 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
864 * "finddir({fname}[, {path}[, {count}]])" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
865 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
866 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
867 f_finddir(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
868 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
869 findfilendir(argvars, rettv, FINDFILE_DIR);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
870 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
871
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
872 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
873 * "findfile({fname}[, {path}[, {count}]])" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
874 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
875 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
876 f_findfile(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
877 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
878 findfilendir(argvars, rettv, FINDFILE_FILE);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
879 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
880
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
881 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
882 * "fnamemodify({fname}, {mods})" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
883 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
884 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
885 f_fnamemodify(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
886 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
887 char_u *fname;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
888 char_u *mods;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
889 int usedlen = 0;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
890 int len;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
891 char_u *fbuf = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
892 char_u buf[NUMBUFLEN];
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
893
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
894 fname = tv_get_string_chk(&argvars[0]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
895 mods = tv_get_string_buf_chk(&argvars[1], buf);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
896 if (fname == NULL || mods == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
897 fname = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
898 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
899 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
900 len = (int)STRLEN(fname);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
901 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
902 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
903
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
904 rettv->v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
905 if (fname == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
906 rettv->vval.v_string = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
907 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
908 rettv->vval.v_string = vim_strnsave(fname, len);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
909 vim_free(fbuf);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
910 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
911
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
912 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
913 * "getcwd()" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
914 *
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
915 * Return the current working directory of a window in a tab page.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
916 * First optional argument 'winnr' is the window number or -1 and the second
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
917 * optional argument 'tabnr' is the tab page number.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
918 *
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
919 * If no arguments are supplied, then return the directory of the current
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
920 * window.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
921 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
922 * the specified window.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
923 * If 'winnr' is 0 then return the directory of the current window.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
924 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
925 * directory of the specified tab page. Otherwise return the directory of the
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
926 * specified window in the specified tab page.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
927 * If the window or the tab page doesn't exist then return NULL.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
928 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
929 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
930 f_getcwd(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
931 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
932 win_T *wp = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
933 tabpage_T *tp = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
934 char_u *cwd;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
935 int global = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
936
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
937 rettv->v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
938 rettv->vval.v_string = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
939
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
940 if (argvars[0].v_type == VAR_NUMBER
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
941 && argvars[0].vval.v_number == -1
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
942 && argvars[1].v_type == VAR_UNKNOWN)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
943 global = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
944 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
945 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
946
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
947 if (wp != NULL && wp->w_localdir != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
948 rettv->vval.v_string = vim_strsave(wp->w_localdir);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
949 else if (tp != NULL && tp->tp_localdir != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
950 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
951 else if (wp != NULL || tp != NULL || global)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
952 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
953 if (globaldir != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
954 rettv->vval.v_string = vim_strsave(globaldir);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
955 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
956 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
957 cwd = alloc(MAXPATHL);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
958 if (cwd != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
959 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
960 if (mch_dirname(cwd, MAXPATHL) != FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
961 rettv->vval.v_string = vim_strsave(cwd);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
962 vim_free(cwd);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
963 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
964 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
965 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
966 #ifdef BACKSLASH_IN_FILENAME
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
967 if (rettv->vval.v_string != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
968 slash_adjust(rettv->vval.v_string);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
969 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
970 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
971
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
972 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
973 * "getfperm({fname})" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
974 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
975 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
976 f_getfperm(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
977 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
978 char_u *fname;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
979 stat_T st;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
980 char_u *perm = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
981 char_u flags[] = "rwx";
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
982 int i;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
983
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
984 fname = tv_get_string(&argvars[0]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
985
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
986 rettv->v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
987 if (mch_stat((char *)fname, &st) >= 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
988 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
989 perm = vim_strsave((char_u *)"---------");
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
990 if (perm != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
991 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
992 for (i = 0; i < 9; i++)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
993 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
994 if (st.st_mode & (1 << (8 - i)))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
995 perm[i] = flags[i % 3];
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
996 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
997 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
998 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
999 rettv->vval.v_string = perm;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1000 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1001
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1002 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1003 * "getfsize({fname})" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1004 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1005 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1006 f_getfsize(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1007 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1008 char_u *fname;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1009 stat_T st;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1010
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1011 fname = tv_get_string(&argvars[0]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1012
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1013 rettv->v_type = VAR_NUMBER;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1014
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1015 if (mch_stat((char *)fname, &st) >= 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1016 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1017 if (mch_isdir(fname))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1018 rettv->vval.v_number = 0;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1019 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1020 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1021 rettv->vval.v_number = (varnumber_T)st.st_size;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1022
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1023 // non-perfect check for overflow
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1024 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1025 rettv->vval.v_number = -2;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1026 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1027 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1028 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1029 rettv->vval.v_number = -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1030 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1031
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1032 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1033 * "getftime({fname})" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1034 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1035 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1036 f_getftime(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1037 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1038 char_u *fname;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1039 stat_T st;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1040
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1041 fname = tv_get_string(&argvars[0]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1042
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1043 if (mch_stat((char *)fname, &st) >= 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1044 rettv->vval.v_number = (varnumber_T)st.st_mtime;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1045 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1046 rettv->vval.v_number = -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1047 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1048
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1049 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1050 * "getftype({fname})" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1051 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1052 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1053 f_getftype(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1054 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1055 char_u *fname;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1056 stat_T st;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1057 char_u *type = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1058 char *t;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1059
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1060 fname = tv_get_string(&argvars[0]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1061
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1062 rettv->v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1063 if (mch_lstat((char *)fname, &st) >= 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1064 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1065 if (S_ISREG(st.st_mode))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1066 t = "file";
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1067 else if (S_ISDIR(st.st_mode))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1068 t = "dir";
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1069 else if (S_ISLNK(st.st_mode))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1070 t = "link";
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1071 else if (S_ISBLK(st.st_mode))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1072 t = "bdev";
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1073 else if (S_ISCHR(st.st_mode))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1074 t = "cdev";
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1075 else if (S_ISFIFO(st.st_mode))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1076 t = "fifo";
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1077 else if (S_ISSOCK(st.st_mode))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1078 t = "socket";
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1079 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1080 t = "other";
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1081 type = vim_strsave((char_u *)t);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1082 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1083 rettv->vval.v_string = type;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1084 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1085
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1086 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1087 * "glob()" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1088 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1089 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1090 f_glob(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1091 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1092 int options = WILD_SILENT|WILD_USE_NL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1093 expand_T xpc;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1094 int error = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1095
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1096 // When the optional second argument is non-zero, don't remove matches
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1097 // for 'wildignore' and don't put matches for 'suffixes' at the end.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1098 rettv->v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1099 if (argvars[1].v_type != VAR_UNKNOWN)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1100 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1101 if (tv_get_number_chk(&argvars[1], &error))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1102 options |= WILD_KEEP_ALL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1103 if (argvars[2].v_type != VAR_UNKNOWN)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1104 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1105 if (tv_get_number_chk(&argvars[2], &error))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1106 rettv_list_set(rettv, NULL);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1107 if (argvars[3].v_type != VAR_UNKNOWN
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1108 && tv_get_number_chk(&argvars[3], &error))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1109 options |= WILD_ALLLINKS;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1110 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1111 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1112 if (!error)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1113 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1114 ExpandInit(&xpc);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1115 xpc.xp_context = EXPAND_FILES;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1116 if (p_wic)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1117 options += WILD_ICASE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1118 if (rettv->v_type == VAR_STRING)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1119 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1120 NULL, options, WILD_ALL);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1121 else if (rettv_list_alloc(rettv) != FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1122 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1123 int i;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1124
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1125 ExpandOne(&xpc, tv_get_string(&argvars[0]),
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1126 NULL, options, WILD_ALL_KEEP);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1127 for (i = 0; i < xpc.xp_numfiles; i++)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1128 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1129
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1130 ExpandCleanup(&xpc);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1131 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1132 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1133 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1134 rettv->vval.v_string = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1135 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1136
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1137 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1138 * "glob2regpat()" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1139 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1140 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1141 f_glob2regpat(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1142 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1143 char_u *pat = tv_get_string_chk(&argvars[0]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1144
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1145 rettv->v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1146 rettv->vval.v_string = (pat == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1147 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1148 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1149
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1150 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1151 * "globpath()" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1152 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1153 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1154 f_globpath(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1155 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1156 int flags = WILD_IGNORE_COMPLETESLASH;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1157 char_u buf1[NUMBUFLEN];
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1158 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1159 int error = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1160 garray_T ga;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1161 int i;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1162
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1163 // When the optional second argument is non-zero, don't remove matches
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1164 // for 'wildignore' and don't put matches for 'suffixes' at the end.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1165 rettv->v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1166 if (argvars[2].v_type != VAR_UNKNOWN)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1167 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1168 if (tv_get_number_chk(&argvars[2], &error))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1169 flags |= WILD_KEEP_ALL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1170 if (argvars[3].v_type != VAR_UNKNOWN)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1171 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1172 if (tv_get_number_chk(&argvars[3], &error))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1173 rettv_list_set(rettv, NULL);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1174 if (argvars[4].v_type != VAR_UNKNOWN
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1175 && tv_get_number_chk(&argvars[4], &error))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1176 flags |= WILD_ALLLINKS;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1177 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1178 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1179 if (file != NULL && !error)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1180 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1181 ga_init2(&ga, (int)sizeof(char_u *), 10);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1182 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1183 if (rettv->v_type == VAR_STRING)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1184 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1185 else if (rettv_list_alloc(rettv) != FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1186 for (i = 0; i < ga.ga_len; ++i)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1187 list_append_string(rettv->vval.v_list,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1188 ((char_u **)(ga.ga_data))[i], -1);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1189 ga_clear_strings(&ga);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1190 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1191 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1192 rettv->vval.v_string = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1193 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1194
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1195 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1196 * "isdirectory()" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1197 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1198 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1199 f_isdirectory(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1200 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1201 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1202 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1203
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1204 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1205 * Evaluate "expr" (= "context") for readdir().
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1206 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1207 static int
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1208 readdir_checkitem(void *context, char_u *name)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1209 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1210 typval_T *expr = (typval_T *)context;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1211 typval_T save_val;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1212 typval_T rettv;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1213 typval_T argv[2];
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1214 int retval = 0;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1215 int error = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1216
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1217 if (expr->v_type == VAR_UNKNOWN)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1218 return 1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1219
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1220 prepare_vimvar(VV_VAL, &save_val);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1221 set_vim_var_string(VV_VAL, name, -1);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1222 argv[0].v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1223 argv[0].vval.v_string = name;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1224
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1225 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1226 goto theend;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1227
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1228 retval = tv_get_number_chk(&rettv, &error);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1229 if (error)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1230 retval = -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1231 clear_tv(&rettv);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1232
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1233 theend:
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1234 set_vim_var_string(VV_VAL, NULL, 0);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1235 restore_vimvar(VV_VAL, &save_val);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1236 return retval;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1237 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1238
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1239 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1240 * Create the directory in which "dir" is located, and higher levels when
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1241 * needed.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1242 * Return OK or FAIL.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1243 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1244 static int
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1245 mkdir_recurse(char_u *dir, int prot)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1246 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1247 char_u *p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1248 char_u *updir;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1249 int r = FAIL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1250
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1251 // Get end of directory name in "dir".
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1252 // We're done when it's "/" or "c:/".
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1253 p = gettail_sep(dir);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1254 if (p <= get_past_head(dir))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1255 return OK;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1256
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1257 // If the directory exists we're done. Otherwise: create it.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1258 updir = vim_strnsave(dir, (int)(p - dir));
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1259 if (updir == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1260 return FAIL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1261 if (mch_isdir(updir))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1262 r = OK;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1263 else if (mkdir_recurse(updir, prot) == OK)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1264 r = vim_mkdir_emsg(updir, prot);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1265 vim_free(updir);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1266 return r;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1267 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1268
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1269 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1270 * "mkdir()" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1271 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1272 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1273 f_mkdir(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1274 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1275 char_u *dir;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1276 char_u buf[NUMBUFLEN];
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1277 int prot = 0755;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1278
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1279 rettv->vval.v_number = FAIL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1280 if (check_restricted() || check_secure())
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1281 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1282
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1283 dir = tv_get_string_buf(&argvars[0], buf);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1284 if (*dir == NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1285 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1286
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1287 if (*gettail(dir) == NUL)
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1288 // remove trailing slashes
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1289 *gettail_sep(dir) = NUL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1290
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1291 if (argvars[1].v_type != VAR_UNKNOWN)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1292 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1293 if (argvars[2].v_type != VAR_UNKNOWN)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1294 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1295 prot = (int)tv_get_number_chk(&argvars[2], NULL);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1296 if (prot == -1)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1297 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1298 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1299 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1300 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1301 if (mch_isdir(dir))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1302 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1303 // With the "p" flag it's OK if the dir already exists.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1304 rettv->vval.v_number = OK;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1305 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1306 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1307 mkdir_recurse(dir, prot);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1308 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1309 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1310 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1311 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1312
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1313 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1314 * "readdir()" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1315 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1316 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1317 f_readdir(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1318 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1319 typval_T *expr;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1320 int ret;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1321 char_u *path;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1322 char_u *p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1323 garray_T ga;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1324 int i;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1325
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1326 if (rettv_list_alloc(rettv) == FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1327 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1328 path = tv_get_string(&argvars[0]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1329 expr = &argvars[1];
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1330
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1331 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1332 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1333 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1334 for (i = 0; i < ga.ga_len; i++)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1335 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1336 p = ((char_u **)ga.ga_data)[i];
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1337 list_append_string(rettv->vval.v_list, p, -1);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1338 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1339 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1340 ga_clear_strings(&ga);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1341 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1342
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1343 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1344 * "readfile()" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1345 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1346 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1347 f_readfile(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1348 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1349 int binary = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1350 int blob = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1351 int failed = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1352 char_u *fname;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1353 FILE *fd;
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1354 char_u buf[(IOSIZE/256)*256]; // rounded to avoid odd + 1
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1355 int io_size = sizeof(buf);
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1356 int readlen; // size of last fread()
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1357 char_u *prev = NULL; // previously read bytes, if any
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1358 long prevlen = 0; // length of data in prev
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1359 long prevsize = 0; // size of prev buffer
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1360 long maxline = MAXLNUM;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1361 long cnt = 0;
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1362 char_u *p; // position in buf
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1363 char_u *start; // start of current line
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1364
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1365 if (argvars[1].v_type != VAR_UNKNOWN)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1366 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1367 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1368 binary = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1369 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1370 blob = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1371
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1372 if (argvars[2].v_type != VAR_UNKNOWN)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1373 maxline = (long)tv_get_number(&argvars[2]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1374 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1375
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1376 if (blob)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1377 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1378 if (rettv_blob_alloc(rettv) == FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1379 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1380 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1381 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1382 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1383 if (rettv_list_alloc(rettv) == FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1384 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1385 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1386
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1387 // Always open the file in binary mode, library functions have a mind of
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1388 // their own about CR-LF conversion.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1389 fname = tv_get_string(&argvars[0]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1390 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1391 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1392 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1393 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1394 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1395
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1396 if (blob)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1397 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1398 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1399 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1400 emsg("cannot read file");
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1401 blob_free(rettv->vval.v_blob);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1402 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1403 fclose(fd);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1404 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1405 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1406
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1407 while (cnt < maxline || maxline < 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1408 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1409 readlen = (int)fread(buf, 1, io_size, fd);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1410
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1411 // This for loop processes what was read, but is also entered at end
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1412 // of file so that either:
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1413 // - an incomplete line gets written
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1414 // - a "binary" file gets an empty line at the end if it ends in a
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1415 // newline.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1416 for (p = buf, start = buf;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1417 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1418 ++p)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1419 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1420 if (*p == '\n' || readlen <= 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1421 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1422 listitem_T *li;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1423 char_u *s = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1424 long_u len = p - start;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1425
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1426 // Finished a line. Remove CRs before NL.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1427 if (readlen > 0 && !binary)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1428 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1429 while (len > 0 && start[len - 1] == '\r')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1430 --len;
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1431 // removal may cross back to the "prev" string
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1432 if (len == 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1433 while (prevlen > 0 && prev[prevlen - 1] == '\r')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1434 --prevlen;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1435 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1436 if (prevlen == 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1437 s = vim_strnsave(start, (int)len);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1438 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1439 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1440 // Change "prev" buffer to be the right size. This way
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1441 // the bytes are only copied once, and very long lines are
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1442 // allocated only once.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1443 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1444 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1445 mch_memmove(s + prevlen, start, len);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1446 s[prevlen + len] = NUL;
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1447 prev = NULL; // the list will own the string
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1448 prevlen = prevsize = 0;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1449 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1450 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1451 if (s == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1452 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1453 do_outofmem_msg((long_u) prevlen + len + 1);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1454 failed = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1455 break;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1456 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1457
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1458 if ((li = listitem_alloc()) == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1459 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1460 vim_free(s);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1461 failed = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1462 break;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1463 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1464 li->li_tv.v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1465 li->li_tv.v_lock = 0;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1466 li->li_tv.vval.v_string = s;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1467 list_append(rettv->vval.v_list, li);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1468
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1469 start = p + 1; // step over newline
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1470 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1471 break;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1472 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1473 else if (*p == NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1474 *p = '\n';
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1475 // Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1476 // when finding the BF and check the previous two bytes.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1477 else if (*p == 0xbf && enc_utf8 && !binary)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1478 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1479 // Find the two bytes before the 0xbf. If p is at buf, or buf
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1480 // + 1, these may be in the "prev" string.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1481 char_u back1 = p >= buf + 1 ? p[-1]
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1482 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1483 char_u back2 = p >= buf + 2 ? p[-2]
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1484 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1485 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1486
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1487 if (back2 == 0xef && back1 == 0xbb)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1488 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1489 char_u *dest = p - 2;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1490
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1491 // Usually a BOM is at the beginning of a file, and so at
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1492 // the beginning of a line; then we can just step over it.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1493 if (start == dest)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1494 start = p + 1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1495 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1496 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1497 // have to shuffle buf to close gap
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1498 int adjust_prevlen = 0;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1499
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1500 if (dest < buf)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1501 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1502 adjust_prevlen = (int)(buf - dest); // must be 1 or 2
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1503 dest = buf;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1504 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1505 if (readlen > p - buf + 1)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1506 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1507 readlen -= 3 - adjust_prevlen;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1508 prevlen -= adjust_prevlen;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1509 p = dest - 1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1510 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1511 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1512 }
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1513 } // for
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1514
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1515 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1516 break;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1517 if (start < p)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1518 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1519 // There's part of a line in buf, store it in "prev".
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1520 if (p - start + prevlen >= prevsize)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1521 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1522 // need bigger "prev" buffer
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1523 char_u *newprev;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1524
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1525 // A common use case is ordinary text files and "prev" gets a
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1526 // fragment of a line, so the first allocation is made
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1527 // small, to avoid repeatedly 'allocing' large and
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1528 // 'reallocing' small.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1529 if (prevsize == 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1530 prevsize = (long)(p - start);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1531 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1532 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1533 long grow50pc = (prevsize * 3) / 2;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1534 long growmin = (long)((p - start) * 2 + prevlen);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1535 prevsize = grow50pc > growmin ? grow50pc : growmin;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1536 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1537 newprev = vim_realloc(prev, prevsize);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1538 if (newprev == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1539 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1540 do_outofmem_msg((long_u)prevsize);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1541 failed = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1542 break;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1543 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1544 prev = newprev;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1545 }
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1546 // Add the line part to end of "prev".
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1547 mch_memmove(prev + prevlen, start, p - start);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1548 prevlen += (long)(p - start);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1549 }
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1550 } // while
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1551
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1552 // For a negative line count use only the lines at the end of the file,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1553 // free the rest.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1554 if (!failed && maxline < 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1555 while (cnt > -maxline)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1556 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1557 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1558 --cnt;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1559 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1560
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1561 if (failed)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1562 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1563 // an empty list is returned on error
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1564 list_free(rettv->vval.v_list);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1565 rettv_list_alloc(rettv);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1566 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1567
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1568 vim_free(prev);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1569 fclose(fd);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1570 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1571
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1572 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1573 * "resolve()" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1574 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1575 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1576 f_resolve(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1577 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1578 char_u *p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1579 #ifdef HAVE_READLINK
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1580 char_u *buf = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1581 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1582
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1583 p = tv_get_string(&argvars[0]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1584 #ifdef FEAT_SHORTCUT
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1585 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1586 char_u *v = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1587
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1588 v = mch_resolve_path(p, TRUE);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1589 if (v != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1590 rettv->vval.v_string = v;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1591 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1592 rettv->vval.v_string = vim_strsave(p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1593 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1594 #else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1595 # ifdef HAVE_READLINK
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1596 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1597 char_u *cpy;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1598 int len;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1599 char_u *remain = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1600 char_u *q;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1601 int is_relative_to_current = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1602 int has_trailing_pathsep = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1603 int limit = 100;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1604
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1605 p = vim_strsave(p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1606
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1607 if (p[0] == '.' && (vim_ispathsep(p[1])
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1608 || (p[1] == '.' && (vim_ispathsep(p[2])))))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1609 is_relative_to_current = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1610
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1611 len = STRLEN(p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1612 if (len > 0 && after_pathsep(p, p + len))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1613 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1614 has_trailing_pathsep = TRUE;
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1615 p[len - 1] = NUL; // the trailing slash breaks readlink()
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1616 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1617
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1618 q = getnextcomp(p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1619 if (*q != NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1620 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1621 // Separate the first path component in "p", and keep the
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1622 // remainder (beginning with the path separator).
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1623 remain = vim_strsave(q - 1);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1624 q[-1] = NUL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1625 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1626
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1627 buf = alloc(MAXPATHL + 1);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1628 if (buf == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1629 goto fail;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1630
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1631 for (;;)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1632 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1633 for (;;)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1634 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1635 len = readlink((char *)p, (char *)buf, MAXPATHL);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1636 if (len <= 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1637 break;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1638 buf[len] = NUL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1639
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1640 if (limit-- == 0)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1641 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1642 vim_free(p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1643 vim_free(remain);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1644 emsg(_("E655: Too many symbolic links (cycle?)"));
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1645 rettv->vval.v_string = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1646 goto fail;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1647 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1648
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1649 // Ensure that the result will have a trailing path separator
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1650 // if the argument has one.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1651 if (remain == NULL && has_trailing_pathsep)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1652 add_pathsep(buf);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1653
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1654 // Separate the first path component in the link value and
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1655 // concatenate the remainders.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1656 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1657 if (*q != NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1658 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1659 if (remain == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1660 remain = vim_strsave(q - 1);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1661 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1662 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1663 cpy = concat_str(q - 1, remain);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1664 if (cpy != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1665 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1666 vim_free(remain);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1667 remain = cpy;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1668 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1669 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1670 q[-1] = NUL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1671 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1672
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1673 q = gettail(p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1674 if (q > p && *q == NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1675 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1676 // Ignore trailing path separator.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1677 q[-1] = NUL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1678 q = gettail(p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1679 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1680 if (q > p && !mch_isFullName(buf))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1681 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1682 // symlink is relative to directory of argument
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1683 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1684 if (cpy != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1685 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1686 STRCPY(cpy, p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1687 STRCPY(gettail(cpy), buf);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1688 vim_free(p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1689 p = cpy;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1690 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1691 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1692 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1693 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1694 vim_free(p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1695 p = vim_strsave(buf);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1696 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1697 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1698
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1699 if (remain == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1700 break;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1701
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1702 // Append the first path component of "remain" to "p".
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1703 q = getnextcomp(remain + 1);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1704 len = q - remain - (*q != NUL);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1705 cpy = vim_strnsave(p, STRLEN(p) + len);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1706 if (cpy != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1707 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1708 STRNCAT(cpy, remain, len);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1709 vim_free(p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1710 p = cpy;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1711 }
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1712 // Shorten "remain".
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1713 if (*q != NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1714 STRMOVE(remain, q - 1);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1715 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1716 VIM_CLEAR(remain);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1717 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1718
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1719 // If the result is a relative path name, make it explicitly relative to
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1720 // the current directory if and only if the argument had this form.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1721 if (!vim_ispathsep(*p))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1722 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1723 if (is_relative_to_current
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1724 && *p != NUL
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1725 && !(p[0] == '.'
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1726 && (p[1] == NUL
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1727 || vim_ispathsep(p[1])
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1728 || (p[1] == '.'
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1729 && (p[2] == NUL
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1730 || vim_ispathsep(p[2]))))))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1731 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1732 // Prepend "./".
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1733 cpy = concat_str((char_u *)"./", p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1734 if (cpy != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1735 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1736 vim_free(p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1737 p = cpy;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1738 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1739 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1740 else if (!is_relative_to_current)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1741 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1742 // Strip leading "./".
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1743 q = p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1744 while (q[0] == '.' && vim_ispathsep(q[1]))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1745 q += 2;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1746 if (q > p)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1747 STRMOVE(p, p + 2);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1748 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1749 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1750
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1751 // Ensure that the result will have no trailing path separator
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1752 // if the argument had none. But keep "/" or "//".
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1753 if (!has_trailing_pathsep)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1754 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1755 q = p + STRLEN(p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1756 if (after_pathsep(p, q))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1757 *gettail_sep(p) = NUL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1758 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1759
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1760 rettv->vval.v_string = p;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1761 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1762 # else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1763 rettv->vval.v_string = vim_strsave(p);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1764 # endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1765 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1766
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1767 simplify_filename(rettv->vval.v_string);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1768
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1769 #ifdef HAVE_READLINK
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1770 fail:
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1771 vim_free(buf);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1772 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1773 rettv->v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1774 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1775
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1776 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1777 * "tempname()" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1778 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1779 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1780 f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1781 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1782 static int x = 'A';
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1783
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1784 rettv->v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1785 rettv->vval.v_string = vim_tempname(x, FALSE);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1786
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1787 // Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1788 // names. Skip 'I' and 'O', they are used for shell redirection.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1789 do
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1790 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1791 if (x == 'Z')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1792 x = '0';
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1793 else if (x == '9')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1794 x = 'A';
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1795 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1796 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1797 #ifdef EBCDIC
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1798 if (x == 'I')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1799 x = 'J';
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1800 else if (x == 'R')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1801 x = 'S';
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1802 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1803 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1804 ++x;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1805 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1806 } while (x == 'I' || x == 'O');
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1807 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1808
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1809 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1810 * "writefile()" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1811 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1812 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1813 f_writefile(typval_T *argvars, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1814 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1815 int binary = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1816 int append = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1817 #ifdef HAVE_FSYNC
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1818 int do_fsync = p_fs;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1819 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1820 char_u *fname;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1821 FILE *fd;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1822 int ret = 0;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1823 listitem_T *li;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1824 list_T *list = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1825 blob_T *blob = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1826
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1827 rettv->vval.v_number = -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1828 if (check_secure())
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1829 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1830
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1831 if (argvars[0].v_type == VAR_LIST)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1832 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1833 list = argvars[0].vval.v_list;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1834 if (list == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1835 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1836 for (li = list->lv_first; li != NULL; li = li->li_next)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1837 if (tv_get_string_chk(&li->li_tv) == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1838 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1839 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1840 else if (argvars[0].v_type == VAR_BLOB)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1841 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1842 blob = argvars[0].vval.v_blob;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1843 if (blob == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1844 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1845 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1846 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1847 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1848 semsg(_(e_invarg2), "writefile()");
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1849 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1850 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1851
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1852 if (argvars[2].v_type != VAR_UNKNOWN)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1853 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1854 char_u *arg2 = tv_get_string_chk(&argvars[2]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1855
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1856 if (arg2 == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1857 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1858 if (vim_strchr(arg2, 'b') != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1859 binary = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1860 if (vim_strchr(arg2, 'a') != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1861 append = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1862 #ifdef HAVE_FSYNC
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1863 if (vim_strchr(arg2, 's') != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1864 do_fsync = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1865 else if (vim_strchr(arg2, 'S') != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1866 do_fsync = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1867 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1868 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1869
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1870 fname = tv_get_string_chk(&argvars[1]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1871 if (fname == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1872 return;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1873
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1874 // Always open the file in binary mode, library functions have a mind of
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1875 // their own about CR-LF conversion.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1876 if (*fname == NUL || (fd = mch_fopen((char *)fname,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1877 append ? APPENDBIN : WRITEBIN)) == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1878 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1879 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1880 ret = -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1881 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1882 else if (blob)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1883 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1884 if (write_blob(fd, blob) == FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1885 ret = -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1886 #ifdef HAVE_FSYNC
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1887 else if (do_fsync)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1888 // Ignore the error, the user wouldn't know what to do about it.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1889 // May happen for a device.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1890 vim_ignored = vim_fsync(fileno(fd));
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1891 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1892 fclose(fd);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1893 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1894 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1895 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1896 if (write_list(fd, list, binary) == FAIL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1897 ret = -1;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1898 #ifdef HAVE_FSYNC
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1899 else if (do_fsync)
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1900 // Ignore the error, the user wouldn't know what to do about it.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1901 // May happen for a device.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1902 vim_ignored = vim_fsync(fileno(fd));
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1903 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1904 fclose(fd);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1905 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1906
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1907 rettv->vval.v_number = ret;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1908 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1909
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1910 #endif // FEAT_EVAL
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1911
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1912 #if defined(FEAT_BROWSE) || defined(PROTO)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1913 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1914 * Generic browse function. Calls gui_mch_browse() when possible.
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1915 * Later this may pop-up a non-GUI file selector (external command?).
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1916 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1917 char_u *
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1918 do_browse(
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1919 int flags, // BROWSE_SAVE and BROWSE_DIR
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1920 char_u *title, // title for the window
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1921 char_u *dflt, // default file name (may include directory)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1922 char_u *ext, // extension added
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1923 char_u *initdir, // initial directory, NULL for current dir or
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1924 // when using path from "dflt"
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1925 char_u *filter, // file name filter
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1926 buf_T *buf) // buffer to read/write for
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1927 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1928 char_u *fname;
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1929 static char_u *last_dir = NULL; // last used directory
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1930 char_u *tofree = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1931 int save_browse = cmdmod.browse;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1932
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1933 // Must turn off browse to avoid that autocommands will get the
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1934 // flag too!
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1935 cmdmod.browse = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1936
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1937 if (title == NULL || *title == NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1938 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1939 if (flags & BROWSE_DIR)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1940 title = (char_u *)_("Select Directory dialog");
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1941 else if (flags & BROWSE_SAVE)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1942 title = (char_u *)_("Save File dialog");
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1943 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1944 title = (char_u *)_("Open File dialog");
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1945 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1946
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1947 // When no directory specified, use default file name, default dir, buffer
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1948 // dir, last dir or current dir
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1949 if ((initdir == NULL || *initdir == NUL) && dflt != NULL && *dflt != NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1950 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1951 if (mch_isdir(dflt)) // default file name is a directory
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1952 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1953 initdir = dflt;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1954 dflt = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1955 }
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1956 else if (gettail(dflt) != dflt) // default file name includes a path
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1957 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1958 tofree = vim_strsave(dflt);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1959 if (tofree != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1960 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1961 initdir = tofree;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1962 *gettail(initdir) = NUL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1963 dflt = gettail(dflt);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1964 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1965 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1966 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1967
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1968 if (initdir == NULL || *initdir == NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1969 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1970 // When 'browsedir' is a directory, use it
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1971 if (STRCMP(p_bsdir, "last") != 0
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1972 && STRCMP(p_bsdir, "buffer") != 0
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1973 && STRCMP(p_bsdir, "current") != 0
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1974 && mch_isdir(p_bsdir))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1975 initdir = p_bsdir;
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1976 // When saving or 'browsedir' is "buffer", use buffer fname
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1977 else if (((flags & BROWSE_SAVE) || *p_bsdir == 'b')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1978 && buf != NULL && buf->b_ffname != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1979 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1980 if (dflt == NULL || *dflt == NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1981 dflt = gettail(curbuf->b_ffname);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1982 tofree = vim_strsave(curbuf->b_ffname);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1983 if (tofree != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1984 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1985 initdir = tofree;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1986 *gettail(initdir) = NUL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1987 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1988 }
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1989 // When 'browsedir' is "last", use dir from last browse
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1990 else if (*p_bsdir == 'l')
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1991 initdir = last_dir;
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1992 // When 'browsedir is "current", use current directory. This is the
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1993 // default already, leave initdir empty.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1994 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1995
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1996 # ifdef FEAT_GUI
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
1997 if (gui.in_use) // when this changes, also adjust f_has()!
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1998 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1999 if (filter == NULL
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2000 # ifdef FEAT_EVAL
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2001 && (filter = get_var_value((char_u *)"b:browsefilter")) == NULL
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2002 && (filter = get_var_value((char_u *)"g:browsefilter")) == NULL
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2003 # endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2004 )
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2005 filter = BROWSE_FILTER_DEFAULT;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2006 if (flags & BROWSE_DIR)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2007 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2008 # if defined(FEAT_GUI_GTK) || defined(MSWIN)
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2009 // For systems that have a directory dialog.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2010 fname = gui_mch_browsedir(title, initdir);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2011 # else
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2012 // Generic solution for selecting a directory: select a file and
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2013 // remove the file name.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2014 fname = gui_mch_browse(0, title, dflt, ext, initdir, (char_u *)"");
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2015 # endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2016 # if !defined(FEAT_GUI_GTK)
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2017 // Win32 adds a dummy file name, others return an arbitrary file
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2018 // name. GTK+ 2 returns only the directory,
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2019 if (fname != NULL && *fname != NUL && !mch_isdir(fname))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2020 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2021 // Remove the file name.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2022 char_u *tail = gettail_sep(fname);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2023
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2024 if (tail == fname)
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2025 *tail++ = '.'; // use current dir
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2026 *tail = NUL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2027 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2028 # endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2029 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2030 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2031 fname = gui_mch_browse(flags & BROWSE_SAVE,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2032 title, dflt, ext, initdir, (char_u *)_(filter));
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2033
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2034 // We hang around in the dialog for a while, the user might do some
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2035 // things to our files. The Win32 dialog allows deleting or renaming
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2036 // a file, check timestamps.
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2037 need_check_timestamps = TRUE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2038 did_check_timestamps = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2039 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2040 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2041 # endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2042 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2043 // TODO: non-GUI file selector here
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2044 emsg(_("E338: Sorry, no file browser in console mode"));
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2045 fname = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2046 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2047
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2048 // keep the directory for next time
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2049 if (fname != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2050 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2051 vim_free(last_dir);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2052 last_dir = vim_strsave(fname);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2053 if (last_dir != NULL && !(flags & BROWSE_DIR))
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2054 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2055 *gettail(last_dir) = NUL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2056 if (*last_dir == NUL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2057 {
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2058 // filename only returned, must be in current dir
17966
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2059 vim_free(last_dir);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2060 last_dir = alloc(MAXPATHL);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2061 if (last_dir != NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2062 mch_dirname(last_dir, MAXPATHL);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2063 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2064 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2065 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2066
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2067 vim_free(tofree);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2068 cmdmod.browse = save_browse;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2069
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2070 return fname;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2071 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2072 #endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2073
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2074 #if defined(FEAT_EVAL) || defined(PROTO)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2075
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2076 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2077 * "browse(save, title, initdir, default)" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2078 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2079 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2080 f_browse(typval_T *argvars UNUSED, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2081 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2082 # ifdef FEAT_BROWSE
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2083 int save;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2084 char_u *title;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2085 char_u *initdir;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2086 char_u *defname;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2087 char_u buf[NUMBUFLEN];
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2088 char_u buf2[NUMBUFLEN];
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2089 int error = FALSE;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2090
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2091 save = (int)tv_get_number_chk(&argvars[0], &error);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2092 title = tv_get_string_chk(&argvars[1]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2093 initdir = tv_get_string_buf_chk(&argvars[2], buf);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2094 defname = tv_get_string_buf_chk(&argvars[3], buf2);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2095
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2096 if (error || title == NULL || initdir == NULL || defname == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2097 rettv->vval.v_string = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2098 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2099 rettv->vval.v_string =
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2100 do_browse(save ? BROWSE_SAVE : 0,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2101 title, defname, NULL, initdir, NULL, curbuf);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2102 # else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2103 rettv->vval.v_string = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2104 # endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2105 rettv->v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2106 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2107
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2108 /*
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2109 * "browsedir(title, initdir)" function
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2110 */
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2111 void
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2112 f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2113 {
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2114 # ifdef FEAT_BROWSE
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2115 char_u *title;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2116 char_u *initdir;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2117 char_u buf[NUMBUFLEN];
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2118
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2119 title = tv_get_string_chk(&argvars[0]);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2120 initdir = tv_get_string_buf_chk(&argvars[1], buf);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2121
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2122 if (title == NULL || initdir == NULL)
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2123 rettv->vval.v_string = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2124 else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2125 rettv->vval.v_string = do_browse(BROWSE_DIR,
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2126 title, NULL, NULL, initdir, NULL, curbuf);
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2127 # else
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2128 rettv->vval.v_string = NULL;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2129 # endif
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2130 rettv->v_type = VAR_STRING;
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2131 }
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2132
46f95606b9ec patch 8.1.1979: code for handling file names is spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2133 #endif // FEAT_EVAL
17978
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2134
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2135 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2136 * Replace home directory by "~" in each space or comma separated file name in
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2137 * 'src'.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2138 * If anything fails (except when out of space) dst equals src.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2139 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2140 void
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2141 home_replace(
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2142 buf_T *buf, // when not NULL, check for help files
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2143 char_u *src, // input file name
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2144 char_u *dst, // where to put the result
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2145 int dstlen, // maximum length of the result
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2146 int one) // if TRUE, only replace one file name, include
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2147 // spaces and commas in the file name.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2148 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2149 size_t dirlen = 0, envlen = 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2150 size_t len;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2151 char_u *homedir_env, *homedir_env_orig;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2152 char_u *p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2153
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2154 if (src == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2155 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2156 *dst = NUL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2157 return;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2158 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2159
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2160 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2161 * If the file is a help file, remove the path completely.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2162 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2163 if (buf != NULL && buf->b_help)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2164 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2165 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2166 return;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2167 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2168
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2169 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2170 * We check both the value of the $HOME environment variable and the
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2171 * "real" home directory.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2172 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2173 if (homedir != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2174 dirlen = STRLEN(homedir);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2175
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2176 #ifdef VMS
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2177 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2178 #else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2179 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2180 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2181 #ifdef MSWIN
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2182 if (homedir_env == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2183 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2184 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2185 // Empty is the same as not set.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2186 if (homedir_env != NULL && *homedir_env == NUL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2187 homedir_env = NULL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2188
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2189 if (homedir_env != NULL && *homedir_env == '~')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2190 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2191 int usedlen = 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2192 int flen;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2193 char_u *fbuf = NULL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2194
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2195 flen = (int)STRLEN(homedir_env);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2196 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2197 &homedir_env, &fbuf, &flen);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2198 flen = (int)STRLEN(homedir_env);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2199 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2200 // Remove the trailing / that is added to a directory.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2201 homedir_env[flen - 1] = NUL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2202 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2203
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2204 if (homedir_env != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2205 envlen = STRLEN(homedir_env);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2206
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2207 if (!one)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2208 src = skipwhite(src);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2209 while (*src && dstlen > 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2210 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2211 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2212 * Here we are at the beginning of a file name.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2213 * First, check to see if the beginning of the file name matches
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2214 * $HOME or the "real" home directory. Check that there is a '/'
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2215 * after the match (so that if e.g. the file is "/home/pieter/bla",
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2216 * and the home directory is "/home/piet", the file does not end up
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2217 * as "~er/bla" (which would seem to indicate the file "bla" in user
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2218 * er's home directory)).
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2219 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2220 p = homedir;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2221 len = dirlen;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2222 for (;;)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2223 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2224 if ( len
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2225 && fnamencmp(src, p, len) == 0
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2226 && (vim_ispathsep(src[len])
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2227 || (!one && (src[len] == ',' || src[len] == ' '))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2228 || src[len] == NUL))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2229 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2230 src += len;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2231 if (--dstlen > 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2232 *dst++ = '~';
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2233
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2234 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2235 * If it's just the home directory, add "/".
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2236 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2237 if (!vim_ispathsep(src[0]) && --dstlen > 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2238 *dst++ = '/';
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2239 break;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2240 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2241 if (p == homedir_env)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2242 break;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2243 p = homedir_env;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2244 len = envlen;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2245 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2246
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2247 // if (!one) skip to separator: space or comma
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2248 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2249 *dst++ = *src++;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2250 // skip separator
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2251 while ((*src == ' ' || *src == ',') && --dstlen > 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2252 *dst++ = *src++;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2253 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2254 // if (dstlen == 0) out of space, what to do???
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2255
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2256 *dst = NUL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2257
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2258 if (homedir_env != homedir_env_orig)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2259 vim_free(homedir_env);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2260 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2261
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2262 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2263 * Like home_replace, store the replaced string in allocated memory.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2264 * When something fails, NULL is returned.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2265 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2266 char_u *
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2267 home_replace_save(
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2268 buf_T *buf, // when not NULL, check for help files
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2269 char_u *src) // input file name
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2270 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2271 char_u *dst;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2272 unsigned len;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2273
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2274 len = 3; // space for "~/" and trailing NUL
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2275 if (src != NULL) // just in case
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2276 len += (unsigned)STRLEN(src);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2277 dst = alloc(len);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2278 if (dst != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2279 home_replace(buf, src, dst, len, TRUE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2280 return dst;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2281 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2282
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2283 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2284 * Compare two file names and return:
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2285 * FPC_SAME if they both exist and are the same file.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2286 * FPC_SAMEX if they both don't exist and have the same file name.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2287 * FPC_DIFF if they both exist and are different files.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2288 * FPC_NOTX if they both don't exist.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2289 * FPC_DIFFX if one of them doesn't exist.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2290 * For the first name environment variables are expanded if "expandenv" is
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2291 * TRUE.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2292 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2293 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2294 fullpathcmp(
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2295 char_u *s1,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2296 char_u *s2,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2297 int checkname, // when both don't exist, check file names
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2298 int expandenv)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2299 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2300 #ifdef UNIX
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2301 char_u exp1[MAXPATHL];
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2302 char_u full1[MAXPATHL];
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2303 char_u full2[MAXPATHL];
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2304 stat_T st1, st2;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2305 int r1, r2;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2306
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2307 if (expandenv)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2308 expand_env(s1, exp1, MAXPATHL);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2309 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2310 vim_strncpy(exp1, s1, MAXPATHL - 1);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2311 r1 = mch_stat((char *)exp1, &st1);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2312 r2 = mch_stat((char *)s2, &st2);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2313 if (r1 != 0 && r2 != 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2314 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2315 /* if mch_stat() doesn't work, may compare the names */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2316 if (checkname)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2317 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2318 if (fnamecmp(exp1, s2) == 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2319 return FPC_SAMEX;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2320 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2321 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2322 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2323 return FPC_SAMEX;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2324 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2325 return FPC_NOTX;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2326 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2327 if (r1 != 0 || r2 != 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2328 return FPC_DIFFX;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2329 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2330 return FPC_SAME;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2331 return FPC_DIFF;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2332 #else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2333 char_u *exp1; // expanded s1
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2334 char_u *full1; // full path of s1
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2335 char_u *full2; // full path of s2
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2336 int retval = FPC_DIFF;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2337 int r1, r2;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2338
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2339 // allocate one buffer to store three paths (alloc()/free() is slow!)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2340 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2341 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2342 full1 = exp1 + MAXPATHL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2343 full2 = full1 + MAXPATHL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2344
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2345 if (expandenv)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2346 expand_env(s1, exp1, MAXPATHL);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2347 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2348 vim_strncpy(exp1, s1, MAXPATHL - 1);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2349 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2350 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2351
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2352 // If vim_FullName() fails, the file probably doesn't exist.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2353 if (r1 != OK && r2 != OK)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2354 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2355 if (checkname && fnamecmp(exp1, s2) == 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2356 retval = FPC_SAMEX;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2357 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2358 retval = FPC_NOTX;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2359 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2360 else if (r1 != OK || r2 != OK)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2361 retval = FPC_DIFFX;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2362 else if (fnamecmp(full1, full2))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2363 retval = FPC_DIFF;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2364 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2365 retval = FPC_SAME;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2366 vim_free(exp1);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2367 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2368 return retval;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2369 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2370 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2371
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2372 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2373 * Get the tail of a path: the file name.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2374 * When the path ends in a path separator the tail is the NUL after it.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2375 * Fail safe: never returns NULL.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2376 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2377 char_u *
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2378 gettail(char_u *fname)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2379 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2380 char_u *p1, *p2;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2381
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2382 if (fname == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2383 return (char_u *)"";
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2384 for (p1 = p2 = get_past_head(fname); *p2; ) // find last part of path
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2385 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2386 if (vim_ispathsep_nocolon(*p2))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2387 p1 = p2 + 1;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2388 MB_PTR_ADV(p2);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2389 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2390 return p1;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2391 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2392
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2393 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2394 * Get pointer to tail of "fname", including path separators. Putting a NUL
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2395 * here leaves the directory name. Takes care of "c:/" and "//".
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2396 * Always returns a valid pointer.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2397 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2398 char_u *
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2399 gettail_sep(char_u *fname)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2400 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2401 char_u *p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2402 char_u *t;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2403
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2404 p = get_past_head(fname); // don't remove the '/' from "c:/file"
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2405 t = gettail(fname);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2406 while (t > p && after_pathsep(fname, t))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2407 --t;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2408 #ifdef VMS
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2409 // path separator is part of the path
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2410 ++t;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2411 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2412 return t;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2413 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2414
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2415 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2416 * get the next path component (just after the next path separator).
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2417 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2418 char_u *
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2419 getnextcomp(char_u *fname)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2420 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2421 while (*fname && !vim_ispathsep(*fname))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2422 MB_PTR_ADV(fname);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2423 if (*fname)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2424 ++fname;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2425 return fname;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2426 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2427
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2428 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2429 * Get a pointer to one character past the head of a path name.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2430 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2431 * If there is no head, path is returned.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2432 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2433 char_u *
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2434 get_past_head(char_u *path)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2435 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2436 char_u *retval;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2437
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2438 #if defined(MSWIN)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2439 // may skip "c:"
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2440 if (isalpha(path[0]) && path[1] == ':')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2441 retval = path + 2;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2442 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2443 retval = path;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2444 #else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2445 # if defined(AMIGA)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2446 // may skip "label:"
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2447 retval = vim_strchr(path, ':');
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2448 if (retval == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2449 retval = path;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2450 # else // Unix
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2451 retval = path;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2452 # endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2453 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2454
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2455 while (vim_ispathsep(*retval))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2456 ++retval;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2457
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2458 return retval;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2459 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2460
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2461 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2462 * Return TRUE if 'c' is a path separator.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2463 * Note that for MS-Windows this includes the colon.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2464 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2465 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2466 vim_ispathsep(int c)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2467 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2468 #ifdef UNIX
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2469 return (c == '/'); // UNIX has ':' inside file names
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2470 #else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2471 # ifdef BACKSLASH_IN_FILENAME
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2472 return (c == ':' || c == '/' || c == '\\');
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2473 # else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2474 # ifdef VMS
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2475 // server"user passwd"::device:[full.path.name]fname.extension;version"
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2476 return (c == ':' || c == '[' || c == ']' || c == '/'
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2477 || c == '<' || c == '>' || c == '"' );
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2478 # else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2479 return (c == ':' || c == '/');
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2480 # endif // VMS
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2481 # endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2482 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2483 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2484
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2485 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2486 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2487 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2488 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2489 vim_ispathsep_nocolon(int c)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2490 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2491 return vim_ispathsep(c)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2492 #ifdef BACKSLASH_IN_FILENAME
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2493 && c != ':'
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2494 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2495 ;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2496 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2497
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2498 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2499 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2500 * It's done in-place.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2501 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2502 void
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2503 shorten_dir(char_u *str)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2504 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2505 char_u *tail, *s, *d;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2506 int skip = FALSE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2507
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2508 tail = gettail(str);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2509 d = str;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2510 for (s = str; ; ++s)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2511 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2512 if (s >= tail) // copy the whole tail
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2513 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2514 *d++ = *s;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2515 if (*s == NUL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2516 break;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2517 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2518 else if (vim_ispathsep(*s)) // copy '/' and next char
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2519 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2520 *d++ = *s;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2521 skip = FALSE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2522 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2523 else if (!skip)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2524 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2525 *d++ = *s; // copy next char
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2526 if (*s != '~' && *s != '.') // and leading "~" and "."
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2527 skip = TRUE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2528 if (has_mbyte)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2529 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2530 int l = mb_ptr2len(s);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2531
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2532 while (--l > 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2533 *d++ = *++s;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2534 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2535 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2536 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2537 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2538
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2539 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2540 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2541 * Also returns TRUE if there is no directory name.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2542 * "fname" must be writable!.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2543 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2544 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2545 dir_of_file_exists(char_u *fname)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2546 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2547 char_u *p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2548 int c;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2549 int retval;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2550
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2551 p = gettail_sep(fname);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2552 if (p == fname)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2553 return TRUE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2554 c = *p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2555 *p = NUL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2556 retval = mch_isdir(fname);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2557 *p = c;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2558 return retval;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2559 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2560
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2561 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2562 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2563 * and deal with 'fileignorecase'.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2564 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2565 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2566 vim_fnamecmp(char_u *x, char_u *y)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2567 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2568 #ifdef BACKSLASH_IN_FILENAME
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2569 return vim_fnamencmp(x, y, MAXPATHL);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2570 #else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2571 if (p_fic)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2572 return MB_STRICMP(x, y);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2573 return STRCMP(x, y);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2574 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2575 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2576
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2577 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2578 vim_fnamencmp(char_u *x, char_u *y, size_t len)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2579 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2580 #ifdef BACKSLASH_IN_FILENAME
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2581 char_u *px = x;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2582 char_u *py = y;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2583 int cx = NUL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2584 int cy = NUL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2585
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2586 while (len > 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2587 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2588 cx = PTR2CHAR(px);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2589 cy = PTR2CHAR(py);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2590 if (cx == NUL || cy == NUL
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2591 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2592 && !(cx == '/' && cy == '\\')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2593 && !(cx == '\\' && cy == '/')))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2594 break;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2595 len -= MB_PTR2LEN(px);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2596 px += MB_PTR2LEN(px);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2597 py += MB_PTR2LEN(py);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2598 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2599 if (len == 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2600 return 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2601 return (cx - cy);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2602 #else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2603 if (p_fic)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2604 return MB_STRNICMP(x, y, len);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2605 return STRNCMP(x, y, len);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2606 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2607 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2608
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2609 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2610 * Concatenate file names fname1 and fname2 into allocated memory.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2611 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2612 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2613 char_u *
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2614 concat_fnames(char_u *fname1, char_u *fname2, int sep)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2615 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2616 char_u *dest;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2617
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2618 dest = alloc(STRLEN(fname1) + STRLEN(fname2) + 3);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2619 if (dest != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2620 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2621 STRCPY(dest, fname1);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2622 if (sep)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2623 add_pathsep(dest);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2624 STRCAT(dest, fname2);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2625 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2626 return dest;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2627 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2628
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2629 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2630 * Add a path separator to a file name, unless it already ends in a path
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2631 * separator.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2632 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2633 void
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2634 add_pathsep(char_u *p)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2635 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2636 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2637 STRCAT(p, PATHSEPSTR);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2638 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2639
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2640 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2641 * FullName_save - Make an allocated copy of a full file name.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2642 * Returns NULL when out of memory.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2643 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2644 char_u *
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2645 FullName_save(
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2646 char_u *fname,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2647 int force) // force expansion, even when it already looks
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2648 // like a full path name
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2649 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2650 char_u *buf;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2651 char_u *new_fname = NULL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2652
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2653 if (fname == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2654 return NULL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2655
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2656 buf = alloc(MAXPATHL);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2657 if (buf != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2658 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2659 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2660 new_fname = vim_strsave(buf);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2661 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2662 new_fname = vim_strsave(fname);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2663 vim_free(buf);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2664 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2665 return new_fname;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2666 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2667
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2668 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2669 * return TRUE if "fname" exists.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2670 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2671 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2672 vim_fexists(char_u *fname)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2673 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2674 stat_T st;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2675
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2676 if (mch_stat((char *)fname, &st))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2677 return FALSE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2678 return TRUE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2679 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2680
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2681 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2682 * Invoke expand_wildcards() for one pattern.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2683 * Expand items like "%:h" before the expansion.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2684 * Returns OK or FAIL.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2685 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2686 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2687 expand_wildcards_eval(
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2688 char_u **pat, // pointer to input pattern
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2689 int *num_file, // resulting number of files
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2690 char_u ***file, // array of resulting files
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2691 int flags) // EW_DIR, etc.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2692 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2693 int ret = FAIL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2694 char_u *eval_pat = NULL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2695 char_u *exp_pat = *pat;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2696 char *ignored_msg;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2697 int usedlen;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2698
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2699 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2700 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2701 ++emsg_off;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2702 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2703 NULL, &ignored_msg, NULL);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2704 --emsg_off;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2705 if (eval_pat != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2706 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2707 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2708
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2709 if (exp_pat != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2710 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2711
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2712 if (eval_pat != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2713 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2714 vim_free(exp_pat);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2715 vim_free(eval_pat);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2716 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2717
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2718 return ret;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2719 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2720
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2721 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2722 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2723 * 'wildignore'.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2724 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2725 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2726 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2727 expand_wildcards(
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2728 int num_pat, // number of input patterns
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2729 char_u **pat, // array of input patterns
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2730 int *num_files, // resulting number of files
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2731 char_u ***files, // array of resulting files
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2732 int flags) // EW_DIR, etc.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2733 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2734 int retval;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2735 int i, j;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2736 char_u *p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2737 int non_suf_match; // number without matching suffix
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2738
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2739 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2740
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2741 // When keeping all matches, return here
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2742 if ((flags & EW_KEEPALL) || retval == FAIL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2743 return retval;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2744
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2745 #ifdef FEAT_WILDIGN
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2746 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2747 * Remove names that match 'wildignore'.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2748 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2749 if (*p_wig)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2750 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2751 char_u *ffname;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2752
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2753 // check all files in (*files)[]
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2754 for (i = 0; i < *num_files; ++i)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2755 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2756 ffname = FullName_save((*files)[i], FALSE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2757 if (ffname == NULL) // out of memory
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2758 break;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2759 # ifdef VMS
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2760 vms_remove_version(ffname);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2761 # endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2762 if (match_file_list(p_wig, (*files)[i], ffname))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2763 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2764 // remove this matching file from the list
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2765 vim_free((*files)[i]);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2766 for (j = i; j + 1 < *num_files; ++j)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2767 (*files)[j] = (*files)[j + 1];
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2768 --*num_files;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2769 --i;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2770 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2771 vim_free(ffname);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2772 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2773
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2774 // If the number of matches is now zero, we fail.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2775 if (*num_files == 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2776 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2777 VIM_CLEAR(*files);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2778 return FAIL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2779 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2780 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2781 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2782
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2783 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2784 * Move the names where 'suffixes' match to the end.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2785 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2786 if (*num_files > 1)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2787 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2788 non_suf_match = 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2789 for (i = 0; i < *num_files; ++i)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2790 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2791 if (!match_suffix((*files)[i]))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2792 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2793 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2794 * Move the name without matching suffix to the front
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2795 * of the list.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2796 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2797 p = (*files)[i];
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2798 for (j = i; j > non_suf_match; --j)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2799 (*files)[j] = (*files)[j - 1];
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2800 (*files)[non_suf_match++] = p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2801 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2802 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2803 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2804
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2805 return retval;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2806 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2807
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2808 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2809 * Return TRUE if "fname" matches with an entry in 'suffixes'.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2810 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2811 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2812 match_suffix(char_u *fname)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2813 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2814 int fnamelen, setsuflen;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2815 char_u *setsuf;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2816 #define MAXSUFLEN 30 // maximum length of a file suffix
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2817 char_u suf_buf[MAXSUFLEN];
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2818
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2819 fnamelen = (int)STRLEN(fname);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2820 setsuflen = 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2821 for (setsuf = p_su; *setsuf; )
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2822 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2823 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2824 if (setsuflen == 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2825 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2826 char_u *tail = gettail(fname);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2827
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2828 // empty entry: match name without a '.'
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2829 if (vim_strchr(tail, '.') == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2830 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2831 setsuflen = 1;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2832 break;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2833 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2834 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2835 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2836 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2837 if (fnamelen >= setsuflen
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2838 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2839 (size_t)setsuflen) == 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2840 break;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2841 setsuflen = 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2842 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2843 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2844 return (setsuflen != 0);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2845 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2846
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2847 #ifdef VIM_BACKTICK
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2848
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2849 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2850 * Return TRUE if we can expand this backtick thing here.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2851 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2852 static int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2853 vim_backtick(char_u *p)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2854 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2855 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2856 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2857
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2858 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2859 * Expand an item in `backticks` by executing it as a command.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2860 * Currently only works when pat[] starts and ends with a `.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2861 * Returns number of file names found, -1 if an error is encountered.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2862 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2863 static int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2864 expand_backtick(
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2865 garray_T *gap,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2866 char_u *pat,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2867 int flags) // EW_* flags
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2868 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2869 char_u *p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2870 char_u *cmd;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2871 char_u *buffer;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2872 int cnt = 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2873 int i;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2874
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2875 // Create the command: lop off the backticks.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2876 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2877 if (cmd == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2878 return -1;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2879
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2880 #ifdef FEAT_EVAL
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2881 if (*cmd == '=') // `={expr}`: Expand expression
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2882 buffer = eval_to_string(cmd + 1, &p, TRUE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2883 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2884 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2885 buffer = get_cmd_output(cmd, NULL,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2886 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2887 vim_free(cmd);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2888 if (buffer == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2889 return -1;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2890
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2891 cmd = buffer;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2892 while (*cmd != NUL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2893 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2894 cmd = skipwhite(cmd); // skip over white space
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2895 p = cmd;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2896 while (*p != NUL && *p != '\r' && *p != '\n') // skip over entry
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2897 ++p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2898 // add an entry if it is not empty
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2899 if (p > cmd)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2900 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2901 i = *p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2902 *p = NUL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2903 addfile(gap, cmd, flags);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2904 *p = i;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2905 ++cnt;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2906 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2907 cmd = p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2908 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2909 ++cmd;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2910 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2911
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2912 vim_free(buffer);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2913 return cnt;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2914 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2915 #endif // VIM_BACKTICK
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2916
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2917 #if defined(MSWIN)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2918 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2919 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2920 * it's shared between these systems.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2921 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2922
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2923 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2924 * comparison function for qsort in dos_expandpath()
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2925 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2926 static int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2927 pstrcmp(const void *a, const void *b)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2928 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2929 return (pathcmp(*(char **)a, *(char **)b, -1));
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2930 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2931
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2932 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2933 * Recursively expand one path component into all matching files and/or
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2934 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2935 * Return the number of matches found.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2936 * "path" has backslashes before chars that are not to be expanded, starting
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2937 * at "path[wildoff]".
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2938 * Return the number of matches found.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2939 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2940 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2941 static int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2942 dos_expandpath(
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2943 garray_T *gap,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2944 char_u *path,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2945 int wildoff,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2946 int flags, // EW_* flags
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2947 int didstar) // expanded "**" once already
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2948 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2949 char_u *buf;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2950 char_u *path_end;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2951 char_u *p, *s, *e;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2952 int start_len = gap->ga_len;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2953 char_u *pat;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2954 regmatch_T regmatch;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2955 int starts_with_dot;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2956 int matches;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2957 int len;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2958 int starstar = FALSE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2959 static int stardepth = 0; // depth for "**" expansion
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2960 HANDLE hFind = INVALID_HANDLE_VALUE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2961 WIN32_FIND_DATAW wfb;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2962 WCHAR *wn = NULL; // UCS-2 name, NULL when not used.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2963 char_u *matchname;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2964 int ok;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2965
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2966 // Expanding "**" may take a long time, check for CTRL-C.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2967 if (stardepth > 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2968 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2969 ui_breakcheck();
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2970 if (got_int)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2971 return 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2972 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2973
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2974 // Make room for file name. When doing encoding conversion the actual
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2975 // length may be quite a bit longer, thus use the maximum possible length.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2976 buf = alloc(MAXPATHL);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2977 if (buf == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2978 return 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2979
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2980 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2981 * Find the first part in the path name that contains a wildcard or a ~1.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2982 * Copy it into buf, including the preceding characters.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2983 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2984 p = buf;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2985 s = buf;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2986 e = NULL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2987 path_end = path;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2988 while (*path_end != NUL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2989 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2990 // May ignore a wildcard that has a backslash before it; it will
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2991 // be removed by rem_backslash() or file_pat_to_reg_pat() below.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2992 if (path_end >= path + wildoff && rem_backslash(path_end))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2993 *p++ = *path_end++;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2994 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2995 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2996 if (e != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2997 break;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2998 s = p + 1;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
2999 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3000 else if (path_end >= path + wildoff
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3001 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3002 e = p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3003 if (has_mbyte)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3004 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3005 len = (*mb_ptr2len)(path_end);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3006 STRNCPY(p, path_end, len);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3007 p += len;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3008 path_end += len;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3009 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3010 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3011 *p++ = *path_end++;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3012 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3013 e = p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3014 *e = NUL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3015
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3016 // now we have one wildcard component between s and e
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3017 // Remove backslashes between "wildoff" and the start of the wildcard
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3018 // component.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3019 for (p = buf + wildoff; p < s; ++p)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3020 if (rem_backslash(p))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3021 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3022 STRMOVE(p, p + 1);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3023 --e;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3024 --s;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3025 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3026
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3027 // Check for "**" between "s" and "e".
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3028 for (p = s; p < e; ++p)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3029 if (p[0] == '*' && p[1] == '*')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3030 starstar = TRUE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3031
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3032 starts_with_dot = *s == '.';
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3033 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3034 if (pat == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3035 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3036 vim_free(buf);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3037 return 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3038 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3039
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3040 // compile the regexp into a program
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3041 if (flags & (EW_NOERROR | EW_NOTWILD))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3042 ++emsg_silent;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3043 regmatch.rm_ic = TRUE; // Always ignore case
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3044 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3045 if (flags & (EW_NOERROR | EW_NOTWILD))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3046 --emsg_silent;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3047 vim_free(pat);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3048
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3049 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3050 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3051 vim_free(buf);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3052 return 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3053 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3054
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3055 // remember the pattern or file name being looked for
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3056 matchname = vim_strsave(s);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3057
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3058 // If "**" is by itself, this is the first time we encounter it and more
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3059 // is following then find matches without any directory.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3060 if (!didstar && stardepth < 100 && starstar && e - s == 2
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3061 && *path_end == '/')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3062 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3063 STRCPY(s, path_end + 1);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3064 ++stardepth;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3065 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3066 --stardepth;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3067 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3068
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3069 // Scan all files in the directory with "dir/ *.*"
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3070 STRCPY(s, "*.*");
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3071 wn = enc_to_utf16(buf, NULL);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3072 if (wn != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3073 hFind = FindFirstFileW(wn, &wfb);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3074 ok = (hFind != INVALID_HANDLE_VALUE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3075
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3076 while (ok)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3077 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3078 p = utf16_to_enc(wfb.cFileName, NULL); // p is allocated here
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3079 if (p == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3080 break; // out of memory
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3081
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3082 // Ignore entries starting with a dot, unless when asked for. Accept
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3083 // all entries found with "matchname".
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3084 if ((p[0] != '.' || starts_with_dot
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3085 || ((flags & EW_DODOT)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3086 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3087 && (matchname == NULL
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3088 || (regmatch.regprog != NULL
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3089 && vim_regexec(&regmatch, p, (colnr_T)0))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3090 || ((flags & EW_NOTWILD)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3091 && fnamencmp(path + (s - buf), p, e - s) == 0)))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3092 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3093 STRCPY(s, p);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3094 len = (int)STRLEN(buf);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3095
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3096 if (starstar && stardepth < 100)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3097 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3098 // For "**" in the pattern first go deeper in the tree to
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3099 // find matches.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3100 STRCPY(buf + len, "/**");
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3101 STRCPY(buf + len + 3, path_end);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3102 ++stardepth;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3103 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3104 --stardepth;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3105 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3106
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3107 STRCPY(buf + len, path_end);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3108 if (mch_has_exp_wildcard(path_end))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3109 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3110 // need to expand another component of the path
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3111 // remove backslashes for the remaining components only
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3112 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3113 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3114 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3115 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3116 // no more wildcards, check if there is a match
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3117 // remove backslashes for the remaining components only
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3118 if (*path_end != 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3119 backslash_halve(buf + len + 1);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3120 if (mch_getperm(buf) >= 0) // add existing file
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3121 addfile(gap, buf, flags);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3122 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3123 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3124
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3125 vim_free(p);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3126 ok = FindNextFileW(hFind, &wfb);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3127
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3128 // If no more matches and no match was used, try expanding the name
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3129 // itself. Finds the long name of a short filename.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3130 if (!ok && matchname != NULL && gap->ga_len == start_len)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3131 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3132 STRCPY(s, matchname);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3133 FindClose(hFind);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3134 vim_free(wn);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3135 wn = enc_to_utf16(buf, NULL);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3136 if (wn != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3137 hFind = FindFirstFileW(wn, &wfb);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3138 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3139 hFind = INVALID_HANDLE_VALUE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3140 ok = (hFind != INVALID_HANDLE_VALUE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3141 VIM_CLEAR(matchname);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3142 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3143 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3144
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3145 FindClose(hFind);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3146 vim_free(wn);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3147 vim_free(buf);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3148 vim_regfree(regmatch.regprog);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3149 vim_free(matchname);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3150
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3151 matches = gap->ga_len - start_len;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3152 if (matches > 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3153 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3154 sizeof(char_u *), pstrcmp);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3155 return matches;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3156 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3157
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3158 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3159 mch_expandpath(
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3160 garray_T *gap,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3161 char_u *path,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3162 int flags) // EW_* flags
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3163 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3164 return dos_expandpath(gap, path, 0, flags, FALSE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3165 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3166 #endif // MSWIN
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3167
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3168 #if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3169 || defined(PROTO)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3170 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3171 * Unix style wildcard expansion code.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3172 * It's here because it's used both for Unix and Mac.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3173 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3174 static int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3175 pstrcmp(const void *a, const void *b)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3176 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3177 return (pathcmp(*(char **)a, *(char **)b, -1));
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3178 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3179
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3180 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3181 * Recursively expand one path component into all matching files and/or
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3182 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3183 * "path" has backslashes before chars that are not to be expanded, starting
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3184 * at "path + wildoff".
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3185 * Return the number of matches found.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3186 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3187 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3188 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3189 unix_expandpath(
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3190 garray_T *gap,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3191 char_u *path,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3192 int wildoff,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3193 int flags, // EW_* flags
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3194 int didstar) // expanded "**" once already
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3195 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3196 char_u *buf;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3197 char_u *path_end;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3198 char_u *p, *s, *e;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3199 int start_len = gap->ga_len;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3200 char_u *pat;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3201 regmatch_T regmatch;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3202 int starts_with_dot;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3203 int matches;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3204 int len;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3205 int starstar = FALSE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3206 static int stardepth = 0; // depth for "**" expansion
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3207
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3208 DIR *dirp;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3209 struct dirent *dp;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3210
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3211 // Expanding "**" may take a long time, check for CTRL-C.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3212 if (stardepth > 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3213 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3214 ui_breakcheck();
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3215 if (got_int)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3216 return 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3217 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3218
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3219 // make room for file name
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3220 buf = alloc(STRLEN(path) + BASENAMELEN + 5);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3221 if (buf == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3222 return 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3223
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3224 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3225 * Find the first part in the path name that contains a wildcard.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3226 * When EW_ICASE is set every letter is considered to be a wildcard.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3227 * Copy it into "buf", including the preceding characters.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3228 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3229 p = buf;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3230 s = buf;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3231 e = NULL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3232 path_end = path;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3233 while (*path_end != NUL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3234 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3235 // May ignore a wildcard that has a backslash before it; it will
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3236 // be removed by rem_backslash() or file_pat_to_reg_pat() below.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3237 if (path_end >= path + wildoff && rem_backslash(path_end))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3238 *p++ = *path_end++;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3239 else if (*path_end == '/')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3240 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3241 if (e != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3242 break;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3243 s = p + 1;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3244 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3245 else if (path_end >= path + wildoff
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3246 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3247 || (!p_fic && (flags & EW_ICASE)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3248 && isalpha(PTR2CHAR(path_end)))))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3249 e = p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3250 if (has_mbyte)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3251 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3252 len = (*mb_ptr2len)(path_end);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3253 STRNCPY(p, path_end, len);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3254 p += len;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3255 path_end += len;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3256 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3257 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3258 *p++ = *path_end++;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3259 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3260 e = p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3261 *e = NUL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3262
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3263 // Now we have one wildcard component between "s" and "e".
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3264 // Remove backslashes between "wildoff" and the start of the wildcard
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3265 // component.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3266 for (p = buf + wildoff; p < s; ++p)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3267 if (rem_backslash(p))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3268 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3269 STRMOVE(p, p + 1);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3270 --e;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3271 --s;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3272 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3273
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3274 // Check for "**" between "s" and "e".
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3275 for (p = s; p < e; ++p)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3276 if (p[0] == '*' && p[1] == '*')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3277 starstar = TRUE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3278
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3279 // convert the file pattern to a regexp pattern
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3280 starts_with_dot = *s == '.';
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3281 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3282 if (pat == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3283 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3284 vim_free(buf);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3285 return 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3286 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3287
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3288 // compile the regexp into a program
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3289 if (flags & EW_ICASE)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3290 regmatch.rm_ic = TRUE; // 'wildignorecase' set
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3291 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3292 regmatch.rm_ic = p_fic; // ignore case when 'fileignorecase' is set
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3293 if (flags & (EW_NOERROR | EW_NOTWILD))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3294 ++emsg_silent;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3295 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3296 if (flags & (EW_NOERROR | EW_NOTWILD))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3297 --emsg_silent;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3298 vim_free(pat);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3299
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3300 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3301 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3302 vim_free(buf);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3303 return 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3304 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3305
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3306 // If "**" is by itself, this is the first time we encounter it and more
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3307 // is following then find matches without any directory.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3308 if (!didstar && stardepth < 100 && starstar && e - s == 2
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3309 && *path_end == '/')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3310 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3311 STRCPY(s, path_end + 1);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3312 ++stardepth;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3313 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3314 --stardepth;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3315 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3316
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3317 // open the directory for scanning
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3318 *s = NUL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3319 dirp = opendir(*buf == NUL ? "." : (char *)buf);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3320
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3321 // Find all matching entries
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3322 if (dirp != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3323 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3324 for (;;)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3325 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3326 dp = readdir(dirp);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3327 if (dp == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3328 break;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3329 if ((dp->d_name[0] != '.' || starts_with_dot
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3330 || ((flags & EW_DODOT)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3331 && dp->d_name[1] != NUL
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3332 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3333 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3334 (char_u *)dp->d_name, (colnr_T)0))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3335 || ((flags & EW_NOTWILD)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3336 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3337 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3338 STRCPY(s, dp->d_name);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3339 len = STRLEN(buf);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3340
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3341 if (starstar && stardepth < 100)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3342 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3343 // For "**" in the pattern first go deeper in the tree to
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3344 // find matches.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3345 STRCPY(buf + len, "/**");
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3346 STRCPY(buf + len + 3, path_end);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3347 ++stardepth;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3348 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3349 --stardepth;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3350 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3351
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3352 STRCPY(buf + len, path_end);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3353 if (mch_has_exp_wildcard(path_end)) // handle more wildcards
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3354 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3355 // need to expand another component of the path
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3356 // remove backslashes for the remaining components only
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3357 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3358 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3359 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3360 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3361 stat_T sb;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3362
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3363 // no more wildcards, check if there is a match
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3364 // remove backslashes for the remaining components only
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3365 if (*path_end != NUL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3366 backslash_halve(buf + len + 1);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3367 // add existing file or symbolic link
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3368 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3369 : mch_getperm(buf) >= 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3370 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3371 #ifdef MACOS_CONVERT
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3372 size_t precomp_len = STRLEN(buf)+1;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3373 char_u *precomp_buf =
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3374 mac_precompose_path(buf, precomp_len, &precomp_len);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3375
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3376 if (precomp_buf)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3377 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3378 mch_memmove(buf, precomp_buf, precomp_len);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3379 vim_free(precomp_buf);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3380 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3381 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3382 addfile(gap, buf, flags);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3383 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3384 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3385 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3386 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3387
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3388 closedir(dirp);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3389 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3390
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3391 vim_free(buf);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3392 vim_regfree(regmatch.regprog);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3393
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3394 matches = gap->ga_len - start_len;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3395 if (matches > 0)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3396 qsort(((char_u **)gap->ga_data) + start_len, matches,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3397 sizeof(char_u *), pstrcmp);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3398 return matches;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3399 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3400 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3401
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3402 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3403 * Return TRUE if "p" contains what looks like an environment variable.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3404 * Allowing for escaping.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3405 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3406 static int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3407 has_env_var(char_u *p)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3408 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3409 for ( ; *p; MB_PTR_ADV(p))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3410 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3411 if (*p == '\\' && p[1] != NUL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3412 ++p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3413 else if (vim_strchr((char_u *)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3414 #if defined(MSWIN)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3415 "$%"
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3416 #else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3417 "$"
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3418 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3419 , *p) != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3420 return TRUE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3421 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3422 return FALSE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3423 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3424
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3425 #ifdef SPECIAL_WILDCHAR
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3426 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3427 * Return TRUE if "p" contains a special wildcard character, one that Vim
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3428 * cannot expand, requires using a shell.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3429 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3430 static int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3431 has_special_wildchar(char_u *p)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3432 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3433 for ( ; *p; MB_PTR_ADV(p))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3434 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3435 // Disallow line break characters.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3436 if (*p == '\r' || *p == '\n')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3437 break;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3438 // Allow for escaping.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3439 if (*p == '\\' && p[1] != NUL && p[1] != '\r' && p[1] != '\n')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3440 ++p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3441 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3442 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3443 // A { must be followed by a matching }.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3444 if (*p == '{' && vim_strchr(p, '}') == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3445 continue;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3446 // A quote and backtick must be followed by another one.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3447 if ((*p == '`' || *p == '\'') && vim_strchr(p, *p) == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3448 continue;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3449 return TRUE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3450 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3451 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3452 return FALSE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3453 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3454 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3455
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3456 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3457 * Generic wildcard expansion code.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3458 *
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3459 * Characters in "pat" that should not be expanded must be preceded with a
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3460 * backslash. E.g., "/path\ with\ spaces/my\*star*"
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3461 *
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3462 * Return FAIL when no single file was found. In this case "num_file" is not
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3463 * set, and "file" may contain an error message.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3464 * Return OK when some files found. "num_file" is set to the number of
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3465 * matches, "file" to the array of matches. Call FreeWild() later.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3466 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3467 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3468 gen_expand_wildcards(
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3469 int num_pat, // number of input patterns
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3470 char_u **pat, // array of input patterns
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3471 int *num_file, // resulting number of files
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3472 char_u ***file, // array of resulting files
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3473 int flags) // EW_* flags
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3474 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3475 int i;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3476 garray_T ga;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3477 char_u *p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3478 static int recursive = FALSE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3479 int add_pat;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3480 int retval = OK;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3481 #if defined(FEAT_SEARCHPATH)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3482 int did_expand_in_path = FALSE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3483 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3484
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3485 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3486 * expand_env() is called to expand things like "~user". If this fails,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3487 * it calls ExpandOne(), which brings us back here. In this case, always
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3488 * call the machine specific expansion function, if possible. Otherwise,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3489 * return FAIL.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3490 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3491 if (recursive)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3492 #ifdef SPECIAL_WILDCHAR
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3493 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3494 #else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3495 return FAIL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3496 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3497
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3498 #ifdef SPECIAL_WILDCHAR
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3499 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3500 * If there are any special wildcard characters which we cannot handle
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3501 * here, call machine specific function for all the expansion. This
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3502 * avoids starting the shell for each argument separately.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3503 * For `=expr` do use the internal function.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3504 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3505 for (i = 0; i < num_pat; i++)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3506 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3507 if (has_special_wildchar(pat[i])
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3508 # ifdef VIM_BACKTICK
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3509 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3510 # endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3511 )
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3512 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3513 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3514 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3515
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3516 recursive = TRUE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3517
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3518 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3519 * The matching file names are stored in a growarray. Init it empty.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3520 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3521 ga_init2(&ga, (int)sizeof(char_u *), 30);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3522
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3523 for (i = 0; i < num_pat; ++i)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3524 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3525 add_pat = -1;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3526 p = pat[i];
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3527
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3528 #ifdef VIM_BACKTICK
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3529 if (vim_backtick(p))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3530 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3531 add_pat = expand_backtick(&ga, p, flags);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3532 if (add_pat == -1)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3533 retval = FAIL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3534 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3535 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3536 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3537 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3538 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3539 * First expand environment variables, "~/" and "~user/".
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3540 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3541 if ((has_env_var(p) && !(flags & EW_NOTENV)) || *p == '~')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3542 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3543 p = expand_env_save_opt(p, TRUE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3544 if (p == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3545 p = pat[i];
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3546 #ifdef UNIX
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3547 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3548 * On Unix, if expand_env() can't expand an environment
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3549 * variable, use the shell to do that. Discard previously
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3550 * found file names and start all over again.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3551 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3552 else if (has_env_var(p) || *p == '~')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3553 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3554 vim_free(p);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3555 ga_clear_strings(&ga);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3556 i = mch_expand_wildcards(num_pat, pat, num_file, file,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3557 flags|EW_KEEPDOLLAR);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3558 recursive = FALSE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3559 return i;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3560 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3561 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3562 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3563
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3564 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3565 * If there are wildcards: Expand file names and add each match to
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3566 * the list. If there is no match, and EW_NOTFOUND is given, add
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3567 * the pattern.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3568 * If there are no wildcards: Add the file name if it exists or
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3569 * when EW_NOTFOUND is given.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3570 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3571 if (mch_has_exp_wildcard(p))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3572 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3573 #if defined(FEAT_SEARCHPATH)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3574 if ((flags & EW_PATH)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3575 && !mch_isFullName(p)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3576 && !(p[0] == '.'
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3577 && (vim_ispathsep(p[1])
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3578 || (p[1] == '.' && vim_ispathsep(p[2]))))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3579 )
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3580 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3581 // :find completion where 'path' is used.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3582 // Recursiveness is OK here.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3583 recursive = FALSE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3584 add_pat = expand_in_path(&ga, p, flags);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3585 recursive = TRUE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3586 did_expand_in_path = TRUE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3587 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3588 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3589 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3590 add_pat = mch_expandpath(&ga, p, flags);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3591 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3592 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3593
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3594 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3595 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3596 char_u *t = backslash_halve_save(p);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3597
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3598 // When EW_NOTFOUND is used, always add files and dirs. Makes
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3599 // "vim c:/" work.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3600 if (flags & EW_NOTFOUND)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3601 addfile(&ga, t, flags | EW_DIR | EW_FILE);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3602 else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3603 addfile(&ga, t, flags);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3604
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3605 if (t != p)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3606 vim_free(t);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3607 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3608
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3609 #if defined(FEAT_SEARCHPATH)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3610 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3611 uniquefy_paths(&ga, p);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3612 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3613 if (p != pat[i])
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3614 vim_free(p);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3615 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3616
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3617 *num_file = ga.ga_len;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3618 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3619
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3620 recursive = FALSE;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3621
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3622 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3623 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3624
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3625 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3626 * Add a file to a file list. Accepted flags:
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3627 * EW_DIR add directories
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3628 * EW_FILE add files
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3629 * EW_EXEC add executable files
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3630 * EW_NOTFOUND add even when it doesn't exist
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3631 * EW_ADDSLASH add slash after directory name
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3632 * EW_ALLLINKS add symlink also when the referred file does not exist
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3633 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3634 void
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3635 addfile(
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3636 garray_T *gap,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3637 char_u *f, /* filename */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3638 int flags)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3639 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3640 char_u *p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3641 int isdir;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3642 stat_T sb;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3643
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3644 // if the file/dir/link doesn't exist, may not add it
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3645 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3646 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3647 return;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3648
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3649 #ifdef FNAME_ILLEGAL
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3650 // if the file/dir contains illegal characters, don't add it
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3651 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3652 return;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3653 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3654
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3655 isdir = mch_isdir(f);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3656 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3657 return;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3658
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3659 // If the file isn't executable, may not add it. Do accept directories.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3660 // When invoked from expand_shellcmd() do not use $PATH.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3661 if (!isdir && (flags & EW_EXEC)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3662 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3663 return;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3664
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3665 // Make room for another item in the file list.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3666 if (ga_grow(gap, 1) == FAIL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3667 return;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3668
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3669 p = alloc(STRLEN(f) + 1 + isdir);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3670 if (p == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3671 return;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3672
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3673 STRCPY(p, f);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3674 #ifdef BACKSLASH_IN_FILENAME
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3675 slash_adjust(p);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3676 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3677 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3678 * Append a slash or backslash after directory names if none is present.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3679 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3680 #ifndef DONT_ADD_PATHSEP_TO_DIR
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3681 if (isdir && (flags & EW_ADDSLASH))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3682 add_pathsep(p);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3683 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3684 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3685 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3686
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3687 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3688 * Free the list of files returned by expand_wildcards() or other expansion
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3689 * functions.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3690 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3691 void
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3692 FreeWild(int count, char_u **files)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3693 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3694 if (count <= 0 || files == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3695 return;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3696 while (count--)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3697 vim_free(files[count]);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3698 vim_free(files);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3699 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3700
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3701 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3702 * Compare path "p[]" to "q[]".
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3703 * If "maxlen" >= 0 compare "p[maxlen]" to "q[maxlen]"
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3704 * Return value like strcmp(p, q), but consider path separators.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3705 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3706 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3707 pathcmp(const char *p, const char *q, int maxlen)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3708 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3709 int i, j;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3710 int c1, c2;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3711 const char *s = NULL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3712
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3713 for (i = 0, j = 0; maxlen < 0 || (i < maxlen && j < maxlen);)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3714 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3715 c1 = PTR2CHAR((char_u *)p + i);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3716 c2 = PTR2CHAR((char_u *)q + j);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3717
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3718 // End of "p": check if "q" also ends or just has a slash.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3719 if (c1 == NUL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3720 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3721 if (c2 == NUL) // full match
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3722 return 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3723 s = q;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3724 i = j;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3725 break;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3726 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3727
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3728 // End of "q": check if "p" just has a slash.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3729 if (c2 == NUL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3730 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3731 s = p;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3732 break;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3733 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3734
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3735 if ((p_fic ? MB_TOUPPER(c1) != MB_TOUPPER(c2) : c1 != c2)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3736 #ifdef BACKSLASH_IN_FILENAME
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3737 // consider '/' and '\\' to be equal
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3738 && !((c1 == '/' && c2 == '\\')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3739 || (c1 == '\\' && c2 == '/'))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3740 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3741 )
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3742 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3743 if (vim_ispathsep(c1))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3744 return -1;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3745 if (vim_ispathsep(c2))
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3746 return 1;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3747 return p_fic ? MB_TOUPPER(c1) - MB_TOUPPER(c2)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3748 : c1 - c2; // no match
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3749 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3750
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3751 i += MB_PTR2LEN((char_u *)p + i);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3752 j += MB_PTR2LEN((char_u *)q + j);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3753 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3754 if (s == NULL) // "i" or "j" ran into "maxlen"
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3755 return 0;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3756
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3757 c1 = PTR2CHAR((char_u *)s + i);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3758 c2 = PTR2CHAR((char_u *)s + i + MB_PTR2LEN((char_u *)s + i));
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3759 // ignore a trailing slash, but not "//" or ":/"
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3760 if (c2 == NUL
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3761 && i > 0
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3762 && !after_pathsep((char_u *)s, (char_u *)s + i)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3763 #ifdef BACKSLASH_IN_FILENAME
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3764 && (c1 == '/' || c1 == '\\')
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3765 #else
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3766 && c1 == '/'
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3767 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3768 )
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3769 return 0; // match with trailing slash
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3770 if (s == q)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3771 return -1; // no match
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3772 return 1;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3773 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3774
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3775 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3776 * Return TRUE if "name" is a full (absolute) path name or URL.
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3777 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3778 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3779 vim_isAbsName(char_u *name)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3780 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3781 return (path_with_url(name) != 0 || mch_isFullName(name));
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3782 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3783
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3784 /*
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3785 * Get absolute file name into buffer "buf[len]".
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3786 *
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3787 * return FAIL for failure, OK otherwise
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3788 */
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3789 int
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3790 vim_FullName(
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3791 char_u *fname,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3792 char_u *buf,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3793 int len,
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3794 int force) // force expansion even when already absolute
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3795 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3796 int retval = OK;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3797 int url;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3798
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3799 *buf = NUL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3800 if (fname == NULL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3801 return FAIL;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3802
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3803 url = path_with_url(fname);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3804 if (!url)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3805 retval = mch_FullName(fname, buf, len, force);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3806 if (url || retval == FAIL)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3807 {
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3808 // something failed; use the file name (truncate when too long)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3809 vim_strncpy(buf, fname, len - 1);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3810 }
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3811 #if defined(MSWIN)
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3812 slash_adjust(buf);
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3813 #endif
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3814 return retval;
8f4cc259ed7a patch 8.1.1985: code for dealing with paths is spread out
Bram Moolenaar <Bram@vim.org>
parents: 17966
diff changeset
3815 }