/
eguile-utilities.scm
118 lines (106 loc) · 4.62 KB
/
eguile-utilities.scm
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
;; eguile-utilities.scm
;; Useful functions for eguile-based reports.
;; See also eguile-html-utilities.scm
;; Version 0.01
;;
;; (c) 2009 Chris Dennis chris@starsoftanalysis.co.uk
;;
;; $Author: chris $ $Date: 2009/07/27 15:51:02 $ $Revision: 1.28 $
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
;; 02111-1307 USA
(use-modules (ice-9 match))
; using all of these seems like overkill --
; not sure which are really required
(use-modules (gnucash engine))
(use-modules (gnucash utilities))
(use-modules (gnucash app-utils))
(use-modules (gnucash core-utils))
(use-modules (gnucash report))
(define-public (fmtnumber n)
;; Format a number (integer or real) into something printable
(number->string (if (integer? n) (inexact->exact n) n)))
;; Format gnc-numeric n with as many decimal places as required
(define-public fmtnumeric fmtnumber)
(define-public (gnc-monetary-neg? monetary)
;; return true if the monetary value is negative
(issue-deprecated-warning "gnc-monetary-neg? is deprecated")
(negative? (gnc:gnc-monetary-amount monetary)))
;; 'Safe' versions of cdr and cadr that don't crash
;; if the list is empty (is there a better way?)
(define-public safe-cdr
;; deprecate
(match-lambda
((_ . x) x)
(_ '())))
(define-public safe-cadr
;; deprecate
(match-lambda
((_ x . y) x)
(_ '())))
; deprecated - use find-stylesheet or find-template instead
(define-public (find-file fname)
;; Find the file 'fname', and return its full path.
;; First look in the user's .config/gnucash directory.
;; Then look in Gnucash's standard report directory.
;; If no file is found, returns just 'fname' for use in error messages.
(let* ((stylesheetpath (find-stylesheet fname))
(templatepath (find-template fname)))
; make sure there's a trailing delimiter
(issue-deprecation-warning "find-file is deprecated. Please use find-stylesheet or find-template instead.")
(cond
((access? stylesheetpath R_OK) stylesheetpath)
((access? templatepath R_OK) templatepath)
(else fname))))
(define (find-internal ftype fname)
;; Find the file fname', and return its full path.
;; First look in the user's .config/gnucash directory.
;; Then look in Gnucash' gnucash/reports/'ftype' directory.
;; If no file is found, returns just 'fname' for use in error messages.
(let* ((userpath (gnc-build-userdata-path fname))
(frelpath (string-join (list (symbol->string ftype) fname) "/"))
(syspath (gnc-build-reports-path frelpath)))
(if (access? userpath R_OK)
userpath
(if (access? syspath R_OK)
syspath
fname))))
(define-public (find-stylesheet fname)
;; Find the stylesheet 'fname', and return its full path.
;; First look in the user's .config/gnucash directory.
;; Then look in Gnucash' gnucash/reports/stylesheets directory.
;; If no file is found, returns just 'fname' for use in error messages.
(find-internal 'stylesheets fname))
(define-public (find-template fname)
;; Find the template 'ftype'/'fname', and return its full path.
;; First look in the user's .config/gnucash directory.
;; Then look in Gnucash' gnucash/reports/templates directory.
;; If no file is found, returns just 'fname' for use in error messages.
(find-internal 'templates fname))
; Define syntax for more readable for loops (the built-in for-each requires an
; explicit lambda and has the list expression all the way at the end).
(export for)
(define-syntax for
(syntax-rules (for in do)
;; Multiple variables and equal number of lists (in
;; parenthesis). e.g.:
;; (for (a b) in (lsta lstb) do (display (+ a b)))
;; Note that this template must be defined before the
;; next one, since the template are evaluated in-order.
((for (<var> ...) in (<list> ...) do <expr> ...)
(for-each (lambda (<var> ...) <expr> ...) <list> ...))
;; Single variable and list. e.g.: (for a in lst do (display a))
((for <var> in <list> do <expr> ...)
(for-each (lambda (<var>) <expr> ...) <list>))))