mu search backend for Gnus' nnir
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.

138 lines
4.0 KiB

  1. ;;; nnmu.el --- mu backend for nnir -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2020 Jai Flack
  3. ;; Author: Jai Flack <jflack@disroot.org>
  4. ;; Version: 2020-09-15
  5. ;; URL: https://git.disroot.org/jflack/nnir-mu
  6. ;; Package-Requires: ((emacs "24.1"))
  7. ;; Keywords: Gnus nnir mu
  8. ;; This file is NOT part of GNU Emacs.
  9. ;; This program is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. ;;; Commentary
  20. ;; This package provides an `mu' backend for Gnus' nnir as an
  21. ;; alternative to notmuch or any other backends. It relies on the `mu'
  22. ;; executable and thus can only search local mail and requires `mu' to
  23. ;; be configured for your mail setup. Tested with an nnmaildir backend
  24. ;; synced with mbsync.
  25. ;;; Configuration:
  26. ;; Generally the defaults will work find but first mu must be
  27. ;; installed and configured, see mu(1) and mu-init(1).
  28. ;; Available options:
  29. ;; - `nnir-mu-program'
  30. ;; - `nnir-mu-remove-prefix'
  31. ;;; Code:
  32. (require 'nnir)
  33. (defcustom nnir-mu-program "mu"
  34. "Name of mu search executable."
  35. :type '(string)
  36. :group 'nnir)
  37. (defcustom nnir-mu-additional-switches '()
  38. "A list of strings, to be given as additional arguments to mu."
  39. :type '(repeat (string))
  40. :group 'nnir)
  41. (defcustom nnir-mu-remove-prefix nil
  42. "The prefix to remove from each file name returned by notmuch
  43. in order to get a group name. Generally this should be set to
  44. your path to your mail directory. This is a regular expression.
  45. If it is `nil' then the maildir returned from mu will be used
  46. instead. This can be an expensive process but works without any
  47. configuration.
  48. This is very similar to `nnir-notmuch-remove-prefix' and
  49. `nnir-namazu-remove-prefix'."
  50. :type '(regexp)
  51. :group 'nnir)
  52. ;; TODO: part of notmuch engine
  53. ;; (defcustom nnir-mu-filter-group-names-function nil)
  54. (defun nnir-run-mu (query server &optional groups)
  55. "Run QUERY against mu."
  56. (message "nnir-run-mu: ")
  57. (save-excursion
  58. (let* (artlist
  59. (qstring (cdr (assq 'query query)))
  60. (prefix (nnir-read-server-parm 'nnir-mu-remove-prefix server))
  61. (article-pattern (if (string-prefix-p "nnmaildir:"
  62. (gnus-group-server server))
  63. ":[0-9]+"
  64. "^[0-9]+$")))
  65. (when (string-equal "" qstring)
  66. (error "mu: You need a search term"))
  67. (set-buffer (gnus-get-buffer-create nnir-tmp-buffer))
  68. (erase-buffer)
  69. (let ((cp-list `(,nnir-mu-program
  70. nil
  71. t
  72. nil
  73. "find"
  74. "--format=sexp"
  75. ,@(nnir-read-server-parm 'nnir-mu-additional-switches
  76. server)
  77. ,qstring)))
  78. (apply #'call-process cp-list))
  79. (goto-char (point-min))
  80. (let (point-start objcons filenam artno dirnam)
  81. (while (not (looking-at "\n$"))
  82. (setq objcons (car
  83. (read-from-string
  84. (decode-coding-string
  85. (buffer-substring-no-properties
  86. (1- (search-forward-regexp "^(" nil t))
  87. (search-forward-regexp "^)" nil t))
  88. 'utf-8 t))))
  89. (setq filenam (plist-get objcons :path)
  90. artno (file-name-nondirectory filenam)
  91. dirnam (file-name-directory filenam))
  92. (when (and (string-match article-pattern artno)
  93. (not (null dirnam)))
  94. (unless prefix
  95. (setq prefix (string-trim-right filenam
  96. (concat (regexp-quote
  97. (plist-get objcons
  98. :maildir))
  99. ".*"))))
  100. (print (list dirnam artno "" prefix server artlist))
  101. (nnir-add-result dirnam artno "" prefix server artlist))))
  102. (message "Getting massaged by mu...done")
  103. artlist)))
  104. (add-to-list 'nnir-engines '(mu nnir-run-mu
  105. ()))
  106. (provide 'nnir-mu)