summaryrefslogtreecommitdiff
path: root/org-fc-type-vocab.el
blob: 674085149f57df9a8ad45f71dbf84b6d36565a18 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
;;; org-fc-type-vocab.el --- Card type for learning vocabulary -*- lexical-binding: t; -*-

;; Copyright (C) 2020-2021  Leon Rische

;; Author: Leon Rische <emacs@leonrische.me>
;; Url: https://www.leonrische.me/pages/org_flashcards.html
;; Package-requires: ((emacs "26.3") (org "9.3"))
;; Version: 0.1.0

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Cards of this type should have the (foreign language) word as their
;; heading with a definition on the back.
;;
;; During review, the user is prompted for the definition of the word
;; or asked to type in the word based on its definitions.
;;
;;; Code:

(require 'org-fc-core)
(require 'org-fc-diff)
(require 'org-fc-audio)

(defcustom org-fc-type-vocab-slow-speed 0.7
  "Speed to use for slow playback."
  :type 'number
  :group 'org-fc)

(defcustom org-fc-type-vocab-audio-property "FC_VOCAB_AUDIO"
  "Property with path to audio file."
  :type 'string
  :group 'org-fc)

(defun org-fc-type-vocab-init ()
  "Mark headline as card of the vocab type."
  (interactive)
  (org-fc--init-card "vocab")
  (org-fc-review-data-update '("front" "back")))

(defun org-fc-type-vocab-setup (position)
  "Prepare POSITION of a vocab card for review."
  (pcase position
    ("front"
     (org-fc-audio-play org-fc-type-vocab-audio-property)
     (org-fc-type-normal-setup position))
    ("back"
     (org-fc-type-vocab-typing-setup)
     (org-fc-audio-play org-fc-type-vocab-audio-property)
     'rate)
    (_ (error "Invalid vocab position %s" position))))

(defun org-fc-type-vocab-flip ()
  "Flip a vocab card."
  (org-fc-type-normal-flip))

(defun org-fc-type-vocab-play ()
  "Play vocab audio file at normal speed."
  (interactive)
  (org-fc-audio-play org-fc-type-vocab-audio-property))

(defun org-fc-type-vocab-play-slow ()
  "Play vocab audio file at slow speed."
  (interactive)
  (org-fc-audio-play org-fc-type-vocab-audio-property org-fc-type-vocab-slow-speed))

(defun org-fc-vocab-content ()
  "Heading position & text as a (pos . string) pair."
  (save-excursion
    (org-fc-goto-entry-heading)
    (let ((case-fold-search nil))
      (if (looking-at org-complex-heading-regexp)
          (cons
           (match-beginning 4)
           (buffer-substring-no-properties (match-beginning 4) (match-end 4)))))))

(defun org-fc-type-vocab-typing-setup ()
  "Prepare a text-input vocab card for review."
  (interactive)
  (org-show-subtree)
  (let* ((pos-content (org-fc-vocab-content))
         (content (cdr pos-content))
         (start (car pos-content))
         (end (+ start (length content)))
         (ov (org-fc-hide-region start end "..."))
         (deemph (org-fc-deemphasize content))
         (diff (org-fc-diff (read-string "Answer: ") (cdr deemph))))
    (delete-overlay ov)
    ;; Overlay for user input
    (when (car deemph)
      (setq start (1+ start))
      (setq end (1- end)))
    (org-fc-hide-region start end (car diff))
    ;; Overlay for expected answer, using the newline after the answer
    (if (cdr diff)
        (org-fc-hide-region
         end (1+ end)
         (concat
          "\n! "
          (if (null (car deemph))
              (cdr diff)
            (org-fc-emphasize
             (concat (car deemph) (cdr diff) (car deemph))))
          "")))))

(org-fc-register-type
 'vocab
 'org-fc-type-vocab-setup
 'org-fc-type-vocab-flip
 'org-fc-noop)

;;; Footer

(provide 'org-fc-type-vocab)

;;; org-fc-type-vocab.el ends here