mcabber/mcabber/hbuf.c
author franky
Sun, 12 May 2019 11:32:30 +0200
changeset 2337 ffd0e57e9563
parent 2268 f5402d705f67
permissions -rw-r--r--
Defined types shall end with "_t"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
     1
/*
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
     2
 * hbuf.c       -- History buffer implementation
393
f8f3c7493457 Whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 392
diff changeset
     3
 *
1729
e6e89b1d7831 Minor style and header updates
Mikael Berthe <mikael@lilotux.net>
parents: 1668
diff changeset
     4
 * Copyright (C) 2005-2010 Mikael Berthe <mikael@lilotux.net>
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
     5
 *
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
     8
 * the Free Software Foundation; either version 2 of the License, or (at
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
     9
 * your option) any later version.
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    10
 *
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful, but
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    14
 * General Public License for more details.
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    15
 *
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License
2268
f5402d705f67 Fix FSF addresses in all files
Mikael Berthe <mikael@lilotux.net>
parents: 2032
diff changeset
    17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    18
 */
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    19
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    20
#include <string.h>
1485
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
    21
#include <sys/types.h>
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
    22
#include <sys/stat.h>
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
    23
#include <unistd.h>
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    24
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    25
#include "hbuf.h"
804
339c76ed5c0d Remove a warning on Cygwin (implicit strcasestr() declaration)
Mikael Berthe <mikael@lilotux.net>
parents: 790
diff changeset
    26
#include "utils.h"
930
a75f7a13df7b UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents: 924
diff changeset
    27
#include "utf8.h"
1485
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
    28
#include "screen.h"
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    29
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    30
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    31
/* This is a private structure type */
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    32
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    33
typedef struct {
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    34
  char *ptr;
182
f7b03201877a [/trunk] Changeset 194 by mikael
mikael
parents: 153
diff changeset
    35
  char *ptr_end;        // beginning of the block
f7b03201877a [/trunk] Changeset 194 by mikael
mikael
parents: 153
diff changeset
    36
  char *ptr_end_alloc;  // end of the current persistent block
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    37
  guchar flags;
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    38
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    39
  // XXX This should certainly be a pointer, and be allocated only when needed
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    40
  // (for ex. when HBB_FLAG_PERSISTENT is set).
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    41
  struct { // hbuf_line_info
184
b5aa2b9c425a [/trunk] Changeset 196 by mikael
mikael
parents: 182
diff changeset
    42
    time_t timestamp;
1290
e42f48103609 Drawing the MUC nicks by a different color
Michal 'vorner' Vaner <vorner@ucw.cz>
parents: 1268
diff changeset
    43
    unsigned mucnicklen;
850
d0781ca2dd08 Use new status buffer window
Mikael Berthe <mikael@lilotux.net>
parents: 804
diff changeset
    44
    guint  flags;
1602
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
    45
    gpointer xep184;
184
b5aa2b9c425a [/trunk] Changeset 196 by mikael
mikael
parents: 182
diff changeset
    46
  } prefix;
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
    47
} hbuf_block_t;
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    48
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    49
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
    50
//  do_wrap(p_hbuf, first_hbuf_elt, width)
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
    51
// Wrap hbuf lines with the specified width.
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
    52
// '\n' are handled by this routine (they are removed and persistent lines
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
    53
// are created).
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
    54
// All hbuf elements are processed, starting from first_hbuf_elt.
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
    55
