Commit 3ed34ca4b9

3ed34ca4b93422a50fc90318784131e8baea73bf

parent: 6dc4a8715b

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-26 08:48 UTC
committer: <strk@kbt.io>

feat: render recent replies inline

Layout: unified · split

README.md +7
@@ -195,6 +195,13 @@ The inactivity delay defaults to five minutes and can be changed with:
195195
196196 /set matrix-rust.look.smart_filter_delay 300000
197197
198Recent replies can be rendered on one line in the form `> Alice: message`. By
199default, weechat-matrix-rs keeps the Matrix reply fallback quote when one is
200available. Set a positive threshold to render replies to recently printed lines
201in the compact one-line form:
202
203 /set matrix-rust.look.reply_full_quote_threshold 20
204
198205# Room Status And Verification
199206
200207The `buffer_modes` bar item shows Matrix room state. Encrypted rooms show
src/config.rs +11
@@ -136,6 +136,17 @@ config!(
136136 // Range: 0 ms to one day.
137137 0..86400000,
138138 },
139
140 reply_full_quote_threshold: Integer {
141 // Description
142 "Render replies to recently printed events on one line when this \
143 value is positive. The default 0 keeps the Matrix reply fallback \
144 quote when available",
145 // Default value.
146 0,
147 // Range: quote-preserving default to a large scrollback window.
148 0..100000,
149 },
139150 },
140151
141152 Section color {
src/render.rs +70 −11
@@ -56,6 +56,12 @@ pub struct RenderedEvent {
5656 pub content: RenderedContent,
5757}
5858
59#[derive(Debug, Clone, Copy, Eq, PartialEq)]
60pub enum ReplyContext {
61 Inline,
62 Full,
63}
64
5965impl RenderedEvent {
6066 const MSG_TAGS: &'static [&'static str] = &["notify_message"];
6167 const SELF_TAGS: &'static [&'static str] =
@@ -81,6 +87,7 @@ impl RenderedEvent {
8187 mut self,
8288 event_id: &EventId,
8389 sender: Option<&str>,
90 context: ReplyContext,
8491 ) -> Self {
8592 let reply_fallback = self.content.reply_fallback.take();
8693 let reply_sender = sender
@@ -99,6 +106,15 @@ impl RenderedEvent {
99106 .unwrap_or_default();
100107 tags.push("matrix_reply".to_owned());
101108
109 if context == ReplyContext::Inline {
110 if let Some(line) = self.content.lines.first_mut() {
111 line.tags = tags;
112 line.message = format!("> {}: {}", reply_sender, line.message);
113 }
114
115 return self;
116 }
117
102118 let mut context_lines = match reply_fallback {
103119 Some(fallback) => {
104120 let mut lines = vec![RenderedLine {
@@ -1631,18 +1647,21 @@ mod tests {
16311647 message: "reply body".to_owned(),
16321648 }]),
16331649 }
1634 .add_reply_context(&event_id, Some("Alice"));
1650 .add_reply_context(
1651 &event_id,
1652 Some("Alice"),
1653 ReplyContext::Inline,
1654 );
16351655
1636 assert_eq!(2, rendered.content.lines.len());
1637 assert_eq!("Reply to Alice", rendered.content.lines[0].message);
1656 assert_eq!(1, rendered.content.lines.len());
1657 assert_eq!("> Alice: reply body", rendered.content.lines[0].message);
16381658 assert!(rendered.content.lines[0]
16391659 .tags
16401660 .contains(&"matrix_reply".to_owned()));
1641 assert_eq!("reply body", rendered.content.lines[1].message);
16421661 }
16431662
16441663 #[test]
1645 fn reply_context_falls_back_to_event_id() {
1664 fn inline_reply_context_falls_back_to_event_id() {
16461665 let event_id =
16471666 matrix_sdk::ruma::owned_event_id!("$replyevent:example.org");
16481667 let rendered = RenderedEvent {
@@ -1653,16 +1672,15 @@ mod tests {
16531672 message: "reply body".to_owned(),
16541673 }]),
16551674 }
1656 .add_reply_context(&event_id, None);
1675 .add_reply_context(&event_id, None, ReplyContext::Inline);
16571676
16581677 assert_eq!(
1659 "Reply to $replyevent:example.org",
1678 "> $replyevent:example.org: reply body",
16601679 rendered.content.lines[0].message
16611680 );
16621681 assert!(rendered.content.lines[0]
16631682 .tags
16641683 .contains(&"matrix_reply".to_owned()));
1665 assert_eq!("reply body", rendered.content.lines[1].message);
16661684 }
16671685
16681686 #[test]
@@ -1685,7 +1703,11 @@ mod tests {
16851703 prefix: "bob\t".to_owned(),
16861704 content: content.render(&()),
16871705 }
1688 .add_reply_context(&event_id, Some("Alice"));
1706 .add_reply_context(
1707 &event_id,
1708 Some("Alice"),
1709 ReplyContext::Full,
1710 );
16891711
16901712 assert_eq!(3, rendered.content.lines.len());
16911713 assert_eq!("Reply to Alice:", rendered.content.lines[0].message);
@@ -1716,7 +1738,7 @@ mod tests {
17161738 prefix: "bob\t".to_owned(),
17171739 content: content.render(&()),
17181740 }
1719 .add_reply_context(&event_id, None);
1741 .add_reply_context(&event_id, None, ReplyContext::Full);
17201742
17211743 assert_eq!(
17221744 "Reply to @alice:example.org:",
@@ -1745,7 +1767,11 @@ mod tests {
17451767 prefix: "bob\t".to_owned(),
17461768 content: content.render(&()),
17471769 }
1748 .add_reply_context(&event_id, Some("Alice"));
1770 .add_reply_context(
1771 &event_id,
1772 Some("Alice"),
1773 ReplyContext::Full,
1774 );
17491775
17501776 let messages = rendered
17511777 .content
@@ -1760,6 +1786,39 @@ mod tests {
17601786 );
17611787 }
17621788
1789 #[test]
1790 fn inline_reply_context_keeps_reply_on_one_line_with_fallback() {
1791 let event_id =
1792 matrix_sdk::ruma::owned_event_id!("$replyevent:example.org");
1793 let body = "\
1794 <mx-reply>\
1795 <blockquote>\
1796 <a href=\"https://matrix.to/#/!room:example.org/$replyevent:example.org\">In reply to</a> \
1797 <a href=\"https://matrix.to/#/@alice:example.org\">@alice:example.org</a>\
1798 <br>\
1799 Previous message\
1800 </blockquote>\
1801 </mx-reply>\
1802 <p>new message</p>";
1803 let content = TextMessageEventContent::html("fallback", body);
1804 let rendered = RenderedEvent {
1805 message_timestamp: 0,
1806 prefix: "bob\t".to_owned(),
1807 content: content.render(&()),
1808 }
1809 .add_reply_context(
1810 &event_id,
1811 Some("Alice"),
1812 ReplyContext::Inline,
1813 );
1814
1815 assert_eq!(1, rendered.content.lines.len());
1816 assert_eq!("> Alice: new message", rendered.content.lines[0].message);
1817 assert!(rendered.content.lines[0]
1818 .tags
1819 .contains(&"matrix_reply".to_owned()));
1820 }
1821
17631822 #[test]
17641823 fn formatted_body_plain_text_keeps_block_structure() {
17651824 let body = "<blockquote><p>quoted</p><p>text</p></blockquote>\
src/room/buffer.rs +31
@@ -186,6 +186,23 @@ impl RoomBuffer {
186186 reply_sender_id_from_tags(&line.tags())
187187 }
188188
189 /// Count printed lines after an event that is still visible in a buffer.
190 pub fn reply_line_distance(&self, event_id: &EventId) -> Option<usize> {
191 let event_id_tag = Cow::from(event_id.to_tag());
192
193 self.buffer_handle()
194 .upgrade()
195 .ok()
196 .and_then(|buffer| buffer_line_distance(&buffer, &event_id_tag))
197 .or_else(|| {
198 self.thread_buffers.borrow().values().find_map(|handle| {
199 handle.upgrade().ok().and_then(|buffer| {
200 buffer_line_distance(&buffer, &event_id_tag)
201 })
202 })
203 })
204 }
205
189206 /// Return whether an event is already rendered in the buffer.
190207 pub fn contains_event(&self, event_id: &EventId) -> bool {
191208 let event_id_tag = Cow::from(event_id.to_tag());
@@ -623,6 +640,20 @@ where
623640 line.set_tags(&tags);
624641}
625642
643fn buffer_line_distance(buffer: &Buffer, tag: &Cow<str>) -> Option<usize> {
644 let mut lines_after = 0;
645
646 for line in buffer.lines().rev() {
647 if line.tags().contains(tag) {
648 return Some(lines_after);
649 }
650
651 lines_after += 1;
652 }
653
654 None
655}
656
626657fn sanitize_thread_id(thread_root: &EventId) -> String {
627658 const THREAD_ID_DISPLAY_CHARS: usize = 12;
628659
src/room/mod.rs +42 −2
@@ -85,7 +85,7 @@ use weechat::{
8585use crate::{
8686 config::{Config, RedactionStyle},
8787 connection::Connection,
88 render::{Render, RenderedEvent},
88 render::{Render, RenderedEvent, ReplyContext},
8989 utils::{Edit, VerificationEvent},
9090 PLUGIN_NAME,
9191};
@@ -826,7 +826,21 @@ impl MatrixRoom {
826826 };
827827
828828 if let Some((event_id, sender)) = reply_to {
829 rendered.add_reply_context(event_id, sender.as_deref())
829 let threshold = self
830 .config
831 .borrow()
832 .look()
833 .reply_full_quote_threshold();
834 let context = reply_context_for_distance(
835 threshold,
836 self.buffer.reply_line_distance(event_id),
837 );
838
839 rendered.add_reply_context(
840 event_id,
841 sender.as_deref(),
842 context,
843 )
830844 } else {
831845 rendered
832846 }
@@ -1794,6 +1808,16 @@ impl MatrixRoom {
17941808 }
17951809}
17961810
1811fn reply_context_for_distance(
1812 threshold: i64,
1813 distance: Option<usize>,
1814) -> ReplyContext {
1815 distance
1816 .filter(|distance| threshold > 0 && *distance <= threshold as usize)
1817 .map(|_| ReplyContext::Inline)
1818 .unwrap_or(ReplyContext::Full)
1819}
1820
17971821#[cfg(test)]
17981822mod tests {
17991823 use super::*;
@@ -1821,6 +1845,22 @@ mod tests {
18211845 assert!(!should_render_event(true));
18221846 }
18231847
1848 #[test]
1849 fn zero_reply_quote_threshold_keeps_full_reply_context() {
1850 assert_eq!(ReplyContext::Full, reply_context_for_distance(0, Some(0)));
1851 assert_eq!(ReplyContext::Full, reply_context_for_distance(0, Some(1)));
1852 }
1853
1854 #[test]
1855 fn positive_reply_quote_threshold_allows_inline_recent_replies() {
1856 assert_eq!(
1857 ReplyContext::Inline,
1858 reply_context_for_distance(2, Some(2))
1859 );
1860 assert_eq!(ReplyContext::Full, reply_context_for_distance(2, Some(3)));
1861 assert_eq!(ReplyContext::Full, reply_context_for_distance(2, None));
1862 }
1863
18241864 #[test]
18251865 fn thread_buffer_input_sends_thread_relation() {
18261866 let content = make_text_message_content(