source: style.c @ 67f6d7d

release-1.10release-1.9
Last change on this file since 67f6d7d was 14be3a5, checked in by Jason Gross <jgross@mit.edu>, 13 years ago
Give owl_text_indent an indent_first_line parameter This is useful if you need to indent text that isn't broken into chunks that end with newlines. This is primarily in preparation for the next commit, where we need to replace the indent on the first line by a prefix. The other (reasonable) option is to make owl_text_indent always prefix the string it's given with an indent, even when it's given the empty string. This would break the nice property that indent(A + B) = indent(A) + indent(B) whenever A ended with a newline. After some discussion on zephyr and on the github pull request, I decided to go with this option.
  • 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;
25  OWL_PERL_CALL_METHOD(s->perlobj,
26                       "description",
[c0ddaea]27                       ;,
[864ed35]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  }
[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 */
52  OWL_PERL_CALL_METHOD(s->perlobj,
53                       "format_message",
[1639ff7]54                       XPUSHs(sv_2mortal(owl_perlconfig_message2hashref(m)));,
[864ed35]55                       "Error in format_message: %s",
56                       0,
57                       sv = SvREFCNT_inc(POPs);
58                       );
[282ec9b]59
[864ed35]60  if(sv) {
61    body = SvPV_nolen(sv);
62  } else {
63    body = "<unformatted message>";
[bd3f232]64  }
[864ed35]65
66  /* indent and ensure ends with a newline */
[14be3a5]67  indent = owl_text_indent(body, OWL_TAB, true);
[864ed35]68  curlen = strlen(indent);
[fa180a3]69  if (curlen == 0 || indent[curlen-1] != '\n') {
70    char *tmp = indent;
71    indent = g_strconcat(tmp, "\n", NULL);
72    g_free(tmp);
[864ed35]73  }
74
[30da473]75  owl_fmtext_init_null(&with_tabs);
[864ed35]76  /* fmtext_append.  This needs to change */
[30da473]77  owl_fmtext_append_ztext(&with_tabs, indent);
78  /* Expand tabs, taking the indent into account. Otherwise, tabs from the
79   * style display incorrectly due to our own indent. */
80  owl_fmtext_expand_tabs(&with_tabs, fm, OWL_TAB_WIDTH - OWL_TAB);
81  owl_fmtext_cleanup(&with_tabs);
[864ed35]82
[ddbbcffa]83  g_free(indent);
[864ed35]84  if(sv)
[e574a00]85    SvREFCNT_dec(sv);
[bd3f232]86}
87
[1fdab04]88int owl_style_validate(const owl_style *s) {
[864ed35]89  if (!s || !s->perlobj || !SvOK(s->perlobj)) {
[f1e629d]90    return -1;
91  }
[864ed35]92  return 0;
[f1e629d]93}
94
[b585ba2]95void owl_style_cleanup(owl_style *s)
[bd3f232]96{
[ddbbcffa]97  if (s->name) g_free(s->name);
[864ed35]98  SvREFCNT_dec(s->perlobj);
[bd3f232]99}
[516c27e]100
101void owl_style_delete(owl_style *s)
102{
[b585ba2]103  owl_style_cleanup(s);
[ddbbcffa]104  g_free(s);
[516c27e]105}
Note: See TracBrowser for help on using the repository browser.