1
0
mirror of https://github.com/djcb/mu.git synced 2024-06-28 07:41:04 +02:00
mu/guile/examples/contacts-export
2012-01-01 18:17:29 +02:00

97 lines
2.9 KiB
Scheme
Executable File

#!/bin/sh
exec guile -e main -s $0 $@
!#
;;
;; Copyright (C) 2011 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.
(use-modules (ice-9 getopt-long))
(use-modules (srfi srfi-1))
(use-modules (mu) (mu contact))
(define (sort-by-freq c1 c2)
(< (frequency c1) (frequency c2)))
(define (sort-by-newness c1 c2)
(< (timestamp c1) (timestamp c2)))
(define (export-contact contact form)
(cond
((string= form "org-contacts")
(format #t "* ~a\n:PROPERTIES:\n:EMAIL:~a\n:END:\n\n"
(or (name contact) (email contact)) (email contact)))
((string= form "plain")
(format #t "~a~a\n"
(or (name contact) "")
(if (name contact)
(string-append " <" (email contact) ">")
(email contact))))))
(define (main args)
(let* ((optionspec '( (muhome (value #t))
(sort-by (value #t))
(revert (value #f))
(format (value #t))
(limit (value #t))
(help (single-char #\h) (value #f))))
(options (getopt-long args optionspec))
(msg (string-append
"usage: contacts-export [--help] [--muhome=<muhome>] "
"--format=<org-contacts|wl|mutt-ab|plain(*)> "
"--sort-by=<frequency(*)|newness> [--revert] [--limit=<n>]\n"))
(help (option-ref options 'help #f))
(muhome (option-ref options 'muhome #f))
(sort-by (or (option-ref options 'sort-by #f) "frequency"))
(revert (option-ref options 'revert #f))
(form (or (option-ref options 'format #f) "plain"))
(limit (string->number (option-ref options 'limit "1000000"))))
(if help
(begin
(display msg)
(exit 0))
(begin
(mu:initialize muhome)
(let* ((sort-func
(cond
((string= sort-by "frequency") sort-by-freq)
((string= sort-by "newness") sort-by-newness)
(else (begin (display msg) (exit 1)))))
(contacts '()))
;; make a list of all contacts
(mu:for-each-contact
(lambda (c) (set! contacts (cons c contacts))))
;; should we sort it?
(if sort-by
(set! contacts (sort! contacts
(if revert (negate sort-func) sort-func))))
;; should we limit the number?
(if (and limit (< limit (length contacts)))
(set! contacts (take! contacts limit)))
;; export!
(for-each
(lambda (c)
(export-contact c form))
contacts))))))
;; Local Variables:
;; mode: scheme
;; End: