bible.el 66 KB

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