bible.el 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. ;;; bible.el --- A Bible browsing application -*- lexical-binding: t; mode: EMACS-LISP; indent-tabs-mode: nil -*-
  2. ;; Copyright (c) 2025-2026 Fred Gilham
  3. ;; Author: Fred Gilham <fmgilham@gmail.com>
  4. ;; Version: 1.2.0
  5. ;; Keywords: files, text, hypermedia
  6. ;; Package-Requires: ((emacs "29.1") cl-lib dom shr)
  7. ;; URL: https://gitbot.homedns.org/fred/bible
  8. ;; This file is not part of GNU Emacs.
  9. ;; bible.el is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 3, or (at your option)
  12. ;; any later version.
  13. ;; bible.el is distributed in the hope that it will be useful, but
  14. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. ;; General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with this file; see the file LICENSE. If not, see
  19. ;; <https://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; Forked and extensively modified from package by Zacalot
  22. ;; Url: https://github.com/Zacalot/bible-mode
  23. ;; This package uses the `diatheke' program to browse and search
  24. ;; Biblical texts provided by the Sword project (https://crosswire.org).
  25. ;; Word study is also supported.
  26. ;;;; Usage
  27. ;; Use M-x `bible-open' to open a Bible buffer.
  28. ;; Use C-h f `bible' to see available keybindings.
  29. ;; The program also installs a Bible menu with keybindings and other
  30. ;; commands.
  31. ;; You may customize `bible-text' to set a default browsing module, as
  32. ;; well as `bible-word-study-enabled' to enable word study by default.
  33. ;; NB: Currently this just shows the lemmas in the original language
  34. ;; if present. Tooltips will display whenever there are strongs
  35. ;; numbers in the module.
  36. ;;;; Design
  37. ;; The idea here is to use the diatheke program to lookup text from
  38. ;; modules (biblical texts), then insert this text into buffers. The
  39. ;; main bible display uses diatheke's internal XML format. The whole
  40. ;; buffer gets parsed by `libxml-parse-html-region' to create a dom
  41. ;; tree. This gets parsed by `bible--insert-domnode-recursive' to render
  42. ;; the text into reading format.
  43. ;; The text is then decorated using information from the dom format as
  44. ;; necessary along with regular expressions to identify the verse
  45. ;; references. This is for red letters, purple highlighting of the
  46. ;; verse numbers, bold face of the divine name in the OT and so on.
  47. ;; If Strongs tags and/or morphological tags are present, they are
  48. ;; looked up in appropriate lexical and morphological modules and used
  49. ;; to add tooltips to the text so that hovering over words will bring
  50. ;; up a tooltip with information about the word. Clicking on a word
  51. ;; with lexical information will display that information in a "term"
  52. ;; buffer.
  53. ;;; Code:
  54. ;;;; Environment stuff
  55. ;; Turn off modes because we are greedy for pixels....
  56. (tool-bar-mode -1)
  57. (scroll-bar-mode -1)
  58. ;; eldoc isn't meaningful in this program, and this saves space in the
  59. ;; mode line.
  60. (global-eldoc-mode -1)
  61. ;;;; Requirements
  62. (require 'cl-lib)
  63. (require 'dom)
  64. (require 'shr)
  65. (require 'menu+ nil t) ; If you have it, it looks nice.
  66. ;;;; Aliases for obsolete functions
  67. ;; dom-text and dom-texts declared obsolescent in Emacs 31. Check for
  68. ;; new function, retain backward compatibility.
  69. ;; Note that the following is the simplest way I found to avoid compile warnings.
  70. (defalias 'bible-dom-text (if (fboundp 'dom-inner-text) 'dom-inner-text 'dom-text))
  71. (defalias 'bible-dom-texts (if (fboundp 'dom-inner-texts) 'dom-inner-text 'dom-texts))
  72. ;;;; Customization Variables
  73. (defgroup bible nil
  74. "Settings for `bible'."
  75. :group 'tools
  76. :link '(url-link "https://gitbot.homedns.org/fred/bible-mode"))
  77. (defcustom bible-text
  78. "KJV"
  79. "Customize default bible text module for Diatheke to query.
  80. \(For full list of installed modules, run `diatheke -b system -l bibliography'\)"
  81. :type '(choice (const :tag "None" nil)
  82. (string :tag "Bible text (e.g. \"KJV\")"))
  83. :local nil
  84. :group 'bible)
  85. (defcustom bible-commentary
  86. "Clarke"
  87. "Customize default commentary module for Diatheke to query.
  88. \(For full list of installed modules, run `diatheke -b system -l bibliography'\)"
  89. :type '(choice (const :tag "None" nil)
  90. (string :tag "Commentary (e.g. \"Clarke\")"))
  91. :local nil
  92. :group 'bible)
  93. ;; TODO: Not implememted yet (FMG 5-Mar-2026)
  94. (defcustom bible-font
  95. "Ezra SIL"
  96. "Default font for bible (not yet implemented)."
  97. :type '(string :tag "Font family name (e.g. \"Ezra SIL\")")
  98. :local t
  99. :group 'bible)
  100. (defcustom bible-sword-query
  101. "diatheke"
  102. "Specify program used to query sword modules.
  103. Must be some version of the sword library's diatheke program."
  104. :type '(string :tag "Sword library query executable (e.g. \"/usr/local/bin/diatheke\").")
  105. :local nil
  106. :group 'bible)
  107. (defcustom bible-greek-lexicon
  108. ;; AbbottSmithStrongs now has both links to lemmas and definitions
  109. ;; keyed by lemma. So we only need the AbbottSmithStrongs lexicon
  110. ;; and not the AbbottSmith lexicon.
  111. "AbbottSmithStrongs"
  112. "Lexicon used for displaying definitions of Greek words using Strong's codes."
  113. :type '(string :tag "Lexicon module (e.g. \"StrongsRealGreek\").")
  114. :local nil
  115. :group 'bible)
  116. (defcustom bible-use-index-for-lexicon t
  117. "Some lexicons are accessed by lemmas rather than Strong's numbers.
  118. Use an index to look up lemmas from Strong's numbers so these lexicons
  119. can be used."
  120. :type 'boolean
  121. :local nil
  122. :group 'bible)
  123. (defcustom bible-lexicon-index "AbbottSmithStrongs"
  124. "A module that consists of an index mapping Strong's numbers to Greek lemmas.
  125. The code is written to use the entries in AbbottSmithStrongs
  126. which are of the form
  127. <strong's number>: @LINK <greek lemma>"
  128. :type '(string :tag "Lexicon index.")
  129. :local nil
  130. :group 'bible)
  131. (defcustom bible-greek-lexicon-short
  132. "StrongsRealGreek"
  133. "Lexicon used for displaying definitions of Greek words in tooltips."
  134. :type '(string :tag "Lexicon module (e.g. \"StrongsRealGreek\").")
  135. :local nil
  136. :group 'bible)
  137. ;; HACK: The Hebrew lexicons differ on whether they accept keys of the
  138. ;; form `Hnnnn' or `nnnn'. The code does not yet handle this
  139. ;; correctly, so stick with the following. (FMG 5-Mar-2026)
  140. (defcustom bible-hebrew-lexicon
  141. "BDBGlosses_Strongs" ; This seems to work
  142. "Specify Lexicon used to display definitions of Hebrew words.
  143. Note that changing this may require changing some code.
  144. See `bible--display-lemma-hebrew'."
  145. :type '(string :tag "Lexicon module (e.g. \"BDBGlosses_Strongs\")")
  146. :local nil
  147. :group 'bible)
  148. (defcustom bible-hebrew-lexicon-short
  149. "StrongsRealHebrew"
  150. ;; "StrongsHebrew"
  151. "Lexicon used for displaying definitions of Hebrew words in tooltips."
  152. :type '(string :tag "Lexicon module (e.g. \"StrongsRealHebrew\")")
  153. :local nil
  154. :group 'bible)
  155. (defcustom bible-word-study-enabled
  156. nil
  157. "Display original language Lemma words if present in module.
  158. \(KJV New Testament has this.\)"
  159. :type 'boolean
  160. :local t
  161. :group 'bible)
  162. (defcustom bible-red-letter-enabled
  163. t
  164. "Display words of Jesus in red when module has that information."
  165. :type 'boolean
  166. :local t
  167. :group 'bible)
  168. (defcustom bible-show-diatheke-exec
  169. nil
  170. "Show the arguments by which diatheke is executed (mostly for debugging)."
  171. :type 'boolean
  172. :local nil
  173. :group 'bible)
  174. ;;;; Mode line formats for different kinds of buffers.
  175. (defvar bible-mode-line-format
  176. '("%e" mode-line-front-space
  177. mode-line-frame-identification mode-line-buffer-identification " "
  178. bible-text
  179. " "
  180. bible--current-book-name
  181. " " (:eval (number-to-string bible--current-chapter))
  182. " " (:eval (if bible--synced-p "Sync" ""))
  183. (:eval (when bible-search-range (concat " <" bible-search-range ">")))
  184. " " mode-line-modes mode-line-misc-info
  185. mode-line-end-spaces)
  186. "Mode line format for bible buffers.")
  187. (defvar bible-search-mode-line-format
  188. '("%e" mode-line-front-space
  189. mode-line-frame-identification mode-line-buffer-identification " "
  190. bible-search-text-this-query " " bible-search-word-this-query " "
  191. (:eval (when bible-search-range-this-query (concat "<" bible-search-range-this-query "> ")))
  192. (:eval (number-to-string bible-search-matches)) " matches"
  193. " " mode-line-modes mode-line-misc-info
  194. mode-line-end-spaces)
  195. "Mode line format for bible search buffers.")
  196. (defvar bible-term-mode-line-format
  197. '("%e" mode-line-front-space
  198. mode-line-frame-identification mode-line-buffer-identification " "
  199. ;; bible-term-language
  200. ;; " " (:eval bible-term-lemma)
  201. " " mode-line-modes mode-line-misc-info
  202. mode-line-end-spaces)
  203. "Mode line format for bible search buffers.")
  204. ;;;; Modes
  205. (define-derived-mode bible special-mode "Bible"
  206. "Mode for reading the Bible.
  207. \\{bible-map}"
  208. (buffer-disable-undo)
  209. (font-lock-mode t)
  210. (use-local-map bible-map)
  211. (setq-local mode-line-format bible-mode-line-format)
  212. (setq buffer-read-only t)
  213. (visual-line-mode t))
  214. (define-derived-mode bible-search-mode special-mode "Bible Search"
  215. "Mode for performing Bible searches.
  216. \\{bible-search-mode-map}"
  217. (buffer-disable-undo)
  218. (font-lock-mode t)
  219. (use-local-map bible-search-mode-map)
  220. (setq-local mode-line-format bible-search-mode-line-format)
  221. (setq buffer-read-only t)
  222. (visual-line-mode t))
  223. (define-derived-mode bible-term-mode special-mode "Bible Term"
  224. "Mode for researching terms in the Bible.
  225. \\{bible-term-mode-map}"
  226. (buffer-disable-undo)
  227. (font-lock-mode t)
  228. (use-local-map bible-term-mode-map)
  229. (setq-local mode-line-format bible-term-mode-line-format)
  230. (setq buffer-read-only t)
  231. (visual-line-mode t))
  232. (define-derived-mode bible-term-hebrew-mode bible-term-mode "Bible Term"
  233. "Mode for researching Hebrew terms in the Bible.
  234. \\{bible-term-hebrew-mode-map}"
  235. (setq-local bible-term-language "Hebrew"))
  236. (define-derived-mode bible-term-greek-mode bible-term-mode "Bible Term"
  237. "Mode for researching Greek terms in the Bible.
  238. \\{bible-term-greek-mode-map}"
  239. (setq-local bible-term-language "Greek"))
  240. (define-derived-mode bible-text-select-mode special-mode "Select Module"
  241. (buffer-disable-undo)
  242. (font-lock-mode t)
  243. (setq buffer-read-only t))
  244. ;;;; Keymaps
  245. ;; N.B. Bible Menu items appear in reverse order of their definition
  246. ;; below
  247. (defconst bible-map (make-sparse-keymap)
  248. "Keymap for bible.")
  249. (define-key bible-map [menu-bar bible]
  250. (cons "Bible" (make-sparse-keymap "Bible")))
  251. (define-key bible-map
  252. [menu-bar bible toggle-debug]
  253. '("Toggle debug-on-error" . toggle-debug-on-error))
  254. (define-key bible-map
  255. [menu-bar bible display-diatheke]
  256. '("Toggle diatheke display" . bible-toggle-display-diatheke))
  257. (define-key bible-map "d" 'bible-toggle-display-xml)
  258. (define-key bible-map
  259. [menu-bar bible display-xml]
  260. '("Toggle XML Display" . bible-toggle-display-xml))
  261. (define-key bible-map
  262. [menu-bar bible toggle-text-direction]
  263. '("Toggle text direction (for Hebrew display)" . bible-toggle-text-direction))
  264. (define-key bible-map
  265. [menu-bar bible toggle-tooltip-display]
  266. '("Toggle Tooltip Display" . bible-toggle-tooltips))
  267. (define-key bible-map
  268. [menu-bar bible sep]
  269. '(menu-item '"--"))
  270. ;;;;; Misc key bindings
  271. (define-key bible-map "T" 'bible-select-text)
  272. (define-key bible-map "C" 'bible-select-commentary)
  273. (define-key bible-map "w" 'bible-toggle-word-study)
  274. (define-key bible-map "l" 'bible-toggle-red-letter)
  275. (define-key bible-map "z" 'text-scale-adjust)
  276. (define-key bible-map
  277. [menu-bar bible zoom-text]
  278. '("Zoom Text" . text-scale-adjust))
  279. (define-key bible-map "x" 'bible-split-display)
  280. (define-key bible-map
  281. [menu-bar bible split-display]
  282. '("Split Display" . bible-split-display))
  283. (define-key bible-map "S" 'bible-toggle-buffer-sync)
  284. (define-key bible-map [menu-bar bible sync]
  285. '("Toggle Synchronize Buffer" . bible-toggle-buffer-sync))
  286. ;;;;; Navigation
  287. (define-key bible-map "p" 'bible-previous-chapter)
  288. (define-key bible-map
  289. [menu-bar bible previous-chapter]
  290. '("Previous Chapter" . bible-previous-chapter))
  291. (define-key bible-map "n" 'bible-next-chapter)
  292. (define-key bible-map
  293. [menu-bar bible next-chapter]
  294. '("Next Chapter" . bible-next-chapter))
  295. (define-key bible-map (kbd "TAB") 'bible-next-word)
  296. (define-key bible-map (kbd "M-<tab>") 'bible-previous-word)
  297. ;;;;; Direct jump
  298. (define-key bible-map "c" 'bible-select-chapter)
  299. (define-key bible-map
  300. [menu-bar bible select-chapter]
  301. '("Select Chapter" . bible-select-chapter))
  302. (define-key bible-map "b" 'bible-select-book)
  303. (define-key bible-map
  304. [menu-bar bible select-book]
  305. '("Select Book" . bible-select-book))
  306. (define-key bible-map
  307. [menu-bar bible sep]
  308. '(menu-item '"--"))
  309. ;; Deal with visual-line-mode navigation.
  310. (define-key bible-map "\C-n" 'next-logical-line)
  311. (define-key bible-map "\C-p" 'previous-logical-line)
  312. ;;;;; Search
  313. (define-key bible-map "/" 'bible-search)
  314. (define-key bible-map "s" 'bible-search)
  315. (define-key bible-map
  316. [menu-bar bible search]
  317. '("Search" . bible-search))
  318. (define-key bible-map "r" 'bible-set-search-range)
  319. (define-key bible-map
  320. [menu-bar bible range]
  321. '("Set Search Range" . bible-set-search-range))
  322. (define-key bible-map
  323. [menu-bar bible sepp]
  324. '(menu-item '"--"))
  325. (define-key bible-map
  326. [menu-bar bible sepp]
  327. '(menu-item '"--"))
  328. (define-key bible-map
  329. [menu-bar bible select-biblical-commentary]
  330. '("Select Commentary" . bible-display-available-commentaries))
  331. (define-key bible-map
  332. [menu-bar bible select-biblical-text]
  333. '("Select Text" . bible-display-available-texts))
  334. (defconst bible-search-mode-map (make-keymap))
  335. (define-key bible-search-mode-map "s" 'bible-search)
  336. (define-key bible-search-mode-map "w" 'bible-toggle-word-study)
  337. (define-key bible-search-mode-map "n" 'bible-next-search-item)
  338. (define-key bible-search-mode-map "p" 'bible-previous-search-item)
  339. (define-key bible-search-mode-map (kbd "RET") 'bible-follow-verse)
  340. (define-key bible-search-mode-map [mouse-1] 'bible-follow-verse)
  341. ;;;;; Term mode keymaps
  342. (defconst bible-term-mode-map (make-sparse-keymap))
  343. (define-key bible-term-mode-map "z" 'text-scale-adjust)
  344. (define-key bible-term-mode-map [mouse-1] 'bible-follow-xref)
  345. (defconst bible-greek-keymap (make-sparse-keymap))
  346. (define-key bible-greek-keymap (kbd "RET") 'bible--display-greek)
  347. (define-key bible-greek-keymap [mouse-1] 'bible--display-greek)
  348. (defconst bible-hebrew-keymap (make-sparse-keymap))
  349. (define-key bible-hebrew-keymap (kbd "RET") 'bible--display-hebrew)
  350. (define-key bible-hebrew-keymap [mouse-1] 'bible--display-hebrew)
  351. (defconst bible-lemma-keymap (make-sparse-keymap))
  352. (define-key bible-lemma-keymap (kbd "RET")
  353. (lambda ()
  354. (interactive)))
  355. ;; Not used. Not really sure what to do here or if it's useful to do anything.
  356. (defconst bible-morph-keymap (make-sparse-keymap))
  357. (define-key bible-morph-keymap (kbd "RET")
  358. (lambda ()
  359. (interactive)
  360. ;; (let ((thing (thing-at-point 'word)))
  361. ;; (message "thing at point: %s" thing)
  362. ;; (message "morph property %s" (get-text-property 0 'field thing))
  363. ))
  364. ;;;;; Module choice keymaps.
  365. (defconst bible-text-map (make-keymap))
  366. (define-key bible-text-map [mouse-1] 'bible-pick-module)
  367. (define-key bible-text-map (kbd "RET") 'bible-pick-module)
  368. ;;;; Variable definitions
  369. (defconst bible--verse-regexp "\\(I \\|1 \\|II \\|2 \\|III \\|3 \\)??[a-zA-Z]+?[ \t\n][0-9]+[:][0-9]+")
  370. ;; Don't know how to get footnotes and scripture cross references yet.
  371. ;;(defconst bible-diatheke-filter-options " afilmnsvw")
  372. (defconst bible-diatheke-filter-options " almnvw")
  373. (defvar bible--text-buffers nil
  374. "List of Bible text buffers.")
  375. (defvar bible--commentary-buffers nil
  376. "List of commentary buffers.")
  377. (defvar bible--synced-buffers nil
  378. "List of buffers that are synchronized so that navigation in one applies
  379. to all of them.")
  380. (defvar-local bible--synced-p nil
  381. "Is this buffer syncronized?")
  382. (defvar bible--texts (lazy-completion-table bible--texts bible--list-biblical-texts))
  383. (defvar bible--commentaries (lazy-completion-table bible--commentaries bible--list-biblical-commentaries))
  384. ;; REVIEW: I believe these chapter counts aren't the same for all modules, e.g. JPS. (FMG 5-Mar-2026)
  385. (defvar bible--books
  386. '(;; Old Testament
  387. ("Genesis" . 50) ("Exodus" . 40) ("Leviticus" . 27) ("Numbers" . 36)
  388. ("Deuteronomy" . 34) ("Joshua" . 24) ("Judges" . 21) ("Ruth" . 4)
  389. ("I Samuel" . 31) ("II Samuel" . 24) ("I Kings" . 22) ("II Kings" . 25)
  390. ("1 Samuel" . 31) ("2 Samuel" . 24) ("1 Kings" . 22) ("2 Kings" . 25)
  391. ("I Chronicles" . 29) ("II Chronicles" . 36) ("Ezra" . 10) ("Nehemiah" . 13)
  392. ("1 Chronicles" . 29) ("2 Chronicles" . 36)
  393. ("Ezra" . 10) ("Nehemiah" . 13)
  394. ("Esther" . 10) ("Job" . 42) ("Psalms" . 150) ("Proverbs" . 31)
  395. ("Ecclesiastes" . 12) ("Song of Solomon" . 8) ("Isaiah" . 66) ("Jeremiah" . 52)
  396. ("Lamentations" . 5) ("Ezekiel" . 48) ("Daniel" . 12) ("Hosea" . 14)
  397. ("Joel" . 3) ("Amos" . 9) ("Obadiah" . 1) ("Jonah" . 4)
  398. ("Micah" . 7) ("Nahum" . 3) ("Habakkuk" . 3) ("Zephaniah" . 3)
  399. ("Haggai" . 2) ("Zechariah" . 14) ("Malachi" . 4)
  400. ;; New Testament
  401. ("Matthew" . 28) ("Mark" . 16) ("Luke" . 24) ("John" . 21)
  402. ("Acts" . 28) ("Romans" . 16)
  403. ("I Corinthians" . 16) ("II Corinthians" . 13)
  404. ("1 Corinthians" . 16) ("2 Corinthians" . 13)
  405. ("Galatians" . 6) ("Ephesians" . 6) ("Philippians" . 4) ("Colossians" . 4)
  406. ("I Thessalonians" . 5) ("II Thessalonians" . 3) ("I Timothy" . 6) ("II Timothy" . 4)
  407. ("1 Thessalonians" . 5) ("2 Thessalonians" . 3) ("1 Timothy" . 6) ("2 Timothy" . 4)
  408. ("Titus" . 3) ("Philemon" . 1) ("Hebrews" . 13) ("James" . 5)
  409. ("I Peter" . 5) ("II Peter" . 3) ("I John" . 5) ("II John" . 1)
  410. ("1 Peter" . 5) ("2 Peter" . 3) ("1 John" . 5) ("2 John" . 1)
  411. ("III John" . 1) ("Jude" . 1) ("Revelation of John" . 22)
  412. ("3 John" . 1))
  413. "A-list of name / chapter count for Bible books.")
  414. ;; These abbreviations are used to follow cross-references in commentaries and lexicons.
  415. ;; Abbreviations from NETnote module and Clarke module (commentaries).
  416. ;; Abbreviations from some lexicons.
  417. ;; Standard abbreviations come first.
  418. (defvar bible--book-name-abbreviations
  419. '(;; Old Testament
  420. ("Gen" . "Genesis") ("Ge" . "Genesis")
  421. ("Exod" . "Exodus") ("Ex" . "Exodus") ("Exo" . "Exodus")
  422. ("Lev" . "Leviticus") ("Le" . "Leviticus")
  423. ("Num" . "Numbers") ("Nu" . "Numbers")
  424. ("Deut" . "Deuteronomy") ("De" . "Deuteronomy") ("Deu" . "Deuteronomy")
  425. ("Josh" . "Joshua") ("Js" . "Joshua") ("Jos" . "Joshua")
  426. ("Judg" . "Judges") ("Jg" . "Judges") ("Jdg" . "Judges")
  427. ("Ru" . "Ruth") ("Rut" . "Ruth")
  428. ("1 Sam" . "I Samuel") ("1 Samuel" . "I Samuel") ("I Sa" . "I Samuel") ("1 Sa" . "I Samuel") ("1Sam" . "I Samuel")
  429. ("2 Sam" . "II Samuel") ("2 Samuel" . "II Samuel") ("II Sa" . "II Samuel") ("2 Sa" . "II Samuel") ("2Sam" . "II Samuel")
  430. ("1 Kgs" . "I Kings") ("1 Kings" . "I Kings") ("I Ki" . "I Kings") ("1 Ki" . "I Kings") ("1Ki" . "I Kings")
  431. ("2 Kgs" . "II Kings") ("2 Kings" . "II Kings") ("II Ki" . "II Kings") ("2 Ki" . "II Kings") ("2Ki" . "II Kings")
  432. ("1 Chr" . "I Chronicles") ("1 Chronicles" . "I Chronicles") ("I Ch" . "I Chronicles") ("1 Ch" . "I Chronicles")
  433. ("2 Chr" . "II Chronicles") ("2 Chronicles" . "II Chronicles") ("II Ch" . "II Chronicles") ("2 Ch" . "II Chronicles")
  434. ("Ezr" . "Ezra")
  435. ("Neh" . "Nehemiah") ("Ne" . "Nehemiah")
  436. ("Esth" . "Esther") ("Es" . "Esther") ("Est" . "Esther")
  437. ("Jb" . "Job")
  438. ("Ps" . "Psalms") ("Psa" . "Psalms")
  439. ("Prov" . "Proverbs") ("Pr" . "Proverbs") ("Pro" . "Proverbs")
  440. ("Eccl" . "Ecclesiastes") ("Ec" . "Ecclesiastes") ("Ecc" . "Ecclesiastes")
  441. ("Song" . "Song of Solomon") ("So" . "Song of Solomon") ("Sol" . "Song of Solomon")
  442. ("Isa" . "Isaiah") ("Is" . "Isaiah")
  443. ("Jer" . "Jeremiah") ("Je" . "Jeremiah")
  444. ("Lam" . "Lamentations") ("La" . "Lamentations")
  445. ("Ezek" . "Ezekiel") ("Ez" . "Ezekiel") ("Eze" . "Ezekiel")
  446. ("Dan" . "Daniel") ("Da" . "Daniel")
  447. ("Hos" . "Hosea") ("Ho" . "Hosea")
  448. ("Joe" . "Joel")
  449. ("Am" . "Amos") ("Amo" . "Amos")
  450. ("Obad" . "Obadiah") ("Ob" . "Obadiah") ("Oba" . "Obadiah")
  451. ("Jon" . "Jonah")
  452. ("Mic" . "Micah") ("Mi" . "Micah")
  453. ("Nah" . "Nahum") ("Na" . "Nahum")
  454. ("Hab" . "Habakkuk") ("Ha" . "Habakkuk")
  455. ("Zeph" . "Zephaniah") ("Zep" . "Zephaniah")
  456. ("Hag" . "Haggai")
  457. ("Zech" . "Zechariah") ("Ze" . "Zechariah") ("Zac" . "Zechariah") ;; Is the last one correct??
  458. ("Mal" . "Malachi")
  459. ;; New Testament
  460. ("Mt" . "Matthew") ("Mat" . "Matthew") ("Matt" . "Matthew")
  461. ("Mk" . "Mark") ("Mar" . "Mark")
  462. ("Lk" . "Luke") ("Luk" . "Luke")
  463. ("Jn" . "John") ("Jo" . "John") ("Joh" . "John")
  464. ("Ac" . "Acts") ("Act" . "Acts")
  465. ("Rom" . "Romans") ("Ro" . "Romans")
  466. ("1 Cor" . "I Corinthians") ("1 Corintihans" . "I Corinthians") ("I Co" . "I Corinthians") ("1 Co" . "I Corinthians")
  467. ("ICor" . "I Corinthians") ("1Cor" . "I Corinthians")
  468. ("2 Cor" . "II Corinthians") ("2 Corinthians" . "II Corinthians") ("II Co" . "II Corinthians") ("2 Co" . "II Corinthians")
  469. ("IICor" . "II Corinthians") ("2Cor" . "II Corinthians")
  470. ("Gal" . "Galatians") ("Ga" . "Galatians")
  471. ("Eph" . "Ephesians")
  472. ("Phil" . "Philippians") ("Phl" . "Philippians")
  473. ("Col" . "Colossians")
  474. ("1 Thess" . "I Thessalonians") ("1 Thessalonians" . "I Thessalonians") ("I Th" . "I Thessalonians") ("1 Th" . "I Thessalonians")
  475. ("IThess" . "I Thessalonians") ("1Thes" . "I Thessalonians") ("1Thess" . "I Thessalonians")
  476. ("2 Thess" . "II Thessalonians") ("2 Thessalonians" . "II Thessalonians") ("II Th" . "II Thessalonians") ("2 Th" . "II Thessalonians")
  477. ("IIThess" . "II Thessalonians") ("2Thes" . "II Thessalonians") ("2Thess" . "II Thessalonians")
  478. ("1 Tim" . "I Timothy") ("1 Timothy" . "I Timothy") ("I Ti" . "I Timothy") ("1 Ti" . "I Timothy") ("ITim" . "I Timothy")
  479. ("1Tim" . "I Timothy")
  480. ("2 Tim" . "II Timothy") ("2 Timothy" . "II Timothy") ("II Ti" . "II Timothy") ("2 Ti" . "II Timothy") ("IITim" . "II Timothy")
  481. ("2Tim" . "II Timothy")
  482. ("Tit" . "Titus")
  483. ("Phlm" . "Philemon") ("Phm" . "Philemon") ("Plm" . "Philemon")
  484. ("Heb" . "Hebrews") ("He" . "Hebrews")
  485. ("Jas" . "James") ("Ja" . "James") ("Jam" . "James")
  486. ("1 Pet" . "I Peter") ("1 Peter" . "I Peter") ("I Pe" . "I Peter") ("1 Pe" . "I Peter") ("IPet" . "I Peter") ("1Pet" . "I Peter")
  487. ("2 Pet" . "II Peter") ("2 Peter" . "II Peter") ("II Pe" . "II Peter") ("2 Pe" . "II Peter") ("IIPet" . "II Peter") ("2Pet" . "II Peter")
  488. ("1 Jn" . "I John") ("1 John" . "I John") ("I Jo" . "I John") ("1 Jo" . "I John") ("IJohn" . "I John") ("1Jn" . "I John")
  489. ("2 Jn" . "I John") ("2 John" . "II John") ("II Jo" . "II John") ("2 Jo" . "II John") ("IIJohn" . "II John") ("2Jn" . "I John")
  490. ("3 Jn" . "I John") ("3 John" . "III John") ("III Jo" . "III John") ("3 Jo" . "III John") ("IIIJohn" . "III John") ("3Jn" . "I John")
  491. ("Ju" . "Jude") ("Jde" . "Jude")
  492. ("Rev" . "Revelation of John") ("Re" . "Revelation of John"))
  493. "A-list of abbreviations for Bible books.")
  494. ;;;;; Book / chapter
  495. (defvar-local bible--current-book (assoc "Genesis" bible--books)
  496. "Current book data (name . chapter).")
  497. (defvar-local bible--current-book-name "Genesis"
  498. "Current book name.")
  499. (defvar-local bible--current-chapter 1
  500. "Current book chapter number.")
  501. ;;;;; Search / query
  502. (defvar-local bible-query nil
  503. "Search query associated with the buffer.")
  504. (defvar-local bible-search-mode "phrase"
  505. "Search mode: either `lucene', `phrase', `regex' or `multiword'.")
  506. (defvar bible-search-range nil)
  507. ;;;;; Lexemes / morphemes
  508. (defvar-local bible-term-language nil
  509. "Displaying terms of this language.")
  510. (defvar-local bible-term-lemma nil
  511. "Lemma for term mode line.")
  512. (defvar-local bible-has-lexemes nil
  513. "Set if the module being displayed has lexical entries availabile.")
  514. (defvar-local bible-has-morphemes nil
  515. "Set if the module being displayed has morphemes availabile.")
  516. (defvar-local bible-text-direction 'left-to-right)
  517. (defvar-local bible-debugme nil
  518. "Make text show up as XML when set.")
  519. (defvar bible-use-tooltips t)
  520. (setq tooltip-delay 1)
  521. (setq tooltip-short-delay .5)
  522. (setq use-system-tooltips nil)
  523. (defvar-local bible-search-query nil
  524. "Query used in toggles (word study and red letter).")
  525. (defvar-local bible-chapter-title nil
  526. "Text preceding start of chapter.
  527. Mostly in Psalms, like `Of David' or the like.")
  528. (defvar-local bible-level "0"
  529. "Used by some modules for indentation and line breaks.")
  530. ;;;; Greek and Hebrew lexeme and morpheme tooltip rendering.
  531. ;;;;; Hash tables for Lexical definitions.
  532. (defvar bible-hash-greek (make-hash-table :test 'equal :size 10000))
  533. (defvar bible-hash-hebrew (make-hash-table :test 'equal :size 10000))
  534. ;;;;; Hash tables for tooltips.
  535. (defvar lex-hash (make-hash-table :test 'equal :size 10000))
  536. (defvar morph-hash (make-hash-table :test 'equal :size 10000))
  537. (defvar bible-outline-strings
  538. '(;;(". ." . ".")
  539. (" I. ." . "\nI.")
  540. (" II. ." . " II.")
  541. (" III. ." . " III.")
  542. (" IV. ." . " IV.")
  543. (" V. ." . " V.")
  544. ("1. ." . "\n 1.")
  545. ("2. ." . "2.")
  546. ("3. ." . "3.")
  547. ("4. ." . "4.")
  548. ("5. ." . "5.")
  549. ("6. ." . "6.")
  550. ("7. ." . "7.")
  551. ("8. ." . "8.")
  552. ("9. ." . "9.")
  553. ("a. ." . "\n a.")
  554. ("(a)." . "\n (a).")
  555. ("b. ." . " b.")
  556. ("c. ." . " c.")
  557. ("d. ." . " d.")
  558. ("e. ." . " e.")
  559. ("f. ." . " f.")
  560. ("g. ." . " g.")
  561. ("h. ." . " h.")
  562. (" . " . ". ")
  563. ("\n\n" . "\n")))
  564. ;;;;; Variables for mode-line-format for search buffers.
  565. (defvar-local bible-search-word-this-query "")
  566. (defvar-local bible-search-text-this-query "")
  567. (defvar-local bible-search-range-this-query nil)
  568. (defvar-local bible-search-matches 0)
  569. ;;;; Functions
  570. ;;;;; Keymap helpers
  571. (defun bible-toggle-display-diatheke ()
  572. "Toggle diatheke args display."
  573. (interactive)
  574. (setq bible-show-diatheke-exec (not bible-show-diatheke-exec))
  575. (message ""))
  576. (defun bible-next-search-item ()
  577. "Go to next item in list of found verses."
  578. (interactive)
  579. (search-forward-regexp bible--verse-regexp))
  580. (defun bible-previous-search-item ()
  581. "Go to previous item in list of found verses."
  582. (interactive)
  583. (search-backward-regexp bible--verse-regexp))
  584. (defun bible-toggle-display-xml ()
  585. "Toggle XML display."
  586. (interactive)
  587. (setq-local bible-debugme (not bible-debugme))
  588. (bible--display))
  589. (defun bible-toggle-text-direction ()
  590. "Switch between left-to-right and right-to-left text direction."
  591. (interactive)
  592. (if (eq bible-text-direction 'left-to-right)
  593. (setq-local bible-text-direction 'right-to-left)
  594. (setq-local bible-text-direction 'left-to-right))
  595. (setq-local bidi-paragraph-direction bible-text-direction))
  596. (defun bible-toggle-tooltips ()
  597. "Toggle use of tooltips to display lexical/morphological items."
  598. (interactive)
  599. (setq bible-use-tooltips (not bible-use-tooltips))
  600. (tooltip-mode 'toggle)
  601. (setq tooltip-resize-echo-area (not bible-use-tooltips))
  602. (setq bible-show-diatheke-exec (and bible-show-diatheke-exec bible-use-tooltips)) ; Don't conflict with echo area
  603. (message ""))
  604. ;;;;; Commands (interactive)
  605. (defun bible-open (&optional book-name chapter verse module)
  606. "Create and open a `bible' buffer.
  607. Optional arguments BOOK-NAME, CHAPTER and VERSE, when supplied, give the
  608. starting verse reference for the buffer. If no optional location
  609. arguments are supplied, Genesis 1:1 is used. Optional argument MODULE
  610. specifies the module to use."
  611. (interactive)
  612. (with-current-buffer (get-buffer-create (generate-new-buffer-name "*bible*"))
  613. (bible)
  614. (when module (setq-default bible-text module))
  615. (setq-local bible-text (default-value 'bible-text))
  616. (bible--set-location
  617. (assoc (or book-name "Genesis") bible--books)
  618. (or chapter 1)
  619. verse)
  620. (cl-pushnew (current-buffer) bible--text-buffers)
  621. (set-window-buffer (get-buffer-window (current-buffer)) (current-buffer))))
  622. ;;;;;; Navigation
  623. (defun bible--do-set-location (book chapter &optional verse)
  624. (setq-local bible--current-book book)
  625. (setq-local bible--current-book-name (car book))
  626. (setq-local bible--current-chapter chapter)
  627. (bible--display verse))
  628. (defun bible--set-location (book chapter &optional verse)
  629. "Set the BOOK, CHAPTER and optionally VERSE of the active `bible' buffer."
  630. (let ((buffer (current-buffer)))
  631. (bible--do-set-location book chapter verse)
  632. (when (cl-find buffer bible--synced-buffers)
  633. (save-excursion
  634. (dolist (buf bible--synced-buffers)
  635. (unless (eq buf buffer)
  636. (with-current-buffer buf
  637. (bible--do-set-location book chapter verse))))))))
  638. (defun bible-next-chapter ()
  639. "Page to the next chapter for the active `bible' buffer and
  640. for any synchronized buffers."
  641. (interactive)
  642. (let* ((book-chapters (cdr bible--current-book))
  643. (chapter (min book-chapters (1+ bible--current-chapter))))
  644. (bible--set-location bible--current-book chapter)))
  645. (defun bible-previous-chapter ()
  646. "Page to the previous chapter for the active `bible' buffer and
  647. for any synchronized buffers."
  648. (interactive)
  649. (bible--set-location bible--current-book (max 1 (1- bible--current-chapter))))
  650. (defun bible-next-word ()
  651. "Move forward a word, taking into account the relevant text properties."
  652. (interactive)
  653. (unless (eobp)
  654. (let ((next-change (text-property-search-forward 'strong nil nil t)))
  655. (when next-change
  656. (goto-char (1- (prop-match-end next-change)))))))
  657. (defun bible-previous-word ()
  658. "Move back a word, taking into account the relevant text properties."
  659. (interactive)
  660. (unless (bobp)
  661. (let ((previous-change (text-property-search-backward 'strong)))
  662. (when previous-change
  663. (goto-char (prop-match-beginning previous-change))))))
  664. ;;;;;; Select Location
  665. (defun bible-select-book ()
  666. "Ask user for a new book and chapter for the current `bible' buffer."
  667. (interactive)
  668. (let* ((completion-ignore-case t)
  669. (book-data (assoc (completing-read "Book: " bible--books nil t) bible--books))
  670. (book-data-string (car book-data))
  671. (chapter (string-to-number
  672. (completing-read
  673. "Chapter [1]: "
  674. (bible--list-number-range 1 (cdr book-data)) nil t nil nil "1"))))
  675. (pcase (aref book-data-string 0)
  676. (?1 (setq book-data (cons (concat "I" (substring book-data-string 1)) (cdr book-data))))
  677. (?2 (setq book-data (cons (concat "II" (substring book-data-string 1)) (cdr book-data))))
  678. (?3 (setq book-data (cons (concat "III" (substring book-data-string 1)) (cdr book-data)))))
  679. (bible--set-location book-data chapter)))
  680. (defun bible-select-chapter ()
  681. "Ask user for a new chapter for the current `bible' buffer."
  682. (interactive)
  683. (let* ((book-chapters (cdr bible--current-book))
  684. (chapter (string-to-number
  685. (completing-read
  686. "Chapter [1]: "
  687. (bible--list-number-range 1 book-chapters) nil t nil nil "1"))))
  688. (when chapter
  689. (bible--set-location bible--current-book chapter))))
  690. ;;;;;; Select modules
  691. ;; Choose a module.
  692. (defun bible-pick-module ()
  693. "Keymap action function---select module that the user chooses."
  694. (interactive)
  695. (let ((item (get-text-property (point) 'module)))
  696. (bible-open bible--current-book-name bible--current-chapter 1 item)))
  697. (defun bible-select-text ()
  698. "Ask user for a new text module for the current `bible' buffer."
  699. (interactive)
  700. (let ((text (completing-read "Text: " bible--texts)))
  701. (unless (string= text "")
  702. (setq-default bible-text text)
  703. (setq-local bible-text text)
  704. (bible--display))))
  705. (defun bible-select-commentary ()
  706. "Ask user for a new text module for the current `bible' buffer."
  707. (interactive)
  708. (let ((commentary (completing-read "Commentary: " bible--commentaries)))
  709. (unless (string= commentary "")
  710. (setq-local bible-text commentary)
  711. (bible--display))))
  712. ;;;;;; Toggles
  713. (defun bible-toggle-word-study ()
  714. "Toggle the inclusion of word study for the active `bible' buffer."
  715. (interactive)
  716. (setq bible-word-study-enabled (not bible-word-study-enabled))
  717. (bible--display))
  718. (defun bible-toggle-red-letter ()
  719. "Toggle red letter mode for the active `bible' buffer."
  720. (interactive)
  721. (setq bible-red-letter-enabled (not bible-red-letter-enabled))
  722. (bible--display))
  723. (defun bible-toggle-buffer-sync ()
  724. "Either add or remove the current buffer from the
  725. `bible--synced-buffers' list."
  726. (interactive)
  727. (let ((buffer (current-buffer)))
  728. (if bible--synced-p
  729. (progn
  730. (setq bible--synced-buffers (cl-delete buffer bible--synced-buffers))
  731. (setq-local bible--synced-p nil))
  732. (cl-pushnew buffer bible--synced-buffers)
  733. (setq-local bible--synced-p t))
  734. (force-mode-line-update)))
  735. (defun bible-split-display ()
  736. "Copy the active `bible' buffer into a new buffer in another window."
  737. (interactive)
  738. (split-window-right)
  739. (balance-windows)
  740. (other-window 1)
  741. (bible-open bible--current-book-name bible--current-chapter 1 bible-text))
  742. ;;;;;; Search helpers
  743. (defun bible-set-search-range ()
  744. "Ask user for a new text module for the current `bible' buffer."
  745. (interactive)
  746. (let ((range (read-string "Range (<return> to clear): ")))
  747. (if (string-equal range "")
  748. (setq bible-search-range nil)
  749. (setq bible-search-range range))))
  750. (defun bible-search (query)
  751. "Search for a QUERY: a word or phrase.
  752. Asks the user for type of search: either `lucene', `phrase', `regex'
  753. or `multiword'. `lucene' is the default search.
  754. `lucene' mode requires an index to be built using the `mkfastmod' program."
  755. (interactive "sBible Search: ")
  756. (when (> (length query) 0)
  757. (let ((searchmode (completing-read "Search Mode: " '("lucene" "phrase" "regex" "multiword") nil t "lucene")))
  758. (bible--open-search query searchmode (buffer-local-value 'bible-text (current-buffer))))))
  759. (defun bible-follow-verse ()
  760. "Follow the hovered verse in a bible search mode buffer.
  761. Create a new `bible' buffer positioned at the selected verse."
  762. (interactive)
  763. (let* ((text (thing-at-point 'line t))
  764. book
  765. chapter
  766. verse)
  767. (when (string-match bible--verse-regexp text)
  768. (setq text (match-string 0 text))
  769. (string-match "I?I?I? ?[A-Z]?[a-z]* " text)
  770. (setq book (match-string 0 text))
  771. (string-match "[0-9]?[0-9]?[0-9]?:" text)
  772. (setq chapter (substring (match-string 0 text) 0 (1- (length (match-string 0 text)))))
  773. (string-match ":[0-9]?[0-9]?[0-9]?" text)
  774. (setq verse (substring (match-string 0 text) 1))
  775. (bible-open (string-trim book) (string-to-number chapter) (string-to-number verse)) bible-text)))
  776. (defun bible-follow-xref ()
  777. "Follow the hovered verse in a bible term buffer.
  778. Create a new `bible' buffer positioned at the specified verse.
  779. Handle abbreviations."
  780. (interactive)
  781. ;; (message "Following xref.")
  782. (let ((xref (get-text-property (point) 'xref)))
  783. (when xref
  784. (let ((verse-ref (split-string xref))
  785. book-abbrev
  786. chapter-verse)
  787. ;; (message "xref: %s" xref)
  788. ;; (message "Verse-ref: %s" verse-ref)
  789. (cond ((= (length verse-ref) 2) ; Mat 5 or the like
  790. (setq book-abbrev (car verse-ref)
  791. chapter-verse (split-string (cadr verse-ref) ":")))
  792. ((= (length verse-ref) 3) ; II Cor 3:17 or the like
  793. (setq book-abbrev (concat (car verse-ref) " " (cadr verse-ref))
  794. chapter-verse (split-string (caddr verse-ref) ":"))))
  795. ;; Use book abbreviation if present or try whatever is in verse-ref.
  796. (let ((book (or (alist-get book-abbrev bible--book-name-abbreviations nil nil #'string-equal-ignore-case) (car verse-ref)))
  797. (chapter (car chapter-verse))
  798. (verse (cadr chapter-verse)))
  799. (bible-open (string-trim book) (string-to-number chapter) (string-to-number verse) (default-value 'bible-text)))))))
  800. ;;;;;; User visible actions.
  801. ;; These can be called interactively if you know the Strong's number
  802. ;; you want to look up.
  803. (defun bible-term-hebrew (term)
  804. "Query user for a Strong's Hebrew Lexicon TERM."
  805. (interactive "sTerm: ")
  806. (bible--open-term-hebrew term))
  807. (defun bible-term-greek (term)
  808. "Query user for a Strong's Greek Lexicon TERM."
  809. (interactive "sTerm: ")
  810. (bible--open-term-greek term))
  811. ;; Interactively insert a verse into an arbitrary current buffer.
  812. (defun bible-insert ()
  813. "Query user to select a verse for insertion into the current buffer."
  814. (interactive)
  815. (let* ((completion-ignore-case t)
  816. (book-data (assoc (completing-read "Book: " bible--books nil t) bible--books))
  817. (chapter (when book-data (completing-read "Chapter: " (bible--list-number-range 1 (cdr book-data)) nil t "1" nil "1")))
  818. (verse (when chapter (read-from-minibuffer "Verse: ")))
  819. (query (concat (car book-data) " " chapter ":" verse))
  820. (args (list bible-sword-query nil (current-buffer) t "-b" bible-text "-f" "plain" "-k" query)))
  821. (apply #'call-process args)))
  822. ;;;;;; Support (internal)
  823. ;;;;;;; Diatheke interface
  824. (defun bible--exec-diatheke (query &optional filter format module)
  825. "Execute `diatheke' with specified QUERY options.
  826. FILTER is the Diatheke filter argument. FORMAT is either plain or
  827. the default of internal. MODULE is the text module to use. Returns
  828. string containing query result."
  829. (let ((module (or module bible-text)))
  830. (with-temp-buffer
  831. (let ((args (list bible-sword-query nil (current-buffer) t "-b" module)))
  832. (if filter
  833. (setq filter (concat filter bible-diatheke-filter-options))
  834. (setq filter bible-diatheke-filter-options))
  835. (setq args (append args (list "-o" filter)))
  836. (setq args (append args (list "-f" (pcase format ("plain" "plain") (_ "internal")) "-k" query)))
  837. (when bible-show-diatheke-exec
  838. (message "%s" args))
  839. (apply #'call-process args))
  840. (buffer-string))))
  841. (defun bible--diatheke-search (query searchtype &optional format module)
  842. "Execute `diatheke' on QUERY with SEARCHTYPE.
  843. Optional argument FORMAT is either plain or the default of internal.
  844. MODULE is the text module to use and defaults to the current module."
  845. (with-temp-buffer
  846. (let ((args (list bible-sword-query nil (current-buffer) t "-b" (or module bible-text))))
  847. (setq args (append args (list "-s" (pcase searchtype
  848. ("lucene" "lucene")
  849. ("phrase" "phrase")
  850. ("regex" "regex")
  851. ("multiword" "multiword")))))
  852. (when bible-search-range (setq args (append args (list "-r" bible-search-range))))
  853. (setq args (append args (list "-f" (pcase format ("plain" "plain") (_ "internal")) "-k" query)))
  854. (when bible-show-diatheke-exec
  855. (message "%s" args))
  856. (apply #'call-process args))
  857. (buffer-string)))
  858. ;; Use HTMLHREF format with diatheke, post-process to render html.
  859. (defun bible--morph-query (query module)
  860. "Execute `diatheke' to do morph QUERY, using MODULE.
  861. Render HTML, return string. Do some tweaking specific to morphology."
  862. (with-temp-buffer
  863. (let ((args (list bible-sword-query nil (current-buffer) t "-b" module "-o" "m" "-f" "HTMLHREF" "-k" query)))
  864. (when bible-show-diatheke-exec
  865. (message "%s" args))
  866. (apply #'call-process args)
  867. (shr-render-region (point-min) (point-max))
  868. (format-replace-strings
  869. '(("\n:" . "") ; This makes the Packard morphology display look better.
  870. ("Part of Speech" . "")) ; This helps the Robinson display look better.
  871. nil (point-min) (point-max))
  872. (substring (buffer-string) (1+ (length query)))))) ; This tries to get rid of unnecessary query identifier.
  873. ;; Use "plain" format with diatheke.
  874. (defun bible--lex-query (query module)
  875. "Execute `diatheke' for QUERY, using MODULE.
  876. Plain format, returns string."
  877. (bible--exec-diatheke query nil "plain" module))
  878. ;;;;;; Lexicon processing
  879. ;; The Greek lexical definitions are done using the HTMLHREF output
  880. ;; format so they come out looking nice and having clickable
  881. ;; cross-references and/or Strong's references.
  882. (defun bible--process-href ()
  883. "Fix the XML so cross-references are in the right format.
  884. These cross-references get processed later when the term is displayed.
  885. First, find the links put in by diatheke's HTMLHREF output format.
  886. Replace the links with verse references that get changed to clickable
  887. cross-references when the term is displayed.
  888. The verse refs look like this: <bookname>.<chapter>.<verse>. We convert
  889. them to the <bookname> <chapter>:<verse> format."
  890. (goto-char (point-min))
  891. (while (re-search-forward "<a href=\"passagestudy.*?</a>" nil t) ; HTMLHREF cross references.
  892. (let ((match-text (match-string 0)))
  893. ;; Delete original link.
  894. (replace-match "" nil nil)
  895. ;; Get the verse reference from the string we saved. Put it in
  896. ;; good format, then insert it into buffer where href was.
  897. (when (string-match "value=.*?&" match-text)
  898. (let* ((value-string (match-string 0 match-text))
  899. ;; Strip off value= and trailing &.
  900. (verse-ref-string (substring value-string 6 (1- (length value-string))))
  901. (verse-ref-length (length verse-ref-string))
  902. period)
  903. ;; Convert periods
  904. ;; Substitute first period with space
  905. (when (setq period (cl-search "." verse-ref-string))
  906. (aset verse-ref-string period ? ))
  907. ;; Substitute second period with colon
  908. (when (setq period (cl-search "." verse-ref-string))
  909. (aset verse-ref-string period ?:))
  910. ;; Replace numbers (1, 2 or 3) with roman numerals (I, II, III).
  911. (pcase (aref verse-ref-string 0)
  912. (?1 (setq verse-ref-string (concat "I" (substring verse-ref-string 1))))
  913. (?2 (setq verse-ref-string (concat "II" (substring verse-ref-string 1))))
  914. (?3 (setq verse-ref-string (concat "III" (substring verse-ref-string 1)))))
  915. (set-text-properties 0 verse-ref-length nil verse-ref-string) ; Clear unwanted properties (if any)
  916. (insert verse-ref-string))))))
  917. (defun bible--cleanup-lex-text (lex-text)
  918. "Reformat tooltip text LEX-TEXT so tooltips look nice."
  919. (dolist (outline-string bible-outline-strings)
  920. (setq lex-text (string-replace (car outline-string) (cdr outline-string) lex-text)))
  921. lex-text)
  922. (defun bible--lookup-def-greek (key)
  923. "Execute `diatheke' to do query on KEY.
  924. Massage output so verse cross references are usable. Returns string."
  925. (with-temp-buffer
  926. (let ((args (list bible-sword-query nil (current-buffer) t "-b" bible-greek-lexicon "-o" "m" "-f" "plain" "-k" key)))
  927. (when bible-show-diatheke-exec
  928. (message "%s" args))
  929. (apply #'call-process args)
  930. (bible--cleanup-lex-text (bible--remove-module-name bible-greek-lexicon (buffer-string))))))
  931. (defun bible--lookup-lemma-index (key)
  932. "Return the Greek lemma from lemma index with a strong's number as KEY."
  933. (string-trim
  934. (bible--remove-module-name bible-lexicon-index (bible--lex-query key bible-lexicon-index))))
  935. (defun bible--lookup-lemma-greek-indexed (key)
  936. "Lookup Greek lemma using Strong's number KEY.
  937. Then look up the definition of that lemma. Used when two-stage
  938. lexical definition is set for a particular lexicon."
  939. (let ((lemma-entry (bible--lookup-lemma-index key))) ; Get lemma from Strong's number
  940. (when lemma-entry
  941. (let ((lemma (caddr (split-string lemma-entry " "))))
  942. (bible--lookup-def-greek lemma)))))
  943. (defun bible--lookup-lemma-greek (key)
  944. "Lookup lexical definition using Strong's number KEY.
  945. 1. Check hash table first. If entry found, return.
  946. 2. Otherwise, if a lexicon is accessed by lemmas, do lookup using index method.
  947. 3. Otherwise just use the Strong's number method."
  948. (or (gethash key bible-hash-greek)
  949. (puthash key
  950. (if bible-use-index-for-lexicon
  951. (bible--lookup-lemma-greek-indexed key)
  952. (bible--lookup-def-greek key))
  953. bible-hash-greek)))
  954. (defun bible--lookup-def-hebrew (key)
  955. "Execute `diatheke' to do query on KEY.
  956. Massage output so various cross references are usable. Returns string."
  957. (with-temp-buffer
  958. (let ((args (list bible-sword-query nil (current-buffer) t "-b" bible-hebrew-lexicon "-f" "plain" "-k" key)))
  959. (when bible-show-diatheke-exec
  960. (message "%s" args))
  961. (apply #'call-process args)
  962. (bible--process-href)
  963. (concat (string ?\x200e)
  964. (bible--remove-module-name bible-hebrew-lexicon (substring (buffer-string) 7))))))
  965. (defun bible--lookup-lemma-hebrew (key)
  966. "Lookup lexical definition using Strong's number KEY.
  967. 1. Check hash table first. If entry found, return.
  968. 2. Otherwise, if a lexicon is accessed by lemmas, do lookup using index method.
  969. 3. Otherwise just use the Strong's number method."
  970. (or (gethash key bible-hash-hebrew)
  971. (puthash key
  972. (bible--lookup-def-hebrew key)
  973. bible-hash-hebrew)))
  974. ;; We use the shorter lexicons for text in tooltips. We also cache the
  975. ;; lex and morph strings, hoping to speed up tooltip rendering.
  976. (defun bible--lookup-lemma-short (lemma lexicon)
  977. "Look up lexical entry for LEMMA in short LEXICON.
  978. Returns a string that is intended to be displayed in a tooltip.
  979. Uses short lexicon (e.g. StrongsRealHebrew or StrongsRealGreek)."
  980. (when (string-match "[0-9]+" lemma)
  981. (bible--remove-module-name
  982. lexicon
  983. ;; Get rid of unnecessary strongs codes at the beginning.
  984. (replace-regexp-in-string
  985. ".*[0-9]+ [0-9]+ " ""
  986. (bible--lex-query (concat (match-string 0 lemma)) lexicon)))))
  987. (defun bible--lookup-lex (lex)
  988. "Look up lexical item LEX. This is used for tooltips.
  989. Return hash table entry if present in `lex-hash' cache, else look up in
  990. database and stash in cache."
  991. (when lex
  992. (let* ((key (substring lex 7)) ; strip off "strong:" prefix.
  993. (lex-text (gethash key lex-hash)))
  994. (if lex-text
  995. lex-text
  996. (setq lex-text
  997. (cond ((string-prefix-p "G" key)
  998. (bible--lookup-lemma-short key bible-greek-lexicon-short))
  999. ((string-prefix-p "H" key)
  1000. (concat (string ?\x200e)
  1001. (bible--lookup-lemma-short key bible-hebrew-lexicon-short)))))
  1002. (puthash key (string-fill (bible--cleanup-lex-text lex-text) 75) lex-hash)))))
  1003. (defun bible--lookup-morph-entry (morph)
  1004. "Look up entry for morphological item MORPH.
  1005. Return hash table entry if present in `morph-hash' cache, else look up in
  1006. database and stash in cache."
  1007. (when morph
  1008. (or (gethash morph morph-hash)
  1009. (puthash morph
  1010. (let (morph-module morph-key)
  1011. ;; We know about these modules. (Assume they're installed.)
  1012. (cond ((string-prefix-p "robinson:" morph)
  1013. (setq morph-module "Robinson")
  1014. (setq morph-key (substring morph (length "robinson:"))))
  1015. ((string-prefix-p "packard:" morph)
  1016. (setq morph-module "Packard")
  1017. (setq morph-key (substring morph (length "packard:"))))
  1018. ((string-prefix-p "oshm:" morph)
  1019. (setq morph-module "OSHM")
  1020. (setq morph-key (substring morph (length "oshm:")))))
  1021. (bible--remove-module-name morph-module (bible--morph-query morph-key morph-module)))
  1022. morph-hash))))
  1023. ;; Get string for tooltip display
  1024. (defun bible--show-lex-morph (_window object pos)
  1025. "Get text for tooltip display for OBJECT at POS in WINDOW.
  1026. Includes both lex and morph definitions if text module has
  1027. both tags, otherwise just get lex definition."
  1028. (let* ((lex (get-text-property pos 'strong object))
  1029. (lex-text (bible--lookup-lex lex))
  1030. (morph (get-text-property pos 'morph object))
  1031. (morph-text (bible--lookup-morph-entry morph)))
  1032. (when lex-text
  1033. ;; This removes backslashes to prevent bogus command
  1034. ;; substitutions (that is, Emacs mistakenly filling in a key
  1035. ;; binding for some command---see Info doc on Substituting Key
  1036. ;; Bindings) in the tooltip.
  1037. ;; REVIEW: I couldn't figure out a better way to bypass command
  1038. ;; substitution in the tooltips. (FMG 5-Mar-2026)
  1039. (subst-char-in-string
  1040. ?\\
  1041. ?
  1042. (if morph-text
  1043. (concat (string-trim lex-text) "\n" (string-trim morph-text))
  1044. (string-trim lex-text))))))
  1045. ;;;; Display module text
  1046. (defun bible-handle-divine-name (item)
  1047. "When ITEM is divine name, display it as such."
  1048. (let ((start (point))
  1049. (strongs (dom-attr item 'savlm)))
  1050. (insert "LORD")
  1051. (let ((end (point)))
  1052. (add-face-text-property start end 'bold)
  1053. (put-text-property start end 'keymap bible-hebrew-keymap)
  1054. (when (and strongs (string-match "strong:H" strongs))
  1055. (put-text-property start end 'help-echo 'bible--show-lex-morph)
  1056. (put-text-property start end 'strong (match-string 0 strongs))))))
  1057. (defun bible--process-word (item iproperties)
  1058. "Handle <w ...> fubar </w> tag in ITEM. Check IPROPERTIES for qualifiers.
  1059. Add tooltips for definitions and morphology. Also insert lemmas in
  1060. buffer if `word study' is turned on (must be done after item is inserted
  1061. in buffer)."
  1062. (let ((word (string-trim (bible-dom-text item)))
  1063. (morph (dom-attr item 'morph))
  1064. (savlm (dom-attr item 'savlm))
  1065. (lemma (dom-attr item 'lemma))
  1066. (divinename (dom-by-tag item 'divinename)))
  1067. (let ((start (point))
  1068. (end (+ (point) (length word))))
  1069. (insert word)
  1070. ;; REVIEW: Special case this. Some modules do this differently.
  1071. ;; (FMG 5-Mar-2026)
  1072. (when divinename
  1073. (just-one-space)
  1074. (bible-handle-divine-name item)
  1075. (just-one-space))
  1076. ;; Red letter.
  1077. (when (plist-get iproperties 'jesus)
  1078. (add-face-text-property start end '(:foreground "red")))
  1079. ;; lexical definitions
  1080. ;; N.B. There are some severe issues with Strongs numbers in some modules.
  1081. (when (or savlm lemma)
  1082. (let* ((matched nil)
  1083. (lexemes (split-string (or savlm lemma)))
  1084. (lexeme
  1085. ;; HACK: Kludge alert. KJV module conflates Greek
  1086. ;; articles with nouns. Deal with this.
  1087. ;; (FMG 5-Mar-2026)
  1088. (let ((lexeme-list
  1089. (if (string= bible-text "KJV")
  1090. (reverse lexemes) ; Use the last `strong:' entry.
  1091. lexemes)))
  1092. (catch 'loop
  1093. (dolist (item lexeme-list)
  1094. (when (string-prefix-p "strong:" item)
  1095. (throw 'loop item)))))))
  1096. (when lexeme
  1097. (cond ((string-match "strong:G.*" lexeme) ; Greek
  1098. (setq matched (match-string 0 lexeme))
  1099. (put-text-property start end 'keymap bible-greek-keymap))
  1100. ((string-match "strong:H.*" lexeme) ; Hebrew
  1101. (setq matched (match-string 0 lexeme))
  1102. (put-text-property start end 'keymap bible-hebrew-keymap)))
  1103. ;; Add help-echo, strongs reference for tooltips if match.
  1104. (when matched
  1105. (setq bible-has-lexemes " Lex")
  1106. (put-text-property start end 'help-echo 'bible--show-lex-morph)
  1107. (put-text-property start end 'strong matched))))
  1108. ;; morphology
  1109. (when morph
  1110. (let* ((matched nil)
  1111. (morphemes (split-string morph))
  1112. (morpheme (car (last morphemes)))) ; KJV kludge as above
  1113. (if (or
  1114. (string-match "robinson:.*" morpheme) ; Robinson Greek morphology
  1115. (string-match "packard:.*" morpheme) ; Packard Greek morphology --- LXX seems to use this
  1116. (string-match "oshm:.*" morpheme)) ; OSHM Hebrew morphology
  1117. (setq matched (match-string 0 morpheme)))
  1118. (when matched
  1119. (setq bible-has-morphemes " Morph")
  1120. (put-text-property start end 'morph matched)
  1121. (put-text-property start end 'help-echo 'bible--show-lex-morph))))
  1122. ;; Insert lemma into buffer. Lemma tag will be part of lemma/savelm item.
  1123. ;; TODO: Should I enable lexicon lookups on these lemmas? I
  1124. ;; don't use this anyway.... (FMG 5-Mar-2026)
  1125. (when (and bible-word-study-enabled lemma (string-match "lemma.*:.*" lemma))
  1126. (dolist (word (split-string (match-string 0 lemma) " "))
  1127. (setq word (replace-regexp-in-string "[.:a-zA-Z0-9]+" "" word))
  1128. (just-one-space)
  1129. (let ((refstart (point)))
  1130. (insert word)
  1131. (add-face-text-property refstart (point) '(:foreground "blue"))
  1132. (put-text-property refstart (point) 'keymap bible-lemma-keymap))))))))
  1133. (defun bible--insert-title (title-node)
  1134. "Insert the text in TITLE-NODE into buffer as a chapter title.
  1135. Since each verse will have a `title' tag, keep track and only emit a
  1136. title when the new title in `title-node' is different from the one
  1137. stored in `bible-chapter-title'."
  1138. (unless (equal bible-chapter-title title-node)
  1139. (setq-local bible-chapter-title title-node)
  1140. (let ((title-text
  1141. (replace-regexp-in-string ; Clear out XML.
  1142. "<.*?>" ""
  1143. (bible-dom-texts bible-chapter-title)))
  1144. (start (point)))
  1145. (bible-new-line)
  1146. ;; Insert the LRM character to make the text render left-to-right.
  1147. ;; This is necessary in the KJV module when displaying psalm 119.
  1148. (insert (string ?\x200e) title-text)
  1149. (put-text-property start (point) 'face 'bold)
  1150. (newline)
  1151. (delete-blank-lines))))
  1152. ;; These tags appear in ESV modules (and maybe others?)
  1153. ;; REVIEW: Is this right? (FMG 5-Mar-2026)
  1154. (defun bible--level-tag (node)
  1155. "Indent or break line as dictated by NODE."
  1156. (let ((type (dom-attr node 'type))
  1157. (level (dom-attr node 'level)))
  1158. (cond ((and type (string-equal-ignore-case type "x-br"))
  1159. (newline))
  1160. ((and type (string-equal-ignore-case type "x-indent"))
  1161. (insert "\t"))
  1162. ;; REVIEW: Some modules use `level' tag but
  1163. ;; not in a consistent way. (FMG 7-Mar-2026)
  1164. ((equal level "1")
  1165. (just-one-space))
  1166. ((equal level "2")
  1167. (newline)
  1168. (delete-blank-lines)))))
  1169. (defvar-local bible-current-xref-book nil)
  1170. (defvar-local bible-current-xref-chapter nil)
  1171. (defun bible--insert-xref (node)
  1172. "Insert a cross reference specified by NODE."
  1173. ;; HACK: What a mess! There are still some broken edge cases! (FMG 29-Mar-2026)
  1174. (let* ((refs-text (bible-dom-text node))
  1175. (refs (split-string refs-text "," t " ")))
  1176. ;; (message "Got refs |%s|" refs)
  1177. (dolist (ref refs)
  1178. ;; (message "Got ref %s" ref)
  1179. (let ((a-ref (split-string ref ";" t " ")))
  1180. ;; (message "Got a-ref %s" a-ref)
  1181. (dolist (b-ref a-ref)
  1182. ;; (message "Got b-ref %s" b-ref)
  1183. ;; b-ref is an individual reference.
  1184. ;; At this point b-ref will, we hope, look like one of the following:
  1185. ;; <book> <chapter>:<verse> (or maybe <book> <chapter>)
  1186. ;; <chapter>:verse
  1187. ;; <verse> (or maybe <verse>-<verse>)
  1188. ;; Books may look like this: <1 Cor> or <Gal> so we have to deal with the possible space.
  1189. ;; We ignore verse ranges and hope for the best (it seems to do the right thing).
  1190. (let ((set-chapter-p nil))
  1191. (when (string-match ".* " b-ref)
  1192. (setq-local bible-current-xref-book (match-string 0 b-ref))
  1193. ;; (message "Setting book to |%s|" bible-current-xref-book)
  1194. )
  1195. (when (string-match "[0-9]*:" b-ref)
  1196. (setq-local bible-current-xref-chapter (string-trim (substring (match-string 0 b-ref) 0 (1- (length (match-string 0 b-ref))))))
  1197. ;; (message "Setting chapter to |%s|" bible-current-xref-chapter)
  1198. (setq set-chapter-p t))
  1199. (let* ((match (string-match "[0-9]*" b-ref (if set-chapter-p (match-end 0) 0)))
  1200. (verse (string-trim (substring b-ref match (match-end 0)))))
  1201. ;; (message "verse is |%s|" verse)
  1202. (just-one-space)
  1203. (let ((start (point))
  1204. (the-ref (concat bible-current-xref-book bible-current-xref-chapter)))
  1205. (when verse (setq the-ref (concat the-ref ":" verse)))
  1206. (insert b-ref)
  1207. (put-text-property start (point) 'xref the-ref)
  1208. (put-text-property start (point) 'keymap bible-term-mode-map)
  1209. (put-text-property start (point) 'help-echo (concat "Go to " the-ref))
  1210. (add-face-text-property start (point) '(:foreground "blue"))))))))))
  1211. (defun bible--insert-domnode-recursive (node &optional iproperties)
  1212. "Recursively parse domnode NODE obtained from `libxml-parse-html-region'.
  1213. Inserts resulting text into active buffer with properties specified in
  1214. IPROPERTIES.
  1215. In processing subnodes, each case will prepend a space if it needs it."
  1216. (when (and bible-red-letter-enabled (equal (dom-attr node 'who) "Jesus"))
  1217. ;; For red-letter display.
  1218. (setq iproperties (plist-put iproperties 'jesus t)))
  1219. (dolist (subnode (dom-children node))
  1220. (cond ((null subnode) nil)
  1221. ((stringp subnode)
  1222. ;; Red letter
  1223. (when (plist-get iproperties 'jesus)
  1224. (add-face-text-property 0 (length subnode) '(:foreground "red") nil subnode))
  1225. (insert subnode))
  1226. ((consp subnode)
  1227. (let ((tag (dom-tag subnode)))
  1228. (pcase tag
  1229. ;; TODO: There are lots of tags we don't handle, especially in commentaries.
  1230. ;; Maybe process these at some point? Include footnotes etc.
  1231. ;; (FMG 5-Mar-2026)
  1232. ;; 'w is usual case.
  1233. ('w (insert " ") (bible--process-word subnode iproperties))
  1234. ('title
  1235. ;; This mess is to deal with the possibility that the
  1236. ;; title might change in the middle of the chapter. I'm
  1237. ;; talking about YOU, Psalm 119.
  1238. (if bible-chapter-title
  1239. (bible--insert-title subnode) ; Middle of chapter.
  1240. (save-excursion ; Beginning of chapter.
  1241. (goto-char (point-min))
  1242. (bible--insert-title subnode))))
  1243. ;; Font tag ignored for now, treat as if 'w.
  1244. ('font (insert " ") (bible--process-word subnode iproperties))
  1245. ('hi (when (equal (dom-attr subnode 'type) "bold")
  1246. (just-one-space)
  1247. (let ((word (bible-dom-text subnode))
  1248. (start (point)))
  1249. (insert word)
  1250. (put-text-property start (point) 'face 'bold))))
  1251. ;; Italic face (special case for certain module)
  1252. ('i
  1253. (just-one-space)
  1254. (let ((word (bible-dom-text subnode))
  1255. (start (point)))
  1256. (insert word)
  1257. (put-text-property start (point) 'face 'bold)
  1258. (add-face-text-property start (point) '(:foreground "orange"))))
  1259. ;; 'q is used for red letter.
  1260. ;; NASB Module uses 'seg to indicate OT quotations (and others?).
  1261. ((or 'body 'seg 'p 'q) (bible--insert-domnode-recursive subnode iproperties))
  1262. ('l (bible--level-tag subnode))
  1263. ;; REVIEW: divine name handling doesn't seem to work the same
  1264. ;; with all modules. (FMG 26-Mar-2026)
  1265. ('divinename (bible-handle-divine-name subnode))
  1266. ;; Some modules use this for line breaks and such.
  1267. ('milestone
  1268. (pcase (dom-attr subnode 'type)
  1269. ("line" (bible-new-line))
  1270. ;; ("x-PN" (bible-new-line)) ; REVIEW: Don't yet understand this one. (FMG 26-Mar-2026)
  1271. ("x-p" (insert (dom-attr subnode 'marker) " "))))
  1272. ('br (bible-new-line))
  1273. ('lb (when (equal (dom-attr subnode 'type) "x-begin-paragraph") (bible-new-line)))
  1274. ('div (when (or (equal (dom-attr subnode 'type) "paragraph")
  1275. (equal (dom-attr subnode 'type) "x-p"))
  1276. (bible-new-line)))
  1277. ;; For commentaries and the like.
  1278. ((or 'scripref 'reference) (bible--insert-xref subnode))
  1279. ;; Various text properties---ignore for now. REVIEW: (FMG 26-Mar-2026)
  1280. ((or 'b 'u) (bible--insert-domnode-recursive subnode iproperties))
  1281. ;; Word inserted by translation, not in original, give visual indication.
  1282. ('transchange
  1283. (insert " ")
  1284. (let ((word (bible-dom-text subnode))
  1285. (start (point))
  1286. (face (if (plist-get iproperties 'jesus) '(:foreground "salmon") '(:foreground "gray50"))))
  1287. (insert word)
  1288. (add-face-text-property start (point) face)))))))))
  1289. (defun bible--display (&optional verse)
  1290. "Render a page (chapter) of a Bible module.
  1291. Defaults to using `bible-text'.
  1292. If optional argument VERSE is supplied, set cursor at verse."
  1293. (let ((buffer-read-only nil)
  1294. (bible-has-lexemes nil)
  1295. (bible-has-morphemes nil))
  1296. (erase-buffer)
  1297. (insert (bible--exec-diatheke (concat bible--current-book-name ":" (number-to-string bible--current-chapter))))
  1298. ;; Parse the xml in the buffer into a DOM tree.
  1299. (let ((html-dom-tree (libxml-parse-html-region (point-min) (point-max))))
  1300. ;; Render the DOM tree into the buffer.
  1301. (unless bible-debugme ; If this is true, display the XML.
  1302. (erase-buffer)
  1303. (setq-local bible-chapter-title nil)
  1304. ;; Looking for the "body" tag in the DOM node.
  1305. (bible--insert-domnode-recursive (dom-by-tag html-dom-tree 'body))
  1306. (goto-char (point-min))))
  1307. (save-excursion
  1308. (let ((search-string (concat " *" (car bible--current-book) " " (number-to-string bible--current-chapter) ":")))
  1309. ;; Delete <Book Ch:> at beginning of verse, just leave verse number.
  1310. (while (re-search-forward search-string nil t)
  1311. (replace-match "")
  1312. (bible-new-line)
  1313. ;; Highlight verse number
  1314. (when (re-search-forward " *[0-9]+:" nil t 1)
  1315. (add-face-text-property (match-beginning 0) (match-end 0) '(:foreground "purple"))))))
  1316. (save-excursion
  1317. ;; Fix divine name lossage.
  1318. (while (re-search-forward "Lord LORD" nil t)
  1319. (replace-match "LORD")
  1320. (add-face-text-property (point) (- (point) 4) 'bold))
  1321. (while (re-search-forward "Lord.+s LORD" nil t -1)
  1322. (replace-match "LORD's")
  1323. (add-face-text-property (1- (point)) (- (point) 5) 'bold))
  1324. ;; Remove the module name from the buffer.
  1325. (while (re-search-forward (concat "^.*" bible-text ".*$") nil t)
  1326. (replace-match ""))
  1327. (delete-blank-lines))
  1328. (save-excursion
  1329. (format-replace-strings '(("." . ". ")
  1330. ("," . ", ")
  1331. (";" . "; ")
  1332. (":" . ": ")
  1333. ("?" . "? ")
  1334. ("!" . "! ")
  1335. (" ." . ". ")
  1336. (" ," . ", ")
  1337. (" ;" . "; ")
  1338. (" :" . ": ")
  1339. (" ?" . "? ")
  1340. (" !" . "! ")
  1341. ("“ " . "“")
  1342. ("‘ " . "‘")
  1343. (" ’" . "’")
  1344. (". ”" . ".”")
  1345. ("? ”" . "?”"))
  1346. nil (point-min) (point-max)))
  1347. ;; Get rid of multiple consecutive spaces.
  1348. (save-excursion
  1349. (while (re-search-forward " *" nil t) ; More than one space in a row
  1350. (just-one-space)))
  1351. ;; Set the mode line of the biffer.
  1352. (if bible-has-lexemes
  1353. (unless (string-match " Lex" mode-name) (setq mode-name (concat mode-name " Lex")))
  1354. (setq mode-name (replace-regexp-in-string " Lex" "" mode-name)))
  1355. (if bible-has-morphemes
  1356. (unless (string-match " Morph" mode-name) (setq mode-name (concat mode-name " Morph")))
  1357. (setq mode-name (replace-regexp-in-string " Morph" "" mode-name)))
  1358. (force-mode-line-update))
  1359. ;; If optional verse specification go to that verse.
  1360. (when verse
  1361. (re-search-forward (concat " ?" (number-to-string verse)) nil t)))
  1362. ;;;; Modules (Bible texts, commentaries)
  1363. (defun compare-module-names (n1 n2)
  1364. "Compare N1 and N2, ignoring case, using collation order."
  1365. (string-collate-lessp n1 n2 nil t))
  1366. (defun bible--get-biblical-modules ()
  1367. "Populate `bible--texts' and `bible--commentaries' lists."
  1368. (let ((lines
  1369. (split-string
  1370. (bible--exec-diatheke "modulelist" nil "plain" "system")
  1371. "[\n\r]+"))
  1372. (texts nil)
  1373. (commentaries nil)
  1374. (doing-texts nil)
  1375. (doing-commentaries nil))
  1376. (setq bible--texts nil)
  1377. (setq bible--commentaries nil)
  1378. (catch 'done
  1379. (dolist (line lines)
  1380. (when doing-texts
  1381. (push (split-string line " : ") texts))
  1382. (when doing-commentaries
  1383. (push (split-string line " : ") commentaries))
  1384. (when (string-equal line "Biblical Texts:")
  1385. (setq doing-texts t))
  1386. (when (string-equal line "Commentaries:")
  1387. (setq doing-texts nil)
  1388. (pop texts) ; Remove `Commentaries:' line from `bible--texts'.
  1389. (setq doing-commentaries t))
  1390. (when (string-equal line "Lexicons / Dictionaries:")
  1391. (pop commentaries) ; Remove `Lexicons / Dictionaries:' line
  1392. ; from bible--commentaries.
  1393. (throw 'done nil))))
  1394. (setq bible--texts (cl-sort texts #'compare-module-names :key #'car))
  1395. (setq bible--commentaries (cl-sort commentaries #'compare-module-names :key #'car)))
  1396. nil)
  1397. (defun bible--list-biblical-texts ()
  1398. "Return a list of accessible Biblical Text modules."
  1399. (bible--get-biblical-modules) ; Make sure the lists are fresh.
  1400. bible--texts)
  1401. (defun bible--list-biblical-commentaries ()
  1402. "Return a list of accessible Biblical Text modules."
  1403. (bible--get-biblical-modules) ; Make sure the lists are fresh.
  1404. bible--commentaries)
  1405. (defun bible-display-available-texts ()
  1406. "Display available modules, allow user to select."
  1407. (interactive)
  1408. (bible--get-biblical-modules) ; Make sure lists are fresh.
  1409. (with-current-buffer (get-buffer-create "Texts")
  1410. (bible-text-select-mode)
  1411. (let ((buffer-read-only nil))
  1412. (erase-buffer)
  1413. (setq-local tab-stop-list '(25))
  1414. (dolist (text bible--texts)
  1415. (let ((name (string-trim (car text)))
  1416. (description (string-trim-left (cadr text))))
  1417. (insert
  1418. (propertize (string-trim name)
  1419. 'face 'bold
  1420. 'module name
  1421. 'help-echo (concat "Select " name)
  1422. 'keymap bible-text-map))
  1423. (move-to-tab-stop)
  1424. (insert (format "%s\n" description)))))
  1425. (goto-char (point-min))
  1426. (pop-to-buffer (current-buffer) nil t)))
  1427. (defun bible-display-available-commentaries ()
  1428. "Display available modules, allow user to select."
  1429. (interactive)
  1430. (bible--get-biblical-modules) ; Make sure lists are fresh.
  1431. (with-current-buffer (get-buffer-create "Commentaries")
  1432. (bible-text-select-mode)
  1433. (let ((buffer-read-only nil))
  1434. (erase-buffer)
  1435. (setq-local tab-stop-list '(25))
  1436. (dolist (commentary bible--commentaries)
  1437. (let ((name (string-trim (car commentary)))
  1438. (description (string-trim-left (cadr commentary))))
  1439. (insert
  1440. (propertize (string-trim name)
  1441. 'face 'bold
  1442. 'module name
  1443. 'help-echo (concat "Select " name)
  1444. 'keymap bible-text-map))
  1445. (move-to-tab-stop)
  1446. (insert (format "%s\n" description)))))
  1447. (goto-char (point-min))
  1448. (pop-to-buffer (current-buffer) nil t)))
  1449. ;;;; Bible Searching
  1450. (defun bible--open-search (query searchmode module)
  1451. "Open a search buffer of QUERY using SEARCHMODE in module MODULE."
  1452. (let ((results (string-trim (replace-regexp-in-string
  1453. "Entries .+?--" ""
  1454. (bible--diatheke-search query searchmode "plain" module)))))
  1455. (if (equal results (concat "none (" module ")"))
  1456. (message (concat
  1457. "No results found."
  1458. (when (equal searchmode "lucene")
  1459. " Verify index has been build with mkfastmod.")))
  1460. (with-current-buffer (get-buffer-create (generate-new-buffer-name (concat "*bible-search*")))
  1461. (bible-search-mode)
  1462. (bible--display-search results module)
  1463. (setq-local bible-search-word-this-query query
  1464. bible-search-text-this-query module
  1465. bible-search-range-this-query bible-search-range)
  1466. (pop-to-buffer (current-buffer) nil t)))))
  1467. (defun bible--display-search (results module)
  1468. "Render RESULTS of search query with MODULE."
  1469. (let ((match 0)
  1470. (matchstr "")
  1471. (verses nil)
  1472. (query-verses "")
  1473. (buffer-read-only nil))
  1474. ;; (message "display-search %s" module)
  1475. (setq-default bible-text module)
  1476. (erase-buffer)
  1477. (while match
  1478. (setq match (string-match ".+?:[0-9]?[0-9]?" results (+ match (length matchstr)))
  1479. matchstr (match-string 0 results))
  1480. (when match
  1481. (push
  1482. ;; Massage match to make it more sortable, get rid of some characters.
  1483. (replace-regexp-in-string
  1484. ".+; " ""
  1485. (string-replace
  1486. "I " "1"
  1487. (string-replace
  1488. "II " "2"
  1489. (string-replace
  1490. "III " "3"
  1491. matchstr))))
  1492. verses)))
  1493. (setq verses (cl-sort verses #'string-version-lessp))
  1494. (dolist (verse verses)
  1495. (if query-verses
  1496. (setq query-verses (concat query-verses ";" verse))
  1497. (setq query-verses verse)))
  1498. (let ((bible-show-diatheke-exec nil))
  1499. (insert (bible--exec-diatheke query-verses nil nil module)))
  1500. (let* ((html-dom-tree (libxml-parse-html-region (point-min) (point-max))))
  1501. (erase-buffer)
  1502. (bible--insert-domnode-recursive (dom-by-tag html-dom-tree 'body)))
  1503. (goto-char (point-min))
  1504. (save-excursion
  1505. ;; Remove module name from buffer.
  1506. (while (re-search-forward (concat "^.*" module ".*$") nil t)
  1507. (replace-match ""))
  1508. (delete-blank-lines))
  1509. (setq mode-name "Bible Search ")
  1510. (setq-local bible-search-matches (length verses))))
  1511. ;;;; Terms (lemmas, morphemes)
  1512. (defun bible--get-lemma (language strongs)
  1513. "Get the lemma from lexicon for LANGUAGE for strong's term STRONGS.
  1514. Used to display lemmas in mode lines. Assumes that StrongsHebrew
  1515. and StrongsGreek lexicons have been installed."
  1516. (let ((lemma-entry
  1517. (pcase language
  1518. ('hebrew
  1519. ;; Use Strong's Hebrew lexicon to look up Hebrew lemma.
  1520. (bible--lex-query strongs "StrongsHebrew" ))
  1521. ('greek
  1522. ;; Use Strong's Greek lexicon to look up Greek lemma.
  1523. (bible--lex-query strongs "StrongsGreek")))))
  1524. (unless (equal lemma-entry "")
  1525. ;; Entry will look like <num>: <num>. <lemma> <definition>. Get
  1526. ;; rid of everything before and after <lemma>.
  1527. (let* ((lemma-line (split-string lemma-entry))
  1528. (lemma (caddr lemma-line)))
  1529. lemma))))
  1530. (defun bible--display-greek ()
  1531. "Display Greek term.
  1532. This command is run by clicking on text, not directly by the user."
  1533. (interactive)
  1534. (let ((item (car (split-string (get-text-property (point) 'strong)))))
  1535. ;; Remove "strong:G" prefix
  1536. (bible--open-term-greek (replace-regexp-in-string "strong:G" "" item))))
  1537. (defun bible--display-hebrew ()
  1538. "Display Hebrew term.
  1539. This command is run by clicking on text, not directly by the user."
  1540. (interactive)
  1541. (let ((item (car (split-string (get-text-property (point) 'strong)))))
  1542. ;; Remove "strong:H" prefix and any alphabetic suffixes.
  1543. (bible--open-term-hebrew (replace-regexp-in-string "strong:H" "" item))))
  1544. ;;(defun bible-display-morphology (morph)
  1545. ;; ;; REVIEW: Do something here? (FMG 5-Mar-2026)
  1546. ;; )
  1547. (defun bible--fixup-lexicon-display (_termtype)
  1548. "Fixup the display of a lexical entry whose language is given by TERMTYPE."
  1549. (let ((buffer-read-only nil))
  1550. (goto-char (point-min))
  1551. ;; This enables clicking on verse references.
  1552. (save-excursion
  1553. (while (search-forward-regexp bible--verse-regexp nil t)
  1554. (let ((match (match-string 0))
  1555. (start (match-beginning 0))
  1556. (end (match-end 0)))
  1557. (put-text-property start end 'xref match)
  1558. (put-text-property start end 'keymap bible-term-mode-map)
  1559. (put-text-property start end 'help-echo (concat "Go to " (substring-no-properties match)))
  1560. (add-face-text-property start end '(:foreground "blue")))))
  1561. (save-excursion
  1562. (while (search-forward "()" nil t)
  1563. (replace-match ""))
  1564. (delete-blank-lines))))
  1565. (defun bible--open-term-hebrew (term)
  1566. "Open a buffer of the Strong's Hebrew TERM's definition."
  1567. (with-current-buffer (get-buffer-create (generate-new-buffer-name "*bible-term*"))
  1568. (bible-term-hebrew-mode)
  1569. (setq-local bidi-paragraph-direction 'left-to-right)
  1570. (setq-local mode-name (concat (bible--get-lemma 'hebrew term) " Term (Hebrew)"))
  1571. (bible--display-lemma-hebrew term)
  1572. (pop-to-buffer (current-buffer) nil t)
  1573. (fit-window-to-buffer)))
  1574. (defun bible--display-lemma-hebrew (lemma)
  1575. "Render the definition of the Strong's Hebrew LEMMA.
  1576. This code is customized for the BDBGlosses_Strongs lexicon."
  1577. (let ((buffer-read-only nil))
  1578. (erase-buffer)
  1579. ;; BDBGlosses_Strongs needs the prefixed `H'.
  1580. (insert (substring (bible--cleanup-lex-text (bible--lookup-lemma-hebrew (concat "H" lemma))) 7))
  1581. (bible--fixup-lexicon-display 'hebrew)))
  1582. (defun bible--open-term-greek (term)
  1583. "Open a buffer of the Strong's Greek TERM definition."
  1584. (with-current-buffer (get-buffer-create (generate-new-buffer-name "*bible-term*"))
  1585. (bible-term-greek-mode)
  1586. (setq-local mode-name (concat (bible--get-lemma 'greek term) " Term (Greek)"))
  1587. (bible--display-lemma-greek term)
  1588. (pop-to-buffer (current-buffer) nil t)
  1589. (fit-window-to-buffer)))
  1590. (defun bible--display-lemma-greek (lemma)
  1591. "Render the definition of the Strong's Greek LEMMA."
  1592. (let ((buffer-read-only nil))
  1593. (erase-buffer)
  1594. (insert (bible--lookup-lemma-greek lemma))
  1595. (bible--fixup-lexicon-display 'greek)))
  1596. ;;;; Utilities
  1597. (defun bible-new-line ()
  1598. "Ensure beginning of line. Try to avoid redundant blank lines."
  1599. (unless (bolp)
  1600. (newline)))
  1601. (defun bible--remove-module-name (module-name string)
  1602. "Remove parenthesized MODULE-NAME from STRING.
  1603. Also deals with bug where some versions of diatheke return string that
  1604. is missing close parenthesis."
  1605. (replace-regexp-in-string (concat "^(" module-name ".*$") "" string))
  1606. (defun bible--list-number-range (min max &optional prefix)
  1607. "Returns a list containing entries for each integer between MIN and MAX.
  1608. If PREFIX is supplied, prepend PREFIX to the entries.
  1609. Used in tandem with `completing-read' for chapter selection."
  1610. (let ((range-list nil))
  1611. (dotimes (num (1+ max))
  1612. (when (>= num min)
  1613. (push (cons (concat prefix (number-to-string num)) num) range-list)))
  1614. (nreverse range-list)))
  1615. ;;; Provides
  1616. (provide 'bible)
  1617. ;;; bible.el ends here.