Commit 3396d8c8de
Verified · cmc
Changed files (10)
- Sources/OrgEditorAppKit/OrgEditor+Styling.swift +60 −1
- Sources/OrgEditorAppKit/OrgEditor.swift +7 −1
- Sources/OrgPresentation/TagGap.swift added +52
- Sources/OrgstarMobile/MobileEditor+Styling.swift +61 −1
- Sources/OrgstarMobile/MobileEditor.swift +3
- Sources/OrgstarMobile/ReaderScreen.swift +32 −2
- Tests/OrgEditorAppKitTests/TagGapEditorTests.swift added +52
- Tests/OrgPresentationTests/TagGapTests.swift added +23
- docs/manual/guide/02-the-editor.org +2
- docs/manual/guide/14-ios.org +1
Sources/OrgEditorAppKit/OrgEditor+Styling.swift +60 −1 · view
| @@ -31,10 +31,13 @@ extension OrgEditor { | ||
| 31 | 31 | document.tree, in: range, highlighter: highlighter, indented: indented, hideStars: hideStars, |
| 32 | 32 | showMarkup: showMarkup, options: options, revealed: revealed.location..<NSMaxRange(revealed) |
| 33 | 33 | ) |
| 34 | var tags: [Range<Int>] = [] | |
| 34 | 35 | for change in changes { |
| 35 | 36 | switch change { |
| 36 | 37 | case .indent(let line): storage.addAttribute(.paragraphStyle, value: paragraphStyle(line.firstLine, line.wrapped), range: NSRange(line.range)) |
| 37 | case .role(let role, let range): theme.apply(role, to: storage, range: NSRange(range)) | |
| 38 | case .role(let role, let range): | |
| 39 | theme.apply(role, to: storage, range: NSRange(range)) | |
| 40 | if role == .tags { tags.append(range) } | |
| 38 | 41 | case .hidden(let range): storage.addAttributes(theme.hidden, range: NSRange(range)) |
| 39 | 42 | } |
| 40 | 43 | } |
| @@ -49,6 +52,62 @@ extension OrgEditor { | ||
| 49 | 52 | applyHighlights(range, in: storage) |
| 50 | 53 | applyImages(range, in: storage, revealed: revealed) |
| 51 | 54 | applyLatex(range, in: storage, revealed: revealed) |
| 55 | fitTagGaps(tags, in: storage) | |
| 56 | } | |
| 57 | ||
| 58 | /// The text container's width, for tag gaps: the narrowest view's, as images are sized. | |
| 59 | var tagGapAvailableWidth: CGFloat { | |
| 60 | let widths = textViews.compactMap { view in view.textContainer.map { $0.size.width - 2 * $0.lineFragmentPadding } } | |
| 61 | return widths.filter { $0 > 0 }.min() ?? 0 | |
| 62 | } | |
| 63 | ||
| 64 | /// Draws the spaces before each heading's tags in `tags` as one gap that fits the view | |
| 65 | /// (`Presentation.tagGapWidth`): all but the last space hidden, the last widened. | |
| 66 | func fitTagGaps(_ tags: [Range<Int>], in storage: NSTextStorage) { | |
| 67 | guard !tags.isEmpty else { return } | |
| 68 | let available = tagGapAvailableWidth | |
| 69 | guard available > 0 else { return } | |
| 70 | // The storage's own string: `string` copies the whole text. | |
| 71 | let text = storage.mutableString | |
| 72 | for range in tags where range.upperBound <= text.length { | |
| 73 | guard let gap = Presentation.tagGap(tags: range, in: text) else { continue } | |
| 74 | let line = text.lineRange(for: NSRange(location: gap.gap.lowerBound, length: 0)) | |
| 75 | let indent = (storage.attribute(.paragraphStyle, at: line.location, effectiveRange: nil) as? NSParagraphStyle)?.firstLineHeadIndent ?? 0 | |
| 76 | // Without the paragraph style, whose indent is taken from the width apart. | |
| 77 | func width(_ range: Range<Int>) -> CGFloat { | |
| 78 | let part = NSMutableAttributedString(attributedString: storage.attributedSubstring(from: NSRange(range))) | |
| 79 | part.removeAttribute(.paragraphStyle, range: NSRange(location: 0, length: part.length)) | |
| 80 | return part.size().width | |
| 81 | } | |
| 82 | // Spaces measured before the tags: trailing spaces may not count. | |
| 83 | let last = gap.gap.upperBound - 1 | |
| 84 | let title = width(line.location..<gap.gap.lowerBound), tagsWidth = width(gap.tags) | |
| 85 | let space = width(last..<gap.tags.upperBound) - tagsWidth | |
| 86 | // A point to spare, so rounding doesn't wrap the tags. | |
| 87 | guard let shown = Presentation.tagGapWidth( | |
| 88 | title: title, gap: width(line.location..<gap.tags.upperBound) - title - tagsWidth, space: space, tags: tagsWidth, | |
| 89 | available: available - indent - 1 | |
| 90 | ) else { continue } | |
| 91 | storage.addAttributes(theme.hidden, range: NSRange(gap.gap.lowerBound..<last)) | |
| 92 | storage.addAttribute(.kern, value: max(0, shown - space), range: NSRange(location: last, length: 1)) | |
| 93 | } | |
| 94 | } | |
| 95 | ||
| 96 | /// Draws tag gaps again once the view's width has changed, after the layout pass that changed it. | |
| 97 | func refitTagGaps() { | |
| 98 | guard isOrg, !fittingTagGaps else { return } | |
| 99 | fittingTagGaps = true | |
| 100 | DispatchQueue.main.async { [weak self] in | |
| 101 | guard let self else { return } | |
| 102 | self.fittingTagGaps = false | |
| 103 | guard self.tagGapWidth != self.tagGapAvailableWidth else { return } | |
| 104 | let text = self.document.text as NSString | |
| 105 | for gap in Presentation.tagGaps(self.document.tree) where gap.tags.upperBound <= text.length { | |
| 106 | let line = text.lineRange(for: NSRange(location: gap.gap.lowerBound, length: 0)) | |
| 107 | self.restyleOutsideEditing(line.location..<NSMaxRange(line)) | |
| 108 | } | |
| 109 | self.tagGapWidth = self.tagGapAvailableWidth | |
| 110 | } | |
| 52 | 111 | } |
| 53 | 112 | |
| 54 | 113 | /// `org-pretty-entities`: entities drawn as their character over the entity's last |
Sources/OrgEditorAppKit/OrgEditor.swift +7 −1 · view
| @@ -189,7 +189,10 @@ public final class OrgEditor: NSObject { | ||
| 189 | 189 | onImage(data, ext) |
| 190 | 190 | return true |
| 191 | 191 | } |
| 192 | view.onWidthChange = { [weak self] in self?.fitImages() } | |
| 192 | view.onWidthChange = { [weak self] in | |
| 193 | self?.fitImages() | |
| 194 | self?.refitTagGaps() | |
| 195 | } | |
| 193 | 196 | view.onMouseDown = { [weak self, unowned view] in |
| 194 | 197 | self?.active = view |
| 195 | 198 | self?.markActive = false |
| @@ -714,6 +717,9 @@ public final class OrgEditor: NSObject { | ||
| 714 | 717 | /// The width images were last sized for, and whether sizing them again is waiting. |
| 715 | 718 | var imageWidth: CGFloat = 0 |
| 716 | 719 | var fittingImages = false |
| 720 | /// The text width heading tag gaps were last drawn for, and whether drawing them again is waiting. | |
| 721 | var tagGapWidth: CGFloat = 0 | |
| 722 | var fittingTagGaps = false | |
| 717 | 723 | /// `org-latex-preview`: LaTeX fragments show as typeset math. |
| 718 | 724 | public internal(set) var showsLatex = false |
| 719 | 725 | /// An `attachment:` link's file, from its name, offset and the tree: the app's |
Sources/OrgPresentation/TagGap.swift added +52 · view
| @@ -0,0 +1,52 @@ | ||
| 1 | import Foundation | |
| 2 | import OrgCore | |
| 3 | ||
| 4 | /// A heading's tags and the spaces before them, which `org-tags-column` pads to line the tags | |
| 5 | /// up. The editors and the reader draw the spaces as one gap so the line never wraps inside | |
| 6 | /// the padding or the tags; the file keeps its spaces. | |
| 7 | public struct TagGap: Sendable, Equatable { | |
| 8 | /// The spaces between the title and the tags. | |
| 9 | public let gap: Range<Int> | |
| 10 | public let tags: Range<Int> | |
| 11 | ||
| 12 | public init(gap: Range<Int>, tags: Range<Int>) { | |
| 13 | self.gap = gap | |
| 14 | self.tags = tags | |
| 15 | } | |
| 16 | } | |
| 17 | ||
| 18 | extension Presentation { | |
| 19 | /// The spaces before `tags`, when there are two or more. A gap with a tab is left as written. | |
| 20 | public static func tagGap(tags: Range<Int>, in text: NSString) -> TagGap? { | |
| 21 | var start = tags.lowerBound | |
| 22 | while start > 0, text.character(at: start - 1) == 0x20 { start -= 1 } | |
| 23 | guard tags.lowerBound - start > 1, start > 0, text.character(at: start - 1) != 0x09 else { return nil } | |
| 24 | return TagGap(gap: start..<tags.lowerBound, tags: tags) | |
| 25 | } | |
| 26 | ||
| 27 | /// Every heading's tag gap, in document order. | |
| 28 | public static func tagGaps(_ tree: OrgTree) -> [TagGap] { | |
| 29 | var gaps: [TagGap] = [] | |
| 30 | let text = tree.text as NSString | |
| 31 | func visit(_ node: SyntaxNode) { | |
| 32 | for section in node.children where section.kind == .section { | |
| 33 | if let tags = section.firstChild(.heading)?.tokens.first(where: { $0.kind == .tags }), | |
| 34 | tags.range.upperBound <= text.length, let gap = tagGap(tags: tags.range, in: text) { | |
| 35 | gaps.append(gap) | |
| 36 | } | |
| 37 | visit(section) | |
| 38 | } | |
| 39 | } | |
| 40 | visit(tree.root) | |
| 41 | return gaps | |
| 42 | } | |
| 43 | ||
| 44 | /// The width to draw a tag gap at, on a line `available` wide whose text before the gap is | |
| 45 | /// `title` wide: nil, as written, when the line fits; else the tags at the right edge when | |
| 46 | /// they fit after one space; else one space, and the line wraps before the tags. | |
| 47 | public static func tagGapWidth(title: CGFloat, gap: CGFloat, space: CGFloat, tags: CGFloat, available: CGFloat) -> CGFloat? { | |
| 48 | if title + gap + tags <= available { return nil } | |
| 49 | if title + space + tags <= available { return available - title - tags } | |
| 50 | return space | |
| 51 | } | |
| 52 | } | |
Sources/OrgstarMobile/MobileEditor+Styling.swift +61 −1 · view
| @@ -104,6 +104,7 @@ extension MobileEditor { | ||
| 104 | 104 | /// wider line in a truncated file, which widens the band on each layout pass, redraws |
| 105 | 105 | /// the blocks alone: laying the whole file out again moves the text under the view. |
| 106 | 106 | func viewResized() { |
| 107 | refitTagGaps() | |
| 107 | 108 | // With several views, the band spans the widest and images fit the narrowest. |
| 108 | 109 | let band = fragmentStyle.blockBand |
| 109 | 110 | let width = textViews.map { max($0.bounds.width, truncatesLines ? $0.contentSize.width : 0) }.max() ?? 0 |
| @@ -224,10 +225,13 @@ extension MobileEditor { | ||
| 224 | 225 | document.tree, in: whole, highlighter: Self.highlighter, indented: indented, hideStars: hideStars, |
| 225 | 226 | showMarkup: showMarkup, options: options, revealed: revealed.location..<NSMaxRange(revealed) |
| 226 | 227 | ) |
| 228 | var tags: [Range<Int>] = [] | |
| 227 | 229 | for change in changes { |
| 228 | 230 | switch change { |
| 229 | 231 | case .indent(let line): storage.addAttribute(.paragraphStyle, value: paragraphStyle(line.firstLine, line.wrapped), range: NSRange(line.range)) |
| 230 | case .role(let role, let range): theme.apply(role, to: storage, range: NSRange(range)) | |
| 232 | case .role(let role, let range): | |
| 233 | theme.apply(role, to: storage, range: NSRange(range)) | |
| 234 | if role == .tags { tags.append(range) } | |
| 231 | 235 | case .hidden(let range): storage.addAttributes(theme.hidden, range: NSRange(range)) |
| 232 | 236 | } |
| 233 | 237 | } |
| @@ -243,10 +247,66 @@ extension MobileEditor { | ||
| 243 | 247 | applyShrunk(whole, in: storage) |
| 244 | 248 | applyImages(whole, in: storage, revealed: revealed) |
| 245 | 249 | applyLatex(whole, in: storage, revealed: revealed) |
| 250 | fitTagGaps(tags, in: storage) | |
| 246 | 251 | if options.spellCheck { markMisspellings(in: whole, storage: storage) } |
| 247 | 252 | storage.endEditing() |
| 248 | 253 | } |
| 249 | 254 | |
| 255 | /// The text container's width, for tag gaps: the narrowest view's, as images are sized. | |
| 256 | var tagGapAvailableWidth: CGFloat { | |
| 257 | textViews.map { $0.textContainer.size.width - 2 * $0.textContainer.lineFragmentPadding }.filter { $0 > 0 }.min() ?? 0 | |
| 258 | } | |
| 259 | ||
| 260 | /// Draws the spaces before each heading's tags in `tags` as one gap that fits the view | |
| 261 | /// (`Presentation.tagGapWidth`): all but the last space hidden, the last widened. | |
| 262 | private func fitTagGaps(_ tags: [Range<Int>], in storage: NSTextStorage) { | |
| 263 | guard !tags.isEmpty else { return } | |
| 264 | let available = tagGapAvailableWidth | |
| 265 | guard available > 0 else { return } | |
| 266 | // The storage's own string: `string` copies the whole text. | |
| 267 | let text = storage.mutableString | |
| 268 | for range in tags where range.upperBound <= text.length { | |
| 269 | guard let gap = Presentation.tagGap(tags: range, in: text) else { continue } | |
| 270 | let line = text.lineRange(for: NSRange(location: gap.gap.lowerBound, length: 0)) | |
| 271 | let indent = (storage.attribute(.paragraphStyle, at: line.location, effectiveRange: nil) as? NSParagraphStyle)?.firstLineHeadIndent ?? 0 | |
| 272 | // Without the paragraph style, whose indent is taken from the width apart. | |
| 273 | func width(_ range: Range<Int>) -> CGFloat { | |
| 274 | let part = NSMutableAttributedString(attributedString: storage.attributedSubstring(from: NSRange(range))) | |
| 275 | part.removeAttribute(.paragraphStyle, range: NSRange(location: 0, length: part.length)) | |
| 276 | return part.size().width | |
| 277 | } | |
| 278 | // Spaces measured before the tags: trailing spaces may not count. | |
| 279 | let last = gap.gap.upperBound - 1 | |
| 280 | let title = width(line.location..<gap.gap.lowerBound), tagsWidth = width(gap.tags) | |
| 281 | let space = width(last..<gap.tags.upperBound) - tagsWidth | |
| 282 | // A point to spare, so rounding doesn't wrap the tags. | |
| 283 | guard let shown = Presentation.tagGapWidth( | |
| 284 | title: title, gap: width(line.location..<gap.tags.upperBound) - title - tagsWidth, space: space, tags: tagsWidth, | |
| 285 | available: available - indent - 1 | |
| 286 | ) else { continue } | |
| 287 | storage.addAttributes(theme.hidden, range: NSRange(gap.gap.lowerBound..<last)) | |
| 288 | storage.addAttribute(.kern, value: max(0, shown - space), range: NSRange(location: last, length: 1)) | |
| 289 | } | |
| 290 | } | |
| 291 | ||
| 292 | /// Draws tag gaps again once the text's width has changed, after the layout pass that changed it. | |
| 293 | private func refitTagGaps() { | |
| 294 | guard !fittingTagGaps, tagGapWidth != tagGapAvailableWidth else { return } | |
| 295 | fittingTagGaps = true | |
| 296 | DispatchQueue.main.async { [weak self] in | |
| 297 | guard let self else { return } | |
| 298 | self.fittingTagGaps = false | |
| 299 | let width = self.tagGapAvailableWidth | |
| 300 | guard self.tagGapWidth != width else { return } | |
| 301 | self.tagGapWidth = width | |
| 302 | let text = self.textView.textStorage.string as NSString | |
| 303 | for gap in Presentation.tagGaps(self.document.tree) where gap.tags.upperBound <= min(text.length, self.styled.end) { | |
| 304 | let line = text.lineRange(for: NSRange(location: gap.gap.lowerBound, length: 0)) | |
| 305 | self.restyle(line.location..<NSMaxRange(line)) | |
| 306 | } | |
| 307 | } | |
| 308 | } | |
| 309 | ||
| 250 | 310 | func paragraphStyle(_ firstLine: Int, _ wrapped: Int) -> NSParagraphStyle { |
| 251 | 311 | if let style = paragraphStyles[[firstLine, wrapped]] { return style } |
| 252 | 312 | let style = theme.paragraphStyle(firstLine: firstLine, wrapped: wrapped) |
Sources/OrgstarMobile/MobileEditor.swift +3 · view
| @@ -136,6 +136,9 @@ final class MobileEditor: NSObject, DocumentBuffer { | ||
| 136 | 136 | var showsLatex = false |
| 137 | 137 | /// The view width images were last sized for. |
| 138 | 138 | var imageWidth: CGFloat = 0 |
| 139 | /// The text width heading tag gaps were last drawn for, and whether drawing them again is waiting. | |
| 140 | var tagGapWidth: CGFloat = 0 | |
| 141 | var fittingTagGaps = false | |
| 139 | 142 | /// The file's folder, for relative image paths. |
| 140 | 143 | let baseDirectory: URL? |
| 141 | 144 | /// An `attachment:` link's file, from its name, offset and the tree; nil leaves those links |
Sources/OrgstarMobile/ReaderScreen.swift +32 −2 · view
| @@ -35,6 +35,8 @@ struct ReaderScreen: View { | ||
| 35 | 35 | @State private var loadedPath: String? |
| 36 | 36 | /// The file's display: indentation, leading stars, inline images, truncation. |
| 37 | 37 | @State private var startup: StartupDisplay? |
| 38 | /// Headings' padded tags, by the heading's start. | |
| 39 | @State private var tagGaps: [Int: TagGap] = [:] | |
| 38 | 40 | /// The view's width, which images in a line's text are no wider than. |
| 39 | 41 | @State private var width: CGFloat = 0 |
| 40 | 42 | @State private var showingOutline = false |
| @@ -188,7 +190,10 @@ struct ReaderScreen: View { | ||
| 188 | 190 | let ns = tree.text as NSString |
| 189 | 191 | let line = ns.lineRange(for: NSRange(location: entry.start, length: 0)) |
| 190 | 192 | let start = indented ? min(entry.start + entry.level + 1, NSMaxRange(line)) : entry.start |
| 191 | let text = attributed(ReadingText.segments(tree, in: start..<NSMaxRange(line), display: display), heading: entry.level, theme) | |
| 193 | let tags = tagGaps[entry.start].flatMap { $0.tags.upperBound <= NSMaxRange(line) ? $0 : nil } | |
| 194 | func styled(_ range: Range<Int>) -> AttributedString { | |
| 195 | attributed(ReadingText.segments(tree, in: range, display: display), heading: entry.level, theme) | |
| 196 | } | |
| 192 | 197 | let hasContent = entry.end > NSMaxRange(line) |
| 193 | 198 | let foldedMark = fold.folds.contains(entry.start) && hasContent |
| 194 | 199 | return HStack(alignment: .firstTextBaseline, spacing: 0) { |
| @@ -196,7 +201,12 @@ struct ReaderScreen: View { | ||
| 196 | 201 | .font(.caption.weight(.semibold)).foregroundStyle(theme.swiftColor("markup") ?? Color(uiColor: .tertiaryLabel)) |
| 197 | 202 | .opacity(hasContent ? 1 : 0) |
| 198 | 203 | .frame(width: 2 * theme.column, alignment: .leading) |
| 199 | lineText(Text(text.trimmingTrailingNewlines()), theme) | |
| 204 | if let tags, tags.gap.lowerBound >= start { | |
| 205 | headingWithTags(title: styled(start..<tags.gap.lowerBound), gap: styled(tags.gap), | |
| 206 | tags: styled(tags.tags.lowerBound..<NSMaxRange(line)).trimmingTrailingNewlines(), theme) | |
| 207 | } else { | |
| 208 | lineText(Text(styled(start..<NSMaxRange(line)).trimmingTrailingNewlines()), theme) | |
| 209 | } | |
| 200 | 210 | } |
| 201 | 211 | .padding(.leading, headingIndent(entry, theme)) |
| 202 | 212 | .padding(.top, entry.level == 1 ? 6 : 0) |
| @@ -206,6 +216,24 @@ struct ReaderScreen: View { | ||
| 206 | 216 | .accessibilityHint(foldedMark ? "Unfolds the heading" : "Folds the heading") |
| 207 | 217 | } |
| 208 | 218 | |
| 219 | /// A heading whose tags are padded: as written when the line fits, the tags at the right | |
| 220 | /// edge when they fit after one space, else the tags on a line of their own at the right. | |
| 221 | private func headingWithTags(title: AttributedString, gap: AttributedString, tags: AttributedString, _ theme: MobileTheme) -> some View { | |
| 222 | ViewThatFits(in: .horizontal) { | |
| 223 | Text(title + gap + tags).fixedSize() | |
| 224 | HStack(alignment: .firstTextBaseline, spacing: 0) { | |
| 225 | Text(title).fixedSize() | |
| 226 | Spacer(minLength: theme.column) | |
| 227 | Text(tags).fixedSize() | |
| 228 | } | |
| 229 | VStack(alignment: .trailing, spacing: 0) { | |
| 230 | lineText(Text(title), theme) | |
| 231 | Text(tags).fixedSize() | |
| 232 | } | |
| 233 | } | |
| 234 | .lineSpacing(CGFloat(max(0, theme.spec.lineSpacing))) | |
| 235 | } | |
| 236 | ||
| 209 | 237 | private func textView(_ tree: OrgTree, _ range: Range<Int>, _ theme: MobileTheme) -> some View { |
| 210 | 238 | VStack(alignment: .leading, spacing: 4) { |
| 211 | 239 | ForEach(parts(tree, range), id: \.range.lowerBound) { part in |
| @@ -422,6 +450,8 @@ struct ReaderScreen: View { | ||
| 422 | 450 | guard let tree = document?.tree, showsOwnFile else { return } |
| 423 | 451 | let display = Presentation.startupDisplay(tree, options: session.options) |
| 424 | 452 | startup = display |
| 453 | let text = tree.text as NSString | |
| 454 | tagGaps = Dictionary(Presentation.tagGaps(tree).map { (text.lineRange(for: NSRange(location: $0.gap.lowerBound, length: 0)).location, $0) }) { a, _ in a } | |
| 425 | 455 | folded = display.wrappers.map(\.hidden) |
| 426 | 456 | foldable = folded |
| 427 | 457 | guard loadedPath != shownPath else { return } |
Tests/OrgEditorAppKitTests/TagGapEditorTests.swift added +52 · view
| @@ -0,0 +1,52 @@ | ||
| 1 | import AppKit | |
| 2 | import OrgCore | |
| 3 | import Testing | |
| 4 | @testable import OrgEditorAppKit | |
| 5 | ||
| 6 | /// The spaces before a heading's tags, drawn as one gap that fits the view's width. | |
| 7 | @MainActor | |
| 8 | struct TagGapEditorTests { | |
| 9 | static func heading(_ title: String, _ tags: String, column: Int = 77) -> String { | |
| 10 | title + String(repeating: " ", count: max(1, column - title.count - tags.count)) + tags | |
| 11 | } | |
| 12 | ||
| 13 | func y(_ h: Harness, _ offset: Int) -> CGFloat { | |
| 14 | h.layout() | |
| 15 | return h.textView.firstRect(forCharacterRange: NSRange(location: offset, length: 1), actualRange: nil).minY | |
| 16 | } | |
| 17 | ||
| 18 | func x(_ h: Harness, _ offset: Int) -> CGFloat { | |
| 19 | h.layout() | |
| 20 | return h.textView.firstRect(forCharacterRange: NSRange(location: offset, length: 1), actualRange: nil).maxX | |
| 21 | } | |
| 22 | ||
| 23 | @Test func theGapIsAsWrittenInAWideViewAndFitsANarrowOne() async throws { | |
| 24 | let text = Self.heading("* TODO Call the office", ":office:") + "\n" + Self.heading("** A title long enough to need the line twice over in a narrow view", ":report:", column: 90) + "\nbody\n" | |
| 25 | let h = Harness(text) | |
| 26 | h.window.setContentSize(NSSize(width: 900, height: 800)) | |
| 27 | h.window.contentView?.layoutSubtreeIfNeeded() | |
| 28 | try await Task.sleep(for: .milliseconds(100)) | |
| 29 | let ns = text as NSString | |
| 30 | let tags = ns.range(of: ":office:").location | |
| 31 | let storage = try #require(h.textView.textStorage) | |
| 32 | // Wide enough: the spaces stay as written. | |
| 33 | #expect(storage.attribute(.kern, at: tags - 1, effectiveRange: nil) == nil) | |
| 34 | #expect(y(h, tags) == y(h, 0)) | |
| 35 | ||
| 36 | h.window.setContentSize(NSSize(width: 450, height: 800)) | |
| 37 | h.window.contentView?.layoutSubtreeIfNeeded() | |
| 38 | try await Task.sleep(for: .milliseconds(100)) | |
| 39 | // The tags at the right edge of the heading's line. | |
| 40 | #expect(y(h, tags) == y(h, 0)) | |
| 41 | let container = try #require(h.textView.textContainer) | |
| 42 | let indent = (storage.attribute(.paragraphStyle, at: 0, effectiveRange: nil) as? NSParagraphStyle)?.firstLineHeadIndent ?? 0 | |
| 43 | let lineStart = h.textView.firstRect(forCharacterRange: NSRange(location: 0, length: 1), actualRange: nil).minX | |
| 44 | #expect(abs(x(h, tags + 7) - lineStart - (container.size.width - 2 * container.lineFragmentPadding - indent)) < 3) | |
| 45 | // A title that wraps: one space, and the tags whole on the last line. | |
| 46 | let report = ns.range(of: ":report:").location | |
| 47 | #expect(y(h, report) == y(h, report + 7)) | |
| 48 | // Screen coordinates: lower lines have a smaller y. | |
| 49 | #expect(y(h, report) < y(h, ns.range(of: "** A").location)) | |
| 50 | #expect(storage.string == text) | |
| 51 | } | |
| 52 | } | |
Tests/OrgPresentationTests/TagGapTests.swift added +23 · view
| @@ -0,0 +1,23 @@ | ||
| 1 | import Foundation | |
| 2 | import OrgCore | |
| 3 | import Testing | |
| 4 | @testable import OrgPresentation | |
| 5 | ||
| 6 | struct TagGapTests { | |
| 7 | @Test func aPaddedHeadingHasAGap() { | |
| 8 | let text = "* TODO Call :office:\n** Short :a:b:\n* Tab\t :x:\n*** Deep :y:\n" | |
| 9 | let gaps = Presentation.tagGaps(OrgParser.parse(text)) | |
| 10 | let ns = text as NSString | |
| 11 | #expect(gaps.map { ns.substring(with: NSRange($0.gap)) } == [" ", " "]) | |
| 12 | #expect(gaps.map { ns.substring(with: NSRange($0.tags)) } == [":office:", ":y:"]) | |
| 13 | } | |
| 14 | ||
| 15 | @Test func theGapFitsTheWidth() { | |
| 16 | // As written when the line fits, as on a wide Mac window. | |
| 17 | #expect(Presentation.tagGapWidth(title: 100, gap: 300, space: 7, tags: 50, available: 500) == nil) | |
| 18 | // The tags at the right edge when only the padding doesn't fit. | |
| 19 | #expect(Presentation.tagGapWidth(title: 100, gap: 300, space: 7, tags: 50, available: 300) == 150) | |
| 20 | // One space when the title and tags don't fit on the line. | |
| 21 | #expect(Presentation.tagGapWidth(title: 280, gap: 300, space: 7, tags: 50, available: 300) == 7) | |
| 22 | } | |
| 23 | } | |
docs/manual/guide/02-the-editor.org +2 · view
| @@ -307,6 +307,8 @@ Long lines wrap at the edge of the editor by default. View ▸ Truncate Long Lin | ||
| 307 | 307 | |
| 308 | 308 | To truncate lines in every file, turn on Settings ▸ Editing ▸ "Long lines run off the edge instead of wrapping", or set =org-startup-truncated = true= in =config.toml=. The default is to wrap, as Doom does; Emacs's own default is to truncate. |
| 309 | 309 | |
| 310 | The spaces that align a heading's tags (see [[file:05-todos-and-tags.org][TODOs and tags]]) never wrap. When the heading fits the line as written, it shows as written. When only the spaces don't fit, the tags show at the right edge; when the title and tags don't fit on one line, one space separates them. A line can't break between a title's last word and its tags, so at the largest text sizes, when the two together are wider than the line, the tags break. The reader on iPhone and iPad puts the tags on a line of their own instead. The file keeps its spaces. | |
| 311 | ||
| 310 | 312 | Wrapping is display only. To rewrap the text of a paragraph, use Org ▸ Fill Paragraph (=org-fill-paragraph=), which breaks the paragraph's lines at the fill column: =M-q= in the Emacs preset and in every Doom state, =⌃⌘P= in the Mac preset. With a selection, it fills every paragraph the selection touches. In Doom's normal and visual states, =gq= and =gw= with a motion fill the paragraphs in the lines it covers (=gqq= and =gww= for the current lines); =gq= leaves the caret on the last line, =gw= where it was. Unlike Fill Paragraph, =gq= and =gw= keep extra spaces between words and at the ends of lines, as Doom does. See [[file:03-keys.org][Keys and commands]]. The fill column is Settings ▸ Editing ▸ "Fill Paragraph wraps lines at column N" (80 by default, 40 to 200), or =fill-column= in =config.toml=. |
| 311 | 313 | |
| 312 | 314 | =auto-fill-mode= breaks lines as you type: typing a space past the fill column breaks the line at the last space before the column, as often as it takes, and the new line starts with the line's fill prefix, as =org-auto-fill-function= has it: a list item's text lines up under the item, a comment line starts with =#=, and headings, tables and code aren't broken. Turn it on for every file with =auto-fill-mode = true= in =config.toml=, or for the current buffer with View ▸ Auto Fill (=auto-fill-mode=, in the palette too); the echo area says =Auto-Fill mode enabled in current buffer= or =disabled=. Only a typed space fills, not =RET=. |
docs/manual/guide/14-ios.org +1 · view
| @@ -41,6 +41,7 @@ Tap a file in Folders or Search, or Open in File in an agenda entry's sheet, to | ||
| 41 | 41 | - The reader follows the editor's display settings (see Themes and display below): Show Markup, hidden emphasis markers, pretty entities, indentation, leading stars, inline images and truncated lines. Tables show at full width, without narrowed columns. |
| 42 | 42 | - Tap a link to follow it. Links to headings and files in your folders open in the reader; links to other files, such as an attached PDF or image, open in Quick Look; web links open in the browser. |
| 43 | 43 | - The toolbar has Find, Outline (a list of headings to jump to), Export, and Edit (the pencil). |
| 44 | - Tags aligned with spaces show at the right edge of the heading's line, or on a line of their own at the right when the heading wraps (see [[file:02-the-editor.org][The editor]], Wrapping and filling). | |
| 44 | 45 | - Find opens the system find bar over the file's text. Results are marked, and the one shown is unfolded and scrolled to. |
| 45 | 46 | |
| 46 | 47 | * The editor |