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.

khardel.el 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.2.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. (defcustom khardel-edit-finished-hook nil
  30. "Hook run when a contact edition is completed."
  31. :type 'hook)
  32. (defcustom khardel-vcard-version "3.0"
  33. "Version of the vcard format used.
  34. This is passed to \"vcard new\".'"
  35. :type 'string)
  36. (defvar khardel--emails nil
  37. "Cache a list of strings of the form \"Name <email>\".")
  38. (defun khardel--list-contacts ()
  39. "Return a map whose keys are names and values are contacts."
  40. (save-match-data
  41. (with-temp-buffer
  42. (call-process khardel-command nil t nil "ls" "--parsable")
  43. (goto-char (point-min))
  44. (let ((contacts (make-hash-table :test 'equal)))
  45. (cl-loop
  46. while (re-search-forward "^\\([-a-z0-9]*\\)\t\\(.*\\)\t[^\t]*$" nil t)
  47. do (map-put contacts (match-string 2) (cons (match-string 1) (match-string 2)))
  48. finally return contacts)))))
  49. (defun khardel--fetch-emails ()
  50. "Return a list of strings of the form \"Name <email>\" by asking `khardel-command'."
  51. (save-match-data
  52. (with-temp-buffer
  53. (call-process khardel-command nil t nil "email" "--parsable" "--remove-first-line")
  54. (goto-char (point-min))
  55. (cl-loop
  56. while (re-search-forward "^\\([^\t\n]*\\)\t\\([^\t\n]*\\)\t.*$" nil t)
  57. collect (format "\"%s\" <%s>" (match-string 2) (match-string 1))))))
  58. (defun khardel--list-emails ()
  59. "Return a list of strings of the form \"Name <email>\"."
  60. (if khardel--emails
  61. khardel--emails
  62. (setq khardel--emails (khardel--fetch-emails))))
  63. (defun khardel-flush-caches ()
  64. "Delete cached data to force a refresh."
  65. (interactive)
  66. (setq khardel--emails nil))
  67. (defun khardel-choose-contact ()
  68. "Let the user select a contact from a list of all contacts.
  69. Return the contact."
  70. (let* ((contacts (khardel--list-contacts))
  71. (contact-name (completing-read "Select a contact: "
  72. (map-keys contacts)
  73. nil
  74. t)))
  75. (map-elt contacts contact-name)))
  76. (defvar-local khardel-edit-contact nil
  77. "Store the contact associated with current buffer.
  78. If nil, the buffer represents a new contact.")
  79. ;;;###autoload
  80. (defun khardel-edit-contact (contact)
  81. "Open an editor on CONTACT."
  82. (interactive (list (khardel-choose-contact)))
  83. (let ((buffer (generate-new-buffer (format "*khardel<%s>*" (cdr contact)))))
  84. (with-current-buffer buffer
  85. (call-process "khard" nil t nil "show" "--format" "yaml" "--uid" (car contact))
  86. (goto-char (point-min))
  87. (khardel-edit-mode)
  88. (setq-local khardel-edit-contact contact))
  89. (switch-to-buffer buffer)
  90. (message "Press %s to save the contact and close the buffer."
  91. (substitute-command-keys "\\[khardel-edit-finish]"))))
  92. ;;;###autoload
  93. (defun khardel-new-contact ()
  94. "Open an editor to creat a new CONTACT."
  95. (interactive)
  96. (let ((buffer (generate-new-buffer "*khardel<new>*")))
  97. (with-current-buffer buffer
  98. (call-process "khard" nil t nil "template")
  99. (khardel-edit-mode)
  100. (setq-local khardel-edit-contact nil))
  101. (switch-to-buffer buffer)
  102. (message "Press %s to save the contact and close the buffer."
  103. (substitute-command-keys "\\[khardel-edit-finish]"))))
  104. (defvar khardel-edit-mode-map
  105. (let ((map (make-sparse-keymap)))
  106. (define-key map (kbd "C-c C-c") #'khardel-edit-finish)
  107. map)
  108. "Keymap for `khardel-edit-mode'.")
  109. (define-derived-mode khardel-edit-mode yaml-mode "Khardel"
  110. "Edit a contact through a yaml representation.")
  111. (defun khardel-edit-finish ()
  112. "Save contact in current buffer with khard."
  113. (interactive)
  114. (let* ((filename (make-temp-file "khard"))
  115. (args (if khardel-edit-contact
  116. `("modify"
  117. "--uid" ,(car khardel-edit-contact)
  118. "--input-file" ,filename)
  119. `("new"
  120. "--input-file" ,filename
  121. "--vcard-version" ,khardel-vcard-version))))
  122. (write-region (point-min) (point-max) filename)
  123. (when (equal 0 (apply
  124. #'call-process-region
  125. "y\n" ;; ⇐ khard asks for confirmation
  126. nil
  127. "khard"
  128. nil t nil
  129. args))
  130. (kill-buffer)
  131. (run-hooks 'khardel-edit-finished-hook))))
  132. ;;;###autoload
  133. (defun khardel-insert-email ()
  134. "Let the user select an email from a list and insert it."
  135. (interactive)
  136. (let* ((emails (khardel--list-emails))
  137. (email (completing-read "Select email: " emails)))
  138. (when (stringp email)
  139. (insert email))))
  140. (provide 'khardel)
  141. ;;; khardel.el ends here