static inline void do_wrap(GList **p_hbuf, GList *first_hbuf_elt,
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
    56
                           unsigned int width)
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    57
{
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
    58
  GList *curr_elt = first_hbuf_elt;
942
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    59
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    60
  // Let's add non-persistent blocs if necessary
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    61
  // - If there are '\n' in the string
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    62
  // - If length > width (and width != 0)
942
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    63
  while (curr_elt) {
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
    64
    hbuf_block_t *hbuf_b_curr, *hbuf_b_prev;
942
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    65
    char *c, *end;
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    66
    char *br = NULL; // break pointer
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    67
    char *cr = NULL; // CR pointer
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    68
    unsigned int cur_w = 0;
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    69
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    70
    // We want to break where we can find a space char or a CR
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    71
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
    72
    hbuf_b_curr = (hbuf_block_t*)(curr_elt->data);
942
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    73
    hbuf_b_prev = hbuf_b_curr;
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    74
    c = hbuf_b_curr->ptr;
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    75
942
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    76
    while (*c && (!width || cur_w <= width)) {
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    77
      if (*c == '\n') {
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    78
        br = cr = c;
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    79
        *c = 0;
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    80
        break;
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    81
      }
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    82
      if (iswblank(get_char(c)))
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    83
        br = c;
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    84
      cur_w += get_char_width(c);
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    85
      c = next_char(c);
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    86
    }
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    87
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    88
    if (cr || (*c && cur_w > width)) {
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    89
      if (!br || br == hbuf_b_curr->ptr)
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    90
        br = c;
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
    91
      else
942
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    92
        br = next_char(br);
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    93
      end = hbuf_b_curr->ptr_end;
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    94
      hbuf_b_curr->ptr_end = br;
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    95
      // Create another block
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
    96
      hbuf_b_curr = g_new0(hbuf_block_t, 1);
942
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    97
      // The block must be persistent after a CR
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    98
      if (cr) {
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
    99
        hbuf_b_curr->ptr    = hbuf_b_prev->ptr_end + 1; // == cr+1
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
   100
        hbuf_b_curr->flags  = HBB_FLAG_PERSISTENT;
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
   101
      } else {
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
   102
        hbuf_b_curr->ptr    = hbuf_b_prev->ptr_end; // == br
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
   103
        hbuf_b_curr->flags    = 0;
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
   104
      }
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
   105
      hbuf_b_curr->ptr_end  = end;
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
   106
      hbuf_b_curr->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc;
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
   107
      // This is OK because insert_before(NULL) == append():
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
   108
      *p_hbuf = g_list_insert_before(*p_hbuf, curr_elt->next, hbuf_b_curr);
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   109
    }
942
c6bd42119c31 Make hbuf_add_line() UTF-8 aware
Mikael Berthe <mikael@lilotux.net>
parents: 932
diff changeset
   110
    curr_elt = g_list_next(curr_elt);
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   111
  }
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   112
}
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   113
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   114
//  hbuf_add_line(p_hbuf, text, prefix_flags, width, maxhbufblocks)
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   115
// Add a line to the given buffer.  If width is not null, then lines are
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   116
// wrapped at this length.
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   117
// maxhbufblocks is the maximum number of hbuf blocks we can allocate.  If
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   118
// null, there is no limit.  If non-null, it should be >= 2.
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   119
//
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   120
// Note 1: Splitting according to width won't work if there are tabs; they
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   121
//         should be expanded before.
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   122
// Note 2: width does not include the ending \0.
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   123
void hbuf_add_line(GList **p_hbuf, const char *text, time_t timestamp,
1290
e42f48103609 Drawing the MUC nicks by a different color
Michal 'vorner' Vaner <vorner@ucw.cz>
parents: 1268
diff changeset
   124
        guint prefix_flags, guint width, guint maxhbufblocks,
1602
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   125
        unsigned mucnicklen, gpointer xep184)
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   126
{
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   127
  GList *curr_elt;
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   128
  char *line;
1364
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   129
  guint hbb_blocksize, textlen;
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   130
  hbuf_block_t *hbuf_block_elt;
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   131
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   132
  if (!text) return;
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   133
1602
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   134
  prefix_flags |= (xep184 ? HBB_PREFIX_RECEIPT : 0);
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   135
1364
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   136
  textlen = strlen(text);
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   137
  hbb_blocksize = MAX(textlen+1, HBB_BLOCKSIZE);
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   138
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   139
  hbuf_block_elt = g_new0(hbuf_block_t, 1);
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   140
  hbuf_block_elt->prefix.timestamp  = timestamp;
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   141
  hbuf_block_elt->prefix.flags      = prefix_flags;
1290
e42f48103609 Drawing the MUC nicks by a different color
Michal 'vorner' Vaner <vorner@ucw.cz>
parents: 1268
diff changeset
   142
  hbuf_block_elt->prefix.mucnicklen = mucnicklen;
1602
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   143
  hbuf_block_elt->prefix.xep184     = xep184;
1011
b5bcc223cf51 Fix a bug in hbuf_add_line()
Mikael Berthe <mikael@lilotux.net>
parents: 943
diff changeset
   144
  if (!*p_hbuf) {
1364
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   145
    hbuf_block_elt->ptr  = g_new(char, hbb_blocksize);
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   146
    if (!hbuf_block_elt->ptr) {
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   147
      g_free(hbuf_block_elt);
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   148
      return;
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   149
    }
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   150
    hbuf_block_elt->flags  = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT;
1364
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   151
    hbuf_block_elt->ptr_end_alloc = hbuf_block_elt->ptr + hbb_blocksize;
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   152
  } else {
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   153
    hbuf_block_t *hbuf_b_prev;
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   154
    // Set p_hbuf to the end of the list, to speed up history loading
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   155
    // (or CPU time will be used by g_list_last() for each line)
1011
b5bcc223cf51 Fix a bug in hbuf_add_line()
Mikael Berthe <mikael@lilotux.net>
parents: 943
diff changeset
   156
    *p_hbuf = g_list_last(*p_hbuf);
b5bcc223cf51 Fix a bug in hbuf_add_line()
Mikael Berthe <mikael@lilotux.net>
parents: 943
diff changeset
   157
    hbuf_b_prev = (*p_hbuf)->data;
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   158
    hbuf_block_elt->ptr    = hbuf_b_prev->ptr_end;
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   159
    hbuf_block_elt->flags  = HBB_FLAG_PERSISTENT;
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   160
    hbuf_block_elt->ptr_end_alloc = hbuf_b_prev->ptr_end_alloc;
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   161
  }
1011
b5bcc223cf51 Fix a bug in hbuf_add_line()
Mikael Berthe <mikael@lilotux.net>
parents: 943
diff changeset
   162
  *p_hbuf = g_list_append(*p_hbuf, hbuf_block_elt);
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   163
1364
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   164
  if (hbuf_block_elt->ptr + textlen >= hbuf_block_elt->ptr_end_alloc) {
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   165
    // Too long for the current allocated bloc, we need another one
1364
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   166
    if (!maxhbufblocks || textlen >= HBB_BLOCKSIZE) {
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   167
      // No limit, let's allocate a new block
1364
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   168
      // If the message text is big, we won't bother to reuse an old block
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   169
      // as well (it could be too small and cause a segfault).
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   170
      hbuf_block_elt->ptr  = g_new0(char, hbb_blocksize);
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   171
      hbuf_block_elt->ptr_end_alloc = hbuf_block_elt->ptr + hbb_blocksize;
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   172
      // XXX We should check the return value.
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   173
    } else {
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   174
      GList *hbuf_head, *hbuf_elt;
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   175
      hbuf_block_t *hbuf_b_elt;
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   176
      guint n = 0;
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   177
      hbuf_head = g_list_first(*p_hbuf);
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   178
      // We need at least 2 allocated blocks
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   179
      if (maxhbufblocks == 1)
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   180
        maxhbufblocks = 2;
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   181
      // Let's count the number of allocated areas
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   182
      for (hbuf_elt = hbuf_head; hbuf_elt; hbuf_elt = g_list_next(hbuf_elt)) {
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   183
        hbuf_b_elt = (hbuf_block_t*)(hbuf_elt->data);
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   184
        if (hbuf_b_elt->flags & HBB_FLAG_ALLOC)
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   185
          n++;
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   186
      }
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   187
      // If we can't allocate a new area, reuse the previous block(s)
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   188
      if (n < maxhbufblocks) {
1364
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   189
        hbuf_block_elt->ptr  = g_new0(char, hbb_blocksize);
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   190
        hbuf_block_elt->ptr_end_alloc = hbuf_block_elt->ptr + hbb_blocksize;
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   191
      } else {
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   192
        // Let's use an old block, and free the extra blocks if needed
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   193
        char *allocated_block = NULL;
1364
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   194
        char *end_of_allocated_block = NULL;
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   195
        while (n >= maxhbufblocks) {
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   196
          int start_of_block = 1;
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   197
          for (hbuf_elt = hbuf_head; hbuf_elt; hbuf_elt = hbuf_head) {
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   198
            hbuf_b_elt = (hbuf_block_t*)(hbuf_elt->data);
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   199
            if (hbuf_b_elt->flags & HBB_FLAG_ALLOC) {
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   200
              if (start_of_block-- == 0)
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   201
                break;
1364
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   202
              if (n == maxhbufblocks) {
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   203
                allocated_block = hbuf_b_elt->ptr;
1364
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   204
                end_of_allocated_block = hbuf_b_elt->ptr_end_alloc;
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   205
              } else {
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   206
                g_free(hbuf_b_elt->ptr);
1364
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   207
              }
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   208
            }
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   209
            g_free(hbuf_b_elt);
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   210
            hbuf_head = *p_hbuf = g_list_delete_link(hbuf_head, hbuf_elt);
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   211
          }
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   212
          n--;
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   213
        }
1364
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   214
        memset(allocated_block, 0, end_of_allocated_block-allocated_block);
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   215
        hbuf_block_elt->ptr = allocated_block;
1364
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   216
        hbuf_block_elt->ptr_end_alloc = end_of_allocated_block;
1141
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   217
      }
5be2408a6534 Add option "max_history_blocks"
Mikael Berthe <mikael@lilotux.net>
parents: 1011
diff changeset
   218
    }
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   219
    hbuf_block_elt->flags  = HBB_FLAG_ALLOC | HBB_FLAG_PERSISTENT;
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   220
  }
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   221
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   222
  line = hbuf_block_elt->ptr;
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   223
  // Ok, now we can copy the text..
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   224
  strcpy(line, text);
1364
ff1f6facdc0c Allow dynamic HBB sizes
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   225
  hbuf_block_elt->ptr_end = line + textlen + 1;
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   226
1011
b5bcc223cf51 Fix a bug in hbuf_add_line()
Mikael Berthe <mikael@lilotux.net>
parents: 943
diff changeset
   227
  curr_elt = g_list_last(*p_hbuf);
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   228
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   229
  // Wrap lines and handle CRs ('\n')
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   230
  do_wrap(p_hbuf, curr_elt, width);
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   231
}
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   232
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   233
//  hbuf_free()
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   234
// Destroys all hbuf list.
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   235
void hbuf_free(GList **p_hbuf)
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   236
{
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   237
  hbuf_block_t *hbuf_b_elt;
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   238
  GList *hbuf_elt;
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   239
  GList *first_elt = g_list_first(*p_hbuf);
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   240
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   241
  for (hbuf_elt = first_elt; hbuf_elt; hbuf_elt = g_list_next(hbuf_elt)) {
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   242
    hbuf_b_elt = (hbuf_block_t*)(hbuf_elt->data);
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   243
    if (hbuf_b_elt->flags & HBB_FLAG_ALLOC) {
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   244
      g_free(hbuf_b_elt->ptr);
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   245
    }
390
468c9cac2798 Minor changes to hbuf.c
Mikael Berthe <mikael@lilotux.net>
parents: 370
diff changeset
   246
    g_free(hbuf_b_elt);
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   247
  }
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   248
893
92aaf2af786b Fix a small memory leak in /buffer purge
Mikael Berthe <mikael@lilotux.net>
parents: 852
diff changeset
   249
  g_list_free(first_elt);
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   250
  *p_hbuf = NULL;
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   251
}
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   252
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   253
//  hbuf_rebuild()
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   254
// Rebuild all hbuf list, with the new width.
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   255
// If width == 0, lines are not wrapped.
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   256
void hbuf_rebuild(GList **p_hbuf, unsigned int width)
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   257
{
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   258
  GList *first_elt, *curr_elt, *next_elt;
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   259
  hbuf_block_t *hbuf_b_curr, *hbuf_b_next;
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   260
150
5647381a7dfb [/trunk] Changeset 162 by mikael
mikael
parents: 83
diff changeset
   261
  // *p_hbuf needs to be the head of the list
5647381a7dfb [/trunk] Changeset 162 by mikael
mikael
parents: 83
diff changeset
   262
  first_elt = *p_hbuf = g_list_first(*p_hbuf);
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   263
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   264
  // #1 Remove non-persistent blocks (ptr_end should be updated!)
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   265
  curr_elt = first_elt;
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   266
  while (curr_elt) {
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   267
    next_elt = g_list_next(curr_elt);
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   268
    // Last element?
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   269
    if (!next_elt)
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   270
      break;
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   271
    hbuf_b_curr = (hbuf_block_t*)(curr_elt->data);
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   272
    hbuf_b_next = (hbuf_block_t*)(next_elt->data);
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   273
    // Is next line not-persistent?
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   274
    if (!(hbuf_b_next->flags & HBB_FLAG_PERSISTENT)) {
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   275
      hbuf_b_curr->ptr_end = hbuf_b_next->ptr_end;
390
468c9cac2798 Minor changes to hbuf.c
Mikael Berthe <mikael@lilotux.net>
parents: 370
diff changeset
   276
      g_free(hbuf_b_next);
790
5d4ad1d77da4 Kill a few gcc4 warnings
Mikael Berthe <mikael@lilotux.net>
parents: 733
diff changeset
   277
      curr_elt = g_list_delete_link(curr_elt, next_elt);
393
f8f3c7493457 Whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 392
diff changeset
   278
    } else
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   279
      curr_elt = next_elt;
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   280
  }
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   281
  // #2 Go back to head and create non-persistent blocks when needed
943
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   282
  if (width)
9ac0d166a85b hbuf: Refactor line wrapping stuff
Mikael Berthe <mikael@lilotux.net>
parents: 942
diff changeset
   283
    do_wrap(p_hbuf, first_elt, width);
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   284
}
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   285
189
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   286
//  hbuf_previous_persistent()
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   287
// Returns the previous persistent block (line).  If the given line is
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   288
// persistent, then it is returned.
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   289
// This function is used for example when resizing a buffer.  If the top of the
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   290
// screen is on a non-persistent block, then a screen resize could destroy this
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   291
// line...
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   292
GList *hbuf_previous_persistent(GList *l_line)
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   293
{
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   294
  hbuf_block_t *hbuf_b_elt;
189
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   295
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   296
  while (l_line) {
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   297
    hbuf_b_elt = (hbuf_block_t*)l_line->data;
1966
88dafee4ee49 Hopefully fix previous commit
Mikael Berthe <mikael@lilotux.net>
parents: 1965
diff changeset
   298
    if (hbuf_b_elt->flags & HBB_FLAG_PERSISTENT &&
88dafee4ee49 Hopefully fix previous commit
Mikael Berthe <mikael@lilotux.net>
parents: 1965
diff changeset
   299
        (hbuf_b_elt->flags & ~HBB_PREFIX_READMARK))
189
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   300
      return l_line;
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   301
    l_line = g_list_previous(l_line);
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   302
  }
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   303
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   304
  return NULL;
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   305
}
4f3975f1b852 [/trunk] Changeset 201 by mikael
mikael
parents: 184
diff changeset
   306
