bible.el 56 KB

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