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.

163 lines
5.6KB

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