368
da50f08ea058 Update hbuf_get_lines() comment
Mikael Berthe <mikael@lilotux.net>
parents: 197
diff changeset
   307
//  hbuf_get_lines(hbuf, n)
370
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   308
// Returns an array of n hbb_line pointers
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   309
// (The first line will be the line currently pointed by hbuf)
898
c65b71dcda94 Fix memory leak in scr_UpdateWindow()
Mikael Berthe <mikael@lilotux.net>
parents: 893
diff changeset
   310
// Note: The caller should free the array, the hbb_line pointers and the
c65b71dcda94 Fix memory leak in scr_UpdateWindow()
Mikael Berthe <mikael@lilotux.net>
parents: 893
diff changeset
   311
// text pointers after use.
184
b5aa2b9c425a [/trunk] Changeset 196 by mikael
mikael
parents: 182
diff changeset
   312
hbb_line **hbuf_get_lines(GList *hbuf, unsigned int n)
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   313
{
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   314
  unsigned int i;
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   315
  hbuf_block_t *blk;
1204
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1141
diff changeset
   316
  guint last_persist_prefixflags = 0;
733
c502bfe67c31 Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
   317
  GList *last_persist;  // last persistent flags
852
a3b8956db7bc hbuf: minor fixes and typos
Mikael Berthe <mikael@lilotux.net>
parents: 850
diff changeset
   318
  hbb_line **array, **array_elt;
1948
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   319
  hbb_line *prev_array_elt = NULL;
729
39f67cade02c Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
   320
733
c502bfe67c31 Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
   321
  // To be able to correctly highlight multi-line messages,
c502bfe67c31 Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
   322
  // we need to look at the last non-null prefix, which should be the first
1970
10eead2ba1b7 Fix another readmark issue with line wrapping
Mikael Berthe <mikael@lilotux.net>
parents: 1967
diff changeset
   323
  // line of the message.  We also need to check if there's a readmark flag
10eead2ba1b7 Fix another readmark issue with line wrapping
Mikael Berthe <mikael@lilotux.net>
parents: 1967
diff changeset
   324
  // somewhere in the message.
729
39f67cade02c Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
   325
  last_persist = hbuf_previous_persistent(hbuf);
733
c502bfe67c31 Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
   326
  while (last_persist) {
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   327
    blk = (hbuf_block_t*)last_persist->data;
1970
10eead2ba1b7 Fix another readmark issue with line wrapping
Mikael Berthe <mikael@lilotux.net>
parents: 1967
diff changeset
   328
    if ((blk->flags & HBB_FLAG_PERSISTENT) && blk->prefix.flags) {
10eead2ba1b7 Fix another readmark issue with line wrapping
Mikael Berthe <mikael@lilotux.net>
parents: 1967
diff changeset
   329
      // This can be either the beginning of the message,
10eead2ba1b7 Fix another readmark issue with line wrapping
Mikael Berthe <mikael@lilotux.net>
parents: 1967
diff changeset
   330
      // or a persistent line with a readmark flag (or both).
10eead2ba1b7 Fix another readmark issue with line wrapping
Mikael Berthe <mikael@lilotux.net>
parents: 1967
diff changeset
   331
      if (blk->prefix.flags & ~HBB_PREFIX_READMARK) { // First message line
10eead2ba1b7 Fix another readmark issue with line wrapping
Mikael Berthe <mikael@lilotux.net>
parents: 1967
diff changeset
   332
        last_persist_prefixflags |= blk->prefix.flags;
10eead2ba1b7 Fix another readmark issue with line wrapping
Mikael Berthe <mikael@lilotux.net>
parents: 1967
diff changeset
   333
        break;
10eead2ba1b7 Fix another readmark issue with line wrapping
Mikael Berthe <mikael@lilotux.net>
parents: 1967
diff changeset
   334
      } else { // Not the first line, but we need to keep the readmark flag
10eead2ba1b7 Fix another readmark issue with line wrapping
Mikael Berthe <mikael@lilotux.net>
parents: 1967
diff changeset
   335
        last_persist_prefixflags = blk->prefix.flags;
10eead2ba1b7 Fix another readmark issue with line wrapping
Mikael Berthe <mikael@lilotux.net>
parents: 1967
diff changeset
   336
      }
733
c502bfe67c31 Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
   337
    }
c502bfe67c31 Fix highlighting of outgoing multi-line messages
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
   338
    last_persist = g_list_previous(last_persist);
729
39f67cade02c Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
   339
  }
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   340
852
a3b8956db7bc hbuf: minor fixes and typos
Mikael Berthe <mikael@lilotux.net>
parents: 850
diff changeset
   341
  array = g_new0(hbb_line*, n);
a3b8956db7bc hbuf: minor fixes and typos
Mikael Berthe <mikael@lilotux.net>
parents: 850
diff changeset
   342
  array_elt = array;
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   343
852
a3b8956db7bc hbuf: minor fixes and typos
Mikael Berthe <mikael@lilotux.net>
parents: 850
diff changeset
   344
  for (i = 0 ; i < n ; i++) {
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   345
    if (hbuf) {
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   346
      int maxlen;
1485
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   347
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   348
      blk = (hbuf_block_t*)(hbuf->data);
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   349
      maxlen = blk->ptr_end - blk->ptr;
184
b5aa2b9c425a [/trunk] Changeset 196 by mikael
mikael
parents: 182
diff changeset
   350
      *array_elt = (hbb_line*)g_new(hbb_line, 1);
1290
e42f48103609 Drawing the MUC nicks by a different color
Michal 'vorner' Vaner <vorner@ucw.cz>
parents: 1268
diff changeset
   351
      (*array_elt)->timestamp  = blk->prefix.timestamp;
e42f48103609 Drawing the MUC nicks by a different color
Michal 'vorner' Vaner <vorner@ucw.cz>
parents: 1268
diff changeset
   352
      (*array_elt)->flags      = blk->prefix.flags;
e42f48103609 Drawing the MUC nicks by a different color
Michal 'vorner' Vaner <vorner@ucw.cz>
parents: 1268
diff changeset
   353
      (*array_elt)->mucnicklen = blk->prefix.mucnicklen;
e42f48103609 Drawing the MUC nicks by a different color
Michal 'vorner' Vaner <vorner@ucw.cz>
parents: 1268
diff changeset
   354
      (*array_elt)->text       = g_strndup(blk->ptr, maxlen);
184
b5aa2b9c425a [/trunk] Changeset 196 by mikael
mikael
parents: 182
diff changeset
   355
1965
ec737f5f1d6f Couple of readmark-related bugfixes
Mikael Berthe <mikael@lilotux.net>
parents: 1961
diff changeset
   356
      if ((blk->flags & HBB_FLAG_PERSISTENT) &&
ec737f5f1d6f Couple of readmark-related bugfixes
Mikael Berthe <mikael@lilotux.net>
parents: 1961
diff changeset
   357
          (blk->prefix.flags & ~HBB_PREFIX_READMARK)) {
ec737f5f1d6f Couple of readmark-related bugfixes
Mikael Berthe <mikael@lilotux.net>
parents: 1961
diff changeset
   358
        // This is a new message: persistent block flag and no prefix flag
ec737f5f1d6f Couple of readmark-related bugfixes
Mikael Berthe <mikael@lilotux.net>
parents: 1961
diff changeset
   359
        // (except a possible readmark flag)
729
39f67cade02c Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
   360
        last_persist_prefixflags = blk->prefix.flags;
39f67cade02c Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
   361
      } else {
1204
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1141
diff changeset
   362
        // Propagate highlighting flags
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1141
diff changeset
   363
        (*array_elt)->flags |= last_persist_prefixflags &
1268
dbc907b2d92f Add configurable colors for info and incoming messages (Michal 'vorner' Vaner)
Mikael Berthe <mikael@lilotux.net>
parents: 1241
diff changeset
   364
                               (HBB_PREFIX_HLIGHT_OUT | HBB_PREFIX_HLIGHT |
1948
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   365
                                HBB_PREFIX_INFO | HBB_PREFIX_IN |
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   366
                                HBB_PREFIX_READMARK);
1485
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   367
        // Continuation of a message - omit the prefix
1268
dbc907b2d92f Add configurable colors for info and incoming messages (Michal 'vorner' Vaner)
Mikael Berthe <mikael@lilotux.net>
parents: 1241
diff changeset
   368
        (*array_elt)->flags |= HBB_PREFIX_CONT;
1485
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   369
        (*array_elt)->mucnicklen = 0; // The nick is in the first one
1970
10eead2ba1b7 Fix another readmark issue with line wrapping
Mikael Berthe <mikael@lilotux.net>
parents: 1967
diff changeset
   370
10eead2ba1b7 Fix another readmark issue with line wrapping
Mikael Berthe <mikael@lilotux.net>
parents: 1967
diff changeset
   371
        // If there is a readmark on this line, update last_persist_prefixflags
10eead2ba1b7 Fix another readmark issue with line wrapping
Mikael Berthe <mikael@lilotux.net>
parents: 1967
diff changeset
   372
        if (blk->flags & HBB_FLAG_PERSISTENT)
10eead2ba1b7 Fix another readmark issue with line wrapping
Mikael Berthe <mikael@lilotux.net>
parents: 1967
diff changeset
   373
          last_persist_prefixflags |= blk->prefix.flags & HBB_PREFIX_READMARK;
1948
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   374
        // Remove readmark flag from the previous line
1966
88dafee4ee49 Hopefully fix previous commit
Mikael Berthe <mikael@lilotux.net>
parents: 1965
diff changeset
   375
        if (prev_array_elt && last_persist_prefixflags & HBB_PREFIX_READMARK)
1948
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   376
          prev_array_elt->flags &= ~HBB_PREFIX_READMARK;
729
39f67cade02c Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
   377
      }
39f67cade02c Use bold font for outgoing messages
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
   378
1948
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   379
      prev_array_elt = *array_elt;
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   380
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   381
      hbuf = g_list_next(hbuf);
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   382
    } else
184
b5aa2b9c425a [/trunk] Changeset 196 by mikael
mikael
parents: 182
diff changeset
   383
      break;
b5aa2b9c425a [/trunk] Changeset 196 by mikael
mikael
parents: 182
diff changeset
   384
b5aa2b9c425a [/trunk] Changeset 196 by mikael
mikael
parents: 182
diff changeset
   385
    array_elt++;
71
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   386
  }
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   387
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   388
  return array;
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   389
}
1e9d4949bcfd [/trunk] Changeset 85 by mikael
mikael
parents:
diff changeset
   390
