mu/guile/examples/contacts-export

86 lines
2.6 KiB
Plaintext
Raw Normal View History

#!/bin/sh
exec guile -e main -s $0 $@
!#
;;
;; Copyright (C) 2012 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
;;
;; 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 3, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2012-01-09 07:23:56 +01:00
(use-modules (ice-9 getopt-long) (ice-9 format))
2011-12-30 11:36:59 +01:00
(use-modules (srfi srfi-1))
(use-modules (mu))
2011-12-23 15:48:12 +01:00
(define (sort-by-freq c1 c2)
2012-01-15 13:36:09 +01:00
(< (mu:frequency c1) (mu:frequency c2)))
2011-12-23 15:48:12 +01:00
(define (sort-by-newness c1 c2)
2012-01-22 10:44:52 +01:00
(< (mu:last-seen c1) (mu:last-seen c2)))
(define (main args)
(let* ((optionspec '( (muhome (value #t))
(sort-by (value #t))
(revert (value #f))
2011-12-30 11:36:59 +01:00
(format (value #t))
(limit (value #t))
(help (single-char #\h) (value #f))))
(options (getopt-long args optionspec))
(msg (string-append
2011-12-30 11:36:59 +01:00
"usage: contacts-export [--help] [--muhome=<muhome>] "
"--format=<org-contact|mutt-alias|mutt-ab|wanderlust|quoted|plain(*)> "
2011-12-30 11:36:59 +01:00
"--sort-by=<frequency(*)|newness> [--revert] [--limit=<n>]\n"))
(help (option-ref options 'help #f))
2011-12-23 15:48:12 +01:00
(muhome (option-ref options 'muhome #f))
2011-12-30 11:36:59 +01:00
(sort-by (or (option-ref options 'sort-by #f) "frequency"))
(revert (option-ref options 'revert #f))
2011-12-30 11:36:59 +01:00
(form (or (option-ref options 'format #f) "plain"))
2012-01-01 17:17:29 +01:00
(limit (string->number (option-ref options 'limit "1000000"))))
(if help
(begin
(display msg)
(exit 0))
(begin
2012-01-09 07:23:56 +01:00
(setlocale LC_ALL "")
(mu:initialize muhome)
(let* ((sort-func
(cond
2012-01-09 07:23:56 +01:00
((string= sort-by "frequency") sort-by-freq)
((string= sort-by "newness") sort-by-newness)
2011-12-30 11:36:59 +01:00
(else (begin (display msg) (exit 1)))))
(contacts '()))
;; make a list of all contacts
2012-01-01 17:17:29 +01:00
(mu:for-each-contact
2011-12-30 11:36:59 +01:00
(lambda (c) (set! contacts (cons c contacts))))
2011-12-30 11:36:59 +01:00
;; should we sort it?
(if sort-by
(set! contacts (sort! contacts
(if revert (negate sort-func) sort-func))))
;; should we limit the number?
2012-01-01 17:17:29 +01:00
(if (and limit (< limit (length contacts)))
2011-12-30 11:36:59 +01:00
(set! contacts (take! contacts limit)))
;; export!
2011-12-23 15:48:12 +01:00
(for-each
2011-12-30 11:36:59 +01:00
(lambda (c)
2012-01-22 10:44:52 +01:00
(format #t "~a\n" (mu:contact->string c form)))
2011-12-30 11:36:59 +01:00
contacts))))))
2011-12-23 15:48:12 +01:00
;; Local Variables:
;; mode: scheme
;; End: