summaryrefslogtreecommitdiff
path: root/emacs.cc
blob: 63e5332cb4f1350e719a2e0b67e425183ed7aaeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "emacs.h"

namespace ledger {

void format_emacs_transactions::write_entry(entry_t& entry)
{
  int idx = entry.src_idx;

  for (strings_list::iterator i = entry.journal->sources.begin();
       i != entry.journal->sources.end();
       i++)
    if (! idx--) {
      out << "\"" << *i << "\" ";
      break;
    }

  out << (((unsigned long)entry.beg_pos) + 1) << " ";

  switch (entry.state) {
  case entry_t::CLEARED:
    out << "t ";
    break;
  case entry_t::PENDING:
    out << "pending ";
    break;
  case entry_t::UNCLEARED:
    out << "nil ";
    break;
  }

  out << "(" << (entry.date / 65536) << " "
      << (entry.date % 65536) << " 0) ";

  if (entry.code.empty())
    out << "nil ";
  else
    out << "\"" << entry.code << "\" ";

  if (entry.payee.empty())
    out << "nil";
  else
    out << "\"" << entry.payee << "\"";

  out << "\n";
}

void format_emacs_transactions::operator()(transaction_t& xact)
{
  if (! transaction_has_xdata(xact) ||
      ! (transaction_xdata_(xact).dflags & TRANSACTION_DISPLAYED)) {
    if (! last_entry) {
      out << "((";
      write_entry(*xact.entry);
    }
    else if (xact.entry != last_entry) {
      out << ")\n (";
      write_entry(*xact.entry);
    }
    else {
      out << "\n";
    }

    out << "  (\"" << xact.account->fullname() << "\" \""
	<< xact.amount << "\"";
    if (xact.cost)
      out << " \"" << *xact.cost << "\"";
    else if (! xact.note.empty())
      out << " nil";
    if (! xact.note.empty())
      out << " \"" << xact.note << "\"";
    out << ")";

    last_entry = xact.entry;

    transaction_xdata(xact).dflags |= TRANSACTION_DISPLAYED;
  }
}

} // namespace ledger

#ifdef USE_BOOST_PYTHON

#include <boost/python.hpp>

using namespace boost::python;
using namespace ledger;

void export_emacs()
{
  typedef
    pystream_handler_wrap<format_emacs_transactions, transaction_t>
    format_emacs_transactions_wrap;

  class_< format_emacs_transactions_wrap, bases<item_handler<transaction_t> > >
    ("FormatEmacsTransactions",
     init<PyObject *>()[with_custodian_and_ward<1, 2>()])
    .def("flush", &format_emacs_transactions_wrap::flush)
    .def("__call__", &format_emacs_transactions_wrap::operator())
    ;
}

#endif // USE_BOOST_PYTHON