370
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   391
//  hbuf_search(hbuf, direction, string)
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   392
// Look backward/forward for a line containing string in the history buffer
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   393
// Search starts at hbuf, and goes forward if direction == 1, backward if -1
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   394
GList *hbuf_search(GList *hbuf, int direction, const char *string)
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   395
{
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   396
  hbuf_block_t *blk;
370
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   397
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   398
  for (;;) {
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   399
    if (direction > 0)
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   400
      hbuf = g_list_next(hbuf);
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   401
    else
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   402
      hbuf = g_list_previous(hbuf);
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   403
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   404
    if (!hbuf) break;
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   405
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   406
    blk = (hbuf_block_t*)(hbuf->data);
370
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   407
    // XXX blk->ptr is (maybe) not really correct, because the match should
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   408
    // not be after ptr_end.  We should check that...
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   409
    if (strcasestr(blk->ptr, string))
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   410
      break;
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   411
  }
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   412
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   413
  return hbuf;
dd9e2eb52916 Add /buffer search_{backward,forward}
Mikael Berthe <mikael@lilotux.net>
parents: 368
diff changeset
   414
}
462
d580e87c11ed Add "/buffer %n"
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
   415
464
e4840b288be0 Add "/buffer date"
Mikael Berthe <mikael@lilotux.net>
parents: 462
diff changeset
   416
