lirve.el/learning-irregular-verbs-in-English.el

385 lines
13 KiB
EmacsLisp
Raw Normal View History

2023-12-31 10:00:33 +01:00
;;; learning-irregular-verbs-in-English.el --- Application to learn and review irregular verbs in English. -*- lexical-binding: t;
;;
;; Copyright © 2024 Andros Fenollosa
;; Authors: Andros Fenollosa <andros@fenollosa.email>
;; URL: https://github.com/tanrax/learning-irregular-verbs-in-English.el
;; Version: 1.0.0
;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Commentary:
;; Application to learn and review irregular verbs in English.
;;; Code:
2024-01-02 17:32:15 +01:00
;; Imports
(require 'widget)
(eval-when-compile
(require 'wid-edit))
2023-12-31 15:39:31 +01:00
2024-01-02 17:32:15 +01:00
;; Variables
2023-12-31 15:39:31 +01:00
2024-01-03 13:32:56 +01:00
(defvar lire--verbs '(
("beat" "beat" "beaten")
("become" "became" "become")
("begin" "began" "begun")
("bend" "bent" "bent")
("bet" "bet" "bet")
("bite" "bit" "bitten")
("bleed" "bled" "bled")
("blow" "blew" "blown")
("break" "broke" "broken")
("bring" "brought" "brought")
("build" "built" "built")
("burn" "burnt" "burnt")
("buy" "bought" "bought")
("catch" "caught" "caught")
("choose" "chose" "chosen")
("come" "came" "come")
("cost" "cost" "cost")
("cut" "cut" "cut")
("dig" "dug" "dug")
("do" "did" "done")
("draw" "drew" "drawn")
("dream" "dreamt" "dreamt")
("drink" "drank" "drunk")
("drive" "drove" "driven")
("eat" "ate" "eaten")
("fall" "fell" "fallen")
("feed" "fed" "fed")
("feel" "felt" "felt")
("fight" "fought" "fought")
("find" "found" "found")
("fly" "flew" "flown")
("forget" "forgot" "forgotten")
("forgive" "forgave" "forgiven")
("freeze" "froze" "frozen")
("get" "got" "got")
("give" "gave" "given")
("go" "went" "gone")
("grow" "grew" "grown")
("hang" "hung" "hung")
("have" "had" "had")
("hear" "heard" "heard")
("hide" "hid" "hidden")
("hit" "hit" "hit")
("hold" "held" "held")
("hurt" "hurt" "hurt")
("keep" "kept" "kept")
("know" "knew" "known")
("lay" "laid" "laid")
("lead" "led" "led")
("learn" "learnt" "learnt")
("leave" "left" "left")
("lend" "lent" "lent")
("let" "let" "let")
("lie" "lay" "lain")
("light" "lit" "lit")
("lose" "lost" "lost")
("make" "made" "made")
("mean" "meant" "meant")
("meet" "met" "met")
("pay" "paid" "paid")
("put" "put" "put")
("read" "read" "read")
("ride" "rode" "ridden")
("ring" "rang" "rung")
("rise" "rose" "risen")
("run" "ran" "run")
("say" "said" "said")
("see" "saw" "seen")
("sell" "sold" "sold")
("send" "sent" "sent")
("set" "set" "set")
("shake" "shook" "shaken")
("shine" "shone" "shone")
("shoot" "shot" "shot")
("show" "showed" "shown")
("shut" "shut" "shut")
("sing" "sang" "sung")
("sink" "sank" "sunk")
("sit" "sat" "sat")
("sleep" "slept" "slept")
("smell" "smelt" "smelt")
("speak" "spoke" "spoken")
("spend" "spent" "spent")
("spell" "spelt" "spelt")
("spit" "spit" "spit")
("stand" "stood" "stood")
("steal" "stole" "stolen")
("swim" "swam" "swum")
("take" "took" "taken")
("teach" "taught" "taught")
("tear" "tore" "torn")
("tell" "told" "told")
("think" "thought" "thought")
("throw" "threw" "thrown")
("understand" "understood" "understood")
("wake" "woke" "woken")
("wear" "wore" "worn")
("win" "won" "won")
("write" "wrote" "written")))
;; lire
(defvar lire--buffer-name "*Learning irregular verbs in English*")
(defvar lire--state 1) ;; 1: lire--start, 2: playing (before first check), 3: win (show success layout)
(defvar lire--verb-to-learn-infinitive nil)
(defvar lire--verb-to-learn-simple-past nil)
(defvar lire--verb-to-learn-past-participle nil)
(defvar lire--emoji-valid "")
(defvar lire--emoji-error "👎")
(defvar lire--widget-title nil)
(defvar lire--text-title " 🧑‍🎓 Learning irregular verbs in English 🇬🇧")
(defvar lire--widget-item-verb nil)
(defvar lire--widget-field-simple-past nil)
(defvar lire--widget-label-check-simple-past nil)
(defvar lire--widget-field-past-participle nil)
(defvar lire--widget-label-check-past-participle nil)
(defvar lire--text-button-check "Check")
(defvar lire--widget-button-check nil)
(defvar lire--widget-item-space-before-check nil)
(defvar lire--text-button-show-solution "Don't know")
(defvar lire--widget-button-show-solution nil)
(defvar lire--widget-message-success nil)
(defvar lire--widget-item-space-before-success nil)
(defvar lire--text-success "Nice!")
(defvar lire--text-fail "Next time you will do better")
(defvar lire--is-resolve t)
(defvar lire--widget-item-space-after-success nil)
(defvar lire--widget-button-quit nil)
(defvar lire--text-button-quit "Quit")
(defvar lire--widget-item-space-between-buttons nil)
(defvar lire--widget-button-lire--replay nil)
(defvar lire--text-button-lire--replay "New challenge")
2024-01-01 20:39:40 +01:00
2024-01-02 17:32:15 +01:00
;; Functions
2024-01-02 16:18:41 +01:00
2024-01-03 13:32:56 +01:00
(defun lire--kill-app ()
2024-01-02 17:32:15 +01:00
"Kill the application."
2024-01-03 13:32:56 +01:00
(kill-buffer lire--buffer-name))
2024-01-02 16:18:41 +01:00
2024-01-03 13:32:56 +01:00
(defun lire--value-field-simple-past ()
(if (not (eq lire--widget-field-simple-past nil)) (widget-value lire--widget-field-simple-past) ""))
2023-12-31 15:39:31 +01:00
2024-01-03 13:32:56 +01:00
(defun lire--value-field-past-participle ()
(if (not (eq lire--widget-field-past-participle nil)) (widget-value lire--widget-field-past-participle) ""))
2023-12-31 15:39:31 +01:00
2024-01-03 13:32:56 +01:00
(defun lire--set-verb-to-learn ()
2024-01-02 17:32:15 +01:00
"Set the verb to learn."
2024-01-03 13:32:56 +01:00
(let ((verbs-random (nth (random (length lire--verbs)) lire--verbs)))
(setq lire--verb-to-learn-infinitive (nth 0 verbs-random))
(setq lire--verb-to-learn-simple-past (nth 1 verbs-random))
(setq lire--verb-to-learn-past-participle (nth 2 verbs-random))))
2024-01-01 13:10:45 +01:00
2024-01-03 13:32:56 +01:00
(defun lire--format-value-infinitive ()
2024-01-02 17:32:15 +01:00
"Format the value of the infinitive."
2024-01-03 13:32:56 +01:00
(format "Infinitive ➡️ %s" lire--verb-to-learn-infinitive))
2024-01-01 13:10:45 +01:00
2024-01-03 13:32:56 +01:00
(defun lire--format-check-simple-past ()
2024-01-02 17:32:15 +01:00
"Format the value of the simple past."
2024-01-03 13:32:56 +01:00
(if (eq lire--state 1)
2024-01-02 17:32:15 +01:00
""
(format " %s" (if
(and
2024-01-03 13:32:56 +01:00
(string= (lire--value-field-simple-past) lire--verb-to-learn-simple-past)
(not (string= (lire--value-field-simple-past) "")))
lire--emoji-valid lire--emoji-error))))
2024-01-01 20:39:40 +01:00
2024-01-03 13:32:56 +01:00
(defun lire--format-check-past-participle ()
2024-01-02 17:32:15 +01:00
"Format the value of the past participle."
2024-01-03 13:32:56 +01:00
(if (eq lire--state 1)
2024-01-02 17:32:15 +01:00
""
(format " %s" (if
(and
2024-01-03 13:32:56 +01:00
(string= (lire--value-field-past-participle) lire--verb-to-learn-past-participle)
(not (string= (lire--value-field-past-participle) "")))
lire--emoji-valid lire--emoji-error))))
2024-01-01 13:10:45 +01:00
2024-01-03 13:32:56 +01:00
(defun lire--toggle-layout-finish ()
2024-01-02 17:32:15 +01:00
"Toggle the layout to success."
2024-01-03 13:32:56 +01:00
(if (eq lire--state 3)
2024-01-02 17:32:15 +01:00
(progn
;; Cursor to end
(goto-char (point-max))
;; Remove check button
2024-01-03 13:32:56 +01:00
(widget-delete lire--widget-button-check)
(setq lire--widget-button-check nil)
;; Remove space after check button
(widget-delete lire--widget-item-space-before-check)
(setq lire--widget-item-space-before-check nil)
;; Remove show solution button
(widget-delete lire--widget-button-show-solution)
(setq lire--widget-button-show-solution nil)
2024-01-02 17:32:15 +01:00
;; Text success
2024-01-03 13:32:56 +01:00
(setq lire--widget-item-space-before-success (widget-create 'item
2024-01-02 17:32:15 +01:00
""))
2024-01-03 13:32:56 +01:00
(setq lire--widget-message-success (widget-create 'item
(if lire--is-resolve lire--text-success lire--text-fail)))
(setq lire--widget-item-space-after-success (widget-create 'item
"\n"))
;; Lire--Replay button
(setq lire--widget-button-lire--replay (widget-create 'push-button
2024-01-02 17:32:15 +01:00
:size 20
:notify (lambda (&rest ignore)
2024-01-03 13:32:56 +01:00
(lire--replay))
lire--text-button-lire--replay))
2024-01-02 17:32:15 +01:00
;; Space
2024-01-03 13:32:56 +01:00
(setq lire--widget-item-space-between-buttons (widget-create 'item
2024-01-02 17:32:15 +01:00
"\n"))
;; Quit button
2024-01-03 13:32:56 +01:00
(setq lire--widget-button-quit (widget-create 'push-button
2024-01-02 17:32:15 +01:00
:size 20
:notify (lambda (&rest ignore)
2024-01-03 13:32:56 +01:00
(lire--kill-app))
lire--text-button-quit))
2024-01-02 17:32:15 +01:00
(widget-backward 2)
)
(progn
2024-01-03 13:32:56 +01:00
(when (not (eq lire--widget-item-space-before-success nil)) (widget-delete lire--widget-item-space-before-success))
(when (not (eq lire--widget-message-success nil)) (widget-delete lire--widget-message-success))
(when (not (eq lire--widget-item-space-after-success nil)) (widget-delete lire--widget-item-space-after-success))
(when (not (eq lire--widget-button-lire--replay nil)) (widget-delete lire--widget-button-lire--replay))
(when (not (eq lire--widget-item-space-between-buttons nil)) (widget-delete lire--widget-item-space-between-buttons))
(when (not (eq lire--widget-button-quit nil)) (widget-delete lire--widget-button-quit))
2024-01-02 17:32:15 +01:00
)))
2023-12-31 15:39:31 +01:00
2024-01-03 13:32:56 +01:00
(defun lire--make-button-check ()
2024-01-02 17:32:15 +01:00
"Make the button check."
2024-01-03 13:32:56 +01:00
(setq lire--widget-button-check (widget-create 'push-button
2024-01-02 17:32:15 +01:00
:notify (lambda (&rest ignore)
2024-01-03 13:32:56 +01:00
(lire--update))
lire--text-button-check)))
(defun lire--make-space-after-check ()
"Add space between Button check and Button show solution"
(setq lire--widget-item-space-before-check (widget-create 'item "\n")))
2024-01-02 17:32:15 +01:00
2024-01-03 13:32:56 +01:00
(defun lire--show-solutions ()
"Show solutions"
(setq lire--is-resolve nil)
(widget-value-set lire--widget-field-simple-past lire--verb-to-learn-simple-past)
(widget-value-set lire--widget-field-past-participle lire--verb-to-learn-past-participle))
(defun lire--make-button-show-solution ()
"Make the button show solution."
(setq lire--widget-button-show-solution (widget-create 'push-button
:notify (lambda (&rest ignore)
(lire--show-solutions)
(lire--update))
lire--text-button-show-solution)))
(defun lire--start ()
2024-01-02 17:50:43 +01:00
"Start challenge."
2024-01-03 13:32:56 +01:00
;; Set the lire--state
(setq lire--state 1)
2024-01-02 17:32:15 +01:00
;; Get a new verb
2024-01-03 13:32:56 +01:00
(lire--set-verb-to-learn)
2024-01-02 17:32:15 +01:00
;; Show the verb in infinitive
2024-01-03 13:32:56 +01:00
(widget-value-set lire--widget-item-verb (lire--format-value-infinitive))
2024-01-02 17:32:15 +01:00
;; Reset button check
2024-01-03 13:32:56 +01:00
(when (eq lire--widget-button-check nil) (lire--make-button-check))
;; Reset space after check
(when (eq lire--widget-item-space-before-check nil) (lire--make-space-after-check))
;; Reset button show solution
(when (eq lire--widget-button-show-solution nil) (lire--make-button-show-solution))
2024-01-02 17:32:15 +01:00
;; Clear the fields
2024-01-03 13:32:56 +01:00
(widget-value-set lire--widget-field-simple-past "")
(widget-value-set lire--widget-label-check-simple-past "")
(widget-value-set lire--widget-field-past-participle "")
(widget-value-set lire--widget-label-check-past-participle "")
2024-01-02 17:32:15 +01:00
;; Update labels
2024-01-03 13:32:56 +01:00
(lire--update))
2024-01-01 20:39:40 +01:00
2024-01-03 13:32:56 +01:00
(defun lire--replay ()
2024-01-02 17:32:15 +01:00
"Replay the challenge."
(interactive)
2024-01-03 13:32:56 +01:00
(lire--start)
2024-01-02 17:32:15 +01:00
(widget-backward 1))
2023-12-31 15:39:31 +01:00
2024-01-03 13:32:56 +01:00
(defun lire--update ()
"Update state and show temps layouts."
2024-01-02 17:32:15 +01:00
(interactive)
;; Is playing?
2024-01-03 13:32:56 +01:00
(when (and (eq lire--state 1)
2024-01-02 17:32:15 +01:00
(or
2024-01-03 13:32:56 +01:00
(not (string= (lire--value-field-simple-past) ""))
(not (string= (lire--value-field-past-participle) "")))
2024-01-02 17:32:15 +01:00
)
2024-01-03 13:32:56 +01:00
(setq lire--state 2))
2024-01-02 17:32:15 +01:00
;; Check the answers
2024-01-03 13:32:56 +01:00
(when (eq lire--state 2)
2024-01-02 17:32:15 +01:00
;; Is win?
(when (and
2024-01-03 13:32:56 +01:00
(string= (lire--value-field-simple-past) lire--verb-to-learn-simple-past)
(string= (lire--value-field-past-participle) lire--verb-to-learn-past-participle))
;; Set the lire--state
(setq lire--state 3))
2024-01-02 17:32:15 +01:00
;; Update the check labels
2024-01-03 13:32:56 +01:00
(widget-value-set lire--widget-label-check-simple-past (lire--format-check-simple-past))
(widget-value-set lire--widget-label-check-past-participle (lire--format-check-past-participle)))
2024-01-02 17:32:15 +01:00
;; Update the success layout if needed
2024-01-03 13:32:56 +01:00
(lire--toggle-layout-finish)
(setq lire--is-resolve t))
2023-12-31 15:39:31 +01:00
2024-01-03 13:32:56 +01:00
(defun lire--main-layout ()
2024-01-02 17:32:15 +01:00
"Make widgets for the main layout."
;; Create the buffer
2024-01-03 13:32:56 +01:00
(switch-to-buffer lire--buffer-name)
2024-01-02 17:32:15 +01:00
;; Clear the buffer
(kill-all-local-variables)
(let ((inhibit-read-only t))
(erase-buffer))
(remove-overlays)
;; Create the widgets
;; Title
2024-01-03 13:32:56 +01:00
(insert (propertize (format "\n%s\n\n" lire--text-title) 'face '(:height 1.2 :weight bold)))
2024-01-02 17:32:15 +01:00
;; Verb in infinitive
2024-01-03 13:32:56 +01:00
(setq lire--widget-item-verb (widget-create 'item
2024-01-02 17:32:15 +01:00
:value ""))
;; Separator
(insert "\nSimple past ➡️ ")
;; Simple past
2024-01-03 13:32:56 +01:00
(setq lire--widget-field-simple-past (widget-create 'editable-field
2024-01-02 17:32:15 +01:00
:size 8
:help-echo "Type a Simple past"
))
;; Label check
(insert " ")
2024-01-03 13:32:56 +01:00
(setq lire--widget-label-check-simple-past (widget-create 'item
(lire--format-check-simple-past)))
2024-01-02 17:32:15 +01:00
;; Separator
(insert "\nPast participle ➡️ ")
;; Past participle
2024-01-03 13:32:56 +01:00
(setq lire--widget-field-past-participle (widget-create 'editable-field
2024-01-02 17:32:15 +01:00
:size 8
:help-echo "Type a Past participle"))
;; Label check
(insert " ")
2024-01-03 13:32:56 +01:00
(setq lire--widget-label-check-past-participle (widget-create 'item
(lire--format-check-past-participle)))
2024-01-02 17:32:15 +01:00
;; Separator
(insert "\n")
;; Check button
2024-01-03 13:32:56 +01:00
(lire--make-button-check)
;; Separator
(lire--make-space-after-check)
;; Show solution button
(lire--make-button-show-solution)
2024-01-02 17:32:15 +01:00
;; Display the buffer
(use-local-map widget-keymap)
(widget-setup))
2023-12-31 15:39:31 +01:00
2024-01-02 17:32:15 +01:00
;; Keybindings
2024-01-03 13:32:56 +01:00
(define-key widget-keymap (kbd "q") 'lire--kill-app)
2023-12-31 15:39:31 +01:00
2024-01-02 17:32:15 +01:00
;; Init
(defun learning-irregular-verbs-in-English ()
"Application to learn and review irregular verbs in English."
(interactive)
2024-01-03 13:32:56 +01:00
(lire--main-layout)
(lire--start)
2024-01-03 13:35:55 +01:00
(widget-backward 3))
2023-12-31 10:00:33 +01:00
(provide 'learning-irregular-verbs-in-English)
;;; learning-irregular-verbs-in-English.el ends here