From 7acf83ea6d6f36e418f43a3408632944fbb7bcde Mon Sep 17 00:00:00 2001 From: ulteq Date: Thu, 18 Jan 2018 11:07:45 +0100 Subject: Add natural sort of (version) numbers --- src/feh.h | 1 + src/filelist.c | 19 +++++++++++++++---- src/help.raw | 1 + src/options.c | 4 ++++ src/options.h | 1 + 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/feh.h b/src/feh.h index a4a0a7b..da08aba 100644 --- a/src/feh.h +++ b/src/feh.h @@ -27,6 +27,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #ifndef FEH_H #define FEH_H +#define _GNU_SOURCE #include #include #include diff --git a/src/filelist.c b/src/filelist.c index b569b8a..3157e32 100644 --- a/src/filelist.c +++ b/src/filelist.c @@ -399,12 +399,18 @@ void feh_file_dirname(char *dst, feh_file * f, int maxlen) int feh_cmp_filename(void *file1, void *file2) { - return(strcmp(FEH_FILE(file1)->filename, FEH_FILE(file2)->filename)); + if (!opt.version_sort) + return(strcmp(FEH_FILE(file1)->filename, FEH_FILE(file2)->filename)); + else + return(strverscmp(FEH_FILE(file1)->filename, FEH_FILE(file2)->filename)); } int feh_cmp_name(void *file1, void *file2) { - return(strcmp(FEH_FILE(file1)->name, FEH_FILE(file2)->name)); + if (!opt.version_sort) + return(strcmp(FEH_FILE(file1)->name, FEH_FILE(file2)->name)); + else + return(strverscmp(FEH_FILE(file1)->name, FEH_FILE(file2)->name)); } int feh_cmp_dirname(void *file1, void *file2) @@ -413,8 +419,13 @@ int feh_cmp_dirname(void *file1, void *file2) int cmp; feh_file_dirname(dir1, FEH_FILE(file1), PATH_MAX); feh_file_dirname(dir2, FEH_FILE(file2), PATH_MAX); - if ((cmp = strcmp(dir1, dir2)) != 0) - return(cmp); + if (!opt.version_sort) { + if ((cmp = strcmp(dir1, dir2)) != 0) + return(cmp); + } else { + if ((cmp = strverscmp(dir1, dir2)) != 0) + return(cmp); + } return(feh_cmp_name(file1, file2)); } diff --git a/src/help.raw b/src/help.raw index 37e0e71..8c83b5b 100644 --- a/src/help.raw +++ b/src/help.raw @@ -52,6 +52,7 @@ OPTIONS name, filename, mtime, width, height, pixels, size, or format -n, --reverse Reverse sort order + --version-sort Natural sort of (version) numbers within text -A, --action [;]ACTION Specify action to perform when pressing . Executed by /bin/sh, may contain FORMAT SPECIFIERS reloads image with \";\", switches to next otherwise diff --git a/src/options.c b/src/options.c index c874832..3fcbdef 100644 --- a/src/options.c +++ b/src/options.c @@ -412,6 +412,7 @@ static void feh_parse_option_array(int argc, char **argv, int finalrun) {"insecure" , 0, 0, 240}, {"no-recursive" , 0, 0, 241}, {"cache-size" , 1, 0, 243}, + {"version-sort" , 0, 0, 246}, {0, 0, 0, 0} }; int optch = 0, cmdx = 0; @@ -782,6 +783,9 @@ static void feh_parse_option_array(int argc, char **argv, int finalrun) if (opt.cache_size > 2048) opt.cache_size = 2048; break; + case 246: + opt.version_sort = 1; + break; default: break; } diff --git a/src/options.h b/src/options.h index c6959c8..277cede 100644 --- a/src/options.h +++ b/src/options.h @@ -103,6 +103,7 @@ struct __fehoptions { unsigned int thumb_redraw; double reload; int sort; + int version_sort; int debug; int geom_flags; int geom_x; -- cgit v1.2.3 From b251f26261309c933a43c863f81227219a9e8355 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Wed, 28 Feb 2018 17:31:34 +0100 Subject: feh(1): Show state of optional switches right next to them --- man/feh.pre | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/man/feh.pre b/man/feh.pre index ab3f8d6..d332774 100644 --- a/man/feh.pre +++ b/man/feh.pre @@ -194,7 +194,7 @@ Extra actions which can be set and triggered using the appropriate number key. . .It Cm --auto-rotate . -.Pq only if compiled with exif=1 +.Pq optional feature, $MAN_EXIF$ in this build Automatically rotate images based on EXIF data. Does not alter the image files. . .It Cm -Z , --auto-zoom @@ -253,7 +253,7 @@ Draw the defined actions and what they do at the top-left of the image. . .It Cm --draw-exif . -.Pq only if compiled with exif=1 +.Pq optional feature, $MAN_EXIF$ in this build display some EXIF information in the bottom left corner, similar to using .Cm --info with exiv2 / exifgrep . @@ -514,8 +514,8 @@ managers. . .It Cm --no-xinerama . -Disable Xinerama support. Only makes sense when you have Xinerama support -compiled in. +.Pq optional feature, $MAN_XINERAMA$ in this build +Disable Xinerama support. . .It Cm -j , --output-dir Ar directory . @@ -698,6 +698,7 @@ output version information and exit. . .It Cm --xinerama-index Ar screen . +.Pq optional feature, $MAN_XINERAMA$ in this build Override .Nm Ns No 's idea of the active Xinerama screen. May be useful in certain circumstances @@ -1282,7 +1283,7 @@ Toggle filename display . .It e Bq toggle_exif . -.Pq only if compiled with exif=1 +.Pq optional feature, $MAN_EXIF$ in this build Toggle EXIF tag display . .It f Bq save_filelist -- cgit v1.2.3 From 4567042b5145b097c962499f26511aa5106f0e60 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Wed, 28 Feb 2018 17:53:51 +0100 Subject: Add Makefile flag to disable version sorting on C libraries without strverscmp --- README.md | 1 + config.mk | 10 +++++++++- man/Makefile | 1 + man/feh.pre | 3 ++- src/feh.h | 7 +++++++ src/filelist.c | 29 +++++++++++++++-------------- 6 files changed, 35 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 328897a..7848158 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ Available flags are: | exif | 0 | Builtin EXIF tag display support | | help | 0 | include help text (refers to the manpage otherwise) | | stat64 | 0 | Support CIFS shares from 64bit hosts on 32bit machines | +| verscmp | 1 | Support naturing sorting (`--version-sort`). Requires a GNU-compatible libc exposing `strverscmp` | | xinerama | 1 | Support Xinerama/XRandR multiscreen setups | So, by default **libcurl** and **Xinerama** are enabled, the rest is disabled. diff --git a/config.mk b/config.mk index 8769b1d..226c612 100644 --- a/config.mk +++ b/config.mk @@ -5,9 +5,10 @@ app ?= 0 cam ?= 0 curl ?= 1 debug ?= 0 +exif ?= 0 help ?= 0 +verscmp ?= 1 xinerama ?= 1 -exif ?= 0 # Prefix for all installed files PREFIX ?= /usr/local @@ -63,6 +64,13 @@ ifeq (${stat64},1) CFLAGS += -D_FILE_OFFSET_BITS=64 endif +ifeq (${verscmp},1) + CFLAGS += -DHAVE_VERSCMP + MAN_VERSCMP = enabled +else + MAN_VERSCMP = disabled +endif + ifeq (${xinerama},1) CFLAGS += -DHAVE_LIBXINERAMA LDLIBS += -lXinerama diff --git a/man/Makefile b/man/Makefile index 65f2bc2..9209de1 100644 --- a/man/Makefile +++ b/man/Makefile @@ -12,6 +12,7 @@ all: ${TARGETS} -e 's/\$$MAN_CURL\$$/${MAN_CURL}/' \ -e 's/\$$MAN_DEBUG\$$/${MAN_DEBUG}/' \ -e 's/\$$MAN_EXIF\$$/${MAN_EXIF}/' \ + -e 's/\$$MAN_VERSCMP\$$/${MAN_VERSCMP}/' \ -e 's/\$$MAN_XINERAMA\$$/${MAN_XINERAMA}/' \ < ${@:.1=.pre} > $@ diff --git a/man/feh.pre b/man/feh.pre index d332774..640ae3f 100644 --- a/man/feh.pre +++ b/man/feh.pre @@ -24,7 +24,8 @@ $VERSION$ . .Pp . -Compile-time switches: libcurl support $MAN_CURL$, Xinerama support +Compile-time switches: libcurl support $MAN_CURL$, natural sorting support +$MAN_VERSCMP$, Xinerama support $MAN_XINERAMA$, builtin EXIF support $MAN_EXIF$$MAN_DEBUG$ . . diff --git a/src/feh.h b/src/feh.h index 4e7a3ff..3e0cdda 100644 --- a/src/feh.h +++ b/src/feh.h @@ -27,7 +27,14 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #ifndef FEH_H #define FEH_H +/* + * strverscmp(3) is a GNU extension. In most supporting C libraries it + * requires _GNU_SOURCE to be defined. + */ +#ifdef HAVE_VERSCMP #define _GNU_SOURCE +#endif + #include #include #include diff --git a/src/filelist.c b/src/filelist.c index 159491a..453f795 100644 --- a/src/filelist.c +++ b/src/filelist.c @@ -397,20 +397,26 @@ void feh_file_dirname(char *dst, feh_file * f, int maxlen) dst[n] = '\0'; } -int feh_cmp_filename(void *file1, void *file2) +#ifdef HAVE_VERSCMP +inline int strcmp_or_strverscmp(const char *s1, const char *s2) { if (!opt.version_sort) - return(strcmp(FEH_FILE(file1)->filename, FEH_FILE(file2)->filename)); + return(strcmp(s1, s2)); else - return(strverscmp(FEH_FILE(file1)->filename, FEH_FILE(file2)->filename)); + return(strverscmp(s1, s2)); +} +#else +#define strcmp_or_strverscmp strcmp +#endif + +int feh_cmp_filename(void *file1, void *file2) +{ + return(strcmp_or_strverscmp(FEH_FILE(file1)->filename, FEH_FILE(file2)->filename)); } int feh_cmp_name(void *file1, void *file2) { - if (!opt.version_sort) - return(strcmp(FEH_FILE(file1)->name, FEH_FILE(file2)->name)); - else - return(strverscmp(FEH_FILE(file1)->name, FEH_FILE(file2)->name)); + return(strcmp_or_strverscmp(FEH_FILE(file1)->name, FEH_FILE(file2)->name)); } int feh_cmp_dirname(void *file1, void *file2) @@ -419,13 +425,8 @@ int feh_cmp_dirname(void *file1, void *file2) int cmp; feh_file_dirname(dir1, FEH_FILE(file1), PATH_MAX); feh_file_dirname(dir2, FEH_FILE(file2), PATH_MAX); - if (!opt.version_sort) { - if ((cmp = strcmp(dir1, dir2)) != 0) - return(cmp); - } else { - if ((cmp = strverscmp(dir1, dir2)) != 0) - return(cmp); - } + if ((cmp = strcmp_or_strverscmp(dir1, dir2)) != 0) + return(cmp); return(feh_cmp_name(file1, file2)); } -- cgit v1.2.3 From 5a2db8f2ffc4371d2e88a65567d85663e6d16b36 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Wed, 28 Feb 2018 17:54:17 +0100 Subject: Document --version-sort in feh(1) --- man/feh.pre | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/man/feh.pre b/man/feh.pre index 640ae3f..5290f79 100644 --- a/man/feh.pre +++ b/man/feh.pre @@ -697,6 +697,15 @@ output useful information, progress bars, etc. . output version information and exit. . +.It Cm --version-sort +. +.Pq optional feature, $MAN_VERSCMP$ in this build +Use natural sorting for file and directory names. In this mode, filenames are +sorted as an ordinary human would expect, e.g. +.Qq 2.jpg +comes before +.Qq 10.jpg . +. .It Cm --xinerama-index Ar screen . .Pq optional feature, $MAN_XINERAMA$ in this build @@ -1697,10 +1706,14 @@ Show all images in ~/Pictures . Recursively show all images found in ~/Pictures and subdirectories . -.It feh -rSfilename ~/Pictures +.It feh -rSfilename --version-sort ~/Pictures . -Same as above, but sort by filename. By default, feh will show files in the -order it finds them on the hard disk, which is usually somewhat random. +Same as above, but sort naturally. By default, feh will show files in the +string order of their names, meaning e.g. +.Qq foo 10.jpg +will come before +.Qq foo 2.jpg . +In this case, they are instead ordered as a human would expect. . .It feh -t -Sfilename -E 128 -y 128 -W 1024 ~/Pictures . -- cgit v1.2.3