diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2018-01-12 16:49:43 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2018-01-12 16:49:43 -0700 |
commit | c7c41aa8253326bff6f0522edcbab19ea1a25d63 (patch) | |
tree | 635330c3123cc08585930ec52782dd52c55fd380 | |
parent | 9f8f0f7a8a882ad3a04aabdf17c4269f405b364d (diff) | |
parent | de6d4d40ddc844eee643e92d47b9d6a63fbebb48 (diff) |
Merge tag 'v0.20.0'
-rw-r--r-- | CHANGELOG.md | 125 | ||||
-rw-r--r-- | README.md | 144 | ||||
-rw-r--r-- | README.md.tpl | 141 | ||||
-rw-r--r-- | f.el | 47 | ||||
-rw-r--r-- | test/f-destructive-test.el | 8 | ||||
-rw-r--r-- | test/f-paths-test.el | 8 | ||||
-rw-r--r-- | test/f-predicates-test.el | 23 |
7 files changed, 227 insertions, 269 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..9d7b914 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,125 @@ + +## Changelog + +### v0.19.0 + +* Remove deprecated `f-up` function, use `f-traverse-upwards` instead +* Add `f-append-text` and `f-append-bytes` +* Add `f-hidden?` + +### v0.18.0 + +* Add `f-swap-ext` (by @phillord) +* Add `f-depth` (by @cheunghy) + +### v0.17.0 + +* Add `f-common-parent` (by @Fuco1) + +### v0.16.0 + +* Add `f-with-sandbox` + +### v0.15.0 + +* Add `f-split` + +### v0.14.0 + +* Add `f-traverse-upwards` and its anaphoric version `f--traverse-upwards` +* Deprecate `f-up` and its anaphoric version `f--up` + +### v0.13.0 + +* Add `f-uniquify` and `f-uniquify-alist` (by @Fuco1) + +### v0.12.0 + +* `f-parent` returns nil if argument is root + +### v0.11.0 + +* Add `f-descendant-of?` +* Add `f-ancestor-of?` +* Add `f-parent-of?` +* Add `f-child-of?` +* Remove deprecation for `f-read` and `f-write` and make them aliases + to `f-read-text` and `f-write-text` respectively +* Add anaphoric function `f--entries` for `f-entries` +* Add anaphoric function `f--files` for `f-files` +* Add anaphoric function `f--directories` of `f-directories` +* Add `f-up` and anaphoric version `f--up` + +### v0.10.0 + +* Add `f-root` +* Fix `f-root?` bug for weird syntax + +### v0.9.0 + +* Make `s-long` +* Make `s-short` default and `f-abbrev` the alias +* Add `f-full` +* Do not append path separator if file in `f-slash` +* Fixed bug in `f-path-separator` + +### v0.8.0 + +* Moved `f-this-file` to misc section +* Add `f-slash` +* Add `f-path-separator` + +### v0.7.1 + +* Fix coding bug in `f-read-text` + +### v0.7.0 + +* Add `f-touch` + +### v0.6.1 + +* Fix `f-write-text` for unibyte strings + +### v0.6.0 + +* Add `f-write-text` and `f-write-bytes` and deprecate `f-write` +* Add `f-read-text` and `f-read-bytes` and deprecate `f-read` +* Add `f-this-file` +* Add `f-canonical` +* Fix `f-same?` for symlinks + +### v0.5.0 + +* Add `f-same?` (alias `f-equal?`) + +### v0.4.1 + +* Bump `s` and `dash` versions + +### v0.4.0 + +* Add `f-copy` + +### v0.3.0 + +* Add `f-ext?` + +### v0.2.1 + +* Fix `f-filename` when ending with slash + +### v0.2.0 + +* Add `f-root?` +* Fix `f-dirname` when ending with slash + +### v0.1.0 + +* Add `f-abbrev` (alias `f-short`) + +### v0.0.2 + +* `f-join` platform independent + +### v0.0.1 @@ -26,7 +26,7 @@ Or you can just dump `f.el` in your load path somewhere. * [f-common-parent](#f-common-parent-paths) `(paths)` * [f-ext](#f-ext-path) `(path)` * [f-no-ext](#f-no-ext-path) `(path)` -* [f-swap-ext](#f-swap-ext) `(path ext)` +* [f-swap-ext](#f-swap-ext-path-ext) `(path ext)` * [f-base](#f-base-path) `(path)` * [f-relative](#f-relative-path-optional-dir) `(path &optional dir)` * [f-short](#f-short-path) `(path)` @@ -75,6 +75,7 @@ Or you can just dump `f.el` in your load path somewhere. * [f-ancestor-of?](#f-ancestor-of-path-a-path-b) `(path-a path-b)` * [f-descendant-of?](#f-descendant-of-path-a-path-b) `(path-a path-b)` * [f-hidden?](#f-hidden-path) `(path)` +* [f-empty?](#f-empty-path) `(path)` ### Stats @@ -616,6 +617,22 @@ Return t if PATH is hidden, nil otherwise. (f-hidden? "/path/to/.foo") ;; => t ``` +Alias: `f-hidden-p` + +### f-empty? `(path)` + +If PATH is a file, return t if the file in PATH is empty, nil otherwise. +If PATH is directory, return t if directory has no files, nil otherwise. + +```lisp +(f-empty? "/path/to/empty-file") ;; => t +(f-empty? "/path/to/file-with-contents") ;; => nil +(f-empty? "/path/to/empty-dir/") ;; => t +(f-empty? "/path/to/dir-with-contents/") ;; => nil +``` + +Alias: `f-empty-p` + ### f-size `(path)` Return size of PATH. @@ -745,131 +762,6 @@ Only allow PATH-OR-PATHS and decendants to be modified in BODY. (f-touch (f-expand "bar" bar-path))) ;; "Destructive operation outside sandbox" ``` -## Changelog - -### v0.19.0 - -* Remove deprecated `f-up` function, use `f-traverse-upwards` instead -* Add `f-append-text` and `f-append-bytes` -* Add `f-hidden?` - -### v0.18.0 - -* Add `f-swap-ext` (by @phillord) -* Add `f-depth` (by @cheunghy) - -### v0.17.0 - -* Add `f-common-parent` (by @Fuco1) - -### v0.16.0 - -* Add `f-with-sandbox` - -### v0.15.0 - -* Add `f-split` - -### v0.14.0 - -* Add `f-traverse-upwards` and its anaphoric version `f--traverse-upwards` -* Deprecate `f-up` and its anaphoric version `f--up` - -### v0.13.0 - -* Add `f-uniquify` and `f-uniquify-alist` (by @Fuco1) - -### v0.12.0 - -* `f-parent` returns nil if argument is root - -### v0.11.0 - -* Add `f-descendant-of?` -* Add `f-ancestor-of?` -* Add `f-parent-of?` -* Add `f-child-of?` -* Remove deprecation for `f-read` and `f-write` and make them aliases - to `f-read-text` and `f-write-text` respectively -* Add anaphoric function `f--entries` for `f-entries` -* Add anaphoric function `f--files` for `f-files` -* Add anaphoric function `f--directories` of `f-directories` -* Add `f-up` and anaphoric version `f--up` - -### v0.10.0 - -* Add `f-root` -* Fix `f-root?` bug for weird syntax - -### v0.9.0 - -* Make `s-long` -* Make `s-short` default and `f-abbrev` the alias -* Add `f-full` -* Do not append path separator if file in `f-slash` -* Fixed bug in `f-path-separator` - -### v0.8.0 - -* Moved `f-this-file` to misc section -* Add `f-slash` -* Add `f-path-separator` - -### v0.7.1 - -* Fix coding bug in `f-read-text` - -### v0.7.0 - -* Add `f-touch` - -### v0.6.1 - -* Fix `f-write-text` for unibyte strings - -### v0.6.0 - -* Add `f-write-text` and `f-write-bytes` and deprecate `f-write` -* Add `f-read-text` and `f-read-bytes` and deprecate `f-read` -* Add `f-this-file` -* Add `f-canonical` -* Fix `f-same?` for symlinks - -### v0.5.0 - -* Add `f-same?` (alias `f-equal?`) - -### v0.4.1 - -* Bump `s` and `dash` versions - -### v0.4.0 - -* Add `f-copy` - -### v0.3.0 - -* Add `f-ext?` - -### v0.2.1 - -* Fix `f-filename` when ending with slash - -### v0.2.0 - -* Add `f-root?` -* Fix `f-dirname` when ending with slash - -### v0.1.0 - -* Add `f-abbrev` (alias `f-short`) - -### v0.0.2 - -* `f-join` platform independent - -### v0.0.1 - ## Example Here's an example of a function that finds the Git project root. diff --git a/README.md.tpl b/README.md.tpl index a5f6eea..bfd2e45 100644 --- a/README.md.tpl +++ b/README.md.tpl @@ -75,6 +75,7 @@ Or you can just dump `f.el` in your load path somewhere. * [f-ancestor-of?](#f-ancestor-of-path-a-path-b) `(path-a path-b)` * [f-descendant-of?](#f-descendant-of-path-a-path-b) `(path-a path-b)` * [f-hidden?](#f-hidden-path) `(path)` +* [f-empty?](#f-empty-path) `(path)` ### Stats @@ -582,6 +583,21 @@ Alias: `f-descendant-of-p` (f-hidden? "/path/to/.foo") ;; => t ``` +Alias: `f-hidden-p` + +### f-empty? `(path)` + +{{f-empty?}} + +```lisp +(f-empty? "/path/to/empty-file") ;; => t +(f-empty? "/path/to/file-with-contents") ;; => nil +(f-empty? "/path/to/empty-dir/") ;; => t +(f-empty? "/path/to/dir-with-contents/") ;; => nil +``` + +Alias: `f-empty-p` + ### f-size `(path)` {{f-size}} @@ -696,131 +712,6 @@ See: `file-expand-wildcards` (f-touch (f-expand "bar" bar-path))) ;; "Destructive operation outside sandbox" ``` -## Changelog - -### v0.19.0 - -* Remove deprecated `f-up` function, use `f-traverse-upwards` instead -* Add `f-append-text` and `f-append-bytes` -* Add `f-hidden?` - -### v0.18.0 - -* Add `f-swap-ext` (by @phillord) -* Add `f-depth` (by @cheunghy) - -### v0.17.0 - -* Add `f-common-parent` (by @Fuco1) - -### v0.16.0 - -* Add `f-with-sandbox` - -### v0.15.0 - -* Add `f-split` - -### v0.14.0 - -* Add `f-traverse-upwards` and its anaphoric version `f--traverse-upwards` -* Deprecate `f-up` and its anaphoric version `f--up` - -### v0.13.0 - -* Add `f-uniquify` and `f-uniquify-alist` (by @Fuco1) - -### v0.12.0 - -* `f-parent` returns nil if argument is root - -### v0.11.0 - -* Add `f-descendant-of?` -* Add `f-ancestor-of?` -* Add `f-parent-of?` -* Add `f-child-of?` -* Remove deprecation for `f-read` and `f-write` and make them aliases - to `f-read-text` and `f-write-text` respectively -* Add anaphoric function `f--entries` for `f-entries` -* Add anaphoric function `f--files` for `f-files` -* Add anaphoric function `f--directories` of `f-directories` -* Add `f-up` and anaphoric version `f--up` - -### v0.10.0 - -* Add `f-root` -* Fix `f-root?` bug for weird syntax - -### v0.9.0 - -* Make `s-long` -* Make `s-short` default and `f-abbrev` the alias -* Add `f-full` -* Do not append path separator if file in `f-slash` -* Fixed bug in `f-path-separator` - -### v0.8.0 - -* Moved `f-this-file` to misc section -* Add `f-slash` -* Add `f-path-separator` - -### v0.7.1 - -* Fix coding bug in `f-read-text` - -### v0.7.0 - -* Add `f-touch` - -### v0.6.1 - -* Fix `f-write-text` for unibyte strings - -### v0.6.0 - -* Add `f-write-text` and `f-write-bytes` and deprecate `f-write` -* Add `f-read-text` and `f-read-bytes` and deprecate `f-read` -* Add `f-this-file` -* Add `f-canonical` -* Fix `f-same?` for symlinks - -### v0.5.0 - -* Add `f-same?` (alias `f-equal?`) - -### v0.4.1 - -* Bump `s` and `dash` versions - -### v0.4.0 - -* Add `f-copy` - -### v0.3.0 - -* Add `f-ext?` - -### v0.2.1 - -* Fix `f-filename` when ending with slash - -### v0.2.0 - -* Add `f-root?` -* Fix `f-dirname` when ending with slash - -### v0.1.0 - -* Add `f-abbrev` (alias `f-short`) - -### v0.0.2 - -* `f-join` platform independent - -### v0.0.1 - ## Example Here's an example of a function that finds the Git project root. @@ -4,7 +4,7 @@ ;; Author: Johan Andersson <johan.rejeep@gmail.com> ;; Maintainer: Johan Andersson <johan.rejeep@gmail.com> -;; Version: 0.19.0 +;; Version: 0.20.0 ;; Keywords: files, directories ;; URL: http://github.com/rejeep/f.el ;; Package-Requires: ((s "1.7.0") (dash "2.2.0")) @@ -75,9 +75,13 @@ If PATH is not allowed to be modified, throw error." parts))) (defun f-expand (path &optional dir) - "Expand PATH relative to DIR (or `default-directory')." + "Expand PATH relative to DIR (or `default-directory'). +PATH and DIR can be either a directory names or directory file +names. Return a directory name if PATH is a directory name, and +a directory file name otherwise. File name handlers are +ignored." (let (file-name-handler-alist) - (directory-file-name (expand-file-name path dir)))) + (expand-file-name path dir))) (defun f-filename (path) "Return the name of PATH." @@ -86,7 +90,8 @@ If PATH is not allowed to be modified, throw error." (defalias 'f-parent 'f-dirname) (defun f-dirname (path) "Return the parent directory to PATH." - (let ((parent (file-name-directory (f-expand path default-directory)))) + (let ((parent (file-name-directory + (directory-file-name (f-expand path default-directory))))) (unless (f-same? path parent) (if (f-relative? path) (f-relative parent) @@ -284,11 +289,14 @@ If FORCE is t, a directory will be deleted recursively." (f--destructive path (make-symbolic-link source path))) (defun f-move (from to) - "Move or rename FROM to TO." + "Move or rename FROM to TO. +If TO is a directory name, move FROM into TO." (f--destructive to (rename-file from to t))) (defun f-copy (from to) - "Copy file or directory FROM to TO." + "Copy file or directory FROM to TO. +If FROM names a directory and TO is a directory name, copy FROM +into TO as a subdirectory." (f--destructive to (if (f-file? from) (copy-file from to) @@ -312,7 +320,7 @@ If FORCE is t, a directory will be deleted recursively." (unless (f-dir? from) (error "Cannot copy contents as %s is a file" from)) (--each (f-entries from) - (f-copy it to))) + (f-copy it (file-name-as-directory to)))) (defun f-touch (path) "Update PATH last modification date or create if it does not exist." @@ -409,8 +417,8 @@ The extension, in a file name, is the part that follows the last (when (and (f-exists? path-a) (f-exists? path-b)) (equal - (f-canonical (f-expand path-a)) - (f-canonical (f-expand path-b))))) + (f-canonical (directory-file-name (f-expand path-a))) + (f-canonical (directory-file-name (f-expand path-b)))))) (defalias 'f-same-p 'f-same?) @@ -450,6 +458,17 @@ The extension, in a file name, is the part that follows the last (error "Path does not exist: %s" path)) (string= (substring path 0 1) ".")) +(defalias 'f-hidden-p 'f-hidden?) + +(defun f-empty? (path) + "If PATH is a file, return t if the file in PATH is empty, nil otherwise. +If PATH is directory, return t if directory has no files, nil otherwise." + (if (f-directory? path) + (equal (f-files path nil t) nil) + (= (f-size path) 0))) + +(defalias 'f-empty-p 'f-empty?) + ;;;; Stats @@ -481,9 +500,13 @@ detect the depth. byte-compile-current-file) (:else (buffer-file-name)))) +(defvar f--path-separator nil + "A variable to cache result of `f-path-separator'.") + (defun f-path-separator () "Return path separator." - (substring (f-join "x" "y") 1 2)) + (or f--path-separator + (setq f--path-separator (substring (f-join "x" "y") 1 2)))) (defun f-glob (pattern &optional path) "Find PATTERN in PATH." @@ -539,7 +562,7 @@ RECURSIVE - Search for files and directories recursive." ,recursive)) (defun f-directories (path &optional fn recursive) - "Find all directories in PATH. See `f-entries`." + "Find all directories in PATH. See `f-entries'." (let ((directories (-select 'f-directory? (f--collect-entries path recursive)))) (if fn (-select fn directories) directories))) @@ -553,7 +576,7 @@ RECURSIVE - Search for files and directories recursive." ,recursive)) (defun f-files (path &optional fn recursive) - "Find all files in PATH. See `f-entries`." + "Find all files in PATH. See `f-entries'." (let ((files (-select 'f-file? (f--collect-entries path recursive)))) (if fn (-select fn files) files))) diff --git a/test/f-destructive-test.el b/test/f-destructive-test.el index 91e6317..8bed490 100644 --- a/test/f-destructive-test.el +++ b/test/f-destructive-test.el @@ -103,7 +103,7 @@ (with-playground (f-touch "foo.txt") (f-mkdir "bar") - (f-move "foo.txt" "bar") + (f-move "foo.txt" "bar/") (should-exist "bar/foo.txt"))) (ert-deftest f-move-test/move-absolute-path () @@ -112,7 +112,7 @@ (f-mkdir "bar") (f-move (f-expand "foo.txt" f-test/playground-path) - (f-expand "bar" f-test/playground-path)) + (file-name-as-directory (f-expand "bar" f-test/playground-path))) (should-exist "bar/foo.txt"))) (ert-deftest f-move-test/rename-relative-path () @@ -153,7 +153,7 @@ (f-mkdir "foo") (f-mkdir "bar") (f-write "FILE" 'utf-8 "foo/file.txt") - (f-copy "foo" "bar") + (f-copy "foo" "bar/") (should-exist "foo/file.txt" "FILE") (should-exist "bar/foo/file.txt" "FILE"))) @@ -172,7 +172,7 @@ (f-write "FILE" 'utf-8 "foo/file.txt") (f-copy (f-expand "foo" f-test/playground-path) - (f-expand "bar" f-test/playground-path)) + (file-name-as-directory (f-expand "bar" f-test/playground-path))) (should-exist "foo/file.txt" "FILE") (should-exist "bar/foo/file.txt" "FILE"))) diff --git a/test/f-paths-test.el b/test/f-paths-test.el index f6d6418..12852d2 100644 --- a/test/f-paths-test.el +++ b/test/f-paths-test.el @@ -82,6 +82,10 @@ ;; exception, hence this will fail. (f-expand "foo:" "/")) +(ert-deftest f-expand-test/directory-name () + (with-default-directory + (should (equal (f-expand "foo/" "/other/") "/other/foo/")))) + ;;;; f-filename @@ -117,7 +121,7 @@ (should (equal (f-dirname (f-expand "foo/bar/baz" f-test/playground-path)) - (f-expand "foo/bar/" f-test/playground-path))))) + (f-expand "foo/bar" f-test/playground-path))))) (ert-deftest f-dirname-test/file-absolute () (with-playground @@ -126,7 +130,7 @@ (should (equal (f-dirname (f-expand "foo/bar/baz/qux.txt" f-test/playground-path)) - (f-expand "foo/bar/baz/" f-test/playground-path))))) + (f-expand "foo/bar/baz" f-test/playground-path))))) (ert-deftest f-dirname-test/file-with-ending-slash () (with-playground diff --git a/test/f-predicates-test.el b/test/f-predicates-test.el index c1fcf3d..6a5c64d 100644 --- a/test/f-predicates-test.el +++ b/test/f-predicates-test.el @@ -416,6 +416,29 @@ (should-not (f-hidden? "foo")) (should-not (f-hidden? "bar")))) +;;; f-empty? + +(ert-deftest f-empty?-test/empty-file-is-empty () + (with-playground + (f-write-text "" 'utf-8 "foo.txt") + (should (f-empty? "foo.txt")))) + +(ert-deftest f-empty?-test/file-containing-text-is--not-empty () + (with-playground + (f-write-text "hello" 'utf-8 "derp.txt") + (should-not (f-empty? "derp.txt")))) + +(ert-deftest f-empty?-test/empty-directory-is-empty () + (with-playground + (f-mkdir "bar") + (should (f-empty? "bar")))) + +(ert-deftest f-empty?-test/directory-containing-files-is--not-empty () + (with-playground + (f-mkdir "bar") + (f-write-text "hello" 'utf-8 "bar/derp.txt") + (should-not (f-empty? "bar")))) + (provide 'f-predicates-test) ;;; f-predicates-test.el ends here |