NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
sort.c File Reference

Address book sorting functions. More...

#include "config.h"
#include <stddef.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "sort.h"
#include "lib.h"
#include "alias.h"
#include "gui.h"
+ Include dependency graph for sort.c:

Go to the source code of this file.

Macros

#define RSORT(num)   ((sort_alias & SORT_REVERSE) ? -num : num)
 

Functions

static int alias_sort_name (const void *a, const void *b)
 Compare two Aliases by their short names - Implements sort_t. More...
 
static int alias_sort_address (const void *a, const void *b)
 Compare two Aliases by their Addresses - Implements sort_t. More...
 
static int alias_sort_unsort (const void *a, const void *b)
 Compare two Aliases by their original configuration position - Implements sort_t. More...
 
static sort_t alias_get_sort_function (short sort)
 Sorting function decision logic. More...
 
void alias_array_sort (struct AliasViewArray *ava, const struct ConfigSubset *sub)
 Sort and reindex an AliasViewArray. More...
 

Variables

static short sort_alias = 0
 

Detailed Description

Address book sorting functions.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file sort.c.

Macro Definition Documentation

◆ RSORT

#define RSORT (   num)    ((sort_alias & SORT_REVERSE) ? -num : num)

Definition at line 39 of file sort.c.

Function Documentation

◆ alias_sort_name()

static int alias_sort_name ( const void *  a,
const void *  b 
)
static

Compare two Aliases by their short names - Implements sort_t.

Note
Non-visible Aliases are sorted to the end

Definition at line 48 of file sort.c.

49 {
50  const struct AliasView *av_a = a;
51  const struct AliasView *av_b = b;
52 
53  if (av_a->is_visible != av_b->is_visible)
54  return av_a->is_visible ? -1 : 1;
55 
56  if (!av_a->is_visible)
57  return 0;
58 
59  int r = mutt_str_coll(av_a->alias->name, av_b->alias->name);
60 
61  return RSORT(r);
62 }
char * name
Short name.
Definition: alias.h:35
GUI data wrapping an Alias.
Definition: gui.h:35
int mutt_str_coll(const char *a, const char *b)
Collate two strings (compare using locale), safely.
Definition: string.c:677
bool is_visible
Is visible?
Definition: gui.h:43
#define RSORT(num)
Definition: sort.c:39
struct Alias * alias
Alias.
Definition: gui.h:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ alias_sort_address()

static int alias_sort_address ( const void *  a,
const void *  b 
)
static

Compare two Aliases by their Addresses - Implements sort_t.

Note
Non-visible Aliases are sorted to the end

Definition at line 69 of file sort.c.

70 {
71  const struct AliasView *av_a = a;
72  const struct AliasView *av_b = b;
73 
74  const struct AddressList *al_a = &av_a->alias->addr;
75  const struct AddressList *al_b = &av_b->alias->addr;
76 
77  if (av_a->is_visible != av_b->is_visible)
78  return av_a->is_visible ? -1 : 1;
79 
80  if (!av_a->is_visible)
81  return 0;
82 
83  int r;
84  if (al_a == al_b)
85  r = 0;
86  else if (!al_a)
87  r = -1;
88  else if (!al_b)
89  r = 1;
90  else
91  {
92  const struct Address *addr_a = TAILQ_FIRST(al_a);
93  const struct Address *addr_b = TAILQ_FIRST(al_b);
94  if (addr_a && addr_a->personal)
95  {
96  if (addr_b && addr_b->personal)
97  r = mutt_str_coll(addr_a->personal, addr_b->personal);
98  else
99  r = 1;
100  }
101  else if (addr_b && addr_b->personal)
102  r = -1;
103  else if (addr_a && addr_b)
104  r = mutt_str_coll(addr_a->mailbox, addr_b->mailbox);
105  else
106  r = 0;
107  }
108 
109  return RSORT(r);
110 }
#define TAILQ_FIRST(head)
Definition: queue.h:723
An email address.
Definition: address.h:35
char * mailbox
Mailbox and host address.
Definition: address.h:38
GUI data wrapping an Alias.
Definition: gui.h:35
int mutt_str_coll(const char *a, const char *b)
Collate two strings (compare using locale), safely.
Definition: string.c:677
bool is_visible
Is visible?
Definition: gui.h:43
#define RSORT(num)
Definition: sort.c:39
struct Alias * alias
Alias.
Definition: gui.h:44
char * personal
Real name of address.
Definition: address.h:37
struct AddressList addr
List of Addresses the Alias expands to.
Definition: alias.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ alias_sort_unsort()