//  hbuf_jump_date(hbuf, t)
e4840b288be0 Add "/buffer date"
Mikael Berthe <mikael@lilotux.net>
parents: 462
diff changeset
   417
// Return a pointer to the first line after date t in the history buffer
e4840b288be0 Add "/buffer date"
Mikael Berthe <mikael@lilotux.net>
parents: 462
diff changeset
   418
GList *hbuf_jump_date(GList *hbuf, time_t t)
e4840b288be0 Add "/buffer date"
Mikael Berthe <mikael@lilotux.net>
parents: 462
diff changeset
   419
{
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   420
  hbuf_block_t *blk;
464
e4840b288be0 Add "/buffer date"
Mikael Berthe <mikael@lilotux.net>
parents: 462
diff changeset
   421
e4840b288be0 Add "/buffer date"
Mikael Berthe <mikael@lilotux.net>
parents: 462
diff changeset
   422
  hbuf = g_list_first(hbuf);
e4840b288be0 Add "/buffer date"
Mikael Berthe <mikael@lilotux.net>
parents: 462
diff changeset
   423
e4840b288be0 Add "/buffer date"
Mikael Berthe <mikael@lilotux.net>
parents: 462
diff changeset
   424
  for ( ; hbuf && g_list_next(hbuf); hbuf = g_list_next(hbuf)) {
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   425
    blk = (hbuf_block_t*)(hbuf->data);
464
e4840b288be0 Add "/buffer date"
Mikael Berthe <mikael@lilotux.net>
parents: 462
diff changeset
   426
    if (blk->prefix.timestamp >= t) break;
e4840b288be0 Add "/buffer date"
Mikael Berthe <mikael@lilotux.net>
parents: 462
diff changeset
   427
  }
e4840b288be0 Add "/buffer date"
Mikael Berthe <mikael@lilotux.net>
parents: 462
diff changeset
   428
e4840b288be0 Add "/buffer date"
Mikael Berthe <mikael@lilotux.net>
parents: 462
diff changeset
   429
  return hbuf;
e4840b288be0 Add "/buffer date"
Mikael Berthe <mikael@lilotux.net>
parents: 462
diff changeset
   430
}
e4840b288be0 Add "/buffer date"
Mikael Berthe <mikael@lilotux.net>
parents: 462
diff changeset
   431
