From 0a4dc70830f5e8286b47120cabc750cca07a75c1 Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Mon, 19 Apr 2021 12:21:01 +0200 Subject: ; Normalize and add missing first and last lines --- test/src/fileio-tests.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/src/fileio-tests.el') diff --git a/test/src/fileio-tests.el b/test/src/fileio-tests.el index 7f193d4eeab..b989c97fe6b 100644 --- a/test/src/fileio-tests.el +++ b/test/src/fileio-tests.el @@ -1,4 +1,4 @@ -;;; unit tests for src/fileio.c -*- lexical-binding: t; -*- +;;; fileio-tests.el --- unit tests for src/fileio.c -*- lexical-binding: t; -*- ;; Copyright 2017-2021 Free Software Foundation, Inc. -- cgit v1.2.3 From 5431a58e86d3f2579c1edf1dc8d7074de73ac694 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Sat, 24 Jul 2021 13:30:58 +0200 Subject: Add new function `directory-append' * doc/lispref/files.texi (Directory Names): Document it, and remove the concat-based file concatenation description. * lisp/emacs-lisp/shortdoc.el (file-name): Add. And add more expand-file-name examples. * src/fileio.c (Fdirectory_append): New function. --- doc/lispref/files.texi | 50 ++++++++++----------------------------------- etc/NEWS | 4 ++++ lisp/emacs-lisp/shortdoc.el | 9 +++++++- src/fileio.c | 46 +++++++++++++++++++++++++++++++++++++++++ test/src/fileio-tests.el | 8 ++++++++ 5 files changed, 77 insertions(+), 40 deletions(-) (limited to 'test/src/fileio-tests.el') diff --git a/doc/lispref/files.texi b/doc/lispref/files.texi index c7e5537c10c..ac49c5aa745 100644 --- a/doc/lispref/files.texi +++ b/doc/lispref/files.texi @@ -2343,49 +2343,21 @@ entirely of directory separators. @end example @end defun - Given a directory name, you can combine it with a relative file name -using @code{concat}: +@defun directory-append filename directory +Combine @var{filename} with @var{directory} by optionally putting a +slash in the middle. @example -(concat @var{dirname} @var{relfile}) -@end example - -@noindent -Be sure to verify that the file name is relative before doing that. -If you use an absolute file name, the results could be syntactically -invalid or refer to the wrong file. - - If you want to use a directory file name in making such a -combination, you must first convert it to a directory name using -@code{file-name-as-directory}: - -@example -(concat (file-name-as-directory @var{dirfile}) @var{relfile}) -@end example - -@noindent -Don't try concatenating a slash by hand, as in - -@example -;;; @r{Wrong!} -(concat @var{dirfile} "/" @var{relfile}) -@end example - -@noindent -because this is not portable. Always use -@code{file-name-as-directory}. - - To avoid the issues mentioned above, or if the @var{dirname} value -might be @code{nil} (for example, from an element of @code{load-path}), -use: - -@example -(expand-file-name @var{relfile} @var{dirname}) +@group +(directory-append "/tmp" "foo") + @result{} "/tmp/foo" +@end group @end example -However, @code{expand-file-name} expands leading @samp{~} in -@var{relfile}, which may not be what you want. @xref{File Name -Expansion}. +This is almost the same as using @code{concat}, but @var{dirname} may +or may not end with a slash character, and this function will not +double that character. +@end defun To convert a directory name to its abbreviation, use this function: diff --git a/etc/NEWS b/etc/NEWS index e4b0809c5f5..a10c5800374 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -3120,6 +3120,10 @@ The former is now declared obsolete. * Lisp Changes in Emacs 28.1 ++++ +*** New function 'directory-append'. +This appends a file name to a directory name and returns the result. + +++ *** New function 'split-string-shell-command'. This splits a shell command string into separate components, diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el index 22439f4c36c..7506d756d19 100644 --- a/lisp/emacs-lisp/shortdoc.el +++ b/lisp/emacs-lisp/shortdoc.el @@ -273,8 +273,15 @@ There can be any number of :example/:result elements." :eval (file-relative-name "/tmp/foo" "/tmp")) (make-temp-name :eval (make-temp-name "/tmp/foo-")) + (directory-append + :eval (directory-append "/tmp/" "foo") + :eval (directory-append "/tmp" "foo") + :eval (directory-append "/tmp" "~")) (expand-file-name - :eval (expand-file-name "foo" "/tmp/")) + :eval (expand-file-name "foo" "/tmp/") + :eval (expand-file-name "foo" "/tmp///") + :eval (expand-file-name "foo" "/tmp/foo/.././") + :eval (expand-file-name "~" "/tmp/")) (substitute-in-file-name :eval (substitute-in-file-name "$HOME/foo")) "Directory Functions" diff --git a/src/fileio.c b/src/fileio.c index 04c9d7d4af3..277da48315e 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -749,6 +749,51 @@ For that reason, you should normally use `make-temp-file' instead. */) empty_unibyte_string, Qnil); } +DEFUN ("directory-append", Fdirectory_append, Sdirectory_append, 2, 2, 0, + doc: /* Return FILE (a string) appended to DIRECTORY (a string). +DIRECTORY may or may not end with a slash -- the return value from +this function will be the same. */) + (Lisp_Object directory, Lisp_Object file) +{ + USE_SAFE_ALLOCA; + char *p; + + CHECK_STRING (file); + CHECK_STRING (directory); + + if (SCHARS (file) == 0) + xsignal1 (Qfile_error, build_string ("Empty file name")); + + if (SCHARS (directory) == 0) + return file; + + /* Make the strings the same multibytedness. */ + if (STRING_MULTIBYTE (file) != STRING_MULTIBYTE (directory)) + { + if (STRING_MULTIBYTE (file)) + directory = make_multibyte_string (SSDATA (directory), + SCHARS (directory), + SCHARS (directory)); + else + file = make_multibyte_string (SSDATA (file), + SCHARS (file), + SCHARS (file)); + } + + /* Allocate enough extra space in case we need to put a slash in + there. */ + p = SAFE_ALLOCA (SBYTES (file) + SBYTES (directory) + 2); + ptrdiff_t offset = SBYTES (directory); + memcpy (p, SSDATA (directory), offset); + if (! IS_DIRECTORY_SEP (p[offset - 1])) + p[offset++] = DIRECTORY_SEP; + memcpy (p + offset, SSDATA (file), SBYTES (file)); + p[offset + SBYTES (file)] = 0; + Lisp_Object result = build_string (p); + SAFE_FREE (); + return result; +} + /* NAME must be a string. */ static bool file_name_absolute_no_tilde_p (Lisp_Object name) @@ -6488,6 +6533,7 @@ This includes interactive calls to `delete-file' and defsubr (&Sdirectory_file_name); defsubr (&Smake_temp_file_internal); defsubr (&Smake_temp_name); + defsubr (&Sdirectory_append); defsubr (&Sexpand_file_name); defsubr (&Ssubstitute_in_file_name); defsubr (&Scopy_file); diff --git a/test/src/fileio-tests.el b/test/src/fileio-tests.el index b989c97fe6b..80afeae41ba 100644 --- a/test/src/fileio-tests.el +++ b/test/src/fileio-tests.el @@ -160,4 +160,12 @@ Also check that an encoding error can appear in a symlink." (should-error (file-exists-p "/foo\0bar") :type 'wrong-type-argument)) +(ert-deftest fileio-tests/directory-append () + (should (equal (directory-append "foo" "bar") "foo/bar")) + (should (equal (directory-append "foo/" "bar") "foo/bar")) + (should (equal (directory-append "foo//" "bar") "foo//bar")) + (should-error (directory-append "foo" "")) + (should (equal (directory-append "" "bar") "bar")) + (should-error (directory-append "" ""))) + ;;; fileio-tests.el ends here -- cgit v1.2.3 From b4543dfa9e72deeee607ffa9396a680c51a00968 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Sat, 24 Jul 2021 17:22:43 +0200 Subject: Extend directory-append to take an arbitrary number of components * doc/lispref/files.texi (Directory Names): Document it. * lisp/emacs-lisp/shortdoc.el (file-name): Add new example. * src/fileio.c (Fdirectory_append): Change the function to take an arbitrary number of components. --- doc/lispref/files.texi | 13 ++--- lisp/emacs-lisp/shortdoc.el | 1 + src/fileio.c | 116 ++++++++++++++++++++++++++++++-------------- test/src/fileio-tests.el | 8 ++- 4 files changed, 94 insertions(+), 44 deletions(-) (limited to 'test/src/fileio-tests.el') diff --git a/doc/lispref/files.texi b/doc/lispref/files.texi index ec8a2525ed9..a624c2eb937 100644 --- a/doc/lispref/files.texi +++ b/doc/lispref/files.texi @@ -2343,9 +2343,10 @@ entirely of directory separators. @end example @end defun -@defun directory-append directory filename -Combine @var{filename} with @var{directory} by optionally putting a -slash in the middle. +@defun directory-append directory &rest components +Concatenate @var{components} to @var{directory}, inserting a slash +before the components if @var{directory} or the preceding component +didn't end with a slash. @example @group @@ -2354,9 +2355,9 @@ slash in the middle. @end group @end example -This is almost the same as using @code{concat}, but @var{dirname} may -or may not end with a slash character, and this function will not -double that character. +This is almost the same as using @code{concat}, but @var{dirname} (and +the non-final components) may or may not end with slash characters, +and this function will not double those characters. @end defun To convert a directory name to its abbreviation, use this diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el index 7506d756d19..c507ad7c812 100644 --- a/lisp/emacs-lisp/shortdoc.el +++ b/lisp/emacs-lisp/shortdoc.el @@ -276,6 +276,7 @@ There can be any number of :example/:result elements." (directory-append :eval (directory-append "/tmp/" "foo") :eval (directory-append "/tmp" "foo") + :eval (directory-append "/tmp" "foo" "bar/" "zot") :eval (directory-append "/tmp" "~")) (expand-file-name :eval (expand-file-name "foo" "/tmp/") diff --git a/src/fileio.c b/src/fileio.c index 277da48315e..a4f08383776 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -749,48 +749,90 @@ For that reason, you should normally use `make-temp-file' instead. */) empty_unibyte_string, Qnil); } -DEFUN ("directory-append", Fdirectory_append, Sdirectory_append, 2, 2, 0, - doc: /* Return FILE (a string) appended to DIRECTORY (a string). -DIRECTORY may or may not end with a slash -- the return value from -this function will be the same. */) - (Lisp_Object directory, Lisp_Object file) +DEFUN ("directory-append", Fdirectory_append, Sdirectory_append, 1, MANY, 0, + doc: /* Append COMPONENTS to DIRECTORY and return the resulting string. +COMPONENTS must be a list of strings. DIRECTORY or the non-final +elements in COMPONENTS may or may not end with a slash -- if they don't +end with a slash, a slash will be inserted before contatenating. +usage: (record DIRECTORY &rest COMPONENTS) */) + (ptrdiff_t nargs, Lisp_Object *args) { - USE_SAFE_ALLOCA; - char *p; - - CHECK_STRING (file); - CHECK_STRING (directory); + ptrdiff_t chars = 0, bytes = 0, multibytes = 0; + Lisp_Object *elements = args; + Lisp_Object result; + ptrdiff_t i; + + /* First go through the list to check the types and see whether + they're all of the same multibytedness. */ + for (i = 0; i < nargs; i++) + { + Lisp_Object arg = args[i]; + CHECK_STRING (arg); + if (SCHARS (arg) == 0) + xsignal1 (Qfile_error, build_string ("Empty file name")); + /* Multibyte and non-ASCII. */ + if (STRING_MULTIBYTE (arg) && SCHARS (arg) != SBYTES (arg)) + multibytes++; + /* We're not adding a slash to the final part. */ + if (i == nargs - 1 + || IS_DIRECTORY_SEP (*(SSDATA (arg) + SBYTES (arg) - 1))) + { + bytes += SBYTES (arg); + chars += SCHARS (arg); + } + else + { + bytes += SBYTES (arg) + 1; + chars += SCHARS (arg) + 1; + } + } - if (SCHARS (file) == 0) - xsignal1 (Qfile_error, build_string ("Empty file name")); + /* Convert if needed. */ + if (multibytes != 0 && multibytes != nargs) + { + elements = xmalloc (nargs * sizeof *elements); + bytes = 0; + for (i = 0; i < nargs; i++) + { + Lisp_Object arg = args[i]; + if (STRING_MULTIBYTE (arg)) + elements[i] = arg; + else + elements[i] = make_multibyte_string (SSDATA (arg), SCHARS (arg), + SCHARS (arg)); + arg = elements[i]; + /* We have to recompute the number of bytes. */ + if (i == nargs - 1 + || IS_DIRECTORY_SEP (*(SSDATA (arg) + SBYTES (arg) - 1))) + bytes += SBYTES (arg); + else + bytes += SBYTES (arg) + 1; + } + } - if (SCHARS (directory) == 0) - return file; + /* Allocate an empty string. */ + if (multibytes == 0) + result = make_uninit_string (chars); + else + result = make_uninit_multibyte_string (chars, bytes); + /* Null-terminate the string. */ + *(SSDATA (result) + SBYTES (result)) = 0; - /* Make the strings the same multibytedness. */ - if (STRING_MULTIBYTE (file) != STRING_MULTIBYTE (directory)) + /* Copy over the data. */ + char *p = SSDATA (result); + for (i = 0; i < nargs; i++) { - if (STRING_MULTIBYTE (file)) - directory = make_multibyte_string (SSDATA (directory), - SCHARS (directory), - SCHARS (directory)); - else - file = make_multibyte_string (SSDATA (file), - SCHARS (file), - SCHARS (file)); - } - - /* Allocate enough extra space in case we need to put a slash in - there. */ - p = SAFE_ALLOCA (SBYTES (file) + SBYTES (directory) + 2); - ptrdiff_t offset = SBYTES (directory); - memcpy (p, SSDATA (directory), offset); - if (! IS_DIRECTORY_SEP (p[offset - 1])) - p[offset++] = DIRECTORY_SEP; - memcpy (p + offset, SSDATA (file), SBYTES (file)); - p[offset + SBYTES (file)] = 0; - Lisp_Object result = build_string (p); - SAFE_FREE (); + Lisp_Object arg = elements[i]; + memcpy (p, SSDATA (arg), SBYTES (arg)); + p += SBYTES (arg); + /* The last element shouldn't have a slash added at the end. */ + if (i < nargs -1 && !IS_DIRECTORY_SEP (*(p - 1))) + *p++ = DIRECTORY_SEP; + } + + if (multibytes != 0 && multibytes != nargs) + xfree (elements); + return result; } diff --git a/test/src/fileio-tests.el b/test/src/fileio-tests.el index 80afeae41ba..702659fa395 100644 --- a/test/src/fileio-tests.el +++ b/test/src/fileio-tests.el @@ -162,10 +162,16 @@ Also check that an encoding error can appear in a symlink." (ert-deftest fileio-tests/directory-append () (should (equal (directory-append "foo" "bar") "foo/bar")) + (should (equal (directory-append "foo" "bar") "foo/bar")) + (should (equal (directory-append "foo" "bar" "zot") "foo/bar/zot")) (should (equal (directory-append "foo/" "bar") "foo/bar")) (should (equal (directory-append "foo//" "bar") "foo//bar")) + (should (equal (directory-append "foo/" "bar/" "zot") "foo/bar/zot")) + (should (equal (directory-append "fóo" "bar") "fóo/bar")) + (should (equal (directory-append "foo" "bár") "foo/bár")) + (should (equal (directory-append "fóo" "bár") "fóo/bár")) (should-error (directory-append "foo" "")) - (should (equal (directory-append "" "bar") "bar")) + (should-error (directory-append "" "bar")) (should-error (directory-append "" ""))) ;;; fileio-tests.el ends here -- cgit v1.2.3 From 42d4537ed2cae41c969f59b413b4b9adae6dbc9b Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Sat, 24 Jul 2021 18:48:44 +0200 Subject: Really convert to multibyte in Fdirectory_append * src/fileio.c (Fdirectory_append): Fix check for whether we need to convert to multibyte. (Fdirectory_append): --- src/fileio.c | 3 +-- test/src/fileio-tests.el | 5 +++++ 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'test/src/fileio-tests.el') diff --git a/src/fileio.c b/src/fileio.c index 60f5650302c..6d505fd0f01 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -799,8 +799,7 @@ usage: (record DIRECTORY &rest COMPONENTS) */) if (STRING_MULTIBYTE (arg) || string_ascii_p (arg)) elements[i] = arg; else - elements[i] = make_multibyte_string (SSDATA (arg), SCHARS (arg), - SCHARS (arg)); + elements[i] = Fstring_to_multibyte (arg); arg = elements[i]; /* We have to recompute the number of bytes. */ if (i == nargs - 1 diff --git a/test/src/fileio-tests.el b/test/src/fileio-tests.el index 702659fa395..73a7775279a 100644 --- a/test/src/fileio-tests.el +++ b/test/src/fileio-tests.el @@ -170,6 +170,11 @@ Also check that an encoding error can appear in a symlink." (should (equal (directory-append "fóo" "bar") "fóo/bar")) (should (equal (directory-append "foo" "bár") "foo/bár")) (should (equal (directory-append "fóo" "bár") "fóo/bár")) + (let ((string (make-string 5 ?a))) + (should (not (multibyte-string-p string))) + (aset string 2 255) + (should (not (multibyte-string-p string))) + (should (equal (directory-append "fóo" string) "fóo/aa\377aa"))) (should-error (directory-append "foo" "")) (should-error (directory-append "" "bar")) (should-error (directory-append "" ""))) -- cgit v1.2.3 From aa9cba658768aba4da1b74ffb33d9962ffff5756 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Sun, 25 Jul 2021 08:00:50 +0200 Subject: Allow empty elements in directory-append * doc/lispref/files.texi (Directory Names): Document it. * src/fileio.c (Fdirectory_append): Allow empty elements. --- doc/lispref/files.texi | 4 +++- src/fileio.c | 46 +++++++++++++++++++++++++++++++++------------- test/src/fileio-tests.el | 9 ++++++--- 3 files changed, 42 insertions(+), 17 deletions(-) (limited to 'test/src/fileio-tests.el') diff --git a/doc/lispref/files.texi b/doc/lispref/files.texi index 804cef292ee..e7a0ad2d06c 100644 --- a/doc/lispref/files.texi +++ b/doc/lispref/files.texi @@ -2355,7 +2355,9 @@ didn't end with a slash. @end group @end example -A zero-length directory or component is not allowed. +A @var{directory} or components that are @code{nil} or the empty +string are ignored---they are filtered out first and do not affect the +results in any way. This is almost the same as using @code{concat}, but @var{dirname} (and the non-final components) may or may not end with slash characters, diff --git a/src/fileio.c b/src/fileio.c index d6b3e7bca40..3d8b082a59c 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -751,14 +751,14 @@ For that reason, you should normally use `make-temp-file' instead. */) DEFUN ("directory-append", Fdirectory_append, Sdirectory_append, 1, MANY, 0, doc: /* Append COMPONENTS to DIRECTORY and return the resulting string. -COMPONENTS must be strings. +Elements in COMPONENTS must be a string or nil. DIRECTORY or the non-final elements in COMPONENTS may or may not end with a slash -- if they don't end with a slash, a slash will be inserted before contatenating. usage: (record DIRECTORY &rest COMPONENTS) */) (ptrdiff_t nargs, Lisp_Object *args) { - ptrdiff_t chars = 0, bytes = 0, multibytes = 0; + ptrdiff_t chars = 0, bytes = 0, multibytes = 0, eargs = 0; Lisp_Object *elements = args; Lisp_Object result; ptrdiff_t i; @@ -768,9 +768,13 @@ usage: (record DIRECTORY &rest COMPONENTS) */) for (i = 0; i < nargs; i++) { Lisp_Object arg = args[i]; + /* Skip empty and nil elements. */ + if (NILP (arg)) + continue; CHECK_STRING (arg); if (SCHARS (arg) == 0) - xsignal1 (Qfile_error, build_string ("Empty file name")); + continue; + eargs++; /* Multibyte and non-ASCII. */ if (STRING_MULTIBYTE (arg) && SCHARS (arg) != SBYTES (arg)) multibytes++; @@ -789,25 +793,41 @@ usage: (record DIRECTORY &rest COMPONENTS) */) } /* Convert if needed. */ - if (multibytes != 0 && multibytes != nargs) + if ((multibytes != 0 && multibytes != nargs) + || eargs != nargs) { - elements = xmalloc (nargs * sizeof *elements); + int j = 0; + elements = xmalloc (eargs * sizeof *elements); bytes = 0; + chars = 0; + + /* Filter out nil/"". */ for (i = 0; i < nargs; i++) { Lisp_Object arg = args[i]; + if (!NILP (arg) && SCHARS (arg) != 0) + elements[j++] = arg; + } + + for (i = 0; i < eargs; i++) + { + Lisp_Object arg = elements[i]; /* Use multibyte or all-ASCII strings as is. */ - if (STRING_MULTIBYTE (arg) || string_ascii_p (arg)) - elements[i] = arg; - else + if (!STRING_MULTIBYTE (arg) && !string_ascii_p (arg)) elements[i] = Fstring_to_multibyte (arg); arg = elements[i]; /* We have to recompute the number of bytes. */ - if (i == nargs - 1 + if (i == eargs - 1 || IS_DIRECTORY_SEP (*(SSDATA (arg) + SBYTES (arg) - 1))) - bytes += SBYTES (arg); + { + bytes += SBYTES (arg); + chars += SCHARS (arg); + } else - bytes += SBYTES (arg) + 1; + { + bytes += SBYTES (arg) + 1; + chars += SCHARS (arg) + 1; + } } } @@ -821,13 +841,13 @@ usage: (record DIRECTORY &rest COMPONENTS) */) /* Copy over the data. */ char *p = SSDATA (result); - for (i = 0; i < nargs; i++) + for (i = 0; i < eargs; i++) { Lisp_Object arg = elements[i]; memcpy (p, SSDATA (arg), SBYTES (arg)); p += SBYTES (arg); /* The last element shouldn't have a slash added at the end. */ - if (i < nargs - 1 && !IS_DIRECTORY_SEP (*(p - 1))) + if (i < eargs - 1 && !IS_DIRECTORY_SEP (*(p - 1))) *p++ = DIRECTORY_SEP; } diff --git a/test/src/fileio-tests.el b/test/src/fileio-tests.el index 73a7775279a..b1288f943e3 100644 --- a/test/src/fileio-tests.el +++ b/test/src/fileio-tests.el @@ -175,8 +175,11 @@ Also check that an encoding error can appear in a symlink." (aset string 2 255) (should (not (multibyte-string-p string))) (should (equal (directory-append "fóo" string) "fóo/aa\377aa"))) - (should-error (directory-append "foo" "")) - (should-error (directory-append "" "bar")) - (should-error (directory-append "" ""))) + (should (equal (directory-append "foo") "foo")) + (should (equal (directory-append "foo/") "foo/")) + (should (equal (directory-append "foo" "") "foo")) + (should (equal (directory-append "foo" "" "" "" nil) "foo")) + (should (equal (directory-append "" "bar") "bar")) + (should (equal (directory-append "" "") ""))) ;;; fileio-tests.el ends here -- cgit v1.2.3 From f04f8126f016b43c45d432bf353ba2a0ac8f7d96 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Sun, 25 Jul 2021 08:54:20 +0200 Subject: Rename directory-append to file-name-concat * src/fileio.c (Ffile_name_concat): * lisp/files.el (move-file-to-trash): * lisp/emacs-lisp/shortdoc.el (file-name): * doc/lispref/files.texi (Directory Names): Rename `directory-append' to `file-name-concat'. --- doc/lispref/files.texi | 4 ++-- lisp/emacs-lisp/shortdoc.el | 10 +++++----- lisp/files.el | 10 +++++----- src/fileio.c | 4 ++-- test/src/fileio-tests.el | 34 +++++++++++++++++----------------- 5 files changed, 31 insertions(+), 31 deletions(-) (limited to 'test/src/fileio-tests.el') diff --git a/doc/lispref/files.texi b/doc/lispref/files.texi index e7a0ad2d06c..266501d46d0 100644 --- a/doc/lispref/files.texi +++ b/doc/lispref/files.texi @@ -2343,14 +2343,14 @@ entirely of directory separators. @end example @end defun -@defun directory-append directory &rest components +@defun file-name-concat directory &rest components Concatenate @var{components} to @var{directory}, inserting a slash before the components if @var{directory} or the preceding component didn't end with a slash. @example @group -(directory-append "/tmp" "foo") +(file-name-concat "/tmp" "foo") @result{} "/tmp/foo" @end group @end example diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el index c507ad7c812..a74a5a4225c 100644 --- a/lisp/emacs-lisp/shortdoc.el +++ b/lisp/emacs-lisp/shortdoc.el @@ -273,11 +273,11 @@ There can be any number of :example/:result elements." :eval (file-relative-name "/tmp/foo" "/tmp")) (make-temp-name :eval (make-temp-name "/tmp/foo-")) - (directory-append - :eval (directory-append "/tmp/" "foo") - :eval (directory-append "/tmp" "foo") - :eval (directory-append "/tmp" "foo" "bar/" "zot") - :eval (directory-append "/tmp" "~")) + (file-name-concat + :eval (file-name-concat "/tmp/" "foo") + :eval (file-name-concat "/tmp" "foo") + :eval (file-name-concat "/tmp" "foo" "bar/" "zot") + :eval (file-name-concat "/tmp" "~")) (expand-file-name :eval (expand-file-name "foo" "/tmp/") :eval (expand-file-name "foo" "/tmp///") diff --git a/lisp/files.el b/lisp/files.el index 78b76592bd0..60888226865 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -8127,14 +8127,14 @@ Otherwise, trash FILENAME using the freedesktop.org conventions, ;; exists, but the file name may exist in the trash ;; directory even if there is no info file for it. (when (file-exists-p - (directory-append trash-files-dir files-base)) + (file-name-concat trash-files-dir files-base)) (setq overwrite t files-base (file-name-nondirectory (make-temp-file - (directory-append + (file-name-concat trash-files-dir files-base) is-directory)))) - (setq info-fn (directory-append + (setq info-fn (file-name-concat trash-info-dir (concat files-base ".trashinfo"))) ;; Re-check the existence (sort of). @@ -8145,14 +8145,14 @@ Otherwise, trash FILENAME using the freedesktop.org conventions, ;; like Emacs-style backup file names. E.g.: ;; https://bugs.kde.org/170956 (setq info-fn (make-temp-file - (directory-append trash-info-dir files-base) + (file-name-concat trash-info-dir files-base) nil ".trashinfo")) (setq files-base (substring (file-name-nondirectory info-fn) 0 (- (length ".trashinfo")))) (write-region nil nil info-fn nil 'quiet info-fn))) ;; Finally, try to move the file to the trashcan. (let ((delete-by-moving-to-trash nil) - (new-fn (directory-append trash-files-dir files-base))) + (new-fn (file-name-concat trash-files-dir files-base))) (rename-file fn new-fn overwrite))))))))) (defsubst file-attribute-type (attributes) diff --git a/src/fileio.c b/src/fileio.c index 3d8b082a59c..13c99bee109 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -749,7 +749,7 @@ For that reason, you should normally use `make-temp-file' instead. */) empty_unibyte_string, Qnil); } -DEFUN ("directory-append", Fdirectory_append, Sdirectory_append, 1, MANY, 0, +DEFUN ("file-name-concat", Ffile_name_concat, Sfile_name_concat, 1, MANY, 0, doc: /* Append COMPONENTS to DIRECTORY and return the resulting string. Elements in COMPONENTS must be a string or nil. DIRECTORY or the non-final elements in COMPONENTS may or may not end @@ -6596,7 +6596,7 @@ This includes interactive calls to `delete-file' and defsubr (&Sdirectory_file_name); defsubr (&Smake_temp_file_internal); defsubr (&Smake_temp_name); - defsubr (&Sdirectory_append); + defsubr (&Sfile_name_concat); defsubr (&Sexpand_file_name); defsubr (&Ssubstitute_in_file_name); defsubr (&Scopy_file); diff --git a/test/src/fileio-tests.el b/test/src/fileio-tests.el index b1288f943e3..f4d123b4261 100644 --- a/test/src/fileio-tests.el +++ b/test/src/fileio-tests.el @@ -160,26 +160,26 @@ Also check that an encoding error can appear in a symlink." (should-error (file-exists-p "/foo\0bar") :type 'wrong-type-argument)) -(ert-deftest fileio-tests/directory-append () - (should (equal (directory-append "foo" "bar") "foo/bar")) - (should (equal (directory-append "foo" "bar") "foo/bar")) - (should (equal (directory-append "foo" "bar" "zot") "foo/bar/zot")) - (should (equal (directory-append "foo/" "bar") "foo/bar")) - (should (equal (directory-append "foo//" "bar") "foo//bar")) - (should (equal (directory-append "foo/" "bar/" "zot") "foo/bar/zot")) - (should (equal (directory-append "fóo" "bar") "fóo/bar")) - (should (equal (directory-append "foo" "bár") "foo/bár")) - (should (equal (directory-append "fóo" "bár") "fóo/bár")) +(ert-deftest fileio-tests/file-name-concat () + (should (equal (file-name-concat "foo" "bar") "foo/bar")) + (should (equal (file-name-concat "foo" "bar") "foo/bar")) + (should (equal (file-name-concat "foo" "bar" "zot") "foo/bar/zot")) + (should (equal (file-name-concat "foo/" "bar") "foo/bar")) + (should (equal (file-name-concat "foo//" "bar") "foo//bar")) + (should (equal (file-name-concat "foo/" "bar/" "zot") "foo/bar/zot")) + (should (equal (file-name-concat "fóo" "bar") "fóo/bar")) + (should (equal (file-name-concat "foo" "bár") "foo/bár")) + (should (equal (file-name-concat "fóo" "bár") "fóo/bár")) (let ((string (make-string 5 ?a))) (should (not (multibyte-string-p string))) (aset string 2 255) (should (not (multibyte-string-p string))) - (should (equal (directory-append "fóo" string) "fóo/aa\377aa"))) - (should (equal (directory-append "foo") "foo")) - (should (equal (directory-append "foo/") "foo/")) - (should (equal (directory-append "foo" "") "foo")) - (should (equal (directory-append "foo" "" "" "" nil) "foo")) - (should (equal (directory-append "" "bar") "bar")) - (should (equal (directory-append "" "") ""))) + (should (equal (file-name-concat "fóo" string) "fóo/aa\377aa"))) + (should (equal (file-name-concat "foo") "foo")) + (should (equal (file-name-concat "foo/") "foo/")) + (should (equal (file-name-concat "foo" "") "foo")) + (should (equal (file-name-concat "foo" "" "" "" nil) "foo")) + (should (equal (file-name-concat "" "bar") "bar")) + (should (equal (file-name-concat "" "") ""))) ;;; fileio-tests.el ends here -- cgit v1.2.3