bible.el 72 KB

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