source: style.c @ 282ec9b

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 282ec9b was 282ec9b, checked in by Erik Nygren <nygren@mit.edu>, 18 years ago
Make sure that a newline is always at the end of messages returned by perl style formatting functions. Add owl::login to legacy variables populated for format_msg.
  • Property mode set to 100644
File size: 2.4 KB
Line 
1#include "owl.h"
2
3static const char fileIdent[] = "$Id$";
4
5void owl_style_create_internal(owl_style *s, char *name, void (*formatfunc) (owl_fmtext *fm, owl_message *m), char *description)
6{
7  s->type=OWL_STYLE_TYPE_INTERNAL;
8  s->name=owl_strdup(name);
9  if (description) {
10    s->description=owl_strdup(description);
11  } else {
12    s->description=owl_sprintf("Owl internal style %s", name);
13  }
14  s->perlfuncname=NULL;
15  s->formatfunc=formatfunc;
16}
17
18void owl_style_create_perl(owl_style *s, char *name, char *perlfuncname, char *description)
19{
20  s->type=OWL_STYLE_TYPE_PERL;
21  s->name=owl_strdup(name);
22  s->perlfuncname=owl_strdup(perlfuncname);
23  if (description) {
24    s->description=owl_strdup(description);
25  } else {
26    s->description=owl_sprintf("User-defined perl style that calls %s", 
27                               perlfuncname);
28  }
29  s->formatfunc=NULL;
30}
31
32int owl_style_matches_name(owl_style *s, char *name)
33{
34  if (!strcmp(s->name, name)) return(1);
35  return(0);
36}
37
38char *owl_style_get_name(owl_style *s)
39{
40  return(s->name);
41}
42
43char *owl_style_get_description(owl_style *s)
44{
45  return(s->description);
46}
47
48/* Use style 's' to format message 'm' into fmtext 'fm'.
49 * 'fm' should already be be initialzed
50 */
51void owl_style_get_formattext(owl_style *s, owl_fmtext *fm, owl_message *m)
52{
53  if (s->type==OWL_STYLE_TYPE_INTERNAL) {
54    (* s->formatfunc)(fm, m);
55  } else if (s->type==OWL_STYLE_TYPE_PERL) {
56    char *body, *indent, curlen;
57
58    /* run the perl function */
59    body=owl_perlconfig_getmsg(m, 1, s->perlfuncname);
60   
61    /* indent and ensure ends with a newline */
62    indent=owl_malloc(strlen(body)+(owl_text_num_lines(body))*OWL_TAB+10);
63    owl_text_indent(indent, body, OWL_TAB);
64    curlen = strlen(indent);
65    if (curlen==0 || indent[curlen-1] != '\n') {
66      indent[curlen] = '\n';
67      indent[curlen+1] = '\0';
68    }
69
70    /* fmtext_append.  This needs to change */
71    owl_fmtext_append_ztext(fm, indent);
72   
73    owl_free(indent);
74    owl_free(body);
75  }
76}
77
78int owl_style_validate(owl_style *s) {
79  if (!s) {
80    return -1;
81  } else if (s->type==OWL_STYLE_TYPE_INTERNAL) {
82    return 0;
83  } else if (s->type==OWL_STYLE_TYPE_PERL
84             && s->perlfuncname
85             && owl_perlconfig_is_function(s->perlfuncname)) {
86    return 0;
87  } else {
88    return -1;
89  }
90}
91
92void owl_style_free(owl_style *s)
93{
94  if (s->name) owl_free(s->name);
95  if (s->description) owl_free(s->description);
96  if (s->type==OWL_STYLE_TYPE_PERL && s->perlfuncname) {
97    owl_free(s->perlfuncname);
98  }
99}
Note: See TracBrowser for help on using the repository browser.