1 | #define OWL_PERL |
---|
2 | #include "owl.h" |
---|
3 | |
---|
4 | /* Assumes owenership of one existing ref on `obj`*/ |
---|
5 | void owl_style_create_perl(owl_style *s, const char *name, SV *obj) |
---|
6 | { |
---|
7 | s->name=owl_strdup(name); |
---|
8 | s->perlobj = obj; |
---|
9 | } |
---|
10 | |
---|
11 | int owl_style_matches_name(const owl_style *s, const char *name) |
---|
12 | { |
---|
13 | if (!strcmp(s->name, name)) return(1); |
---|
14 | return(0); |
---|
15 | } |
---|
16 | |
---|
17 | const char *owl_style_get_name(const owl_style *s) |
---|
18 | { |
---|
19 | return(s->name); |
---|
20 | } |
---|
21 | |
---|
22 | const char *owl_style_get_description(const owl_style *s) |
---|
23 | { |
---|
24 | SV *sv = NULL; |
---|
25 | OWL_PERL_CALL_METHOD(s->perlobj, |
---|
26 | "description", |
---|
27 | ;, |
---|
28 | "Error in style_get_description: %s", |
---|
29 | 0, |
---|
30 | sv = SvREFCNT_inc(POPs); |
---|
31 | ); |
---|
32 | if(sv) { |
---|
33 | return SvPV_nolen(sv_2mortal(sv)); |
---|
34 | } else { |
---|
35 | return "[error getting description]"; |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | /* Use style 's' to format message 'm' into fmtext 'fm'. |
---|
40 | * 'fm' should already be be initialzed |
---|
41 | */ |
---|
42 | void owl_style_get_formattext(const owl_style *s, owl_fmtext *fm, const owl_message *m) |
---|
43 | { |
---|
44 | const char *body; |
---|
45 | char *indent; |
---|
46 | int curlen; |
---|
47 | owl_fmtext with_tabs; |
---|
48 | |
---|
49 | SV *sv = NULL; |
---|
50 | |
---|
51 | /* Call the perl object */ |
---|
52 | OWL_PERL_CALL_METHOD(s->perlobj, |
---|
53 | "format_message", |
---|
54 | XPUSHs(sv_2mortal(owl_perlconfig_message2hashref(m)));, |
---|
55 | "Error in format_message: %s", |
---|
56 | 0, |
---|
57 | sv = SvREFCNT_inc(POPs); |
---|
58 | ); |
---|
59 | |
---|
60 | if(sv) { |
---|
61 | body = SvPV_nolen(sv); |
---|
62 | } else { |
---|
63 | body = "<unformatted message>"; |
---|
64 | } |
---|
65 | |
---|
66 | /* indent and ensure ends with a newline */ |
---|
67 | indent = owl_text_indent(body, OWL_TAB); |
---|
68 | curlen = strlen(indent); |
---|
69 | if (curlen==0 || indent[curlen-1] != '\n') { |
---|
70 | indent[curlen] = '\n'; |
---|
71 | indent[curlen+1] = '\0'; |
---|
72 | } |
---|
73 | |
---|
74 | owl_fmtext_init_null(&with_tabs); |
---|
75 | /* fmtext_append. This needs to change */ |
---|
76 | owl_fmtext_append_ztext(&with_tabs, indent); |
---|
77 | /* Expand tabs, taking the indent into account. Otherwise, tabs from the |
---|
78 | * style display incorrectly due to our own indent. */ |
---|
79 | owl_fmtext_expand_tabs(&with_tabs, fm, OWL_TAB_WIDTH - OWL_TAB); |
---|
80 | owl_fmtext_cleanup(&with_tabs); |
---|
81 | |
---|
82 | g_free(indent); |
---|
83 | if(sv) |
---|
84 | SvREFCNT_dec(sv); |
---|
85 | } |
---|
86 | |
---|
87 | int owl_style_validate(const owl_style *s) { |
---|
88 | if (!s || !s->perlobj || !SvOK(s->perlobj)) { |
---|
89 | return -1; |
---|
90 | } |
---|
91 | return 0; |
---|
92 | } |
---|
93 | |
---|
94 | void owl_style_cleanup(owl_style *s) |
---|
95 | { |
---|
96 | if (s->name) g_free(s->name); |
---|
97 | SvREFCNT_dec(s->perlobj); |
---|
98 | } |
---|
99 | |
---|
100 | void owl_style_delete(owl_style *s) |
---|
101 | { |
---|
102 | owl_style_cleanup(s); |
---|
103 | g_free(s); |
---|
104 | } |
---|