bible-mode.el 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. ;;;; -*- mode: EMACS-LISP; lexical-binding: t -*-
  2. ;;
  3. ;; bible-mode.el --- A browsing interface for the SWORD Project's Diatheke CLI
  4. ;; Time-stamp: <2024-05-14 11:32:24 fred>
  5. ;; Author: Zacalot
  6. ;; Fixes and modifications by Fred Gilham
  7. ;; Url: https://github.com/fmgilham/bible-mode
  8. ;; Forked from
  9. ;; Url: https://github.com/Zacalot/bible-mode
  10. ;; Version: 1.0.0
  11. ;; Package-Requires: ((emacs "24.1"))
  12. ;; Keywords: diatheke, sword, research, bible
  13. ;;; Commentary:
  14. ;; This package uses the `diatheke' program to browse and search
  15. ;; Biblical texts provided by the Sword project.
  16. ;; Word study is also supported.
  17. ;;; Usage:
  18. ;; First install `diatheke'. On Debian/Ubuntu it's in the `diatheke'
  19. ;; package. In other distributions it might be in the sword package.
  20. ;; For Windows I found that you can simply install the Xiphos package.
  21. ;; It includes the Sword library and its utilities including diatheke,
  22. ;; installmgr and mkfastmod. Add the "Program Files\Xiphos\bin" path
  23. ;; to your execution path.
  24. ;; Use M-x `bible-open' to open a Bible buffer.
  25. ;; Use C-h f `bible-mode' to see available keybindings.
  26. ;; You may customize `bible-mode-module' to set a default browsing
  27. ;; module, as well as `bible-mode-word-study-enabled' to enable word
  28. ;; study by default.
  29. ;;; Design:
  30. ;; The idea here is to use the diatheke program to insert code from
  31. ;; modules into buffers. The main bible display uses an "internal" XML
  32. ;; format. The whole buffer gets parsed by libxml-parse-html-region to
  33. ;; create a dom tree. This gets parsed by
  34. ;; bible-mode--insert-domnode-recursive to render the text into
  35. ;; reading format.
  36. ;; The text is then decorated using information from the dom format as
  37. ;; necessary along with regular expressions to identify the verse
  38. ;; references. This is for red letters, purple highlighting of the
  39. ;; verse numbers, bold face of the divine name in the OT and so on.
  40. ;; If strongs tags and/or morphological tags are present, they are
  41. ;; looked up in appropriate lexical and morphological modules and used
  42. ;; to add tooltips to the text so that mousing over words will bring
  43. ;; up a tooltip with information about the word. Clicking on a word
  44. ;; with lexical information will display that informatio in a "term"
  45. ;; buffer.
  46. ;;;
  47. ;;; bm- is used as shorthand (see Local Variables) for bible-mode-
  48. ;;; Code:
  49. ;;;; Requirements
  50. (require 'cl-lib) ; XXX FMG there are just a few constructs that use this; use elisp versions instead.
  51. ;; (require 'bidi)
  52. (require 'dom)
  53. (require 'shr)
  54. ;;;; Variables
  55. (defgroup bible-mode nil
  56. "Settings for `bible-mode'."
  57. :group 'tools
  58. :link '(url-link "https://github.com/fmgilham/bible-mode"))
  59. (defcustom bm-module
  60. "KJV"
  61. "Book module for Diatheke to query."
  62. :type '(choice (const :tag "None" nil)
  63. (string :tag "Module abbreviation (e.g. \"KJV\")"))
  64. :local t
  65. :group 'bible-mode)
  66. (defcustom bm-font
  67. "Ezra SIL"
  68. "Default font for bible-mode."
  69. :type '(string :tag "Font family name (e.g. \"Ezra SIL\")")
  70. :local t
  71. :group 'bible-mode)
  72. (defcustom bm-greek-lexicon
  73. "MLStrong"
  74. "Lexicon used for displaying definitions of Greek words using Strong's codes."
  75. :type '(string :tag "Lexicon module (e.g. \"StrongsRealGreek\").")
  76. :local nil
  77. :group 'bible-mode)
  78. (defcustom bm-short-greek-lexicon
  79. "StrongsRealGreek"
  80. "Lexicon used for displaying definitions of Greek words in tooltips."
  81. :type '(string :tag "Lexicon module (e.g. \"StrongsRealGreek\").")
  82. :local nil
  83. :group 'bible-mode)
  84. (defcustom bm-hebrew-lexicon
  85. "StrongsRealHebrew" ; Nice to use BDBGlosses_Strongs but it needs to be special-cased
  86. "Lexicon used for displaying definitions of Hebrew words using Strong's codes."
  87. :type '(string :tag "Lexicon module (e.g. \"StrongsRealHebrew\")")
  88. :local nil
  89. :group 'bible-mode)
  90. (defcustom bm-short-hebrew-lexicon
  91. "StrongsRealHebrew"
  92. "Lexicon used for displaying definitions of Hebrew words in tooltips."
  93. :type '(string :tag "Lexicon module (e.g. \"StrongsRealHebrew\")")
  94. :local nil
  95. :group 'bible-mode)
  96. (defcustom bm-word-study-enabled
  97. nil
  98. "Display Strong's Hebrew, Strong's Greek, and Lemma words for study."
  99. :type 'boolean
  100. :local t
  101. :group 'bible-mode)
  102. (defcustom bm-red-letter-enabled
  103. t
  104. "Display words of Jesus in red when module has that information."
  105. :type 'boolean
  106. :local t
  107. :group 'bible-mode)
  108. ;;; defvars
  109. (defvar bm-modules (lazy-completion-table bm-modules bm-list-biblical-modules))
  110. ;; XXX I believe these chapter counts aren't the same for all modules, e.g. JPS.
  111. (defvar bm-books
  112. '(;; Old Testament
  113. ("Genesis" . 50) ("Exodus" . 40) ("Leviticus" . 27) ("Numbers" . 36)
  114. ("Deuteronomy" . 34) ("Joshua" . 24) ("Judges" . 21) ("Ruth" . 4)
  115. ("I Samuel" . 31) ("II Samuel" . 24) ("I Kings" . 22) ("II Kings" . 25)
  116. ("I Chronicles" . 29) ("II Chronicles" . 36) ("Ezra" . 10) ("Nehemiah" . 13)
  117. ("Esther" . 10) ("Job" . 42) ("Psalms" . 150) ("Proverbs" . 31)
  118. ("Ecclesiastes" . 12) ("Song of Solomon" . 8) ("Isaiah" . 66) ("Jeremiah" . 52)
  119. ("Lamentations" . 5) ("Ezekiel" . 48) ("Daniel" . 12) ("Hosea" . 14)
  120. ("Joel" . 3) ("Amos" . 9) ("Obadiah" . 1) ("Jonah" . 4)
  121. ("Micah" . 7) ("Nahum" . 3) ("Habakkuk" . 3) ("Zephaniah" . 3)
  122. ("Haggai" . 2) ("Zechariah" . 14) ("Malachi" . 4)
  123. ;; New Testament
  124. ("Matthew" . 28) ("Mark" . 16) ("Luke" . 24) ("John" . 21)
  125. ("Acts" . 28) ("Romans" . 16) ("I Corinthians" . 16) ("II Corinthians" . 13)
  126. ("Galatians" . 6) ("Ephesians" . 6) ("Philippians" . 4) ("Colossians" . 4)
  127. ("I Thessalonians" . 5) ("II Thessalonians" . 3) ("I Timothy" . 6) ("II Timothy" . 4)
  128. ("Titus" . 3) ("Philemon" . 1) ("Hebrews" . 13) ("James" . 5)
  129. ("I Peter" . 5) ("II Peter" . 3) ("I John" . 5) ("II John" . 1)
  130. ("III John" . 1) ("Jude" . 1) ("Revelation of John" . 22))
  131. "A-list of name / chapter count for Bible books.")
  132. ;;;; Book / chapter
  133. (defvar-local bm-current-book (assoc "Genesis" bm-books)
  134. "Current book data (name . chapter).")
  135. (defvar-local bm-current-book-name "Genesis"
  136. "Current book name.")
  137. (defvar-local bm-current-chapter 1
  138. "Current book chapter number.")
  139. (defvar-local bm-search-query nil
  140. "Search query associated with the buffer.")
  141. (defvar-local bm-search-mode "phrase"
  142. "Search mode: either `lucene' or `phrase'.")
  143. (defvar-local bm-has-strongs nil
  144. "Set if the module being displayed has strongs numbers availabile.")
  145. (defvar-local bm-has-morphology nil
  146. "Set if the module being displayed has morphology availabile.")
  147. ;; (defvar bm-current-module nil)
  148. ;;;; Keymaps
  149. (defconst bm-map (make-keymap))
  150. ;;;;; Navigation
  151. (define-key bm-map "n" 'bm-next-chapter)
  152. (define-key bm-map "p" 'bm-previous-chapter)
  153. (define-key bm-map (kbd "TAB") 'bm-forward-word) ; TODO: bm-forward-word
  154. ;;;;; Direct jump
  155. (define-key bm-map "b" 'bm-select-book)
  156. (define-key bm-map "c" 'bm-select-chapter)
  157. ;;;;; Search
  158. (define-key bm-map "s" 'bible-search)
  159. (define-key bm-map "/" 'bible-search)
  160. ;;;; Not yet
  161. ;;(define-key bm-map "" 'bm-set-search-range)
  162. ;;;;; Misc
  163. (define-key bm-map "m" 'bm-select-module)
  164. (define-key bm-map "w" 'bm-toggle-word-study)
  165. (define-key bm-map "x" 'bm-split-display)
  166. ;;;;; Deal with visual-line-mode
  167. (define-key bm-map "\C-n" 'next-logical-line)
  168. (define-key bm-map "\C-p" 'previous-logical-line)
  169. (defconst bible-search-mode-map (make-keymap))
  170. (define-key bible-search-mode-map "s" 'bible-search)
  171. (define-key bible-search-mode-map "w" 'bm-toggle-word-study)
  172. (define-key bible-search-mode-map (kbd "RET") 'bible-search-mode-follow-verse)
  173. (defconst bible-term-hebrew-mode-map (make-keymap))
  174. (defconst bible-term-greek-mode-map (make-keymap))
  175. (defconst bible-term-morph-mode-map (make-keymap))
  176. ;;;
  177. ;;; Menu bar items
  178. ;;;
  179. ;;; Right now just convenience items. More as I think of them.
  180. ;;;
  181. (define-key global-map [menu-bar bible-mode]
  182. (cons "Bible Mode" (make-sparse-keymap "Bible Mode")))
  183. (defun bm-set-left-to-right ()
  184. (interactive)
  185. (setq-local bidi-paragraph-direction 'left-to-right))
  186. (defun bm-set-right-to-left ()
  187. (interactive)
  188. (setq-local bidi-paragraph-direction 'right-to-left))
  189. (define-key global-map
  190. [menu-bar bible-mode left-to-right]
  191. '("Left-to-right" . bm-set-left-to-right))
  192. (define-key global-map
  193. [menu-bar bible-mode right-to-left]
  194. '("Right-to-left" . bm-set-right-to-left))
  195. (defvar-local bm-debugme nil)
  196. (defun bm-set-display-xml ()
  197. (interactive)
  198. (setq-local bm-debugme t)
  199. (bm-display))
  200. (defun bm-set-display-text ()
  201. (interactive)
  202. (setq-local bm-debugme nil)
  203. (bm-display))
  204. (define-key global-map
  205. [menu-bar bible-mode display-xml]
  206. '("Display XML" . bm-set-display-xml))
  207. (define-key global-map
  208. [menu-bar bible-mode display-text]
  209. '("Display Text" . bm-set-display-text))
  210. (define-key global-map
  211. [menu-bar bible-mode select-biblical-text]
  212. '("Select Text" . bm-display-available-modules))
  213. (defun bm-display-greek ()
  214. (interactive)
  215. (let ((item (car (split-string (get-text-property (point) 'strong)))))
  216. ;; Remove "strong:G" prefix
  217. (bible-term-greek (replace-regexp-in-string "strong:G" "" item))))
  218. (defconst bm-greek-keymap (make-sparse-keymap))
  219. (define-key bm-greek-keymap (kbd "RET") 'bm-display-greek)
  220. (define-key bm-greek-keymap [mouse-1] 'bm-display-greek)
  221. (defun bm-display-hebrew ()
  222. (interactive)
  223. (let ((item (car (split-string (get-text-property (point) 'strong)))))
  224. ;; Remove "strong:H" prefix and any alphabetic suffixes.
  225. (bible-term-hebrew (replace-regexp-in-string "[a-zA-Z]" "" item nil nil nil 8))))
  226. (defconst bm-hebrew-keymap (make-sparse-keymap))
  227. (define-key bm-hebrew-keymap (kbd "RET") 'bm-display-hebrew)
  228. (define-key bm-hebrew-keymap [mouse-1] 'bm-display-hebrew)
  229. (defconst bm-lemma-keymap (make-sparse-keymap))
  230. (define-key bm-lemma-keymap (kbd "RET")
  231. (lambda ()
  232. (interactive)
  233. ))
  234. (defconst bm-morph-keymap (make-sparse-keymap))
  235. (define-key bm-morph-keymap (kbd "RET")
  236. (lambda ()
  237. (interactive)
  238. ;;; (let ((thing (thing-at-point 'word)))
  239. ;;; (message "thing at point: %s" thing)
  240. ;;; (message "morph property %s" (get-text-property 0 'field thing))
  241. ))
  242. ;;;; Modes
  243. (define-derived-mode bible-mode special-mode "Bible"
  244. "Mode for reading the Bible.
  245. \\{bm-map}"
  246. (buffer-disable-undo)
  247. (font-lock-mode t)
  248. (use-local-map bm-map)
  249. (setq buffer-read-only t)
  250. (visual-line-mode t))
  251. (define-derived-mode bible-search-mode special-mode "Bible Search"
  252. "Mode for performing Bible searches.
  253. \\{bible-search-mode-map}"
  254. (buffer-disable-undo)
  255. (font-lock-mode t)
  256. (use-local-map bible-search-mode-map)
  257. (setq buffer-read-only t)
  258. (visual-line-mode t)
  259. )
  260. (define-derived-mode bible-term-hebrew-mode special-mode "Bible Term (Hebrew)"
  261. "Mode for researching Hebrew terms in the Bible.
  262. \\{bible-term-hebrew-mode-map}"
  263. (buffer-disable-undo)
  264. (font-lock-mode t)
  265. (use-local-map bible-term-hebrew-mode-map)
  266. (setq buffer-read-only t)
  267. (visual-line-mode t))
  268. (define-derived-mode bible-term-greek-mode special-mode "Bible Term (Greek)"
  269. "Mode for researching Greek terms in the Bible.
  270. \\{bible-term-greek-mode-map}"
  271. (buffer-disable-undo)
  272. (font-lock-mode t)
  273. (use-local-map bible-term-greek-mode-map)
  274. (setq buffer-read-only t)
  275. (visual-line-mode t))
  276. ;;;; Functions
  277. ;;;;; Commands
  278. ;;;###autoload
  279. (defun bible-open (&optional book-name chapter verse)
  280. "Creates and opens a `bible-mode' buffer"
  281. (interactive)
  282. (let ((buf (get-buffer-create (generate-new-buffer-name (concat "*bible*")))))
  283. (set-buffer buf)
  284. (bible-mode)
  285. (bm-set-location (assoc (or book-name "Genesis") bm-books) (or chapter 1) verse)
  286. (set-window-buffer (get-buffer-window (current-buffer)) buf)))
  287. ;;;###autoload
  288. (defun bm-next-chapter ()
  289. "Pages to the next chapter for the active `bible-mode' buffer."
  290. (interactive)
  291. (let* ((book-chapters (cdr bm-current-book))
  292. (chapter (min book-chapters (+ bm-current-chapter 1))))
  293. (bm-set-location bm-current-book chapter)))
  294. ;;;###autoload
  295. (defun bm-previous-chapter ()
  296. "Pages to the previous chapter for the active `bible-mode' buffer."
  297. (interactive)
  298. (bm-set-location bm-current-book (max 1 (- bm-current-chapter 1))))
  299. (defun bm-forward-word ()
  300. "Moves forward a word, taking into account the relevant text properties.
  301. XXX Doesn't work yet."
  302. (interactive)
  303. (field-end))
  304. ;;;###autoload
  305. (defun bm-select-book ()
  306. "Queries user to select a new book and chapter for the current
  307. `bible-mode' buffer."
  308. (interactive)
  309. (let* ((completion-ignore-case t)
  310. (book-data (assoc (completing-read "Book: " bm-books nil t) bm-books))
  311. (chapter (string-to-number (completing-read "Chapter: " (bm-list-number-range 1 (cdr book-data)) nil t))))
  312. (setq-local bm-current-book book-data)
  313. (setq-local bm-current-book-name (car book-data))
  314. (setq-local bm-current-chapter chapter)
  315. (bm-display)))
  316. ;;;###autoload
  317. (defun bm-select-chapter ()
  318. "Queries user to select a new chapter for the current `bible-mode' buffer."
  319. (interactive)
  320. (let* ((book-chapters (cdr bm-current-book))
  321. (chapter (string-to-number (completing-read "Chapter: " (bm-list-number-range 1 book-chapters) nil t))))
  322. (when chapter
  323. (bm-set-location bm-current-book chapter))))
  324. ;;;###autoload
  325. (defun bm-select-module ()
  326. "Queries user to select a new reading module for the current `bible-mode' buffer."
  327. (interactive)
  328. (let ((module (completing-read "Module: " bm-modules)))
  329. (setq-local bm-module module)
  330. (bm-display)))
  331. ;;;###autoload
  332. (defun bm-toggle-word-study()
  333. "Toggles the inclusion of word study for the active `bible-mode' buffer."
  334. (interactive)
  335. (setq bm-word-study-enabled (not bm-word-study-enabled))
  336. (if (equal major-mode 'bible-search-mode)
  337. (bm-display-search bm-search-query bm-search-mode bm-module)
  338. (bm-display)))
  339. ;;;###autoload
  340. (defun bm-split-display ()
  341. "Copies the active `bible-mode' buffer into a new buffer in another window."
  342. (interactive)
  343. (split-window-right)
  344. (balance-windows)
  345. (other-window 1)
  346. (bible-open bm-current-book-name bm-current-chapter))
  347. ;;;###autoload
  348. (defun bible-search (query)
  349. "Prompts the user for a Bible search query: word or phrase and type of
  350. search: either `lucene' or `phrase'. `lucene' mode requires an index
  351. to be built using the `mkfastmod' program. `lucene' is the default
  352. search."
  353. (interactive "sBible Search: ")
  354. (when (> (length query) 0)
  355. (let* ((searchmode (completing-read "Search Mode: " '("lucene" "phrase") nil t "lucene")))
  356. (bm-open-search query searchmode))))
  357. ;;;###autoload
  358. (defun bible-search-mode-follow-verse ()
  359. "Follows the hovered verse in a `bible-search-mode' buffer,
  360. creating a new `bible-mode' buffer positioned at the specified verse."
  361. (interactive)
  362. (let* ((text (thing-at-point 'line t))
  363. book
  364. chapter
  365. verse)
  366. (string-match ".+ [0-9]?[0-9]?[0-9]?:[0-9]?[0-9]?[0-9]?:" text)
  367. (setq text (match-string 0 text))
  368. (string-match " [0-9]?[0-9]?[0-9]?:" text)
  369. (setq chapter (replace-regexp-in-string "[^0-9]" "" (match-string 0 text)))
  370. (string-match ":[0-9]?[0-9]?[0-9]?" text)
  371. (setq verse (replace-regexp-in-string "[^0-9]" "" (match-string 0 text)))
  372. (setq book (replace-regexp-in-string "[ ][0-9]?[0-9]?[0-9]?:[0-9]?[0-9]?[0-9]?:$" "" text))
  373. (bible-open book (string-to-number chapter) (string-to-number verse))))
  374. ;;;###autoload
  375. (defun bible-term-hebrew (term)
  376. "Queries user for a Strong's Hebrew Lexicon term."
  377. (interactive "sTerm: ")
  378. (bm-open-term-hebrew term))
  379. ;;;###autoload
  380. (defun bible-term-greek (term)
  381. "Queries user for a Strong's Greek Lexicon term."
  382. (interactive "sTerm: ")
  383. (bm-open-term-greek term))
  384. ;; (defun bible-term-morph (term morph-type)
  385. ;; "Queries user for a Strong's Greek Lexicon term."
  386. ;; (interactive "sTerm: ")
  387. ;; ;;; (message "bible-term-morph: %s:%s" term morph-type)
  388. ;; ;;; (bm-open-term-greek term)
  389. ;; )
  390. ;;;###autoload
  391. (defun bible-insert ()
  392. "Queries user to select a verse for insertion into the current buffer."
  393. (interactive)
  394. (let* ((completion-ignore-case t)
  395. (book-data (assoc (completing-read "Book: " bm-books nil t) bm-books))
  396. (chapter (when book-data (completing-read "Chapter: " (bm-list-number-range 1 (cdr book-data)) nil t)))
  397. (verse (when chapter (read-from-minibuffer "Verse: "))))
  398. (when verse
  399. (insert (string-trim
  400. (replace-regexp-in-string
  401. (regexp-opt `(,(concat "(" bm-module ")")))
  402. ""
  403. (bm-exec-diatheke (concat (car book-data) " " chapter ":" verse) nil "plain")))))))
  404. ;;;;; Support
  405. ;;;
  406. ;;; XXX I've magled this in an ad-hoc manner. It needs to be
  407. ;;; re-written so it is clearer (and correct, for that matter).
  408. (defun bm-exec-diatheke (query &optional filter format searchtype module)
  409. "Executes `diatheke' with specified query options, returning the output."
  410. (let ((module (or module bm-module)))
  411. (with-temp-buffer
  412. (let ((args (list "diatheke" nil (current-buffer) t "-b" module)))
  413. (if filter
  414. (setq filter (concat filter " avmws"))
  415. (setq filter "avmws"))
  416. (when filter (setq args (append args (list "-o" filter))))
  417. (when searchtype
  418. (setq args (append args (list "-s" (pcase searchtype ("lucene" "lucene") ("phrase" "phrase"))))))
  419. (setq args (append args (list "-f" (pcase format ("plain" "plain") (_ "internal")) "-k" query)))
  420. (message "%s" args)
  421. (apply 'call-process args))
  422. (buffer-string))))
  423. (defvar-local bm-chapter-title nil
  424. "Document text at start of chapter, mostly in Psalms,
  425. like `Of David' or the like.")
  426. ;;;
  427. ;;; Greek and Hebrew lexicon and morphology tooltip rendering.
  428. ;;;
  429. ;;; Hash tables for STRONGS definitions.
  430. (defvar greek-hash (make-hash-table :test 'equal))
  431. (defvar hebrew-hash (make-hash-table :test 'equal))
  432. ;;; Hash tables for morphologies. Three at present.
  433. (defvar robinson-hash (make-hash-table :test 'equal))
  434. (defvar packard-hash (make-hash-table :test 'equal))
  435. (defvar oshm-hash (make-hash-table :test 'equal))
  436. ;;; Use HTMLHREF format with diatheke, post-process to render html.
  437. (defun bm-morph-query (query module)
  438. "Executes `diatheke' to do morph query, renders HTML, returns string.
  439. Does some tweaking specific to morphology."
  440. (with-temp-buffer
  441. (let ((args (list "diatheke" nil (current-buffer) t "-b" module "-o" "m" "-f" "HTMLHREF" "-k" query)))
  442. (apply 'call-process args)
  443. (shr-render-region (point-min) (point-max))
  444. (replace-regexp-in-string
  445. "\n:" "" ; This makes the Packard morphology display look better.
  446. (replace-regexp-in-string
  447. "Part of Speech" "" ; This helps the Robinson display look better.
  448. (substring (buffer-string) (+ (length query) 1)) ; This tries to get rid of unnecessary query identifier.
  449. )))))
  450. ;;; Use "plain" format with diatheke.
  451. (defun bm-lex-query (query module)
  452. "Executes `diatheke' for query, plain format, returns string."
  453. ;; Get rid of query ID at front of string: ?????:
  454. (bm-exec-diatheke query nil "plain" nil module))
  455. (defun bm-lookup-strongs-greek (window object pos)
  456. "Look up Greek lexical data for object at point. If not found in hash table,
  457. get it from sword database, stash in hash table, and return data.
  458. Note: compiler warns about unused argument `window'."
  459. (let* ((query (get-text-property pos 'strong object))
  460. (match (string-match "[0-9]+" query)) ; Compiler warns about match.
  461. (lookup-key (match-string 0 query)))
  462. (and lookup-key
  463. (or (gethash lookup-key greek-hash)
  464. (puthash lookup-key (bm-lex-query lookup-key bm-short-greek-lexicon) greek-hash)))))
  465. (defun bm-hebrew-lex-query (query module)
  466. "Executes `diatheke' to do hebrew query, renders HTML, returns string."
  467. (with-temp-buffer
  468. (let ((args (list "diatheke" nil (current-buffer) t "-b" module "-o" "m" "-f" "HTMLHREF" "-k" query)))
  469. (apply 'call-process args)
  470. (shr-render-region (point-min) (point-max)))))
  471. (defun bm-lookup-strongs-hebrew (window object pos)
  472. "Look up Hebrew lexical data for object at point. If not found in hash table,
  473. get it from sword database, stash in hash table, and return data.
  474. Note: compiler warns about unused `window' argument."
  475. (let* ((query (get-text-property pos 'strong object))
  476. (match (string-match "[0-9]+" query)) ; Compiler warns about match.
  477. (lookup-key (match-string 0 query)))
  478. (and lookup-key
  479. (or (gethash lookup-key hebrew-hash)
  480. ;; Use PLAIN format for lookup. XXX directionality problems.
  481. (puthash lookup-key (bm-lex-query lookup-key bm-short-hebrew-lexicon) hebrew-hash)))))
  482. (defun bm-morph-database-lookup (query database hash)
  483. (or (gethash query hash)
  484. (puthash query (bm-morph-query query database) hash)))
  485. (defun bm-show-lex-morph (window object pos)
  486. (let* ((lex-morph-text "")
  487. (lex (get-text-property pos 'strong object))
  488. (lex-text
  489. (cond ((string-match "strong:G" lex)
  490. (bm-lookup-strongs-greek window object pos))
  491. ((string-match "strong:H" lex)
  492. (bm-lookup-strongs-hebrew window object pos)))))
  493. (let* ((morph (get-text-property pos 'morph object))
  494. (morph-text
  495. (cond ((null morph) "")
  496. ((string-match "robinson:" morph)
  497. (bm-morph-database-lookup (replace-regexp-in-string "robinson:" "" morph) "Robinson" robinson-hash))
  498. ((string-match "packard:" morph)
  499. (bm-morph-database-lookup (replace-regexp-in-string "packard:" "" morph) "Packard" packard-hash))
  500. ((string-match "oshm:" morph)
  501. (bm-morph-database-lookup (replace-regexp-in-string "oshm:" "" morph) "OSHM" oshm-hash)))))
  502. (when lex-text
  503. (setq lex-morph-text lex-text))
  504. (when morph-text
  505. (setq lex-morph-text (concat lex-morph-text "\n" morph-text)))
  506. ;; This prevents weird substitutions in the tooltip.
  507. (propertize lex-morph-text 'help-echo-inhibit-substitution t)
  508. lex-morph-text)))
  509. (defun bm-process-word (item iproperties)
  510. "Word study. Add tooltips for definitions and morphologyl.
  511. Insert lemmas in buffer. Must be done after item is inserted in buffer."
  512. (let ((word (dom-text item))
  513. (morph (dom-attr item 'morph))
  514. (savlm (dom-attr item 'savlm))
  515. (divinename (dom-by-tag item 'divinename)))
  516. (insert word)
  517. (let ((refstart (- (point) (length word)))
  518. (refend (point)))
  519. ;; Red letter
  520. (when (plist-get iproperties 'jesus)
  521. (add-face-text-property refstart refend '(:foreground "red")))
  522. ;; Special case this. XXX Some modules do this differently.
  523. (when divinename
  524. (insert "LORD")
  525. (let* ((refstart (- (point) (length "LORD")))
  526. (refend (point))
  527. (strongs (dom-attr item 'savlm)))
  528. (string-match "strong:H.*" strongs)
  529. (let ((strongs-ref (match-string 0 strongs)))
  530. (add-face-text-property refstart refend 'bold)
  531. (put-text-property refstart refend 'keymap bm-hebrew-keymap)
  532. (put-text-property refstart refend 'help-echo 'bm-show-lex-morph)
  533. (put-text-property refstart refend 'strong strongs-ref))))
  534. ;; lexical definitions
  535. (when savlm
  536. (let ((matched nil))
  537. (cond ((string-match "strong:G.*" savlm) ; Greek
  538. (setq matched (match-string 0 savlm))
  539. (put-text-property refstart refend 'keymap bm-greek-keymap))
  540. ((string-match "strong:H.*" savlm) ; Hebrew
  541. (setq matched (match-string 0 savlm))
  542. (put-text-property refstart refend 'keymap bm-hebrew-keymap)))
  543. ;; Add help-echo, strongs reference for tooltips if match.
  544. (when matched
  545. (setq-local bm-has-strongs t)
  546. (put-text-property refstart refend 'help-echo 'bm-show-lex-morph)
  547. (put-text-property refstart refend 'strong matched))))
  548. ;; morphology
  549. (when morph
  550. (let ((matched nil))
  551. (cond ((string-match "robinson:.*" morph) ; Robinson Greek morphology
  552. (setq matched (match-string 0 morph)))
  553. ((string-match "packard:.*" morph) ; Packard Greek morphology --- LXX seems to use this
  554. (setq matched (match-string 0 morph)))
  555. ((string-match "oshm:.*" morph) ; OSHM Hebrew morphology
  556. (setq matched (match-string 0 morph)))
  557. (t nil
  558. ;;(message "Unknown morphology %s" morph)
  559. ))
  560. (when matched
  561. (setq-local bm-has-morphology t)
  562. (put-text-property refstart refend 'morph matched)
  563. (put-text-property refstart refend 'help-echo 'bm-show-lex-morph))))
  564. ;; Insert lemma into buffer. Lemma tag will be part of savlm item.
  565. (when (and bm-word-study-enabled savlm (string-match "lemma.*:.*" savlm))
  566. (dolist (word (split-string (match-string 0 savlm) " "))
  567. (setq word (replace-regexp-in-string "[.:a-zA-Z0-9]+" "" word))
  568. (insert " " word)
  569. (let ((refstart (- (point) 1 (length word)))
  570. (refend (point)))
  571. (add-face-text-property refstart refend '(:foreground "blue"))
  572. (put-text-property refstart refend 'keymap bm-lemma-keymap)))))))
  573. (defun bm-insert-domnode-recursive (node &optional iproperties notitle)
  574. "Recursively parses a domnode from `libxml-parse-html-region's usage on text
  575. produced by `bm-exec-diatheke'. Outputs text to active buffer
  576. with properties.
  577. In processing subnodes, each case will prepend a space if it needs it."
  578. (if (and bm-red-letter-enabled (equal (dom-attr node 'who) "Jesus"))
  579. ;; For red-letter display.
  580. (setq iproperties (plist-put iproperties 'jesus t))
  581. (setq iproperties nil))
  582. ;; (when (equal (dom-tag node) 'title)
  583. ;; ;; Space one line down so there's room for the title at the beginning.
  584. ;; (insert "\n"))
  585. (dolist (subnode (dom-children node))
  586. (cond ((null subnode) nil)
  587. ((stringp subnode)
  588. ;; Insert the subnode. Highlight the verse references.
  589. (insert subnode)
  590. ;; XXX this is still not quite right
  591. (let ((verse-start (string-match ".+?:[0-9]?[0-9]?[0-9]?:" subnode)))
  592. (when verse-start
  593. (let* ((verse-match (string-trim (match-string 0 subnode)))
  594. (verse-start-text (string-trim-left (substring subnode verse-start (length subnode))))
  595. (subnode (concat (substring subnode 0 verse-start) verse-start-text))
  596. (start (- (point) 1 (length (string-trim-right verse-start-text)))))
  597. (add-face-text-property start (+ start (length (string-trim-right verse-match))) '(:foreground "purple"))))))
  598. ((eq (dom-tag subnode) 'title)
  599. (if notitle nil
  600. (progn
  601. (setq bm-chapter-title subnode))))
  602. ((eq (dom-tag subnode) 'body) (bm-insert-domnode-recursive subnode iproperties notitle))
  603. ((eq (dom-tag subnode) 'seg) ; NASB Module uses this to indicate OT quotations (and others?).
  604. (bm-insert-domnode-recursive subnode iproperties notitle))
  605. ((eq (dom-tag subnode) 'q) (bm-insert-domnode-recursive subnode iproperties notitle))
  606. ((eq (dom-tag subnode) 'p) (bm-insert-domnode-recursive subnode iproperties notitle))
  607. ((eq (dom-tag subnode) 'w) (insert " ") (bm-process-word subnode iproperties))
  608. ((eq (dom-tag subnode) 'milestone) (insert "\n"))
  609. ((eq (dom-tag subnode) 'transchange) ; Word inserted by translation, not in original, give visual indication.
  610. (let ((word (dom-text subnode)))
  611. (insert " " word)
  612. (add-face-text-property (- (point) (length word)) (point) '(:foreground "gray50")))))))
  613. (defvar bm-debugme nil)
  614. (setf bm-debugme nil)
  615. (defun bm-display (&optional verse)
  616. "Renders text for `bible-mode'"
  617. ;; Clear buffer and insert the result of calling bm-exec-diatheke.
  618. (setq buffer-read-only nil)
  619. (erase-buffer)
  620. (setq bm-chapter-title nil
  621. bm-has-strongs nil
  622. bm-has-morphology nil)
  623. (insert (bm-exec-diatheke (concat bm-current-book-name ":" (number-to-string bm-current-chapter))))
  624. ;; Parse the xml in the buffer into a DOM tree.
  625. (let ((html-dom-tree (libxml-parse-html-region (point-min) (point-max))))
  626. ;; Render the DOM tree into the buffer.
  627. (if (not bm-debugme)
  628. (progn
  629. (erase-buffer)
  630. ;; Looking for the "body" tag in the DOM node.
  631. (bm-insert-domnode-recursive (dom-by-tag html-dom-tree 'body) nil nil)
  632. (goto-char (point-min)))
  633. ;;; (shr-render-region (point-min) (point-max))
  634. ))
  635. ;; Remove the module name from the buffer.
  636. (while (search-forward (concat "(" bm-module ")") nil t)
  637. (replace-match ""))
  638. ;; Set the mode line of the biffer.
  639. (setq mode-name (concat "Bible ("
  640. bm-module
  641. (when bm-has-strongs " Lex")
  642. (when bm-has-morphology " Morph")
  643. ")"))
  644. ;; Deal with chapter titles (i.e. in Psalms)
  645. ;; N.B. This won't change a title inside a chapter, and so it
  646. ;; doesn't work with Psalm 119 where the acrostic letters get
  647. ;; printed as "titles".
  648. (when bm-chapter-title ; This gets set in bm-insert-domnode-recursive.
  649. (goto-char (point-min))
  650. (let ((title-text (dom-texts bm-chapter-title))
  651. (refstart (point-min))
  652. refend)
  653. ;; Insert and make bold the title.
  654. (when (string-or-null-p title-text)
  655. (insert title-text "\n")
  656. (setq refend (point))
  657. (put-text-property refstart refend 'face 'bold))))
  658. (setq buffer-read-only t)
  659. (goto-char (point-min))
  660. ;; If optional verse specification go to that verse.
  661. (when verse
  662. (goto-char (string-match (regexp-opt `(,(concat ":" (number-to-string verse) ":"))) (buffer-string)))
  663. (beginning-of-line)))
  664. (defun bm-list-biblical-modules ()
  665. "Returns a list of accessible Biblical Text modules."
  666. (let ((text (bm-exec-diatheke "modulelist" nil nil nil "system"))
  667. modules)
  668. (catch 'done
  669. (dolist (line (split-string text "\n"))
  670. (when (equal line "Commentaries:")
  671. (throw 'done nil))
  672. (when (not (equal "Biblical Texts:" line))
  673. (push (split-string line " : ") modules))))
  674. modules))
  675. (defun bm-pick-module ()
  676. (interactive)
  677. (message "Picking module at %s" (point))
  678. (let ((item (get-text-property (point) 'module)))
  679. (setq-default bm-module item)
  680. (bible-open)))
  681. (defconst bm-module-map (make-keymap))
  682. (define-key bm-module-map [mouse-1] 'bm-pick-module)
  683. (defun bm-display-available-modules ()
  684. (interactive)
  685. (let ((buf (get-buffer-create "Modules"))
  686. (mods (bm-list-biblical-modules)))
  687. (set-buffer buf)
  688. (setq buffer-read-only nil)
  689. (erase-buffer)
  690. (dolist (mod mods)
  691. (insert
  692. (propertize (car mod)
  693. 'face 'bold
  694. 'module (car mod)
  695. 'help-echo (concat "Select " (car mod))
  696. 'keymap bm-module-map)
  697. "\t\t"
  698. (format "%s\n" (cadr mod))))
  699. (setq buffer-read-only t)
  700. (pop-to-buffer buf)))
  701. ;;;;; Bible Searching
  702. (defun bm-open-search (query searchmode)
  703. "Opens a search buffer of QUERY using SEARCHMODE."
  704. (let ((buf (get-buffer-create (concat "*bible-search-" (downcase bm-module) "-" query "*"))))
  705. (set-buffer buf)
  706. (bible-search-mode)
  707. (bm-display-search query searchmode bm-module)
  708. (pop-to-buffer buf nil t)))
  709. (defun bm-display-search (query searchmode mod)
  710. "Renders results of search QUERY from SEARHCMODE"
  711. (setq buffer-read-only nil)
  712. (erase-buffer)
  713. (let* ((result (string-trim (replace-regexp-in-string
  714. "Entries .+?--" ""
  715. (bm-exec-diatheke query nil "plain" searchmode mod))))
  716. (match 0)
  717. (matchstr "")
  718. (verses "")
  719. fullverses)
  720. (if (equal result (concat "none (" bm-module ")"))
  721. (insert "No results found." (when (equal searchmode "lucene") " Verify index has been build with mkfastmod."))
  722. (progn
  723. (while match
  724. (setq match (string-match ".+?:[0-9]?[0-9]?" result (+ match (length matchstr)))
  725. matchstr (match-string 0 result))
  726. (when match
  727. (setq verses (concat verses (replace-regexp-in-string ".+; " "" matchstr) ";"))))
  728. (setq match 0)
  729. (setq fullverses (bm-exec-diatheke verses))
  730. (insert fullverses)
  731. (sort-lines nil (point-min) (point-max))
  732. (let* ((html-dom-tree (libxml-parse-html-region (point-min) (point-max))))
  733. (erase-buffer)
  734. (bm-insert-domnode-recursive (dom-by-tag html-dom-tree 'body) nil nil)
  735. (goto-char (point-min))
  736. (while (search-forward (concat "(" bm-module ")") nil t)
  737. (replace-match "")))))
  738. (setq mode-name (concat "Bible Search (" bm-module ")"))
  739. (setq buffer-read-only t)
  740. (setq-local bm-search-query query)
  741. (setq-local bm-search-mode searchmode)
  742. (goto-char (point-min))))
  743. ;;;;; Terms
  744. ;;(defun bm-display-morphology (morph)
  745. ;; ;; xxx Do something here?
  746. ;; )
  747. (defun bm-display-term (termtype)
  748. (cl-do* ((text (buffer-string))
  749. (match (string-match "[0-9]+" text) (string-match "[0-9]+" text (match-end 0))))
  750. ((not match))
  751. (let* ((matchstr (match-string 0 text))
  752. (matchstrlen (length matchstr))
  753. (refstart (+ match 1))
  754. (refend (+ match 1 matchstrlen)))
  755. ;; This enables clicking on the Strong's numbers inside the term display.
  756. (add-face-text-property refstart refend `(:foreground "blue"))
  757. (cond ((eq termtype 'hebrew)
  758. (put-text-property refstart refend 'strong (concat "strong:H" matchstr))
  759. (put-text-property refstart refend 'keymap bm-hebrew-keymap))
  760. ((eq termtype 'greek)
  761. (put-text-property refstart refend 'strong (concat "strong:G" matchstr))
  762. (put-text-property refstart refend 'keymap bm-greek-keymap)))))
  763. (goto-char (point-min))
  764. (while (search-forward (concat "(" bm-module ")") nil t)
  765. (replace-match ""))
  766. (while (search-forward "()" nil t)
  767. (replace-match ""))
  768. (goto-char (point-min))
  769. (setq buffer-read-only t))
  770. (defun bm-open-term-hebrew (term)
  771. "Opens a buffer of the Strong's Hebrew TERM's definition"
  772. (let ((buf (get-buffer-create (concat "*bible-term-hebrew-" term "*"))))
  773. (set-buffer buf)
  774. (bible-term-hebrew-mode)
  775. (bm-display-term-hebrew term)
  776. (pop-to-buffer buf nil t)
  777. (fit-window-to-buffer)))
  778. (defun bm-open-term-greek (term)
  779. "Opens a buffer of the Strong's Greek TERM's definition"
  780. (let ((buf (get-buffer-create (concat "*bible-term-greek-" term "*"))))
  781. (set-buffer buf)
  782. (bible-term-greek-mode)
  783. (bm-display-term-greek term)
  784. (pop-to-buffer buf nil t)
  785. (fit-window-to-buffer)))
  786. ;;;
  787. ;;; Note: Hebrew display of terms is backwards; set bidi direction to
  788. ;;; 'left-to-right.
  789. (defun bm-display-term-hebrew (term)
  790. "Render the definition of the Strong's Hebrew TERM. Use
  791. bidi-paragraph-direction so the English text will render
  792. left-to-right. XXX Why doesn't this work for the tooltips?"
  793. (setq buffer-read-only nil)
  794. (erase-buffer)
  795. (insert (replace-regexp-in-string
  796. (regexp-opt `(,bm-hebrew-lexicon))
  797. ""
  798. (bm-exec-diatheke term nil "plain" nil bm-hebrew-lexicon)
  799. nil nil nil 7
  800. ))
  801. (bm-display-term 'hebrew)
  802. (setq bidi-paragraph-direction 'left-to-right))
  803. (defun bm-display-term-greek (term)
  804. "Render the definition of the Strong's Greek TERM."
  805. (setq buffer-read-only nil)
  806. (erase-buffer)
  807. (insert (replace-regexp-in-string
  808. (regexp-opt `(,bm-greek-lexicon))
  809. ""
  810. (bm-exec-diatheke term nil "plain" nil bm-greek-lexicon)
  811. nil nil nil 7
  812. ))
  813. ;; (insert "\n")
  814. (bm-display-term 'greek))
  815. (defun bm-set-location (book chapter &optional verse)
  816. "Sets the global chapter of the active `bible-mode' buffer."
  817. (setq-local bm-current-book book)
  818. (setq-local bm-current-book-name (car book))
  819. (setq-local bm-current-chapter chapter)
  820. (bm-display verse))
  821. ;;;;; Utilities
  822. (defun bm-list-number-range (min max &optional prefix)
  823. "Returns a list containing entries for each integer between min and max.
  824. Used in tandem with `completing-read' for chapter selection."
  825. (let ((range-list nil))
  826. (dotimes (num (1+ max))
  827. (when (>= num min)
  828. (push (cons (concat prefix (number-to-string num)) num) range-list)))
  829. (nreverse range-list)))
  830. ;;; Provides
  831. (provide 'bible-mode)
  832. ;; Local Variables:
  833. ;; read-symbol-shorthands: (("bm-" . "bible-mode-"))
  834. ;; End: