summaryrefslogtreecommitdiff
path: root/gnosis.el
diff options
context:
space:
mode:
Diffstat (limited to 'gnosis.el')
-rw-r--r--gnosis.el165
1 files changed, 75 insertions, 90 deletions
diff --git a/gnosis.el b/gnosis.el
index 86b07ef..0e1f9a1 100644
--- a/gnosis.el
+++ b/gnosis.el
@@ -179,12 +179,6 @@ Example:
"From TABLE use where to delete VALUE."
(emacsql gnosis-db `[:delete :from ,table :where ,value]))
-(defmacro with-gnosis-buffer (&rest body)
- "Execute BODY in gnosis buffer."
- `(with-current-buffer (switch-to-buffer (get-buffer-create "*gnosis*"))
- (gnosis-mode)
- ,@body))
-
(cl-defun gnosis-completing-read (prompt options info &optional (face-for-info 'font-lock-doc-face))
"A version of `completing-read' with text properties, padding & choosable face.
Returns selected option from OPTIONS.
@@ -214,28 +208,25 @@ FACE-FOR-INFO is the face used to display info for option."
if (= i index) collect new-item
else collect item))
-(defun gnosis-display--question (id)
+(defun gnosis-display-question (id)
"Display main row for note ID."
(let ((question (gnosis-get 'main 'notes `(= id ,id))))
- (with-gnosis-buffer
(erase-buffer)
(fill-paragraph (insert (concat "\n"
- (propertize question 'face 'gnosis-face-main)))))))
+ (propertize question 'face 'gnosis-face-main))))))
-(defun gnosis-display--cloze-sentence (sentence clozes)
+(defun gnosis-display-cloze-sentence (sentence clozes)
"Display cloze sentence for SENTENCE with CLOZES."
- (with-gnosis-buffer
- (erase-buffer)
- (fill-paragraph
- (insert
- (concat "\n"
- (gnosis-cloze-replace-words sentence clozes (propertize gnosis-cloze-string 'face 'gnosis-face-cloze)))))))
-
-(defun gnosis-display--basic-answer (answer success user-input)
+ (erase-buffer)
+ (fill-paragraph
+ (insert
+ (concat "\n"
+ (gnosis-cloze-replace-words sentence clozes (propertize gnosis-cloze-string 'face 'gnosis-face-cloze))))))
+
+(defun gnosis-display-basic-answer (answer success user-input)
"Display ANSWER.
When SUCCESS nil, display USER-INPUT as well"
- (with-gnosis-buffer
(insert
(concat "\n\n"
(propertize "Answer:" 'face 'gnosis-face-directions)
@@ -246,7 +237,7 @@ When SUCCESS nil, display USER-INPUT as well"
(insert (concat "\n"
(propertize "Your answer:" 'face 'gnosis-face-directions)
" "
- (propertize user-input 'face 'gnosis-face-false))))))
+ (propertize user-input 'face 'gnosis-face-false)))))
(cl-defun gnosis-display-y-or-n-answer (&key answer success)
"Display y-or-n answer for note ID.
@@ -255,48 +246,43 @@ ANSWER is the correct answer, either y or n. Answer is either 121 or
110, which are the char values for y & n respectively
SUCCESS is t when user-input is correct, else nil"
(let ((answer (if (equal answer 121) "y" "n")))
- (with-gnosis-buffer
(insert
(concat "\n\n"
(propertize "Answer:" 'face 'gnosis-face-directions)
" "
- (propertize answer 'face (if success 'gnosis-face-correct 'gnosis-face-false)))))))
+ (propertize answer 'face (if success 'gnosis-face-correct 'gnosis-face-false))))))
-(defun gnosis-display--hint (hint)
+(defun gnosis-display-hint (hint)
"Display HINT."
- (with-gnosis-buffer
(goto-char (point-max))
(insert (concat
(propertize "\n\n-----\n" 'face 'gnosis-face-seperator)
- (propertize hint 'face 'gnosis-face-hint)))))
+ (propertize hint 'face 'gnosis-face-hint))))
(cl-defun gnosis-display-cloze-reveal (&key (cloze-char gnosis-cloze-string) replace (success t) (face nil))
"Replace CLOZE-CHAR with REPLACE.
If FACE nil, propertize replace using `gnosis-face-correct', or
`gnosis-face-false' when (not SUCCESS). Else use FACE value."
- (with-gnosis-buffer
(goto-char (point-min))
(search-forward cloze-char nil t)
(replace-match (propertize replace 'face (if (not face)
(if success 'gnosis-face-correct 'gnosis-face-false)
- face)))))
+ face))))
(cl-defun gnosis-display-cloze-user-answer (user-input &optional (false t))
"Display USER-INPUT answer for cloze note upon failed review.
If FALSE t, use gnosis-face-false face"
- (with-gnosis-buffer
(goto-char (point-max))
(insert (concat "\n\n"
(propertize "Your answer:" 'face 'gnosis-face-directions)
" "
- (propertize user-input 'face (if false 'gnosis-face-false 'gnosis-face-correct))))))
+ (propertize user-input 'face (if false 'gnosis-face-false 'gnosis-face-correct)))))
-(defun gnosis-display--correct-answer-mcq (answer user-choice)
+(defun gnosis-display-correct-answer-mcq (answer user-choice)
"Display correct ANSWER & USER-CHOICE for MCQ note."
- (with-gnosis-buffer
(insert (concat "\n\n"
(propertize "Correct Answer:" 'face 'gnosis-face-directions)
" "
@@ -306,35 +292,32 @@ If FALSE t, use gnosis-face-false face"
" "
(propertize user-choice 'face (if (string= answer user-choice)
'gnosis-face-correct
- 'gnosis-face-false))))))
+ 'gnosis-face-false)))))
-(defun gnosis-display--extra (id)
+(defun gnosis-display-extra (id)
"Display extra information for note ID."
(let ((extras (gnosis-get 'extra-notes 'extras `(= id ,id))))
- (with-gnosis-buffer
- (goto-char (point-max))
- (insert (propertize "\n\n-----\n" 'face 'gnosis-face-seperator))
- (fill-paragraph (insert (concat "\n" (propertize extras 'face 'gnosis-face-extra)))))))
+ (goto-char (point-max))
+ (insert (propertize "\n\n-----\n" 'face 'gnosis-face-seperator))
+ (fill-paragraph (insert (concat "\n" (propertize extras 'face 'gnosis-face-extra))))))
-(defun gnosis-display--image (id)
+(defun gnosis-display-image (id)
"Display image for note ID."
(let* ((img (gnosis-get 'images 'extras `(= id ,id)))
(path-to-image (concat (file-name-as-directory gnosis-images-dir) img))
(image (create-image path-to-image 'png nil :width 500 :height 300)))
(when img
- (with-gnosis-buffer
- (insert "\n\n")
- (insert-image image)))))
+ (insert "\n\n")
+ (insert-image image))))
-(defun gnosis-display--next-review (id)
+(defun gnosis-display-next-review (id)
"Display next interval for note ID."
(let ((interval (gnosis-get 'next-rev 'review-log `(= id ,id))))
- (with-gnosis-buffer
- (goto-char (point-max))
- (insert (concat "\n\n"
- (propertize "Next review:" 'face 'gnosis-face-directions)
- " "
- (propertize (format "%s" interval) 'face 'gnosis-face-next-review))))))
+ (goto-char (point-max))
+ (insert (concat "\n\n"
+ (propertize "Next review:" 'face 'gnosis-face-directions)
+ " "
+ (propertize (format "%s" interval) 'face 'gnosis-face-next-review)))))
(cl-defun gnosis--prompt (prompt &optional (downcase nil) (split nil))
"PROMPT user for input until `q' is given.
@@ -874,7 +857,7 @@ Returns a list of the form ((yyyy mm dd) ef)."
:successful-reviews t-success
:successful-reviews-c c-success
:fails-c c-fails
- :fail-t t-fails
+ :fails-t t-fails
:initial-interval initial-interval)))
(defun gnosis-review-due-notes--with-tags ()
@@ -928,45 +911,45 @@ SUCCESS is a binary value, 1 is for successful review."
(defun gnosis-review-mcq (id)
"Display multiple choice answers for question ID."
- (gnosis-display--image id)
- (gnosis-display--question id)
- (let* ((choices (gnosis-get 'options 'notes `(= id ,id)))
- (answer (nth (- (gnosis-get 'answer 'notes `(= id ,id)) 1) choices))
- (user-choice (gnosis-mcq-answer id)))
- (if (string= answer user-choice)
- (progn (gnosis-review--update id 1)
- (message "Correct!"))
- (gnosis-review--update id 0)
- (message "False"))
- (gnosis-display--correct-answer-mcq answer user-choice)
- (gnosis-display--extra id)
- (gnosis-display--next-review id)))
+ (gnosis-display-image id)
+ (gnosis-display-question id)
+ (let* ((choices (gnosis-get 'options 'notes `(= id ,id)))
+ (answer (nth (- (gnosis-get 'answer 'notes `(= id ,id)) 1) choices))
+ (user-choice (gnosis-mcq-answer id)))
+ (if (string= answer user-choice)
+ (progn (gnosis-review--update id 1)
+ (message "Correct!"))
+ (gnosis-review--update id 0)
+ (message "False"))
+ (gnosis-display-correct-answer-mcq answer user-choice)
+ (gnosis-display-extra id)
+ (gnosis-display-next-review id)))
(defun gnosis-review-basic (id)
"Review basic type note for ID."
- (gnosis-display--image id)
- (gnosis-display--question id)
- (gnosis-display--hint (gnosis-get 'options 'notes `(= id ,id)))
+ (gnosis-display-image id)
+ (gnosis-display-question id)
+ (gnosis-display-hint (gnosis-get 'options 'notes `(= id ,id)))
(let* ((answer (gnosis-get 'answer 'notes `(= id ,id)))
(user-input (read-string "Answer: "))
(success (gnosis-compare-strings answer user-input)))
- (gnosis-display--basic-answer answer success user-input)
- (gnosis-display--extra id)
+ (gnosis-display-basic-answer answer success user-input)
+ (gnosis-display-extra id)
(gnosis-review--update id (if success 1 0))
- (gnosis-display--next-review id)))
+ (gnosis-display-next-review id)))
(defun gnosis-review-y-or-n (id)
"Review y-or-n type note for ID."
- (gnosis-display--image id)
- (gnosis-display--question id)
- (gnosis-display--hint (gnosis-get 'options 'notes `(= id ,id)))
- (let* ((answer (gnosis-get 'answer 'notes `(= id ,id)))
- (user-input (read-char-choice "[y]es or [n]o: " '(?y ?n)))
- (success (equal answer user-input)))
- (gnosis-display-y-or-n-answer :answer answer :success success)
- (gnosis-display--extra id)
- (gnosis-review--update id (if success 1 0))
- (gnosis-display--next-review id)))
+ (gnosis-display-image id)
+ (gnosis-display-question id)
+ (gnosis-display-hint (gnosis-get 'options 'notes `(= id ,id)))
+ (let* ((answer (gnosis-get 'answer 'notes `(= id ,id)))
+ (user-input (read-char-choice "[y]es or [n]o: " '(?y ?n)))
+ (success (equal answer user-input)))
+ (gnosis-display-y-or-n-answer :answer answer :success success)
+ (gnosis-display-extra id)
+ (gnosis-review--update id (if success 1 0))
+ (gnosis-display-next-review id)))
(defun gnosis-review-cloze--input (cloze)
"Prompt for user input during cloze review.
@@ -989,9 +972,9 @@ Used to reveal all clozes left with `gnosis-face-cloze-unanswered' face."
(num 1)
(clozes-num (length clozes))
(hint (gnosis-get 'options 'notes `(= id ,id))))
- (gnosis-display--image id)
- (gnosis-display--cloze-sentence main clozes)
- (gnosis-display--hint hint)
+ (gnosis-display-image id)
+ (gnosis-display-cloze-sentence main clozes)
+ (gnosis-display-hint hint)
(cl-loop for cloze in clozes
do (let ((input (gnosis-review-cloze--input cloze)))
(if (equal (car input) t)
@@ -1009,21 +992,23 @@ Used to reveal all clozes left with `gnosis-face-cloze-unanswered' face."
(cl-return)))
;; Update note after all clozes are revealed successfully
finally (gnosis-review--update id 1)))
- (gnosis-display--extra id)
- (gnosis-display--next-review id))
+ (gnosis-display-extra id)
+ (gnosis-display-next-review id))
(defun gnosis-review-note (id)
"Start review for note with value of id ID, if note is unsuspended."
(cond ((gnosis-suspended-p id)
(message "Note is suspended."))
(t
- (let ((type (gnosis-get 'type 'notes `(= id ,id))))
- (pcase type
- ("mcq" (gnosis-review-mcq id))
- ("basic" (gnosis-review-basic id))
- ("cloze" (gnosis-review-cloze id))
- ("y-or-n" (gnosis-review-y-or-n id))
- (_ (error "Malformed note type")))))))
+ (with-current-buffer (switch-to-buffer (get-buffer-create "*gnosis*"))
+ (let ((type (gnosis-get 'type 'notes `(= id ,id))))
+ (gnosis-mode)
+ (pcase type
+ ("mcq" (gnosis-review-mcq id))
+ ("basic" (gnosis-review-basic id))
+ ("cloze" (gnosis-review-cloze id))
+ ("y-or-n" (gnosis-review-y-or-n id))
+ (_ (error "Malformed note type"))))))))
(defun gnosis-review-commit (note-num)
"Commit review session on git repository.