Personal finances application for Emacs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

84 lines
2.6 KiB

  1. ;;; elbank-transaction.el --- Major mode for displaying a transaction -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017-2018 Nicolas Petton
  3. ;; Author: Nicolas Petton <nicolas@petton.fr>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;;
  16. ;;; Code:
  17. (require 'seq)
  18. (require 'map)
  19. (require 'elbank-common)
  20. (defvar elbank-transaction-mode-map
  21. (let ((map (make-sparse-keymap)))
  22. (define-key map (kbd "q") #'elbank-quit)
  23. map)
  24. "Keymap for `elbank-transaction-mode'.")
  25. (define-derived-mode elbank-transaction-mode nil "Elbank Transaction"
  26. "Major mode for viewing a single transaction.
  27. \\{elbank-transaction-mode-map}"
  28. (read-only-mode))
  29. ;;;###autoload
  30. (defun elbank-show-transaction (transaction)
  31. "Show the details of TRANSACTION in a separate buffer."
  32. (let ((buf (get-buffer-create "*Elbank transaction*")))
  33. (pop-to-buffer buf)
  34. (elbank-transaction-mode)
  35. (elbank-transaction--refresh transaction)))
  36. (cl-defgeneric elbank-transaction--field (transaction key)
  37. "Return the label of the KEY of TRANSACTION."
  38. (map-elt transaction key))
  39. (cl-defmethod elbank-transaction--field (transaction (_key (eql account)))
  40. (elbank-account-name (elbank-transaction-elt transaction 'account)))
  41. (defun elbank-transaction--refresh (transaction)
  42. "Populate the current buffer with the details of TRANSACTION."
  43. (let* ((inhibit-read-only t)
  44. (keys (map-keys transaction))
  45. (width (1+ (seq-reduce (lambda (acc elt)
  46. (max acc (seq-length (symbol-name elt))))
  47. keys
  48. 0))))
  49. (erase-buffer)
  50. (seq-doseq (key (seq-sort (lambda (k1 k2)
  51. (string-lessp (symbol-name k1)
  52. (symbol-name k2)))
  53. keys))
  54. (let ((label (capitalize (format "%s:" key))))
  55. (dotimes (_ (- width (seq-length label)))
  56. (insert " "))
  57. (insert label)
  58. (put-text-property (point-at-bol) (point)
  59. 'face
  60. 'font-lock-keyword-face)
  61. (insert " ")
  62. (insert (format "%s" (elbank-transaction--field transaction key)))
  63. (insert "\n")))))
  64. (provide 'elbank-transaction)
  65. ;;; elbank-transaction.el ends here