From 9b147ad6af201fa4279f04a778728f48d3d08b18 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Mon, 11 May 2020 00:14:57 -0700 Subject: Add functions to create notes while reading ebooks --- oni-org/oni-org.el | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) (limited to 'oni-org/oni-org.el') diff --git a/oni-org/oni-org.el b/oni-org/oni-org.el index f502d5a..ced5a6a 100644 --- a/oni-org/oni-org.el +++ b/oni-org/oni-org.el @@ -4,7 +4,7 @@ ;; Author: Tom Willemse ;; Keywords: local -;; Version: 2020.0420.220918 +;; Version: 2020.0511.001009 ;; Package-Requires: (oni-yasnippet oni-alert oni-hydra org-plus-contrib org-bullets org-edna diminish all-the-icons) ;; This program is free software; you can redistribute it and/or modify @@ -231,8 +231,63 @@ _l_: Store link ^^ _j_: Journal entry ("documents/gtd/books.org" :maxlevel . 1) ("documents/gtd/bookmarks.org" :maxlevel . 2)))) +(defun oni-org-find-heading-in-file (heading file &optional move) + "Try to find HEADING somewhere in FILE. +This function returns nil if HEADING couldn’t be found, and the +position where the chapter starts otherwise. If MOVE is non-nil +also move point to the start of the heading." + (let ((buffer (find-file-noselect file))) + (with-current-buffer buffer + (save-excursion + (goto-char (point-min)) + (org-find-exact-headline-in-buffer heading buffer (not move)))))) + +(defun oni-org-create-book-heading (book-title file) + "Create a new heading for BOOK-TITLE in FILE." + (let ((buffer (find-file file))) + (with-current-buffer buffer + (goto-char (point-max)) + (insert "\n* " book-title "\n\n ")))) + +(defun oni-org-create-chapter-heading (book-title chapter-title file) + "Create a new heading under BOOK-TITLE for CHAPTER-TITLE in FILE." + (let ((buffer (find-file file))) + (with-current-buffer buffer + (goto-char (point-min)) + (goto-char (oni-org-find-heading-in-file book-title file t)) + (goto-char (org-element-property :end (org-element-at-point))) + (unless (= (point) (point-max)) + (open-line 2)) + (insert "\n** " chapter-title "\n\n ")))) + +(defun oni-org-create-chapter-section (chapter-title file) + "Create a new notes section under CHAPTER-TITLE in FILE." + (let ((buffer (find-file file))) + (with-current-buffer buffer + (goto-char (point-min)) + (goto-char (oni-org-find-heading-in-file chapter-title file t)) + (goto-char (org-element-property :contents-end (org-element-at-point))) + (insert "\n")))) + +(defun oni-org-reading-note () + "Find the org-header associated with the current chapter or page." + (let* ((headers (split-string (format-mode-line header-line-format) ":" t " ")) + (book-name (car headers)) + (chapter-name (cadr headers)) + (notes-file (oni-org-expand-to-home "documents/gtd/reading-notes.org")) + (chapter-start (oni-org-find-heading-in-file chapter-name notes-file))) + (if (not chapter-start) + (let ((book-start (oni-org-find-heading-in-file book-name notes-file))) + (unless book-start + (oni-org-create-book-heading book-name notes-file)) + (oni-org-create-chapter-heading book-name chapter-name notes-file)) + (oni-org-create-chapter-section chapter-name notes-file)))) + +(setq org-capture-templates-contexts + '(("n" ((in-mode . "nov-mode"))))) + (setq org-capture-templates - `(("t" "Note" entry + `(("t" "Task" entry (file ,(oni-org-expand-to-home "documents/gtd/inbox.org")) "* TODO %i%?\n :PROPERTIES:\n :CREATED: %U\n :END:") ("a" "Appointment" entry @@ -243,6 +298,9 @@ _l_: Store link ^^ _j_: Journal entry (file+olp+datetree ,(oni-org-expand-to-home "documents/gtd/journal.org")) "* %<%H:%M:%S>\n %?") + ("n" "Reading note" item (function oni-org-reading-note) + "%?\n\n#+begin_quote\n%i\n#+end_quote" + :empty-lines 1) ("c" "Add to currently clocked item") ("ca" "Note" plain (clock) -- cgit v1.2.3-54-g00ecf