bible.el 60 KB

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