462
d580e87c11ed Add "/buffer %n"
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
   432
//  hbuf_jump_percent(hbuf, pc)
d580e87c11ed Add "/buffer %n"
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
   433
// Return a pointer to the line at % pc of the history buffer
d580e87c11ed Add "/buffer %n"
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
   434
GList *hbuf_jump_percent(GList *hbuf, int pc)
d580e87c11ed Add "/buffer %n"
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
   435
{
d580e87c11ed Add "/buffer %n"
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
   436
  guint hlen;
d580e87c11ed Add "/buffer %n"
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
   437
d580e87c11ed Add "/buffer %n"
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
   438
  hbuf = g_list_first(hbuf);
d580e87c11ed Add "/buffer %n"
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
   439
  hlen = g_list_length(hbuf);
d580e87c11ed Add "/buffer %n"
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
   440
d580e87c11ed Add "/buffer %n"
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
   441
  return g_list_nth(hbuf, pc*hlen/100);
d580e87c11ed Add "/buffer %n"
Mikael Berthe <mikael@lilotux.net>
parents: 393
diff changeset
   442
}
576
8b3db0b555a1 Add Vim modelines
Mikael Berthe <mikael@lilotux.net>
parents: 502
diff changeset
   443
1956
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   444
//  hbuf_jump_readmark(hbuf)
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   445
// Return a pointer to the line following the readmark
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   446
// or NULL if no mark was found.
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   447
GList *hbuf_jump_readmark(GList *hbuf)
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   448
{
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   449
  hbuf_block_t *blk;
1967
f016c9c9d992 Update hbuf_jump_readmark()
Mikael Berthe <mikael@lilotux.net>
parents: 1966
diff changeset
   450
  GList *r = NULL;
1956
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   451
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   452
  hbuf = g_list_last(hbuf);
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   453
  for ( ; hbuf; hbuf = g_list_previous(hbuf)) {
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   454
    blk = (hbuf_block_t*)(hbuf->data);
1956
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   455
    if (blk->prefix.flags & HBB_PREFIX_READMARK)
1967
f016c9c9d992 Update hbuf_jump_readmark()
Mikael Berthe <mikael@lilotux.net>
parents: 1966
diff changeset
   456
      return r;
f016c9c9d992 Update hbuf_jump_readmark()
Mikael Berthe <mikael@lilotux.net>
parents: 1966
diff changeset
   457
    if ((blk->flags & HBB_FLAG_PERSISTENT) &&
f016c9c9d992 Update hbuf_jump_readmark()
Mikael Berthe <mikael@lilotux.net>
parents: 1966
diff changeset
   458
        (blk->prefix.flags & ~HBB_PREFIX_READMARK))
f016c9c9d992 Update hbuf_jump_readmark()
Mikael Berthe <mikael@lilotux.net>
parents: 1966
diff changeset
   459
      r = hbuf;
1956
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   460
  }
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   461
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   462
  return NULL;
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   463
}
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   464
1485
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   465
//  hbuf_dump_to_file(hbuf, filename)
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   466
// Save the buffer to a file.
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   467
void hbuf_dump_to_file(GList *hbuf, const char *filename)
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   468
{
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   469
  hbuf_block_t *blk;
1485
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   470
  hbb_line line;
1547
922e454a8187 Initialize variable
Mikael Berthe <mikael@lilotux.net>
parents: 1487
diff changeset
   471
  guint last_persist_prefixflags = 0;
1486
f83a51eaa5ed Fix multiline output in /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1485
diff changeset
   472
  guint prefixwidth;
1485
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   473
  char pref[96];
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   474
  FILE *fp;
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   475
  struct stat statbuf;
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   476
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   477
  if (!stat(filename, &statbuf)) {
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   478
    scr_LogPrint(LPRINT_NORMAL, "The file already exists.");
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   479
    return;
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   480
  }
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   481
  fp = fopen(filename, "w");
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   482
  if (!fp) {
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   483
    scr_LogPrint(LPRINT_NORMAL, "Unable to open the file.");
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   484
    return;
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   485
  }
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   486
1487
f61ed2466f7c Minor style upgrade
Mikael Berthe <mikael@lilotux.net>
parents: 1486
diff changeset
   487
  prefixwidth = scr_getprefixwidth();
1486
f83a51eaa5ed Fix multiline output in /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1485
diff changeset
   488
  prefixwidth = MIN(prefixwidth, sizeof pref);
f83a51eaa5ed Fix multiline output in /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1485
diff changeset
   489
1485
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   490
  for (hbuf = g_list_first(hbuf); hbuf; hbuf = g_list_next(hbuf)) {
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   491
    int maxlen;
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   492
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   493
    blk = (hbuf_block_t*)(hbuf->data);
1485
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   494
    maxlen = blk->ptr_end - blk->ptr;
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   495
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   496
    memset(&line, 0, sizeof(line));
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   497
    line.timestamp  = blk->prefix.timestamp;
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   498
    line.flags      = blk->prefix.flags;
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   499
    line.mucnicklen = blk->prefix.mucnicklen;
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   500
    line.text       = g_strndup(blk->ptr, maxlen);
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   501
1966
88dafee4ee49 Hopefully fix previous commit
Mikael Berthe <mikael@lilotux.net>
parents: 1965
diff changeset
   502
    if ((blk->flags & HBB_FLAG_PERSISTENT) &&
88dafee4ee49 Hopefully fix previous commit
Mikael Berthe <mikael@lilotux.net>
parents: 1965
diff changeset
   503
        (blk->prefix.flags & ~HBB_PREFIX_READMARK)) {
1485
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   504
      last_persist_prefixflags = blk->prefix.flags;
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   505
    } else {
1966
88dafee4ee49 Hopefully fix previous commit
Mikael Berthe <mikael@lilotux.net>
parents: 1965
diff changeset
   506
      // Propagate necessary highlighting flags
1485
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   507
      line.flags |= last_persist_prefixflags &
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   508
                    (HBB_PREFIX_HLIGHT_OUT | HBB_PREFIX_HLIGHT |
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   509
                     HBB_PREFIX_INFO | HBB_PREFIX_IN);
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   510
      // Continuation of a message - omit the prefix
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   511
      line.flags |= HBB_PREFIX_CONT;
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   512
      line.mucnicklen = 0; // The nick is in the first one
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   513
    }
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   514
1486
f83a51eaa5ed Fix multiline output in /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1485
diff changeset
   515
    scr_line_prefix(&line, pref, prefixwidth);
1485
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   516
    fprintf(fp, "%s%s\n", pref, line.text);
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   517
  }
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   518
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   519
  fclose(fp);
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   520
  return;
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   521
}
0121b6f3047c New command: /buffer save
Mikael Berthe <mikael@lilotux.net>
parents: 1424
diff changeset
   522
