Emacs package integrating khard, a console cardav client
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.

124 lines
4.3KB

  1. ;;; khardel.el --- Integrate with khard -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2018 Damien Cassou
  3. ;; Author: Damien Cassou <damien@cassou.me>
  4. ;; Url: https://gitlab.petton.fr/DamienCassou/khardel
  5. ;; Package-requires: ((emacs "25.1") (yaml-mode "0.0.13"))
  6. ;; Version: 0.1.0
  7. ;; This program is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; This program is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Integrate with khard, a console carddav application to search and
  19. ;; edit contacts in carddav/vcard format.
  20. ;;; Code:
  21. (require 'map)
  22. (require 'yaml-mode)
  23. (defgroup khardel nil
  24. "Integrate with khard."
  25. :group 'external)
  26. (defcustom khardel-command (executable-find "khard")
  27. "Path to the khard executable file."
  28. :type 'file)
  29. (defun khardel--list-contacts ()
  30. "Return a map whose keys are names and values are contacts."
  31. (save-match-data
  32. (with-temp-buffer
  33. (call-process khardel-command nil t nil "ls" "--parsable")
  34. (goto-char (point-min))
  35. (let ((contacts (make-hash-table :test 'equal)))
  36. (cl-loop
  37. while (re-search-forward "^\\([a-z0-9]*\\)\t\\(.*\\)\t[^\t]*$" nil t)
  38. do (map-put contacts (match-string 2) (cons (match-string 1) (match-string 2)))
  39. finally return contacts)))))
  40. (defun khardel--list-emails ()
  41. "Return a list of strings of the form \"Name <email>\"."
  42. (save-match-data
  43. (with-temp-buffer
  44. (call-process khardel-command nil t nil "email" "--parsable" "--remove-first-line")
  45. (goto-char (point-min))
  46. (cl-loop
  47. while (re-search-forward "^\\([^\t\n]*\\)\t\\([^\t\n]*\\)\t.*$" nil t)
  48. collect (format "%s <%s>" (match-string 2) (match-string 1))))))
  49. (defun khardel-choose-contact ()
  50. "Let the user select a contact from a list of all contacts.
  51. Return the contact."
  52. (let* ((contacts (khardel--list-contacts))
  53. (contact-name (completing-read "Select a contact: "
  54. (map-keys contacts)
  55. nil
  56. t)))
  57. (map-elt contacts contact-name)))
  58. (defvar-local khardel-edit-contact nil
  59. "Store the contact associated with current buffer.")
  60. ;;;###autoload
  61. (defun khardel-edit-contact (contact)
  62. "Open an editor on CONTACT."
  63. (interactive (list (khardel-choose-contact)))
  64. (let ((buffer (generate-new-buffer (format "*khardel<%s>*" (cdr contact)))))
  65. (with-current-buffer buffer
  66. (call-process "khard" nil t nil "export" "--uid" (car contact))
  67. (khardel-edit-mode)
  68. (setq-local khardel-edit-contact contact))
  69. (switch-to-buffer buffer)
  70. (message "Press %s to save the contact and close the buffer."
  71. (substitute-command-keys "\\[khardel-edit-finish]"))))
  72. (defvar khardel-edit-mode-map
  73. (let ((map (make-sparse-keymap)))
  74. (define-key map (kbd "C-c C-c") #'khardel-edit-finish)
  75. map)
  76. "Keymap for `khardel-edit-mode'.")
  77. (define-derived-mode khardel-edit-mode yaml-mode "Khardel"
  78. "Edit a contact through a yaml representation.")
  79. (defun khardel-edit-finish ()
  80. "Save contact in current buffer with khard."
  81. (interactive)
  82. (let ((filename (make-temp-file "khard")))
  83. (write-region (point-min) (point-max) filename)
  84. (when (equal 0 (call-process-region
  85. "y\n" ;; ⇐ khard asks for confirmation
  86. nil
  87. "khard"
  88. nil t nil
  89. "modify"
  90. "--input-file" filename
  91. "--uid" (car khardel-edit-contact)))
  92. (kill-buffer))))
  93. ;;;###autoload
  94. (defun khardel-insert-email ()
  95. "Let the user select an email from a list and insert it."
  96. (interactive)
  97. (let* ((emails (khardel--list-emails))
  98. (email (completing-read "Select email: " emails)))
  99. (when (stringp email)
  100. (insert email))))
  101. (provide 'khardel)
  102. ;;; khardel.el ends here