bible-mode.el 45 KB

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