1602
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   523
//  hbuf_remove_receipt(hbuf, xep184)
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   524
// Remove the Receipt Flag for the message with the given xep184 id
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   525
// Returns TRUE if it was found and removed, otherwise FALSE
2032
f740c4128f76 Fix receipts handling according to recent XEP updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1970
diff changeset
   526
gboolean hbuf_remove_receipt(GList *hbuf, gconstpointer xep184)
1602
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   527
{
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   528
  hbuf_block_t *blk;
1602
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   529
1817
fb6d20a17584 Improve hbuf_remove_receipt() efficiency
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   530
  hbuf = g_list_last(hbuf);
1602
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   531
1817
fb6d20a17584 Improve hbuf_remove_receipt() efficiency
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   532
  for ( ; hbuf; hbuf = g_list_previous(hbuf)) {
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   533
    blk = (hbuf_block_t*)(hbuf->data);
2032
f740c4128f76 Fix receipts handling according to recent XEP updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1970
diff changeset
   534
    if (!g_strcmp0(blk->prefix.xep184, xep184)) {
f740c4128f76 Fix receipts handling according to recent XEP updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1970
diff changeset
   535
      g_free(blk->prefix.xep184);
1602
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   536
      blk->prefix.xep184 = NULL;
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   537
      blk->prefix.flags ^= HBB_PREFIX_RECEIPT;
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   538
      return TRUE;
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   539
    }
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   540
  }
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   541
  return FALSE;
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   542
}
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   543
1948
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   544
//  hbuf_set_readmark(hbuf, action)
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   545
// Set/Reset the readmark Flag
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   546
// If action is TRUE, set a mark to the latest line,
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   547
// if action is FALSE, remove a previous readmark flag.
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   548
void hbuf_set_readmark(GList *hbuf, gboolean action)
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   549
{
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   550
  hbuf_block_t *blk;
1948
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   551
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   552
  if (!hbuf) return;
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   553
1965
ec737f5f1d6f Couple of readmark-related bugfixes
Mikael Berthe <mikael@lilotux.net>
parents: 1961
diff changeset
   554
  hbuf = hbuf_previous_persistent(g_list_last(hbuf));
1948
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   555
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   556
  if (action) {
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   557
    // Add a readmark flag
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   558
    blk = (hbuf_block_t*)(hbuf->data);
1965
ec737f5f1d6f Couple of readmark-related bugfixes
Mikael Berthe <mikael@lilotux.net>
parents: 1961
diff changeset
   559
    blk->prefix.flags |= HBB_PREFIX_READMARK;
ec737f5f1d6f Couple of readmark-related bugfixes
Mikael Berthe <mikael@lilotux.net>
parents: 1961
diff changeset
   560
1948
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   561
    // Shift hbuf in order to remove previous flags
1956
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   562
    // (maybe it can be optimized out, if there's no risk
1948
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   563
    //  we have several marks)
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   564
    hbuf = g_list_previous(hbuf);
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   565
  }
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   566
1956
f309f343070c Add command /buffer readmark
Mikael Berthe <mikael@lilotux.net>
parents: 1948
diff changeset
   567
  // Remove old mark
1948
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   568
  for ( ; hbuf; hbuf = g_list_previous(hbuf)) {
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   569
    blk = (hbuf_block_t*)(hbuf->data);
1948
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   570
    if (blk->prefix.flags & HBB_PREFIX_READMARK) {
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   571
      blk->prefix.flags &= ~HBB_PREFIX_READMARK;
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   572
      break;
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   573
    }
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   574
  }
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   575
}
e8cebf5fd36c Add readmark support
Mikael Berthe <mikael@lilotux.net>
parents: 1817
diff changeset
   576
