From ec873ab4fa92c018b65a4785eca55348d256790d Mon Sep 17 00:00:00 2001
From: Arun Prakash Jana <engineerarun@gmail.com>
Date: Fri, 30 Aug 2019 20:55:23 +0530
Subject: [PATCH] Option -a to use file access time

---
 README.md                                     |  7 ++++---
 misc/auto-completion/bash/nnn-completion.bash |  1 +
 misc/auto-completion/fish/nnn.fish            |  1 +
 misc/auto-completion/zsh/_nnn                 |  1 +
 nnn.1                                         |  4 ++++
 src/nnn.c                                     | 18 ++++++++++++------
 6 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/README.md b/README.md
index f8b3b8f..5c83f07 100644
--- a/README.md
+++ b/README.md
@@ -74,7 +74,7 @@ Visit the **[Wiki](https://github.com/jarun/nnn/wiki)** for operational concepts
 - Sorting
   - Ordered pure numeric names by default (visit _/proc_)
   - Case-insensitive version (_aka_ natural) sort
-  - Sort by file name, modification time, size, file extension
+  - By file name, modification/access time, size, extension
 - Search
   - Instant filtering with *search-as-you-type*
   - Regex and substring match
@@ -202,8 +202,8 @@ Option completion scripts for Bash, Fish and Zsh can be found in respective subd
 #### Cmdline options
 
 ```
-usage: nnn [-b key] [-d] [-H] [-i] [-n] [-o] [-p file]
-           [-r] [-s] [-S] [-t] [-v] [-h] [PATH]
+usage: nnn [-a] [-b key] [-d] [-H] [-i] [-n] [-o]
+           [-p file] [-r] [-s] [-S] [-t] [-v] [-h] [PATH]
 
 The missing terminal file manager for X.
 
@@ -211,6 +211,7 @@ positional args:
   PATH   start dir [default: current dir]
 
 optional args:
+ -a      use access time
  -b key  open bookmark key
  -d      detail mode
  -H      show hidden files
diff --git a/misc/auto-completion/bash/nnn-completion.bash b/misc/auto-completion/bash/nnn-completion.bash
index 56d2beb..2b81d31 100644
--- a/misc/auto-completion/bash/nnn-completion.bash
+++ b/misc/auto-completion/bash/nnn-completion.bash
@@ -11,6 +11,7 @@ _nnn () {
     local cur=$2 prev=$3
     local -a opts
     opts=(
+        -a
         -b
         -d
         -H
diff --git a/misc/auto-completion/fish/nnn.fish b/misc/auto-completion/fish/nnn.fish
index 970c24a..1a2eed4 100644
--- a/misc/auto-completion/fish/nnn.fish
+++ b/misc/auto-completion/fish/nnn.fish
@@ -5,6 +5,7 @@
 #   Arun Prakash Jana <engineerarun@gmail.com>
 #
 
+complete -c nnn -s a    -d 'use access time'
 complete -c nnn -s b -r -d 'bookmark key to open'
 complete -c nnn -s d    -d 'start in detail mode'
 complete -c nnn -s H    -d 'show hidden files'
diff --git a/misc/auto-completion/zsh/_nnn b/misc/auto-completion/zsh/_nnn
index 119fc5a..feb3203 100644
--- a/misc/auto-completion/zsh/_nnn
+++ b/misc/auto-completion/zsh/_nnn
@@ -9,6 +9,7 @@
 setopt localoptions noshwordsplit noksharrays
 local -a args
 args=(
+    '(-a)-a[use access time]'
     '(-b)-b[bookmark key to open]:key char'
     '(-d)-d[start in detail mode]'
     '(-H)-H[show hidden files]'
diff --git a/nnn.1 b/nnn.1
index 71127b6..0a774e9 100644
--- a/nnn.1
+++ b/nnn.1
@@ -6,6 +6,7 @@
 .Nd the missing terminal file manager for X
 .Sh SYNOPSIS
 .Nm
+.Op Ar -a
 .Op Ar -b key
 .Op Ar -d
 .Op Ar -H
@@ -36,6 +37,9 @@ to see the list of keybinds.
 .Nm
 supports the following options:
 .Pp
+.Fl a
+        use access time for all operations (default: modification time)
+.Pp
 .Fl "b key"
         specify bookmark key to open
 .Pp
diff --git a/src/nnn.c b/src/nnn.c
index 3cf2fd4..5169fb8 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -215,7 +215,7 @@ typedef struct {
 	uint copymode   : 1;  /* Set when copying files */
 	uint showdetail : 1;  /* Clear to show fewer file info */
 	uint ctxactive  : 1;  /* Context active or not */
-	uint reserved   : 8;
+	uint reserved   : 7;
 	/* The following settings are global */
 	uint curctx     : 2;  /* Current context number */
 	uint dircolor   : 1;  /* Current status of dir color */
@@ -229,6 +229,7 @@ typedef struct {
 	uint runctx     : 2;  /* The context in which plugin is to be run */
 	uint filter_re  : 1;  /* Use regex filters */
 	uint trash      : 1;  /* Move removed files to trash */
+	uint mtime      : 1;  /* Use modification time (else access time) */
 } settings;
 
 /* Contexts or workspaces */
@@ -268,6 +269,7 @@ static settings cfg = {
 	0, /* runctx */
 	1, /* filter_re */
 	0, /* trash */
+	1, /* mtime */
 };
 
 static context g_ctx[CTX_MAX] __attribute__ ((aligned));
@@ -3056,7 +3058,7 @@ static int dentfill(char *path, struct entry **dents)
 		off += dentp->nlen;
 
 		/* Copy other fields */
-		dentp->t = sb.st_mtime;
+		dentp->t = cfg.mtime ? sb.st_mtime : sb.st_atime;
 		if (dp->d_type == DT_LNK && !flags) { /* Do not add sizes for links */
 			dentp->mode = (sb.st_mode & ~S_IFMT) | S_IFLNK;
 			dentp->size = 0;
@@ -3283,7 +3285,7 @@ static void redraw(char *path)
 		char sort[] = "\0 ";
 
 		if (cfg.mtimeorder)
-			sort[0] = 'T';
+			sort[0] = cfg.mtime ? 'T' : 'A';
 		else if (cfg.sizeorder)
 			sort[0] = 'S';
 		else if (cfg.extnorder)
@@ -4536,12 +4538,13 @@ nochange:
 static void usage(void)
 {
 	fprintf(stdout,
-		"%s: nnn [-b key] [-d] [-H] [-i] [-n] [-o] [-p file]\n"
-		"           [-r] [-s] [-S] [-t] [-v] [-h] [PATH]\n\n"
+		"%s: nnn [-a] [-b key] [-d] [-H] [-i] [-n] [-o]\n"
+		"           [-p file] [-r] [-s] [-S] [-t] [-v] [-h] [PATH]\n\n"
 		"The missing terminal file manager for X.\n\n"
 		"positional args:\n"
 		"  PATH   start dir [default: current dir]\n\n"
 		"optional args:\n"
+		" -a      use access time\n"
 		" -b key  open bookmark key\n"
 		" -d      detail mode\n"
 		" -H      show hidden files\n"
@@ -4688,7 +4691,7 @@ int main(int argc, char *argv[])
 	bool progress = FALSE;
 #endif
 
-	while ((opt = getopt(argc, argv, "HSib:dnop:rstvh")) != -1) {
+	while ((opt = getopt(argc, argv, "HSiab:dnop:rstvh")) != -1) {
 		switch (opt) {
 		case 'S':
 			cfg.blkorder = 1;
@@ -4701,6 +4704,9 @@ int main(int argc, char *argv[])
 		case 'i':
 			cfg.filtermode = 1;
 			break;
+		case 'a':
+			cfg.mtime = 0;
+			break;
 		case 'b':
 			arg = optarg;
 			break;