flake-update-20260505
1;;; daily-plan.el --- Emacs interface to daily-plan CLI -*- lexical-binding: t; -*-
2
3;;; Commentary:
4;; Integrates the daily-plan Go CLI with Emacs.
5;; Uses --json output for structured data, renders in org-mode buffers.
6;;
7;; Keybindings in daily-plan buffers:
8;; s - schedule item at point
9;; RET - open item URL in browser
10;; g - refresh buffer
11;; q - quit
12;; y - yank buffer as markdown (for reports)
13;; p - switch period (weekly/review buffers)
14;; TAB - cycle section visibility
15;;
16;; Interactive commands:
17;; M-x daily-plan-show - today's plan
18;; M-x daily-plan-inbox - new items since last check
19;; M-x daily-plan-weekly - weekly review
20;; M-x daily-plan-review - full activity review (markdown output)
21;; M-x daily-plan-schedule - schedule by key
22
23;;; Code:
24
25(require 'json)
26(require 'org)
27
28(defvar daily-plan-command "daily-plan"
29 "Path or name of the daily-plan binary.")
30
31(defvar daily-plan-org-file (expand-file-name "~/desktop/org/todos.org")
32 "Path to the org file for scheduling.")
33
34(defvar-local daily-plan--current-period nil
35 "Current period for review/weekly buffers.")
36
37(defvar-local daily-plan--current-command nil
38 "Current command for refresh.")
39
40(defvar-local daily-plan--current-args nil
41 "Current args for refresh.")
42
43(defvar-local daily-plan--current-render-fn nil
44 "Current render function for refresh.")
45
46;; ── JSON helpers ──
47
48(defun daily-plan--run-json (&rest args)
49 "Run daily-plan with ARGS and --json, return parsed JSON."
50 (with-temp-buffer
51 (let ((exit-code (apply #'call-process daily-plan-command nil t nil
52 (append args '("--json")))))
53 (when (zerop exit-code)
54 (goto-char (point-min))
55 (condition-case nil
56 (json-parse-buffer :object-type 'alist :array-type 'list)
57 (error nil))))))
58
59(defun daily-plan--run-plain (&rest args)
60 "Run daily-plan with ARGS, return output string."
61 (with-temp-buffer
62 (apply #'call-process daily-plan-command nil t nil args)
63 (string-trim (buffer-string))))
64
65;; ── Major mode ──
66
67(defvar daily-plan-mode-map
68 (let ((map (make-sparse-keymap)))
69 (define-key map (kbd "s") #'daily-plan-schedule-at-point)
70 (define-key map (kbd "RET") #'daily-plan-open-at-point)
71 (define-key map (kbd "g") #'daily-plan-refresh)
72 (define-key map (kbd "q") #'quit-window)
73 (define-key map (kbd "y") #'daily-plan-yank-markdown)
74 (define-key map (kbd "p") #'daily-plan-switch-period)
75 (define-key map (kbd "TAB") #'org-cycle)
76 (define-key map (kbd "S-TAB") #'org-shifttab)
77 map)
78 "Keymap for `daily-plan-mode'.")
79
80(define-derived-mode daily-plan-mode org-mode "DailyPlan"
81 "Major mode for daily-plan buffers.
82\\{daily-plan-mode-map}"
83 (read-only-mode 1)
84 (setq-local buffer-read-only t))
85
86;; ── Rendering helpers ──
87
88(defun daily-plan--insert-jira-item (item)
89 "Insert a single Jira ITEM as an org list entry with text properties."
90 (let-alist item
91 (let ((start (point)))
92 (insert (format "- [[%s][%s]] %s =%s=" .url .key .summary .status))
93 (when (and .priority (not (string-empty-p .priority)))
94 (insert (format " /%s/" .priority)))
95 (insert "\n")
96 (put-text-property start (point) 'daily-plan-key .key)
97 (put-text-property start (point) 'daily-plan-url .url)
98 (put-text-property start (point) 'daily-plan-type "jira"))))
99
100(defun daily-plan--insert-gh-item (item)
101 "Insert a single GitHub ITEM as an org list entry with text properties."
102 (let-alist item
103 (let ((start (point))
104 (ref (format "%s#%d" .repo .number)))
105 (insert (format "- [[%s][%s]] %s" .url ref .title))
106 (when (and .author (not (string-empty-p .author)))
107 (insert (format " (@%s)" .author)))
108 (insert "\n")
109 (put-text-property start (point) 'daily-plan-key ref)
110 (put-text-property start (point) 'daily-plan-url .url)
111 (put-text-property start (point) 'daily-plan-type "github"))))
112
113(defun daily-plan--insert-items (items insert-fn empty-msg)
114 "Insert ITEMS using INSERT-FN, or EMPTY-MSG if empty."
115 (if items
116 (dolist (item items)
117 (funcall insert-fn item))
118 (insert (format "- %s\n" (or empty-msg "(none)")))))
119
120(defun daily-plan--insert-org-done-item (item)
121 "Insert a completed org ITEM."
122 (let-alist item
123 (insert (format "- ✓ %s /(%s)/\n" .title .completed_at))))
124
125(defun daily-plan--insert-discussion-item (item)
126 "Insert a GitHub discussion ITEM."
127 (let-alist item
128 (let ((start (point)))
129 (insert (format "- [[%s][%s]] %s" .url .repo .title))
130 (when (and .category (not (string-empty-p .category)))
131 (insert (format " =[%s]=" .category)))
132 (insert "\n")
133 (put-text-property start (point) 'daily-plan-url .url)
134 (put-text-property start (point) 'daily-plan-type "discussion"))))
135
136(defun daily-plan--insert-comment-item (item)
137 "Insert a GitHub comment ITEM."
138 (let-alist item
139 (let ((start (point))
140 (ref (format "%s#%d" .repo .issue_number)))
141 (insert (format "- [[%s][%s]] %s\n" .url ref .issue_title))
142 (put-text-property start (point) 'daily-plan-url .url)
143 (put-text-property start (point) 'daily-plan-type "comment"))))
144
145(defun daily-plan--insert-ai-item (item)
146 "Insert an AI session ITEM."
147 (let-alist item
148 (insert (format "- *%s* %s" .type .title))
149 (when (and .project (not (string-empty-p .project)))
150 (insert (format " =[%s]=" .project)))
151 (insert (format " /(%s)/\n" .date))))
152
153;; ── Render: show ──
154
155(defun daily-plan--render-show (data)
156 "Render show DATA into current buffer."
157 (let-alist data
158 (insert (format "* Daily Plan — %s\n\n" .date))
159
160 (insert "** Org Agenda\n")
161 (if .agenda
162 (dolist (item .agenda)
163 (let-alist item
164 (insert (format "- %s %s\n" (or .state "") .heading))))
165 (insert "- (nothing scheduled)\n"))
166
167 (insert "\n** Jira — In Progress / Code Review\n")
168 (daily-plan--insert-items .jira_in_progress #'daily-plan--insert-jira-item nil)
169
170 (insert "\n** Jira — Backlog\n")
171 (daily-plan--insert-items .jira_backlog #'daily-plan--insert-jira-item nil)
172
173 (insert "\n** GitHub — Assigned Issues\n")
174 (daily-plan--insert-items .github_issues #'daily-plan--insert-gh-item nil)
175
176 (insert "\n** GitHub — PRs Awaiting Review :review:\n")
177 (daily-plan--insert-items .github_reviews #'daily-plan--insert-gh-item nil)
178
179 (insert "\n** GitHub — Your Open PRs\n")
180 (daily-plan--insert-items .github_prs #'daily-plan--insert-gh-item nil)))
181
182;; ── Render: inbox ──
183
184(defun daily-plan--insert-advisory (item)
185 "Insert a GitHub security advisory ITEM."
186 (let-alist item
187 (let ((start (point))
188 (id (or .cve_id .ghsa_id)))
189 (insert (format "- [[%s][%s]] %s =%s= /%s/\n"
190 .url id .summary .severity .repo))
191 (put-text-property start (point) 'daily-plan-key id)
192 (put-text-property start (point) 'daily-plan-url .url)
193 (put-text-property start (point) 'daily-plan-type "advisory"))))
194
195(defun daily-plan--insert-dependabot (item)
196 "Insert a Dependabot alert ITEM."
197 (let-alist item
198 (let ((start (point))
199 (id (or .cve "dependabot")))
200 (insert (format "- [[%s][%s]] %s =%s= ~%s~ /%s/\n"
201 .url id .summary .severity .package .repo))
202 (put-text-property start (point) 'daily-plan-key id)
203 (put-text-property start (point) 'daily-plan-url .url)
204 (put-text-property start (point) 'daily-plan-type "dependabot"))))
205
206(defun daily-plan--render-inbox (data)
207 "Render inbox DATA into current buffer."
208 (let-alist data
209 (insert (format "* Inbox — Since %s\n\n" .since))
210
211 (insert "** Security Advisories (triage/draft) :security:\n")
212 (daily-plan--insert-items .github_security_advisories #'daily-plan--insert-advisory nil)
213
214 (insert "\n** Dependabot Alerts (critical/high) :security:\n")
215 (daily-plan--insert-items .github_dependabot_alerts #'daily-plan--insert-dependabot nil)
216
217 (insert "\n** Jira — CVEs / Security Issues :security:\n")
218 (daily-plan--insert-items .cves #'daily-plan--insert-jira-item nil)
219 (when (and .cve_total (> .cve_total (length .cves)))
220 (insert (format " /(%d total across images)/\n" .cve_total)))
221
222 (insert "\n** Jira — Updated\n")
223 (daily-plan--insert-items .jira_updated #'daily-plan--insert-jira-item nil)
224
225 (insert "\n** GitHub — New Issues\n")
226 (daily-plan--insert-items .github_new_issues #'daily-plan--insert-gh-item nil)
227
228 (insert "\n** GitHub — New PRs\n")
229 (daily-plan--insert-items .github_new_prs #'daily-plan--insert-gh-item nil)
230
231 (insert "\n** GitHub — Review Requests :review:\n")
232 (daily-plan--insert-items .github_reviews #'daily-plan--insert-gh-item nil)))
233
234;; ── Render: weekly ──
235
236(defun daily-plan--render-weekly (data)
237 "Render weekly DATA into current buffer."
238 (let-alist data
239 (insert (format "* Weekly Review — %s\n\n" .week))
240
241 ;; Org completed tasks
242 (when .org_done
243 (insert "** Completed Tasks (Org) :done:\n")
244 ;; Group by section
245 (let ((by-section (make-hash-table :test 'equal))
246 (order '()))
247 (dolist (item .org_done)
248 (let ((section (or (alist-get 'section item) "(uncategorized)")))
249 (unless (gethash section by-section)
250 (push section order))
251 (push item (gethash section by-section))))
252 (dolist (section (nreverse order))
253 (insert (format "*** %s\n" section))
254 (dolist (item (nreverse (gethash section by-section)))
255 (daily-plan--insert-org-done-item item))))
256 (insert "\n"))
257
258 ;; AI sessions
259 (when .ai_sessions
260 (let ((filtered (seq-remove
261 (lambda (item)
262 (string-match-p "auto-recovered"
263 (downcase (or (alist-get 'title item) ""))))
264 .ai_sessions)))
265 (when filtered
266 (insert (format "** AI Sessions (%d) :ai:\n" (length filtered)))
267 (dolist (item filtered)
268 (daily-plan--insert-ai-item item))
269 (insert "\n"))))
270
271 ;; Jira completed
272 (insert "** Completed (Jira) :done:\n")
273 (daily-plan--insert-items .jira_completed #'daily-plan--insert-jira-item "(nothing completed)")
274
275 ;; GitHub merged
276 (insert "\n** Merged PRs :done:\n")
277 (daily-plan--insert-items .github_merged #'daily-plan--insert-gh-item "(no merged PRs)")
278
279 ;; Reviews given
280 (when .github_reviewed
281 (insert (format "\n** Reviews Given (%d) :review:\n" (length .github_reviewed)))
282 (daily-plan--insert-items .github_reviewed #'daily-plan--insert-gh-item nil))
283
284 ;; Issues filed
285 (when .github_issues_created
286 (insert (format "\n** Issues Filed (%d)\n" (length .github_issues_created)))
287 (daily-plan--insert-items .github_issues_created #'daily-plan--insert-gh-item nil))
288
289 ;; Discussions
290 (when .github_discussions
291 (insert (format "\n** Discussions (%d) :community:\n" (length .github_discussions)))
292 (daily-plan--insert-items .github_discussions #'daily-plan--insert-discussion-item nil))
293
294 ;; Comments
295 (when .github_comments
296 (insert (format "\n** Comments (%d)\n" (length .github_comments)))
297 (daily-plan--insert-items .github_comments #'daily-plan--insert-comment-item nil))
298
299 ;; Still in progress
300 (insert "\n** Still In Progress\n")
301 (daily-plan--insert-items .jira_in_progress #'daily-plan--insert-jira-item nil)
302
303 ;; Backlog
304 (insert "\n** Backlog — Candidates for Next Week\n")
305 (daily-plan--insert-items .jira_backlog #'daily-plan--insert-jira-item nil)
306
307 ;; Assigned issues
308 (insert "\n** GitHub Issues (assigned)\n")
309 (daily-plan--insert-items .github_issues #'daily-plan--insert-gh-item nil)))
310
311;; ── Buffer management ──
312
313(defun daily-plan--create-buffer (name render-fn data)
314 "Create or reuse buffer NAME, render DATA with RENDER-FN."
315 (let ((buf (get-buffer-create name)))
316 (with-current-buffer buf
317 (let ((inhibit-read-only t)
318 (pos (point)))
319 (erase-buffer)
320 (funcall render-fn data)
321 (daily-plan-mode)
322 ;; Fold all sections to level 2 for overview
323 (goto-char (point-min))
324 (org-content 2)
325 (goto-char (min pos (point-max)))))
326 (pop-to-buffer buf)))
327
328(defun daily-plan--run-async (buf-name args render-fn)
329 "Run daily-plan with ARGS asynchronously, render into BUF-NAME with RENDER-FN."
330 (message "Fetching %s..." buf-name)
331 (let ((buf (get-buffer-create buf-name))
332 (proc-buf (generate-new-buffer " *daily-plan-proc*")))
333 ;; Show buffer immediately with loading message
334 (with-current-buffer buf
335 (let ((inhibit-read-only t))
336 (erase-buffer)
337 (insert "Loading...")
338 (daily-plan-mode)
339 (setq-local daily-plan--current-args args)
340 (setq-local daily-plan--current-render-fn render-fn)))
341 (pop-to-buffer buf)
342 ;; Run async
343 (make-process
344 :name "daily-plan"
345 :buffer proc-buf
346 :command (append (list daily-plan-command) args (list "--json"))
347 :sentinel
348 (lambda (proc _event)
349 (when (eq (process-status proc) 'exit)
350 (if (zerop (process-exit-status proc))
351 (let ((data (with-current-buffer proc-buf
352 (goto-char (point-min))
353 (condition-case nil
354 (json-parse-buffer :object-type 'alist :array-type 'list)
355 (error nil)))))
356 (if data
357 (progn
358 (daily-plan--create-buffer buf-name render-fn data)
359 ;; Preserve local vars after re-render
360 (with-current-buffer buf-name
361 (setq-local daily-plan--current-args args)
362 (setq-local daily-plan--current-render-fn render-fn))
363 (message "%s ready." buf-name))
364 (message "Failed to parse %s output" buf-name)))
365 (message "daily-plan failed with exit code %d" (process-exit-status proc)))
366 (kill-buffer proc-buf))))))
367
368;; ── Interactive commands ──
369
370;;;###autoload
371(defun daily-plan-show ()
372 "Show today's daily plan."
373 (interactive)
374 (daily-plan--run-async "*daily-plan*" '("show") #'daily-plan--render-show))
375
376;;;###autoload
377(defun daily-plan-inbox (&optional since)
378 "Show new/updated items since last check or SINCE date."
379 (interactive "sInbox since (empty=last check, or YYYY-MM-DD/\"last monday\"): ")
380 (let ((args (if (and since (not (string-empty-p since)))
381 (list "inbox" since)
382 (list "inbox"))))
383 (daily-plan--run-async "*daily-plan-inbox*" args #'daily-plan--render-inbox)))
384
385;;;###autoload
386(defun daily-plan-weekly ()
387 "Show weekly review."
388 (interactive)
389 (daily-plan--run-async "*daily-plan-weekly*" '("weekly") #'daily-plan--render-weekly))
390
391;;;###autoload
392(defun daily-plan-review (&optional period)
393 "Show full activity review for PERIOD.
394PERIOD can be \"this week\", \"last week\", \"this month\", etc."
395 (interactive
396 (list (completing-read "Period: "
397 '("this week" "last week" "this month" "last month")
398 nil nil nil nil "this week")))
399 (let* ((buf-name (format "*daily-plan-review [%s]*" period))
400 (args (list "review" period)))
401 (setq daily-plan--current-period period)
402 (daily-plan--run-async buf-name args #'daily-plan--render-weekly)))
403
404;;;###autoload
405(defun daily-plan-schedule (key &optional date)
406 "Schedule a Jira or GitHub issue KEY for DATE.
407KEY should be PROJ-123 for Jira or owner/repo#123 for GitHub."
408 (interactive
409 (list (read-string "Issue key (PROJ-123 or org/repo#123): ")
410 (org-read-date nil nil nil "Schedule for: ")))
411 (let ((output (daily-plan--run-plain
412 "schedule" key (or date (format-time-string "%Y-%m-%d")))))
413 (message "%s" output)))
414
415(defun daily-plan-schedule-at-point ()
416 "Schedule the item at point for a chosen date."
417 (interactive)
418 (let ((key (get-text-property (point) 'daily-plan-key)))
419 (if key
420 (let* ((date (org-read-date nil nil nil (format "Schedule %s for: " key)))
421 (output (daily-plan--run-plain "schedule" key date)))
422 (message "%s" output))
423 ;; Fallback: try to extract from org link
424 (save-excursion
425 (beginning-of-line)
426 (if (re-search-forward "\\[\\[\\([^]]+\\)\\]\\[\\([^]]+\\)\\]\\]" (line-end-position) t)
427 (let* ((ref (match-string 2))
428 (date (org-read-date nil nil nil (format "Schedule %s for: " ref)))
429 (output (daily-plan--run-plain "schedule" ref date)))
430 (message "%s" output))
431 (message "No schedulable item at point"))))))
432
433(defun daily-plan-open-at-point ()
434 "Open the URL of the item at point in the browser."
435 (interactive)
436 (let ((url (get-text-property (point) 'daily-plan-url)))
437 (if url
438 (browse-url url)
439 ;; Fallback to org link
440 (org-open-at-point))))
441
442(defun daily-plan-refresh ()
443 "Refresh the current daily-plan buffer."
444 (interactive)
445 (let ((args daily-plan--current-args)
446 (render-fn daily-plan--current-render-fn)
447 (buf-name (buffer-name)))
448 (cond
449 ;; Use stored args/render if available
450 ((and args render-fn)
451 (daily-plan--run-async buf-name args render-fn))
452 ;; Fallback by buffer name
453 ((string= buf-name "*daily-plan*") (daily-plan-show))
454 ((string= buf-name "*daily-plan-inbox*") (daily-plan-inbox))
455 ((string= buf-name "*daily-plan-weekly*") (daily-plan-weekly))
456 (t (message "Not a daily-plan buffer")))))
457
458(defun daily-plan-switch-period ()
459 "Switch the time period for review/weekly buffers."
460 (interactive)
461 (let ((period (completing-read "Period: "
462 '("this week" "last week" "this month" "last month")
463 nil nil nil nil (or daily-plan--current-period "this week"))))
464 (cond
465 ((string-match-p "review" (buffer-name))
466 (daily-plan-review period))
467 ((string-match-p "weekly" (buffer-name))
468 ;; Weekly always uses "this week", suggest review instead
469 (daily-plan-review period))
470 (t (daily-plan-review period)))))
471
472(defun daily-plan-yank-markdown ()
473 "Copy the current buffer content as markdown to the kill ring.
474Runs `daily-plan review` with the current period to get markdown."
475 (interactive)
476 (let* ((period (or daily-plan--current-period "this week"))
477 (output (daily-plan--run-plain "review" period)))
478 (kill-new output)
479 (message "Markdown copied to kill ring (%d chars)" (length output))))
480
481(provide 'daily-plan)
482;;; daily-plan.el ends here