static int alias_sort_unsort ( const void *  a,
const void *  b 
)
static

Compare two Aliases by their original configuration position - Implements sort_t.

Note
Non-visible Aliases are sorted to the end

Definition at line 117 of file sort.c.

118 {
119  const struct AliasView *av_a = a;
120  const struct AliasView *av_b = b;
121 
122  if (av_a->is_visible != av_b->is_visible)
123  return av_a->is_visible ? -1 : 1;
124 
125  if (!av_a->is_visible)
126  return 0;
127 
128  int r = (av_a->orig_seq - av_b->orig_seq);
129 
130  return RSORT(r);
131 }
GUI data wrapping an Alias.
Definition: gui.h:35
bool is_visible
Is visible?
Definition: gui.h:43
#define RSORT(num)
Definition: sort.c:39
int orig_seq
Sequence in alias config file.
Definition: gui.h:38
+ Here is the caller graph for this function:

◆ alias_get_sort_function()

static sort_t alias_get_sort_function ( short  sort)
static

Sorting function decision logic.

Parameters
sortSort method, e.g. SORT_ALIAS

Definition at line 137 of file sort.c.

138 {
139  switch ((sort & SORT_MASK))
140  {
141  case SORT_ALIAS:
142  return alias_sort_name;
143  case SORT_ADDRESS:
144  return alias_sort_address;
145  case SORT_ORDER:
146  return alias_sort_unsort;
147  default:
148  return alias_sort_name;
149  }
150 }
static int alias_sort_unsort(const void *a, const void *b)
Compare two Aliases by their original configuration position - Implements sort_t. ...
Definition: sort.c:117
Sort by email address.
Definition: sort2.h:54
static int alias_sort_address(const void *a, const void *b)
Compare two Aliases by their Addresses - Implements sort_t.
Definition: sort.c:69
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:48
static int alias_sort_name(const void *a, const void *b)
Compare two Aliases by their short names - Implements sort_t.
Definition: sort.c:48
Sort by email alias.
Definition: sort2.h:53
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:78
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ alias_array_sort()

void alias_array_sort ( struct AliasViewArray *  ava,
const struct ConfigSubset sub 
)

Sort and reindex an AliasViewArray.

Parameters
avaArray of Aliases
subConfig items

Definition at line 157 of file sort.c.

158 {
159  if (!ava || ARRAY_EMPTY(ava))
160  return;
161 
162  sort_alias = cs_subset_sort(sub, "sort_alias");
164 
165  struct AliasView *avp = NULL;
166  ARRAY_FOREACH(avp, ava)
167  {
168  avp->num = ARRAY_FOREACH_IDX;
169  }
170 }
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:206
GUI data wrapping an Alias.
Definition: gui.h:35
static short sort_alias
Definition: sort.c:41
static sort_t alias_get_sort_function(short sort)
Sorting function decision logic.
Definition: sort.c:137
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:292
#define ARRAY_EMPTY(head)
Check if an array is empty.
Definition: array.h:70
#define ARRAY_SORT(head, fn)
Sort an array.
Definition: array.h:271
int num
Index number in list.
Definition: gui.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ sort_alias

short sort_alias = 0
static

Definition at line 41 of file sort.c.