Skip to content

Commit

Permalink
Protect entry parsing against notmuch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambrevar committed Mar 20, 2019
1 parent ae001ca commit dae426c
Showing 1 changed file with 51 additions and 48 deletions.
99 changes: 51 additions & 48 deletions helm-notmuch.el
Original file line number Diff line number Diff line change
Expand Up @@ -99,54 +99,57 @@ slows down searches."

(defun helm-notmuch-candidate-formatter (cand)
"Format the single entry CAND."
(let ((text (substring cand (+ 2 helm-notmuch-thread-id-length)))
(id (substring cand 0 helm-notmuch-thread-id-length))
cstart astart alen tstart tags)
(with-temp-buffer
(insert text)
(goto-char (point-min))

; Align message counts
(search-forward "[")
(setq cstart (point))
(search-forward "]")
(save-excursion
(save-restriction
(narrow-to-region cstart (point))
(goto-char (point-min))
(when (re-search-forward "\([0-9]\+\)" nil t)
(replace-match ""))))
(forward-char)
(just-one-space (- helm-notmuch-thread-count-width
(- (point) cstart)))
(forward-char)

; Align (and truncate) authors
(setq astart (point))
(search-forward ";")
(delete-char -1)
(setq alen (- (point) astart))
(if (> alen helm-notmuch-author-width)
(progn
(delete-region (- (point) (- alen
(- helm-notmuch-author-width 3)))
(point))
(insert "..."))
(just-one-space (- (+ helm-notmuch-author-width 1) alen)))

; Colour tags
(goto-char (- (point-max) 1))
(save-excursion
(search-backward "(")
(setq tstart (+ (point) 1)))
(setq tags (split-string (buffer-substring tstart (point))))
(delete-region tstart (point))
(insert (notmuch-tag-format-tags tags tags))

; Colour the whole line according to tags
(notmuch-search-color-line (point-min) (point-max) tags)
(setq text (buffer-string)))
(cons text id)))
(if (not (string-match-p "\\[" cand))
;; This protects against notmuch errors sent to standard output.
""
(let ((text (substring cand (+ 2 helm-notmuch-thread-id-length)))
(id (substring cand 0 helm-notmuch-thread-id-length))
cstart astart alen tstart tags)
(with-temp-buffer
(insert text)
(goto-char (point-min))

;; Align message counts
(search-forward "[")
(setq cstart (point))
(search-forward "]")
(save-excursion
(save-restriction
(narrow-to-region cstart (point))
(goto-char (point-min))
(when (re-search-forward "\([0-9]\+\)" nil t)
(replace-match ""))))
(forward-char)
(just-one-space (- helm-notmuch-thread-count-width
(- (point) cstart)))
(forward-char)

;; Align (and truncate) authors
(setq astart (point))
(search-forward ";")
(delete-char -1)
(setq alen (- (point) astart))
(if (> alen helm-notmuch-author-width)
(progn
(delete-region (- (point) (- alen
(- helm-notmuch-author-width 3)))
(point))
(insert "..."))
(just-one-space (- (+ helm-notmuch-author-width 1) alen)))

;; Colour tags
(goto-char (- (point-max) 1))
(save-excursion
(search-backward "(")
(setq tstart (+ (point) 1)))
(setq tags (split-string (buffer-substring tstart (point))))
(delete-region tstart (point))
(insert (notmuch-tag-format-tags tags tags))

;; Colour the whole line according to tags
(notmuch-search-color-line (point-min) (point-max) tags)
(setq text (buffer-string)))
(cons text id))))

(defun helm-notmuch-maybe-match-incomplete (pattern)
(if helm-notmuch-match-incomplete-words
Expand Down

0 comments on commit dae426c

Please sign in to comment.