bible-mode.el 51 KB

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