From a7c45f8ae27bf1b75d9a8122e3475ebfcd49c1cf Mon Sep 17 00:00:00 2001 From: blindingdark Date: Sat, 18 Nov 2023 01:22:56 +0800 Subject: [PATCH 1/3] Highlight quote when jumping to message --- telega-chat.el | 11 ++++++++++- telega-msg.el | 6 ++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/telega-chat.el b/telega-chat.el index 0c6ebbb..d81c013 100644 --- a/telega-chat.el +++ b/telega-chat.el @@ -5766,7 +5766,16 @@ Return non-nil on success." (cl-assert (eq (button-type msg-button) 'telega-msg)) (with-no-warnings (pulse-momentary-highlight-region - (button-start msg-button) (button-end msg-button)))) + (button-start msg-button) (button-end msg-button)) + (when (stringp highlight) + (save-excursion + (goto-char (button-start msg-button)) + (if (search-forward highlight (button-end msg-button) t) + (let ((start-point (match-beginning 0)) + (end-point (match-end 0))) + (sit-for 0.5) + (pulse-momentary-highlight-region start-point end-point)) + (message "The exact quote is not found")))))) (when callback (funcall callback msg-button))) diff --git a/telega-msg.el b/telega-msg.el index 58001f7..100ac1b 100644 --- a/telega-msg.el +++ b/telega-msg.el @@ -354,9 +354,11 @@ Return nil for deleted messages." "Goto message denoted by `:reply_to' field of the message MSG." (let* ((reply-to (plist-get msg :reply_to)) (chat-id (plist-get reply-to :chat_id)) - (msg-id (plist-get reply-to :message_id))) + (msg-id (plist-get reply-to :message_id)) + (quote (plist-get reply-to :quote)) + (quote-text (plist-get quote :text))) (unless (or (telega-zerop chat-id) (telega-zerop msg-id)) - (telega-chat--goto-msg (telega-chat-get chat-id) msg-id 'highlight)))) + (telega-chat--goto-msg (telega-chat-get chat-id) msg-id quote-text)))) (defun telega-msg-open-sticker (msg &optional sticker) "Open content for sticker message MSG." From 9bef62baad13bf13da0bab9ed5b53be54e81fa0c Mon Sep 17 00:00:00 2001 From: blindingdark Date: Tue, 28 Nov 2023 23:45:46 +0800 Subject: [PATCH 2/3] Remove highlight when leave msg. --- telega-chat.el | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/telega-chat.el b/telega-chat.el index d81c013..c280324 100644 --- a/telega-chat.el +++ b/telega-chat.el @@ -5768,19 +5768,34 @@ Return non-nil on success." (pulse-momentary-highlight-region (button-start msg-button) (button-end msg-button)) (when (stringp highlight) - (save-excursion - (goto-char (button-start msg-button)) - (if (search-forward highlight (button-end msg-button) t) - (let ((start-point (match-beginning 0)) - (end-point (match-end 0))) - (sit-for 0.5) - (pulse-momentary-highlight-region start-point end-point)) - (message "The exact quote is not found")))))) - + (sit-for 0.5) + (telega-chatbuf--highlight-quote msg-button highlight)))) (when callback (funcall callback msg-button))) t)) +(defun telega-chatbuf--remove-highlight-quote (&rest _) + "Remove highlighted quote." + (remove-overlays nil nil 'telega-chatbuf--highlight-quote t) + (remove-hook 'telega-msg-hover-out-hook #'telega-chatbuf--remove-highlight-quote :local)) + +(defun telega-chatbuf--highlight-quote (msg-button quote-text) + "Highlight the QUOTE-TEXT in the MSG-BUTTON." + (save-excursion + (goto-char (button-start msg-button)) + (if (search-forward quote-text (button-end msg-button) t) + (let* ((start-point (match-beginning 0)) + (end-point (match-end 0)) + (overlay (make-overlay start-point end-point))) + (overlay-put overlay 'telega-chatbuf--highlight-quote t) + (overlay-put overlay 'face 'telega-highlight-text-face) + (add-hook + 'telega-msg-hover-out-hook + #'telega-chatbuf--remove-highlight-quote + nil + :local)) + (message "The exact quote is not found")))) + (defun telega-chatbuf--goto-msg (msg-id &optional highlight callback) "In CHAT goto message denoted by MSG-ID. If HIGHLIGHT is non-nil then highlight with fading background color. From 09734e8b1bde4b5dc2136d0f38dd124016f7602f Mon Sep 17 00:00:00 2001 From: blindingdark Date: Fri, 8 Dec 2023 02:26:42 +0800 Subject: [PATCH 3/3] Use tdlib searchQuote. --- telega-chat.el | 59 +++++++++++++++++++++++++++++++++----------------- telega-msg.el | 5 ++--- 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/telega-chat.el b/telega-chat.el index c280324..07147de 100644 --- a/telega-chat.el +++ b/telega-chat.el @@ -5767,9 +5767,12 @@ Return non-nil on success." (with-no-warnings (pulse-momentary-highlight-region (button-start msg-button) (button-end msg-button)) - (when (stringp highlight) + (when (and (listp highlight) + (equal (plist-get highlight :@type) "textQuote")) (sit-for 0.5) - (telega-chatbuf--highlight-quote msg-button highlight)))) + (telega-chatbuf--highlight-quote + highlight + (button-get msg-button :value))))) (when callback (funcall callback msg-button))) t)) @@ -5777,24 +5780,40 @@ Return non-nil on success." (defun telega-chatbuf--remove-highlight-quote (&rest _) "Remove highlighted quote." (remove-overlays nil nil 'telega-chatbuf--highlight-quote t) - (remove-hook 'telega-msg-hover-out-hook #'telega-chatbuf--remove-highlight-quote :local)) - -(defun telega-chatbuf--highlight-quote (msg-button quote-text) - "Highlight the QUOTE-TEXT in the MSG-BUTTON." - (save-excursion - (goto-char (button-start msg-button)) - (if (search-forward quote-text (button-end msg-button) t) - (let* ((start-point (match-beginning 0)) - (end-point (match-end 0)) - (overlay (make-overlay start-point end-point))) - (overlay-put overlay 'telega-chatbuf--highlight-quote t) - (overlay-put overlay 'face 'telega-highlight-text-face) - (add-hook - 'telega-msg-hover-out-hook - #'telega-chatbuf--remove-highlight-quote - nil - :local)) - (message "The exact quote is not found")))) + (remove-hook + 'telega-msg-hover-out-hook + #'telega-chatbuf--remove-highlight-quote + :local)) + +(defun telega-chatbuf--highlight-quote (text-quote msg) + "Highlight the TEXT-QUOTE in the MSG." + (let* ((msg-content-fmt (telega--tl-get msg :content :text)) + (quote-fmt (telega--tl-get text-quote :text)) + (quote-position (telega--tl-get text-quote :position)) + (found-position (telega-server--call + (list :@type "searchQuote" + :text msg-content-fmt + :quote quote-fmt + :quote_position quote-position)))) + (if (equal 404 (telega--tl-get found-position :code)) + (message "The exact quote is not found") + (let* ((chat (telega-msg-chat msg)) + (gaps-workaround-p + (telega-chat-match-p chat telega-avatar-workaround-gaps-for)) + ;; FIXME: get the msg content starting point more reliable + (msg-start-point (+ (if gaps-workaround-p 0 4) (save-excursion (forward-line 1) (point)))) + (start-point (+ msg-start-point + (telega--tl-get found-position :position))) + (end-point (+ start-point + (length (telega--tl-get quote-fmt :text)))) + (overlay (make-overlay start-point end-point))) + (overlay-put overlay 'telega-chatbuf--highlight-quote t) + (overlay-put overlay 'face 'telega-highlight-text-face) + (add-hook + 'telega-msg-hover-out-hook + #'telega-chatbuf--remove-highlight-quote + nil + :local))))) (defun telega-chatbuf--goto-msg (msg-id &optional highlight callback) "In CHAT goto message denoted by MSG-ID. diff --git a/telega-msg.el b/telega-msg.el index 100ac1b..b05d948 100644 --- a/telega-msg.el +++ b/telega-msg.el @@ -355,10 +355,9 @@ Return nil for deleted messages." (let* ((reply-to (plist-get msg :reply_to)) (chat-id (plist-get reply-to :chat_id)) (msg-id (plist-get reply-to :message_id)) - (quote (plist-get reply-to :quote)) - (quote-text (plist-get quote :text))) + (text-quote (plist-get reply-to :quote))) (unless (or (telega-zerop chat-id) (telega-zerop msg-id)) - (telega-chat--goto-msg (telega-chat-get chat-id) msg-id quote-text)))) + (telega-chat--goto-msg (telega-chat-get chat-id) msg-id text-quote)))) (defun telega-msg-open-sticker (msg &optional sticker) "Open content for sticker message MSG."