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:
195 195
196 /set matrix-rust.look.smart_filter_delay 300000 196 /set matrix-rust.look.smart_filter_delay 300000
197 197
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
198# Room Status And Verification 205# Room Status And Verification
199 206
200The `buffer_modes` bar item shows Matrix room state. Encrypted rooms show 207The `buffer_modes` bar item shows Matrix room state. Encrypted rooms show
src/config.rs +11
@@ -136,6 +136,17 @@ config!(
136 // Range: 0 ms to one day. 136 // Range: 0 ms to one day.
137 0..86400000, 137 0..86400000,
138 }, 138 },
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 },
139 }, 150 },
140 151
141 Section color { 152 Section color {
src/render.rs +70 −11
@@ -56,6 +56,12 @@ pub struct RenderedEvent {
56 pub content: RenderedContent, 56 pub content: RenderedContent,
57} 57}
58 58
59#[derive(Debug, Clone, Copy, Eq, PartialEq)]
60pub enum ReplyContext {
61 Inline,
62 Full,
63}
64
59impl RenderedEvent { 65impl RenderedEvent {
60 const MSG_TAGS: &'static [&'static str] = &["notify_message"]; 66 const MSG_TAGS: &'static [&'static str] = &["notify_message"];
61 const SELF_TAGS: &'static [&'static str] = 67 const SELF_TAGS: &'static [&'static str] =
@@ -81,6 +87,7 @@ impl RenderedEvent {
81 mut self, 87 mut self,
82 event_id: &EventId, 88 event_id: &EventId,
83 sender: Option<&str>, 89 sender: Option<&str>,
90 context: ReplyContext,
84 ) -> Self { 91 ) -> Self {
85 let reply_fallback = self.content.reply_fallback.take(); 92 let reply_fallback = self.content.reply_fallback.take();
86 let reply_sender = sender 93 let reply_sender = sender
@@ -99,6 +106,15 @@ impl RenderedEvent {
99 .unwrap_or_default(); 106 .unwrap_or_default();
100 tags.push("matrix_reply".to_owned()); 107 tags.push("matrix_reply".to_owned());
101 108
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
102 let mut context_lines = match reply_fallback { 118 let mut context_lines = match reply_fallback {
103 Some(fallback) => { 119 Some(fallback) => {
104 let mut lines = vec![RenderedLine { 120 let mut lines = vec![RenderedLine {
@@ -1631,18 +1647,21 @@ mod tests {
1631 message: "reply body".to_owned(), 1647 message: "reply body".to_owned(),
1632 }]), 1648 }]),
1633 } 1649 }
1634 .add_reply_context(&event_id, Some("Alice")); 1650 .add_reply_context(
1651 &event_id,
1652 Some("Alice"),
1653 ReplyContext::Inline,
1654 );
1635 1655
1636 assert_eq!(2, rendered.content.lines.len()); 1656 assert_eq!(1, rendered.content.lines.len());
1637 assert_eq!("Reply to Alice", rendered.content.lines[0].message); 1657 assert_eq!("> Alice: reply body", rendered.content.lines[0].message);
1638 assert!(rendered.content.lines[0] 1658 assert!(rendered.content.lines[0]
1639 .tags 1659 .tags
1640 .contains(&"matrix_reply".to_owned())); 1660 .contains(&"matrix_reply".to_owned()));
1641 assert_eq!("reply body", rendered.content.lines[1].message);
1642 } 1661 }
1643 1662
1644 #[test] 1663 #[test]
1645 fn reply_context_falls_back_to_event_id() { 1664 fn inline_reply_context_falls_back_to_event_id() {
1646 let event_id = 1665 let event_id =
1647 matrix_sdk::ruma::owned_event_id!("$replyevent:example.org"); 1666 matrix_sdk::ruma::owned_event_id!("$replyevent:example.org");
1648 let rendered = RenderedEvent { 1667 let rendered = RenderedEvent {
@@ -1653,16 +1672,15 @@ mod tests {
1653 message: "reply body".to_owned(), 1672 message: "reply body".to_owned(),
1654 }]), 1673 }]),
1655 } 1674 }
1656 .add_reply_context(&event_id, None); 1675 .add_reply_context(&event_id, None, ReplyContext::Inline);
1657 1676
1658 assert_eq!( 1677 assert_eq!(
1659 "Reply to $replyevent:example.org", 1678 "> $replyevent:example.org: reply body",
1660 rendered.content.lines[0].message 1679 rendered.content.lines[0].message
1661 ); 1680 );
1662 assert!(rendered.content.lines[0] 1681 assert!(rendered.content.lines[0]
1663 .tags 1682 .tags
1664 .contains(&"matrix_reply".to_owned())); 1683 .contains(&"matrix_reply".to_owned()));
1665 assert_eq!("reply body", rendered.content.lines[1].message);
1666 } 1684 }
1667 1685
1668 #[test] 1686 #[test]
@@ -1685,7 +1703,11 @@ mod tests {
1685 prefix: "bob\t".to_owned(), 1703 prefix: "bob\t".to_owned(),
1686 content: content.render(&()), 1704 content: content.render(&()),
1687 } 1705 }
1688 .add_reply_context(&event_id, Some("Alice")); 1706 .add_reply_context(
1707 &event_id,
1708 Some("Alice"),
1709 ReplyContext::Full,
1710 );
1689 1711
1690 assert_eq!(3, rendered.content.lines.len()); 1712 assert_eq!(3, rendered.content.lines.len());
1691 assert_eq!("Reply to Alice:", rendered.content.lines[0].message); 1713 assert_eq!("Reply to Alice:", rendered.content.lines[0].message);
@@ -1716,7 +1738,7 @@ mod tests {
1716 prefix: "bob\t".to_owned(), 1738 prefix: "bob\t".to_owned(),
1717 content: content.render(&()), 1739 content: content.render(&()),
1718 } 1740 }
1719 .add_reply_context(&event_id, None); 1741 .add_reply_context(&event_id, None, ReplyContext::Full);
1720 1742
1721 assert_eq!( 1743 assert_eq!(
1722 "Reply to @alice:example.org:", 1744 "Reply to @alice:example.org:",
@@ -1745,7 +1767,11 @@ mod tests {
1745 prefix: "bob\t".to_owned(), 1767 prefix: "bob\t".to_owned(),
1746 content: content.render(&()), 1768 content: content.render(&()),
1747 } 1769 }
1748 .add_reply_context(&event_id, Some("Alice")); 1770 .add_reply_context(
1771 &event_id,
1772 Some("Alice"),
1773 ReplyContext::Full,
1774 );
1749 1775
1750 let messages = rendered 1776 let messages = rendered
1751 .content 1777 .content
@@ -1760,6 +1786,39 @@ mod tests {
1760 ); 1786 );
1761 } 1787 }
1762 1788
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
1763 #[test] 1822 #[test]
1764 fn formatted_body_plain_text_keeps_block_structure() { 1823 fn formatted_body_plain_text_keeps_block_structure() {
1765 let body = "<blockquote><p>quoted</p><p>text</p></blockquote>\ 1824 let body = "<blockquote><p>quoted</p><p>text</p></blockquote>\
src/room/buffer.rs +31
@@ -186,6 +186,23 @@ impl RoomBuffer {
186 reply_sender_id_from_tags(&line.tags()) 186 reply_sender_id_from_tags(&line.tags())
187 } 187 }
188 188
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
189 /// Return whether an event is already rendered in the buffer. 206 /// Return whether an event is already rendered in the buffer.
190 pub fn contains_event(&self, event_id: &EventId) -> bool { 207 pub fn contains_event(&self, event_id: &EventId) -> bool {
191 let event_id_tag = Cow::from(event_id.to_tag()); 208 let event_id_tag = Cow::from(event_id.to_tag());
@@ -623,6 +640,20 @@ where
623 line.set_tags(&tags); 640 line.set_tags(&tags);
624} 641}
625 642
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
626fn sanitize_thread_id(thread_root: &EventId) -> String { 657fn sanitize_thread_id(thread_root: &EventId) -> String {
627 const THREAD_ID_DISPLAY_CHARS: usize = 12; 658 const THREAD_ID_DISPLAY_CHARS: usize = 12;
628 659
src/room/mod.rs +42 −2
@@ -85,7 +85,7 @@ use weechat::{
85use crate::{ 85use crate::{
86 config::{Config, RedactionStyle}, 86 config::{Config, RedactionStyle},
87 connection::Connection, 87 connection::Connection,
88 render::{Render, RenderedEvent}, 88 render::{Render, RenderedEvent, ReplyContext},
89 utils::{Edit, VerificationEvent}, 89 utils::{Edit, VerificationEvent},
90 PLUGIN_NAME, 90 PLUGIN_NAME,
91}; 91};
@@ -826,7 +826,21 @@ impl MatrixRoom {
826 }; 826 };
827 827
828 if let Some((event_id, sender)) = reply_to { 828 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 )
830 } else { 844 } else {
831 rendered 845 rendered
832 } 846 }
@@ -1794,6 +1808,16 @@ impl MatrixRoom {
1794 } 1808 }
1795} 1809}
1796 1810
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
1797#[cfg(test)] 1821#[cfg(test)]
1798mod tests { 1822mod tests {
1799 use super::*; 1823 use super::*;
@@ -1821,6 +1845,22 @@ mod tests {
1821 assert!(!should_render_event(true)); 1845 assert!(!should_render_event(true));
1822 } 1846 }
1823 1847
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
1824 #[test] 1864 #[test]
1825 fn thread_buffer_input_sends_thread_relation() { 1865 fn thread_buffer_input_sends_thread_relation() {
1826 let content = make_text_message_content( 1866 let content = make_text_message_content(