bible.el 66 KB

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