Commit 70e815441a

70e815441aa336e490b489c200d5236e32f4cbad

parent: b35b748620

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-22 03:53 UTC

Save a typed match or search as an agenda view

Save as View…, in the Agenda window's view menu and iOS's Views menu
while a typed match or search is showing, asks for a name and adds the
view to the end of views.toml.

Closes #92

Layout: unified · split

Sources/OrgApp/AgendaViews.swift +48
@@ -213,6 +213,54 @@ public enum AgendaViewLoader {
213213 return (views, problems)
214214 }
215215
216 /// Why a view wasn't saved.
217 public struct SaveFailure: Error, Equatable, CustomStringConvertible {
218 public let description: String
219 }
220
221 /// The `[[view]]` table that makes `mode` a view named `name`; nil for a block agenda and a
222 /// stuck-projects view with its own definition, which one table can't hold.
223 static func table(name: String, mode: AgendaMode) -> String? {
224 func line(_ key: String, _ value: String?) -> String { value.map { "\(key) = \(UserConfig.format(.string($0)))\n" } ?? "" }
225 var toml = "[[view]]\n" + line("name", name)
226 switch mode {
227 case .agenda(let span, let start):
228 toml += line("type", "agenda") + (span.map { "span = \($0)\n" } ?? "") + (start.map { "start = \($0)\n" } ?? "")
229 case .todo(let keywords):
230 toml += line("type", "todo") + line("keywords", keywords)
231 case .match(let match, let todoOnly):
232 toml += line("type", todoOnly ? "todo-match" : "match") + line("match", match)
233 case .search(let string, let todoOnly):
234 toml += line("type", todoOnly ? "todo-search" : "search") + line("match", string)
235 case .stuck(nil):
236 toml += line("type", "stuck")
237 case .stuck, .blocks:
238 return nil
239 }
240 return toml
241 }
242
243 /// Adds a view of `mode` named `name` to the end of `views.toml`, making the file when it
244 /// isn't there. A name another view has, the built-in ones included, is refused.
245 public static func save(name: String, mode: AgendaMode, userFile: URL = userFile) throws(SaveFailure) {
246 let name = name.trimmingCharacters(in: .whitespacesAndNewlines)
247 guard !name.isEmpty else { throw SaveFailure(description: String(localized: "A view needs a name.")) }
248 guard !load(userFile: userFile).views.contains(where: { $0.name == name }) else {
249 throw SaveFailure(description: String(localized: "There is already a view named \(name)."))
250 }
251 guard let table = table(name: name, mode: mode) else {
252 throw SaveFailure(description: String(localized: "This view can't be saved from here; write it in views.toml."))
253 }
254 var text = (try? String(contentsOf: userFile, encoding: .utf8)) ?? ""
255 if !text.isEmpty { text += text.hasSuffix("\n") ? "\n" : "\n\n" }
256 do {
257 try FileManager.default.createDirectory(at: userFile.deletingLastPathComponent(), withIntermediateDirectories: true)
258 try (text + table).write(to: userFile, atomically: true, encoding: .utf8)
259 } catch {
260 throw SaveFailure(description: error.localizedDescription)
261 }
262 }
263
216264 /// The view a `[[view]]` or `[[view.block]]` table describes.
217265 static func mode(_ table: TOML.Table, name: String, _ problems: inout [String]) -> AgendaMode? {
218266 let match = table.values["match"]?.string
Sources/Orgstar/AgendaView+Toolbar.swift +27
@@ -18,6 +18,9 @@ extension AgendaView {
1818 Button(Self.todoMatch) { viewName = Self.todoMatch }
1919 Button(Self.search) { viewName = Self.search }
2020 Button(Self.todoSearch) { viewName = Self.todoSearch }
21 Divider()
22 Button("Save as View…") { saveView() }
23 .disabled(!canSaveView)
2124 } label: {
2225 Label(viewName, systemImage: "calendar").labelStyle(.titleAndIcon)
2326 }
@@ -219,6 +222,30 @@ extension AgendaView {
219222 return agenda.viewSettings[agenda.mode]?.header ?? agenda.mode.title
220223 }
221224
225 /// A typed match or search is showing, which isn't a named view yet.
226 var canSaveView: Bool {
227 guard isMatch || isSearch else { return false }
228 switch agenda.mode {
229 case .match, .search: return true
230 default: return false
231 }
232 }
233
234 /// Asks for a name and adds the match or search showing to `views.toml`, then shows it as
235 /// that view.
236 func saveView() {
237 let mode = agenda.mode
238 textPrompt = TextPrompt(title: String(localized: "Name for this view:")) { name in
239 do {
240 try AgendaViewLoader.save(name: name, mode: mode)
241 loadViews()
242 viewName = name.trimmingCharacters(in: .whitespacesAndNewlines)
243 } catch {
244 message = "\(error)"
245 }
246 }
247 }
248
222249 func loadViews() {
223250 let loaded = AgendaViewLoader.load()
224251 views = loaded.views
Sources/OrgstarMobile/AgendaScreen.swift +35 −7
@@ -73,6 +73,8 @@ struct AgendaScreen: View, Equatable {
7373 /// A match typed for the tag and property view, or a search string.
7474 private enum Asking: Identifiable {
7575 case match, search
76 /// A name for the match or search showing, to keep it as a view.
77 case save
7678 var id: Self { self }
7779 }
7880
@@ -212,6 +214,7 @@ struct AgendaScreen: View, Equatable {
212214 }
213215 Button("Tags and Properties…") { asking = .match }
214216 Button("Search…") { asking = .search }
217 if canSaveView { Button("Save as View…") { asking = .save } }
215218 Divider()
216219 Button("Board", systemImage: "rectangle.split.3x1") { showingBoard = true }
217220 } label: {
@@ -241,7 +244,9 @@ struct AgendaScreen: View, Equatable {
241244 }
242245 }
243246 .sheet(item: $asking) { kind in
244 if kind == .search {
247 if kind == .save {
248 PromptSheet(prompt: Prompt(key: "view-name", message: "Name for this view:")) { saveView($0) }
249 } else if kind == .search {
245250 PromptSheet(prompt: Prompt(key: "search", message: "Search: a phrase, or +word -word {regexp}; * for headlines, ! for TODO entries")) { answer in
246251 asking = nil
247252 guard let answer, answer.contains(where: { !$0.isWhitespace }) else { return }
@@ -310,12 +315,7 @@ struct AgendaScreen: View, Equatable {
310315 await workspace.refresh()
311316 await agenda.refresh(open: session.openTexts)
312317 }
313 .task {
314 let loaded = AgendaViewLoader.load()
315 views = loaded.views
316 agenda.viewSettings = AgendaViewLoader.settings(loaded.views)
317 if let problem = loaded.problems.first { message = problem }
318 }
318 .task { loadViews() }
319319 // The now line moves each minute; a new day refreshes the statuses.
320320 .task {
321321 while !Task.isCancelled {
@@ -395,6 +395,34 @@ struct AgendaScreen: View, Equatable {
395395 return agenda.list
396396 }
397397
398 private func loadViews() {
399 let loaded = AgendaViewLoader.load()
400 views = loaded.views
401 agenda.viewSettings = AgendaViewLoader.settings(loaded.views)
402 if let problem = loaded.problems.first { message = problem }
403 }
404
405 /// A typed match or search is showing, which isn't a named view yet.
406 private var canSaveView: Bool {
407 switch agenda.mode {
408 case .match, .search: return !views.contains { $0.mode == agenda.mode }
409 default: return false
410 }
411 }
412
413 /// Adds the match or search showing to `views.toml` as `name`, then shows it as that view.
414 private func saveView(_ name: String?) {
415 asking = nil
416 guard let name else { return }
417 do {
418 try AgendaViewLoader.save(name: name, mode: agenda.mode)
419 loadViews()
420 viewName = name.trimmingCharacters(in: .whitespacesAndNewlines)
421 } catch {
422 message = "\(error)"
423 }
424 }
425
398426 private func show(_ definition: AgendaViewDefinition) {
399427 viewName = definition.name
400428 agenda.mode = definition.mode
Tests/OrgAppTests/AgendaViewSaveTests.swift added +53
@@ -0,0 +1,53 @@
1import Foundation
2import Testing
3@testable import OrgApp
4
5@MainActor
6struct AgendaViewSaveTests {
7 private func file() throws -> URL {
8 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-views-\(UUID().uuidString)")
9 return folder.appendingPathComponent("config/views.toml")
10 }
11
12 @Test func savedViewsLoadBackAsTheyWere() throws {
13 let file = try file()
14 defer { try? FileManager.default.removeItem(at: file.deletingLastPathComponent().deletingLastPathComponent()) }
15 let modes: [(String, AgendaMode)] = [
16 ("Work", .match("+work-boss|LEVEL>2/!TODO", todoOnly: false)),
17 ("Waiting \"on\" others", .match("TODO=\"WAIT\"", todoOnly: true)),
18 ("Heron", .search("+heron -pond {ot+er}", todoOnly: false)),
19 ("Open phrases", .search("a phrase", todoOnly: true)),
20 ("Holds", .todo(keywords: "WAIT|HOLD")),
21 ("Three days", .agenda(span: 3, startOffset: -1)),
22 ("Stuck", .stuck(nil)),
23 ]
24 for (name, mode) in modes { try AgendaViewLoader.save(name: name, mode: mode, userFile: file) }
25 let loaded = AgendaViewLoader.load(userFile: file)
26 #expect(loaded.problems.isEmpty)
27 let saved = loaded.views.dropFirst(AgendaViewLoader.builtIn.count)
28 #expect(saved.map(\.name) == modes.map(\.0))
29 #expect(saved.map(\.mode) == modes.map(\.1))
30 }
31
32 @Test func aViewIsAddedAfterWhatTheFileHolds() throws {
33 let file = try file()
34 defer { try? FileManager.default.removeItem(at: file.deletingLastPathComponent().deletingLastPathComponent()) }
35 try FileManager.default.createDirectory(at: file.deletingLastPathComponent(), withIntermediateDirectories: true)
36 let existing = "# mine\n[[view]]\nname = \"Home\"\ntype = \"match\"\nmatch = \"+home\""
37 try existing.write(to: file, atomically: true, encoding: .utf8)
38 try AgendaViewLoader.save(name: " Work ", mode: .match("+work", todoOnly: false), userFile: file)
39 #expect(try String(contentsOf: file, encoding: .utf8) == existing + "\n\n[[view]]\nname = \"Work\"\ntype = \"match\"\nmatch = \"+work\"\n")
40 }
41
42 @Test func refusesEmptyAndTakenNamesAndViewsOneTableCannotHold() throws {
43 let file = try file()
44 defer { try? FileManager.default.removeItem(at: file.deletingLastPathComponent().deletingLastPathComponent()) }
45 let mode = AgendaMode.match("+work", todoOnly: false)
46 #expect(throws: AgendaViewLoader.SaveFailure.self) { try AgendaViewLoader.save(name: " ", mode: mode, userFile: file) }
47 #expect(throws: AgendaViewLoader.SaveFailure.self) { try AgendaViewLoader.save(name: "TODO List", mode: mode, userFile: file) }
48 try AgendaViewLoader.save(name: "Work", mode: mode, userFile: file)
49 #expect(throws: AgendaViewLoader.SaveFailure.self) { try AgendaViewLoader.save(name: "Work", mode: mode, userFile: file) }
50 #expect(throws: AgendaViewLoader.SaveFailure.self) { try AgendaViewLoader.save(name: "Blocks", mode: .blocks([]), userFile: file) }
51 #expect(AgendaViewLoader.load(userFile: file).views.count == AgendaViewLoader.builtIn.count + 1)
52 }
53}
docs/manual/guide/07-agenda.org +5 −3
@@ -465,7 +465,9 @@ The search covers the agenda files only, not =org-agenda-text-search-extra-files
465465
466466* Views
467467
468The view menu lists the built-in views, Agenda, TODO List and Stuck Projects, then the views in =views.toml=, then Match…, TODO Match…, Search… and TODO Search…. Saved views mirror =org-agenda-custom-commands=, with fewer options.
468The view menu lists the built-in views, Agenda, TODO List and Stuck Projects, then the views in =views.toml=, then Match…, TODO Match…, Search… and TODO Search…, then Save as View…. Saved views mirror =org-agenda-custom-commands=, with fewer options.
469
470Save as View… is there while a match or search you typed is showing. It asks for a name and adds the view to the end of =views.toml=, as a =match=, =todo-match=, =search= or =todo-search= view, making the file if it isn't there; the rest of the file is left as it is. A name another view has, the built-in ones included, is refused. To change or remove a view, edit =views.toml=.
469471
470472*In the sidebar.* On the Mac, the calendar button at the right of the bar under the main window's sidebar shows an agenda in place of the files, beside the editor; the same button, then a folder, brings the files back, and Show Agenda or Files in Sidebar (=app.sidebar-agenda=) switches between them.
471473
@@ -473,7 +475,7 @@ The view menu lists the built-in views, Agenda, TODO List and Stuck Projects, th
473475- Days with entries are listed with their entries, today's overdue ones first; a list view or a block shows under its title. Each line has the time or status, the TODO keyword, the title and the category.
474476- A click on an entry shows it in the editor.
475477- It uses the Agenda window's span, start, folders and agenda files. Calendar events and Reminders show once the Agenda window has been given access to them.
476- It has no filters, bulk actions or keys of its own; those are in the Agenda window.
478- It has no filters, bulk actions or keys of its own, and no Save as View…; those are in the Agenda window.
477479
478480=views.toml= lives in the configuration folder beside =config.toml= (=~/.config/orgstar/= by default; see [[file:13-configuration.org][Configuration]]). Each view is a =[[view]]= table:
479481
@@ -724,7 +726,7 @@ Click a row in Today, the Up Next widget or a habit to show that entry in orgsta
724726
725727The Agenda tab shows the same views from the same files, using the settings in the synced =config.toml= and the views in =views.toml= (see [[file:14-ios.org][iOS]]). Differences from the Mac:
726728
727- The Views menu, at the top left, lists the built-in views, your saved views, Tags and Properties…, which asks for a match string, and Search…, which asks for a search string. There is no TODO-only match or search from the menu; use =/!= in the match, =!= at the start of the search string, or a =todo-match= or =todo-search= view.
729- The Views menu, at the top left, lists the built-in views, your saved views, Tags and Properties…, which asks for a match string, and Search…, which asks for a search string; while one of those is showing, Save as View… keeps it as a view in =views.toml=, as on the Mac. There is no TODO-only match or search from the menu; use =/!= in the match, =!= at the start of the search string, or a =todo-match= or =todo-search= view.
728730- A bar floats at the bottom of the screen with Earlier, Filter, Today, Go to Date, the span and Later. The span menu switches between Day, Week, Month and the configured span (=10 Days= by default); Day, Week and Month are around today when today is shown, else around the first day shown. Weeks start on =org-agenda-start-on-weekday=, as on the Mac. In the TODO list and match views the bar has Filter and Agenda, which returns to the day view. Messages show above the bar; tap one to dismiss it.
729731- The title names the days shown: =Thu 8 Oct=, =Week of 5 Oct=, =October 2026=, or =5 Oct – 14 Oct=.
730732- A block view lists its blocks one after the other, each under its title; an agenda block's days follow its title.
docs/manual/guide/14-ios.org +1 −1
@@ -190,7 +190,7 @@ Tap a result to open it in the reader. A heading result opens at that heading.
190190
191191The Agenda tab shows the agenda over all your folders, with the span and start day from the settings (10 days from 3 days ago by default). Each day lists its entries in rows: the time or a status such as =Due today= or =3d late= on the left, the category and the keyword above the title. Days without entries are hidden, except today. Today's overdue entries are in a section of their own above today, and a line marks the current time between timed entries. Pull down to refresh.
192192
193- Views (top left) :: the built-in views and those in =views.toml=, Tags and Properties… (a match such as =+work-home= or ~TODO="WAIT"~), and Search… (a phrase, or snippets such as =+invoice -paid=).
193- Views (top left) :: the built-in views and those in =views.toml=, Tags and Properties… (a match such as =+work-home= or ~TODO="WAIT"~), and Search… (a phrase, or snippets such as =+invoice -paid=); Save as View… keeps the match or search showing as a view in =views.toml=.
194194- Folders (top right, with more than one folder) :: shows or hides each folder's files in the agenda, the widgets, the watch and the badge; the shown folders' names appear under the title. A Focus can hide folders too, and mute their reminders (see Hiding folders in [[file:07-agenda.org][The agenda]]).
195195- The bar at the bottom :: Earlier, Filter, Today, Go to Date, the span (Day, Week, Month or the configured span, and Log Mode, which adds closed entries and clocked time) and Later.
196196- Filter :: keep or leave out tags and categories of the entries shown, or type a filter as Org's =/= takes it: =+keep= and =-drop= tags or categories, =<0:30= for effort, =/regexp/=. The filter in use shows above the bar.
l10n/Localizable.xcstrings +170
@@ -2120,6 +2120,40 @@
21202120 }
21212121 }
21222122 },
2123 "A view needs a name.": {
2124 "localizations": {
2125 "de": {
2126 "stringUnit": {
2127 "state": "translated",
2128 "value": "Eine Ansicht braucht einen Namen."
2129 }
2130 },
2131 "es": {
2132 "stringUnit": {
2133 "state": "translated",
2134 "value": "Una vista necesita un nombre."
2135 }
2136 },
2137 "fr": {
2138 "stringUnit": {
2139 "state": "translated",
2140 "value": "Une vue doit avoir un nom."
2141 }
2142 },
2143 "ja": {
2144 "stringUnit": {
2145 "state": "translated",
2146 "value": "ビューには名前が必要です。"
2147 }
2148 },
2149 "pt-BR": {
2150 "stringUnit": {
2151 "state": "translated",
2152 "value": "Uma visualização precisa de um nome."
2153 }
2154 }
2155 }
2156 },
21232157 "About": {
21242158 "localizations": {
21252159 "de": {
@@ -19202,6 +19236,40 @@
1920219236 }
1920319237 }
1920419238 },
19239 "Name for this view:": {
19240 "localizations": {
19241 "de": {
19242 "stringUnit": {
19243 "state": "translated",
19244 "value": "Name für diese Ansicht:"
19245 }
19246 },
19247 "es": {
19248 "stringUnit": {
19249 "state": "translated",
19250 "value": "Nombre para esta vista:"
19251 }
19252 },
19253 "fr": {
19254 "stringUnit": {
19255 "state": "translated",
19256 "value": "Nom de cette vue :"
19257 }
19258 },
19259 "ja": {
19260 "stringUnit": {
19261 "state": "translated",
19262 "value": "このビューの名前:"
19263 }
19264 },
19265 "pt-BR": {
19266 "stringUnit": {
19267 "state": "translated",
19268 "value": "Nome para esta visualização:"
19269 }
19270 }
19271 }
19272 },
1920519273 "Narrow to Block": {
1920619274 "comment": "Command title",
1920719275 "localizations": {
@@ -26944,6 +27012,40 @@
2694427012 }
2694527013 }
2694627014 },
27015 "Save as View…": {
27016 "localizations": {
27017 "de": {
27018 "stringUnit": {
27019 "state": "translated",
27020 "value": "Als Ansicht sichern …"
27021 }
27022 },
27023 "es": {
27024 "stringUnit": {
27025 "state": "translated",
27026 "value": "Guardar como vista…"
27027 }
27028 },
27029 "fr": {
27030 "stringUnit": {
27031 "state": "translated",
27032 "value": "Enregistrer comme vue…"
27033 }
27034 },
27035 "ja": {
27036 "stringUnit": {
27037 "state": "translated",
27038 "value": "ビューとして保存…"
27039 }
27040 },
27041 "pt-BR": {
27042 "stringUnit": {
27043 "state": "translated",
27044 "value": "Salvar como Visualização…"
27045 }
27046 }
27047 }
27048 },
2694727049 "Save files": {
2694827050 "localizations": {
2694927051 "de": {
@@ -32854,6 +32956,40 @@
3285432956 }
3285532957 }
3285632958 },
32959 "There is already a view named %@.": {
32960 "localizations": {
32961 "de": {
32962 "stringUnit": {
32963 "state": "translated",
32964 "value": "Es gibt bereits eine Ansicht namens %@."
32965 }
32966 },
32967 "es": {
32968 "stringUnit": {
32969 "state": "translated",
32970 "value": "Ya existe una vista llamada %@."
32971 }
32972 },
32973 "fr": {
32974 "stringUnit": {
32975 "state": "translated",
32976 "value": "Il existe déjà une vue nommée %@."
32977 }
32978 },
32979 "ja": {
32980 "stringUnit": {
32981 "state": "translated",
32982 "value": "%@という名前のビューはすでにあります。"
32983 }
32984 },
32985 "pt-BR": {
32986 "stringUnit": {
32987 "state": "translated",
32988 "value": "Já existe uma visualização chamada %@."
32989 }
32990 }
32991 }
32992 },
3285732993 "Third-Party Notices": {
3285832994 "localizations": {
3285932995 "de": {
@@ -33098,6 +33234,40 @@
3309833234 }
3309933235 }
3310033236 },
33237 "This view can't be saved from here; write it in views.toml.": {
33238 "localizations": {
33239 "de": {
33240 "stringUnit": {
33241 "state": "translated",
33242 "value": "Diese Ansicht kann von hier aus nicht gesichert werden; trage sie in views.toml ein."
33243 }
33244 },
33245 "es": {
33246 "stringUnit": {
33247 "state": "translated",
33248 "value": "Esta vista no se puede guardar desde aquí; escríbela en views.toml."
33249 }
33250 },
33251 "fr": {
33252 "stringUnit": {
33253 "state": "translated",
33254 "value": "Cette vue ne peut pas être enregistrée d'ici ; écrivez-la dans views.toml."
33255 }
33256 },
33257 "ja": {
33258 "stringUnit": {
33259 "state": "translated",
33260 "value": "このビューはここから保存できません。views.tomlに記述してください。"
33261 }
33262 },
33263 "pt-BR": {
33264 "stringUnit": {
33265 "state": "translated",
33266 "value": "Esta visualização não pode ser salva daqui; escreva-a em views.toml."
33267 }
33268 }
33269 }
33270 },
3310133271 "Time": {
3310233272 "localizations": {
3310333273 "de": {