source: style.c @ 0d53dfb

Last change on this file since 0d53dfb was 7dcef03, checked in by Anders Kaseorg <andersk@mit.edu>, 8 years ago
Use the Glib slice allocator for fixed-size objects The slice allocator, available since GLib 2.10, is more space-efficient than [g_]malloc. Since BarnOwl is obviously at the leading edge of space-efficient technology, this seems like a natural fit. Use it for every fixed-size object except owl_viewwin_search_data (which would need an extra destroy_cbdata function to g_slice_free it). Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 2.5 KB
RevLine 
[864ed35]1#define OWL_PERL
[bd3f232]2#include "owl.h"
3
[fde6d80]4/* Assumes owenership of one existing ref on `obj`*/
[e19eb97]5void owl_style_create_perl(owl_style *s, const char *name, SV *obj)
[bd3f232]6{
[d4927a7]7  s->name=g_strdup(name);
[fde6d80]8  s->perlobj = obj;
[bd3f232]9}
10
[1fdab04]11int owl_style_matches_name(const owl_style *s, const char *name)
[bd3f232]12{
13  if (!strcmp(s->name, name)) return(1);
14  return(0);
15}
16
[1fdab04]17const char *owl_style_get_name(const owl_style *s)
[ef56a67]18{
19  return(s->name);
20}
21
[1fdab04]22const char *owl_style_get_description(const owl_style *s)
[f1e629d]23{
[864ed35]24  SV *sv = NULL;
[92ffd89]25  OWL_PERL_CALL(call_method("description", G_SCALAR|G_EVAL),
26                XPUSHs(s->perlobj);,
27                "Error in style_get_description: %s",
28                0,
29                false,
30                sv = SvREFCNT_inc(POPs);
31                );
32  if (sv) {
[864ed35]33    return SvPV_nolen(sv_2mortal(sv));
34  } else {
35    return "[error getting description]";
36  }
[f1e629d]37}
38
[5639bf2]39/* Use style 's' to format message 'm' into fmtext 'fm'.
40 * 'fm' should already be be initialzed
41 */
[1fdab04]42void owl_style_get_formattext(const owl_style *s, owl_fmtext *fm, const owl_message *m)
[bd3f232]43{
[e19eb97]44  const char *body;
[65b2173]45  char *indent;
[864ed35]46  int curlen;
[30da473]47  owl_fmtext with_tabs;
[bd3f232]48
[864ed35]49  SV *sv = NULL;
50 
51  /* Call the perl object */
[92ffd89]52  OWL_PERL_CALL(call_method("format_message", G_SCALAR|G_EVAL),
53                XPUSHs(s->perlobj);
54                XPUSHs(sv_2mortal(owl_perlconfig_message2hashref(m)));,
55                "Error in format_message: %s",
56                0,
57                false,
58                sv = SvREFCNT_inc(POPs);
59                );
[282ec9b]60
[92ffd89]61  if (sv) {
[864ed35]62    body = SvPV_nolen(sv);
63  } else {
64    body = "<unformatted message>";
[bd3f232]65  }
[864ed35]66
67  /* indent and ensure ends with a newline */
[14be3a5]68  indent = owl_text_indent(body, OWL_TAB, true);
[864ed35]69  curlen = strlen(indent);
[fa180a3]70  if (curlen == 0 || indent[curlen-1] != '\n') {
71    char *tmp = indent;
72    indent = g_strconcat(tmp, "\n", NULL);
73    g_free(tmp);
[864ed35]74  }
75
[30da473]76  owl_fmtext_init_null(&with_tabs);
[864ed35]77  /* fmtext_append.  This needs to change */
[30da473]78  owl_fmtext_append_ztext(&with_tabs, indent);
79  /* Expand tabs, taking the indent into account. Otherwise, tabs from the
80   * style display incorrectly due to our own indent. */
81  owl_fmtext_expand_tabs(&with_tabs, fm, OWL_TAB_WIDTH - OWL_TAB);
82  owl_fmtext_cleanup(&with_tabs);
[864ed35]83
[ddbbcffa]84  g_free(indent);
[864ed35]85  if(sv)
[e574a00]86    SvREFCNT_dec(sv);
[bd3f232]87}
88
[1fdab04]89int owl_style_validate(const owl_style *s) {
[864ed35]90  if (!s || !s->perlobj || !SvOK(s->perlobj)) {
[f1e629d]91    return -1;
92  }
[864ed35]93  return 0;
[f1e629d]94}
95
[b585ba2]96void owl_style_cleanup(owl_style *s)
[bd3f232]97{
[ddbbcffa]98  if (s->name) g_free(s->name);
[864ed35]99  SvREFCNT_dec(s->perlobj);
[bd3f232]100}
[516c27e]101
102void owl_style_delete(owl_style *s)
103{
[b585ba2]104  owl_style_cleanup(s);
[7dcef03]105  g_slice_free(owl_style, s);
[516c27e]106}
Note: See TracBrowser for help on using the repository browser.