[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/xeft 8e57d01aa6 10/55: Improve perceived latency
From: |
ELPA Syncer |
Subject: |
[elpa] externals/xeft 8e57d01aa6 10/55: Improve perceived latency |
Date: |
Fri, 13 Jan 2023 23:58:36 -0500 (EST) |
branch: externals/xeft
commit 8e57d01aa60d7dcc68e062c8fc13427feb01f190
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
Improve perceived latency
* xeft.el (xeft--ecache):
(xeft--ecache-buffer):
(xeft--insert-file-excerpt):
(xeft--ignore-short-phrase):
(xeft-refresh):
---
xeft.el | 84 ++++++++++++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 62 insertions(+), 22 deletions(-)
diff --git a/xeft.el b/xeft.el
index d59e18f774..2e8eb29a35 100644
--- a/xeft.el
+++ b/xeft.el
@@ -318,6 +318,25 @@ If SELECT is non-nil, select the buffer after displaying
it."
(overlay-put ov 'xeft-highlight t)
(overlay-put ov 'evaporate t))))))
+(defvar xeft--ecache nil
+ "Cache for finding excerpt for a file.")
+
+(defun xeft--ecache-buffer (file)
+ "Return a buffer that has the content of FILE.
+Doesn’t check for modification time, and not used."
+ (or (alist-get (sxhash file) xeft--ecache)
+ (progn
+ (let ((buf (get-buffer-create
+ (format " *xeft-ecache %s*" file))))
+ (with-current-buffer buf
+ (setq buffer-undo-list t)
+ (insert-file-contents file nil nil nil t))
+ (push (cons (sxhash file) buf) xeft--ecache)
+ (when (> (length xeft--ecache) 30)
+ (kill-buffer (cdr (nth 30 xeft--ecache)))
+ (setcdr (nthcdr 29 xeft--ecache) nil))
+ buf))))
+
(defun xeft--insert-file-excerpt (file search-phrase)
"Insert an excerpt for FILE at point.
This excerpt contains note title and content excerpt and is
@@ -329,8 +348,9 @@ search phrase the user typed."
title excerpt)
(with-current-buffer (xeft--work-buffer)
(setq buffer-undo-list t)
- ;; We don’t need to cache file content, because we only insert
- ;; 15 results. And adding cache (with alist) is actually slower.
+ ;; The times saved by caching is not significant enough. So I
+ ;; choose to not cache, but kept the code just in case. See
+ ;; ‘xeft--ecache-buffer’.
(insert-file-contents file nil nil nil t)
(goto-char (point-min))
(search-forward "#+TITLE: " (line-end-position) t)
@@ -403,13 +423,30 @@ Once refreshed the buffer, set this to nil.")
(last lst))
" ")))
+;; This makes the integrative search results much more stable and
+;; experience more fluid. And because we are not showing radically
+;; different results from one key-press to another, the latency goes
+;; down, I’m guessing because caching in CPU or RAM or OS or whatever.
+(defun xeft--ignore-short-phrase (phrase)
+ "If the last term in PHRASE is too short, remove it."
+ (let* ((lst (or (split-string phrase) '("")))
+ (last (car (last lst))))
+ (if (and (not (string-match-p (rx (or (category chinese)
+ (category japanese)
+ (category korean)))
+ last))
+ (< (length last) 3))
+ (string-join (cl-subseq lst 0 (1- (length lst))) " ")
+ (string-join lst " "))))
+
(defun xeft-refresh (&optional full)
"Search for notes and display their summaries.
By default, only display the first 15 results. If FULL is
non-nil, display all results."
(interactive)
(when (derived-mode-p 'xeft-mode)
- (let ((search-phrase (xeft--get-search-phrase)))
+ (let ((search-phrase (xeft--ignore-short-phrase
+ (xeft--get-search-phrase))))
(let* ((phrase-empty (equal search-phrase ""))
(file-list
(if phrase-empty
@@ -417,6 +454,8 @@ non-nil, display all results."
(xeft-query-term
(xeft--tighten-search-phrase search-phrase)
xeft-database
+ ;; 16 is just larger than 15, so we will know it when
+ ;; there are more results.
0 (if full 2147483647 16))))
(list-clipped nil))
(when (and (null full) (> (length file-list) 15))
@@ -451,25 +490,26 @@ non-nil, display all results."
xeft-mode-map t)))))
(buffer-string)))))
(when (stringp new-content)
- (setq buffer-undo-list t)
- ;; Actually insert the new content.
- (goto-char (point-min))
- (forward-line 2)
- (let ((start (point)))
- (delete-region (point) (point-max))
- (insert new-content)
- ;; If we use (- start 2), emacs-rime cannot work.
- (put-text-property (- start 1) (point) 'read-only t)
- (xeft--highlight-search-phrase)
- (set-buffer-modified-p nil)
- ;; Re-apply highlight.
- (xeft--highlight-file-at-point)
- ;; If finished, update this variable.
- (setq xeft--need-refresh nil)
- ;; Save excursion wouldn’t work since we erased the
- ;; buffer and re-inserted contents.
- (goto-char orig-point)
- (buffer-enable-undo))))))))
+ (while-no-input
+ (setq buffer-undo-list t)
+ ;; Actually insert the new content.
+ (goto-char (point-min))
+ (forward-line 2)
+ (let ((start (point)))
+ (delete-region (point) (point-max))
+ (insert new-content)
+ ;; If we use (- start 2), emacs-rime cannot work.
+ (put-text-property (- start 1) (point) 'read-only t)
+ (xeft--highlight-search-phrase)
+ (set-buffer-modified-p nil)
+ ;; Re-apply highlight.
+ (xeft--highlight-file-at-point)
+ ;; If finished, update this variable.
+ (setq xeft--need-refresh nil)
+ (buffer-enable-undo))))
+ ;; Save excursion wouldn’t work since we erased the
+ ;; buffer and re-inserted contents.
+ (goto-char orig-point))))))
;;; Highlight matched phrases
- [elpa] branch externals/xeft created (now 4bdb052d81), ELPA Syncer, 2023/01/13
- [elpa] externals/xeft afc8a69a52 01/55: init, ELPA Syncer, 2023/01/13
- [elpa] externals/xeft b9759e2f57 02/55: Fix for Linux, ELPA Syncer, 2023/01/13
- [elpa] externals/xeft f81dd92048 03/55: Some update, ELPA Syncer, 2023/01/13
- [elpa] externals/xeft b3ff6bf5b6 04/55: Make search result more intuitive, ELPA Syncer, 2023/01/13
- [elpa] externals/xeft 290b146829 05/55: Remove caching, change to showing only 15 results, ELPA Syncer, 2023/01/13
- [elpa] externals/xeft 315126c9ae 08/55: Fix noexcept error, ELPA Syncer, 2023/01/13
- [elpa] externals/xeft 8e57d01aa6 10/55: Improve perceived latency,
ELPA Syncer <=
- [elpa] externals/xeft e7722e479e 14/55: Move the nonexcept qualifier to the correct place, ELPA Syncer, 2023/01/13
- [elpa] externals/xeft 8b3e653f6e 15/55: Really fix the nonexcept qualifier, ELPA Syncer, 2023/01/13
- [elpa] externals/xeft 2b1ff8402f 17/55: Various fixes, ELPA Syncer, 2023/01/13
- [elpa] externals/xeft 8850838345 19/55: Fix "create note" prompt, ELPA Syncer, 2023/01/13
- [elpa] externals/xeft b12a0f58ea 34/55: Factor out two faces xeft-excerpt-title and xeft-excerpt-body, ELPA Syncer, 2023/01/13
- [elpa] externals/xeft 6fcc903bbb 51/55: ; * xeft.el (xeft--compile-module): Refactor., ELPA Syncer, 2023/01/13
- [elpa] externals/xeft af94f77834 07/55: * xeft-module.cc: Fix signiture, ELPA Syncer, 2023/01/13
- [elpa] externals/xeft 8a9f1e41b9 16/55: This got to fix it, ELPA Syncer, 2023/01/13
- [elpa] externals/xeft 951db71170 21/55: Fix default-extension format and add a recursive option, ELPA Syncer, 2023/01/13
- [elpa] externals/xeft f2a7dd9259 30/55: Minor layout change, ELPA Syncer, 2023/01/13