about summary refs log tree commit diff
path: root/tools/emacs-pkgs/notable
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2020-09-17T11·52+0100
committertazjin <mail@tazj.in>2020-09-18T23·32+0000
commitf94dd5e93243d6894a4cfb2869160bb6f24d173c (patch)
tree5843ba505d8038a973d375344d0834c37df0903c /tools/emacs-pkgs/notable
parent781870f2881cc5c2d473d0e5f87767179bec773b (diff)
feat(notable): Add a mode for displaying single notes r/1801
This mode displays the note's timestamp in its header line, and the
content in its body.

Some rudimentary key bindings are set up in the list view to view
notes in full (RET), and in both modes to quit the current buffer (q).

Change-Id: Icb35baec220a9efcfa2b86617b16178669df996f
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1985
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
Diffstat (limited to 'tools/emacs-pkgs/notable')
-rw-r--r--tools/emacs-pkgs/notable/notable.el49
1 files changed, 48 insertions, 1 deletions
diff --git a/tools/emacs-pkgs/notable/notable.el b/tools/emacs-pkgs/notable/notable.el
index 4669579ad73c..b0cedb3de23d 100644
--- a/tools/emacs-pkgs/notable/notable.el
+++ b/tools/emacs-pkgs/notable/notable.el
@@ -122,6 +122,45 @@
       (error "No note with ID %s in note storage!" id))
     (notable--deserialize-note (f-read-text path 'utf-8))))
 
+;; Note view buffer implementation
+
+(defvar-local notable--buffer-note nil "The note ID displayed by this buffer.")
+
+(define-derived-mode notable-note-mode fundamental-mode "notable-note"
+  "Major mode displaying a single Notable note."
+  (set (make-local-variable 'scroll-preserve-screen-position) t)
+  (setq truncate-lines t)
+  (setq buffer-read-only t)
+  (setq buffer-undo-list t))
+
+(setq notable-note-mode-map
+      (let ((map (make-sparse-keymap)))
+        (define-key map "q" 'kill-current-buffer)
+        map))
+
+(defun notable--show-note (id)
+  "Display a single note in a separate buffer."
+  (check-type id integer)
+
+  (let ((note (notable--get-note id))
+        (buffer (get-buffer-create (format "*notable: %d*" id)))
+        (inhibit-read-only t))
+    (with-current-buffer buffer
+      (notable-note-mode)
+      (erase-buffer)
+      (setq notable--buffer-note id)
+      (setq header-line-format
+            (format "Note from %s"
+                    (dottime-format
+                     (seconds-to-time (notable--note-time note))))))
+    (switch-to-buffer buffer)
+    (goto-char (point-min))
+    (insert (notable--note-content note))))
+
+(defun notable--show-note-at-point ()
+  (interactive)
+  (notable--show-note (get-text-property (point) 'notable-note-id)))
+
 ;; Note list buffer implementation
 
 (define-derived-mode notable-list-mode fundamental-mode "notable"
@@ -134,11 +173,19 @@
   (setq buffer-undo-list t)
   (hl-line-mode t))
 
+(setq notable-list-mode-map
+      (let ((map (make-sparse-keymap)))
+        (define-key map "q" 'kill-current-buffer)
+        (define-key map "g" 'notable-list-notes)
+        (define-key map (kbd "RET") 'notable--show-note-at-point)
+        map))
+
 (defun notable--render-note (id note)
   (check-type id integer)
   (check-type note notable--note)
 
-  (let ((first-line (car (s-lines "foo")))
+  (let ((start (point))
+        (first-line (car (s-lines (notable--note-content note))))
         (date (dottime-format (seconds-to-time
                                (notable--note-time note)))))
     (insert (propertize (s-concat date "  " first-line)