+(defun notmuch-search-get-tags-region (beg end)
+ (save-excursion
+ (let ((output nil)
+ (last-line (line-number-at-pos end))
+ (max-line (- (line-number-at-pos (point-max)) 2)))
+ (goto-char beg)
+ (while (<= (line-number-at-pos) (min last-line max-line))
+ (setq output (append output (notmuch-search-get-tags)))
+ (forward-line 1))
+ output)))
+
+(defun notmuch-search-add-tag-thread (tag)
+ (notmuch-search-add-tag-region tag (point) (point)))
+
+(defun notmuch-search-add-tag-region (tag beg end)
+ (let ((search-id-string (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or ")))
+ (notmuch-call-notmuch-process "tag" (concat "+" tag) search-id-string)
+ (save-excursion
+ (let ((last-line (line-number-at-pos end))
+ (max-line (- (line-number-at-pos (point-max)) 2)))
+ (goto-char beg)
+ (while (<= (line-number-at-pos) (min last-line max-line))
+ (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<)))
+ (forward-line))))))
+
+(defun notmuch-search-remove-tag-thread (tag)
+ (notmuch-search-remove-tag-region tag (point) (point)))
+
+(defun notmuch-search-remove-tag-region (tag beg end)
+ (let ((search-id-string (mapconcat 'identity (notmuch-search-find-thread-id-region beg end) " or ")))
+ (notmuch-call-notmuch-process "tag" (concat "-" tag) search-id-string)
+ (save-excursion
+ (let ((last-line (line-number-at-pos end))
+ (max-line (- (line-number-at-pos (point-max)) 2)))
+ (goto-char beg)
+ (while (<= (line-number-at-pos) (min last-line max-line))
+ (notmuch-search-set-tags (delete tag (notmuch-search-get-tags)))
+ (forward-line))))))
+