1961
39021fd6b0e1 Try to improve readmarks in some cases
Mikael Berthe <mikael@lilotux.net>
parents: 1956
diff changeset
   577
//  hbuf_remove_trailing_readmark(hbuf)
39021fd6b0e1 Try to improve readmarks in some cases
Mikael Berthe <mikael@lilotux.net>
parents: 1956
diff changeset
   578
// Unset the buffer readmark if it is on the last line
39021fd6b0e1 Try to improve readmarks in some cases
Mikael Berthe <mikael@lilotux.net>
parents: 1956
diff changeset
   579
void hbuf_remove_trailing_readmark(GList *hbuf)
39021fd6b0e1 Try to improve readmarks in some cases
Mikael Berthe <mikael@lilotux.net>
parents: 1956
diff changeset
   580
{
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   581
  hbuf_block_t *blk;
1961
39021fd6b0e1 Try to improve readmarks in some cases
Mikael Berthe <mikael@lilotux.net>
parents: 1956
diff changeset
   582
39021fd6b0e1 Try to improve readmarks in some cases
Mikael Berthe <mikael@lilotux.net>
parents: 1956
diff changeset
   583
  if (!hbuf) return;
39021fd6b0e1 Try to improve readmarks in some cases
Mikael Berthe <mikael@lilotux.net>
parents: 1956
diff changeset
   584
39021fd6b0e1 Try to improve readmarks in some cases
Mikael Berthe <mikael@lilotux.net>
parents: 1956
diff changeset
   585
  hbuf = g_list_last(hbuf);
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   586
  blk = (hbuf_block_t*)(hbuf->data);
1961
39021fd6b0e1 Try to improve readmarks in some cases
Mikael Berthe <mikael@lilotux.net>
parents: 1956
diff changeset
   587
  blk->prefix.flags &= ~HBB_PREFIX_READMARK;
39021fd6b0e1 Try to improve readmarks in some cases
Mikael Berthe <mikael@lilotux.net>
parents: 1956
diff changeset
   588
}
39021fd6b0e1 Try to improve readmarks in some cases
Mikael Berthe <mikael@lilotux.net>
parents: 1956
diff changeset
   589
1227
79c396678f1b Make buffer list display the number of allocated HBB blocks
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   590
//  hbuf_get_blocks_number()
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   591
// Returns the number of allocated hbuf_block_t's.
1227
79c396678f1b Make buffer list display the number of allocated HBB blocks
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   592
guint hbuf_get_blocks_number(GList *hbuf)
79c396678f1b Make buffer list display the number of allocated HBB blocks
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   593
{
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   594
  hbuf_block_t *hbuf_b_elt;
1227
79c396678f1b Make buffer list display the number of allocated HBB blocks
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   595
  guint count = 0U;
79c396678f1b Make buffer list display the number of allocated HBB blocks
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   596
79c396678f1b Make buffer list display the number of allocated HBB blocks
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   597
  for (hbuf = g_list_first(hbuf); hbuf; hbuf = g_list_next(hbuf)) {
2337
ffd0e57e9563 Defined types shall end with "_t"
franky
parents: 2268
diff changeset
   598
    hbuf_b_elt = (hbuf_block_t*)(hbuf->data);
1227
79c396678f1b Make buffer list display the number of allocated HBB blocks
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   599
    if (hbuf_b_elt->flags & HBB_FLAG_ALLOC)
79c396678f1b Make buffer list display the number of allocated HBB blocks
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   600
      count++;
79c396678f1b Make buffer list display the number of allocated HBB blocks
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   601
  }
79c396678f1b Make buffer list display the number of allocated HBB blocks
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   602
  return count;
79c396678f1b Make buffer list display the number of allocated HBB blocks
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   603
}
79c396678f1b Make buffer list display the number of allocated HBB blocks
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   604
1811
e6d355e50d7a Update Vim modelines
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
   605
/* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2:  For Vim users... */