From: Oleh Krehel Date: Wed, 9 Mar 2016 13:48:16 +0000 (+0100) Subject: counsel.el (counsel--async-sentinel): Start refactor X-Git-Url: https://code.delx.au/gnu-emacs-elpa/commitdiff_plain/50f309204d51d7e8bf3a4910854ccdf23cece49f counsel.el (counsel--async-sentinel): Start refactor This function should only care about producing a list of strings from the process buffer. A new function should take this list and take care of everything else. --- diff --git a/counsel.el b/counsel.el index 2ea12d677..53da88935 100644 --- a/counsel.el +++ b/counsel.el @@ -98,41 +98,44 @@ Or the time of the last minibuffer update.") (set-process-filter proc (or process-filter #'counsel--async-filter)))) (defun counsel--async-sentinel (process event) - (if (string= event "finished\n") - (progn - (with-current-buffer (process-buffer process) - (ivy--set-candidates - (ivy--sort-maybe - (split-string - (buffer-string) - counsel-async-split-string-re - t))) - (if (null ivy--old-cands) - (setq ivy--index - (or (ivy--preselect-index - (ivy-state-preselect ivy-last) - ivy--all-candidates) - 0)) - (let ((re (funcall ivy--regex-function ivy-text))) - (unless (stringp re) - (setq re (caar re))) - (ivy--recompute-index - ivy-text re ivy--all-candidates))) - (setq ivy--old-cands ivy--all-candidates)) - (if (null ivy--all-candidates) - (ivy--insert-minibuffer "") - (ivy--exhibit))) - (if (string-match "exited abnormally with code \\([0-9]+\\)\n" event) - (let* ((exit-code-plist (plist-get counsel--async-exit-code-plist - (ivy-state-caller ivy-last))) - (exit-num (read (match-string 1 event))) - (exit-code (plist-get exit-code-plist exit-num))) - (setq ivy--all-candidates - (list - (or exit-code - (format "error code %d" exit-num)))) - (setq ivy--old-cands ivy--all-candidates) - (ivy--exhibit))))) + (let ((cands + (cond ((string= event "finished\n") + (with-current-buffer (process-buffer process) + (split-string + (buffer-string) + counsel-async-split-string-re + t))) + ((string-match "exited abnormally with code \\([0-9]+\\)\n" event) + (let* ((exit-code-plist (plist-get counsel--async-exit-code-plist + (ivy-state-caller ivy-last))) + (exit-num (read (match-string 1 event))) + (exit-code (plist-get exit-code-plist exit-num))) + (list + (or exit-code + (format "error code %d" exit-num)))))))) + (cond ((string= event "finished\n") + (ivy--set-candidates + (ivy--sort-maybe + cands)) + (if (null ivy--old-cands) + (setq ivy--index + (or (ivy--preselect-index + (ivy-state-preselect ivy-last) + ivy--all-candidates) + 0)) + (let ((re (funcall ivy--regex-function ivy-text))) + (unless (stringp re) + (setq re (caar re))) + (ivy--recompute-index + ivy-text re ivy--all-candidates))) + (setq ivy--old-cands ivy--all-candidates) + (if (null ivy--all-candidates) + (ivy--insert-minibuffer "") + (ivy--exhibit))) + ((string-match "exited abnormally with code \\([0-9]+\\)\n" event) + (setq ivy--all-candidates cands) + (setq ivy--old-cands ivy--all-candidates) + (ivy--exhibit))))) (defun counsel--async-filter (process str) "Receive from PROCESS the output STR.