Prechádzať zdrojové kódy

Cleanups, remove hash table size (unnecessarily large)

Fred Gilham 2 týždňov pred
rodič
commit
c148bde13b
1 zmenil súbory, kde vykonal 83 pridanie a 89 odobranie
  1. 83 89
      bible.el

+ 83 - 89
bible.el

@@ -699,21 +699,26 @@ Mostly in Psalms, like `Of David' or the like.")
 ;;;; Greek and Hebrew lexeme and morpheme tooltip rendering.
 
 ;;;;; Hash tables for Lexical definitions.
-(defvar bible-hash-greek (make-hash-table :test 'equal :size 10000))
-(defvar bible-hash-hebrew (make-hash-table :test 'equal :size 10000))
+(defvar bible-hash-greek (make-hash-table :test 'equal))
+(defvar bible-hash-hebrew (make-hash-table :test 'equal))
 
-;;;;; Hash tables for tooltips.
-(defvar lex-hash (make-hash-table :test 'equal :size 10000))
-(defvar morph-hash (make-hash-table :test 'equal :size 10000))
+;;;;; Hash table for short lexical definitions for tooltips.
+(defvar lex-hash (make-hash-table :test 'equal))
+;;;;; Hash table for morphological items.
+(defvar morph-hash (make-hash-table :test 'equal))
 
 (defvar bible-outline-strings
   '(;;(". ."    .       ".")
     (" I. ."    .       "\nI.")
+    (" .I. "    .       "\nI.")
+    ("1. ."     .       "\n 1.")
+    ("a. ."     .       "\n    a.")
+    ("(a)"      .       "\n    (a)")
+    ("(α)"      .       "\n       (α)")
     (" II. ."   .       " II.")
     (" III. ."  .       " III.")
     (" IV. ."   .       " IV.")
     (" V. ."    .       " V.")
-    ("1. ."     .       "\n 1.")
     ("2. ."     .       "2.")
     ("3. ."     .       "3.")
     ("4. ."     .       "4.")
@@ -722,8 +727,6 @@ Mostly in Psalms, like `Of David' or the like.")
     ("7. ."     .       "7.")
     ("8. ."     .       "8.")
     ("9. ."     .       "9.")
-    ("a. ."     .       "\n    a.")
-    ("(a)."     .       "\n    (a).")
     ("b. ."     .       "    b.")
     ("c. ."     .       "    c.")
     ("d. ."     .       "    d.")
@@ -732,7 +735,9 @@ Mostly in Psalms, like `Of David' or the like.")
     ("g. ."     .       "    g.")
     ("h. ."     .       "    h.")
     (" . "      .       ". ")
-    ("\n\n"     .       "\n")))
+    (".."      .       ".")
+    ("\n\n"     .       "\n"))
+"String replacements to clean up outlines in lexicon entries.")
 
 ;;;;; Variables for mode-line-format for search buffers.
 (defvar-local bible-search-word-this-query "")
@@ -879,15 +884,15 @@ for any synchronized buffers."
 
 (defun bible-next-word (next-word)
   "Move forward a word, taking into account the relevant text properties.
-Note that we stay on the word so that clicking or hitting return will
-bring up the term dispplay for that word."
+Stay on the word so that clicking or hitting return will bring up the
+term dispplay for that word."
   (interactive (list (unless (eobp) (text-property-search-forward 'strong nil nil t))))
   (when next-word (goto-char (1- (prop-match-end next-word)))))
 
 (defun bible-previous-word (previous-word)
   "Move back a word, taking into account the relevant text properties.
-Note that we stay on the word so that clicking or hitting return will
-bring up the term dispplay for that word."
+Stay on the word so that clicking or hitting return will bring up the
+term dispplay for that word."
   (interactive (list (unless (bobp) (text-property-search-backward 'strong))))
   (when previous-word (goto-char (prop-match-beginning previous-word))))
 
@@ -949,7 +954,7 @@ Called by the menu item `Select Commentary'."
 
 (defun bible--new-window (name)
   "Open a new window and generate a new buffer with NAME.
-Returns the new buffer."
+Return the new buffer."
   (split-window-right)
   (balance-windows)
   (other-window 1)
@@ -1047,9 +1052,8 @@ window."
       (setq bible-search-range range))))
 
 (defun bible-search (query searchmode)
-  "Search for a QUERY with search mode SEARCHMODE.
-Searches are done using the diatheke search modes. See diatheke for 
-more information.
+  "Search for a QUERY with search mode SEARCHMODE. Use one of the diatheke
+search modes. See diatheke for more information.
 First ask for the query and then ask the user for search mode: either
 `lucene', `phrase', `regex' or `multiword'.  `lucene' is the default search.
 `lucene' mode requires an index to be built for the module being searched
@@ -1067,8 +1071,8 @@ using the `mkfastmod' program."
   "Find the canonical book entry for NAME in bible--books. First check in
 the bible--books list to see if NAME is the canonical name. If not,
 check if NAME is an abbreviation. If so, look up the abbreviation and
-get the canonical name from there. Once we have the canonical name, use
-that to look up the book entry."
+get the canonical name from there. Use the canonical name to look up the
+book entry."
   (or (cl-find name 
                bible--books :key #'car :test #'string-equal-ignore-case)
       (cl-find (alist-get name bible--book-name-abbreviations nil nil #'string-equal-ignore-case)
@@ -1187,7 +1191,7 @@ MODULE is the text module to use and defaults to the current module."
 ;; Use HTMLHREF format with diatheke, post-process to render html.
 (defun bible--morph-query (query module)
   "Execute `diatheke' to do morph QUERY, using MODULE.
-Render HTML, return string.  Do some tweaking specific to morphology."
+Render html, return string.  Do some tweaking specific to morphology."
   (with-temp-buffer
     (let ((args (list bible-sword-query nil (current-buffer) t "-b" module "-o" "m" "-f" "HTMLHREF" "-k" query)))
       (when bible-show-diatheke-exec
@@ -1195,7 +1199,7 @@ Render HTML, return string.  Do some tweaking specific to morphology."
       (apply #'call-process args)
       (shr-render-region (point-min) (point-max))
       (format-replace-strings
-       '(("\n:" . "")              ; This makes the Packard morphology display look better.
+       '(("\n:" . "")                 ; This makes the Packard morphology display look better.
          ("Part of Speech" . ""))     ; This helps the Robinson display look better.
          nil (point-min) (point-max))
       (substring (buffer-string) (1+ (length query)))))) ; This tries to get rid of unnecessary query identifier.
@@ -1222,7 +1226,7 @@ First, find the links put in by diatheke's HTMLHREF output format.
 Replace the links with verse references that get changed to clickable
 cross-references when the term is displayed.
 
-The verse refs look like this: <bookname>.<chapter>.<verse>.  We convert
+The verse refs look like this: <bookname>.<chapter>.<verse>. Convert
 them to the <bookname> <chapter>:<verse> format."
   (goto-char (point-min))
   (while (re-search-forward "<a href=\"passagestudy.*?</a>" nil t) ; HTMLHREF cross references.
@@ -1254,55 +1258,52 @@ them to the <bookname> <chapter>:<verse> format."
 
 
 (defun bible--cleanup-lex-text (lex-text)
-  "Reformat tooltip text LEX-TEXT so tooltips look nice."
-  (dolist (outline-string bible-outline-strings)
-    (setq lex-text (string-replace (car outline-string) (cdr outline-string) lex-text)))
-  lex-text)
+  "Reformat lexical entry text LEX-TEXT to look nice."
+  (let ((return-string (substring lex-text)))
+    (dolist (outline-string bible-outline-strings return-string)
+      (setq return-string (string-replace (car outline-string) (cdr outline-string) return-string)))))
 
 (defun bible--lookup-def-greek (key)
   "Execute `diatheke' to do query on KEY.
 Massage output so verse cross references are usable.  Returns string."
   (with-temp-buffer
-    (let ((args (list bible-sword-query nil (current-buffer) t "-b" bible-greek-lexicon "-o" "m" "-f" "plain" "-k" key)))
+    (let ((args (list bible-sword-query nil (current-buffer) t "-b" bible-greek-lexicon "-f" "plain" "-k" key)))
       (when bible-show-diatheke-exec
         (message "%s" args))
       (apply #'call-process args)
-      (bible--cleanup-lex-text 
+      (bible--cleanup-lex-text
        (bible--remove-module-name 
         bible-greek-lexicon 
-        (replace-regexp-in-string
-         ".*[0-9]+: [0-9]+. " ""
-         (buffer-string)))))))
+        (buffer-string))))))
 
 (defun bible--lookup-lemma-index (key)
   "Return the Greek lemma from lemma index with a strong's number as KEY."
   (string-trim
    (bible--remove-module-name bible-lexicon-index (bible--lex-query key bible-lexicon-index))))
 
-(defun bible--lookup-lemma-greek-indexed (key)
-  "Lookup Greek lemma using Strong's number KEY.
-Then look up the definition of that lemma.  Used when two-stage
-lexical definition is set for a particular lexicon."
-  (let ((lemma-entry (bible--lookup-lemma-index key))) ; Get lemma from Strong's number
-    (when lemma-entry
-      (let ((lemma (caddr (split-string lemma-entry " "))))
-        (bible--lookup-def-greek lemma)))))
+(defun bible--lookup-entry-greek (key)
+  "Lookup Greek lexicon entry using Strong's number KEY.
+If the lexicon has a lemma index, first use the Strong's number
+to look up the lemma. Then look up the lexicon entry of that lemma."
+  (let ((lex-key key))
+    (when bible-use-index-for-lexicon
+      (let ((lemma-entry (bible--lookup-lemma-index key))) ; Get lemma from Strong's number
+        (when lemma-entry
+          ;; Use lemma as key.
+          (setq lex-key (caddr (split-string lemma-entry " "))))))
+    (bible--lookup-def-greek lex-key)))
 
 (defun bible--lookup-lemma-greek (key)
   "Lookup lexical definition using Strong's number KEY.
-1. Check hash table first.  If entry found, return.
-2. Otherwise, if a lexicon is accessed by lemmas, do lookup using index method.
-3. Otherwise just use the Strong's number method."
+Check hash table first.  If entry found, return it. Otherwise 
+do the diatheke lookup."
   (or (gethash key bible-hash-greek)
-      (puthash key
-               (if bible-use-index-for-lexicon
-                   (bible--lookup-lemma-greek-indexed key)
-                 (bible--lookup-def-greek key))
-               bible-hash-greek)))
+      (puthash key (bible--lookup-entry-greek key) bible-hash-greek)))
 
 (defun bible--lookup-def-hebrew (key)
   "Execute `diatheke' to do query on KEY.
-Massage output so various cross references are usable. Returns string."
+Massage output so various cross references are usable. Return resulting
+string."
   (with-temp-buffer
     (let ((args (list bible-sword-query nil (current-buffer) t "-b" bible-hebrew-lexicon "-f" "plain" "-k" key)))
       (when bible-show-diatheke-exec
@@ -1314,30 +1315,23 @@ Massage output so various cross references are usable. Returns string."
 
 (defun bible--lookup-lemma-hebrew (key)
   "Lookup lexical definition using Strong's number KEY.
-1. Check hash table first.  If entry found, return.
-2. Otherwise, if a lexicon is accessed by lemmas, do lookup using index method.
-3. Otherwise just use the Strong's number method."
+Check hash table first.  If entry found, return it. Otherwisde 
+do the diatheke lookup."
   (or (gethash key bible-hash-hebrew)
-      (puthash key
-               (bible--lookup-def-hebrew key)
-               bible-hash-hebrew)))
+      (puthash key (bible--lookup-def-hebrew key) bible-hash-hebrew)))
 
-;; We use the shorter lexicons for text in tooltips. We also cache the
-;; lex and morph strings, hoping to speed up tooltip rendering.
+;; Use the shorter lexicons for text in tooltips. Cache the lex and
+;; morph strings, hoping to speed up tooltip rendering.
 
 (defun bible--lookup-lemma-short (lemma lexicon)
-  "Look up lexical entry for LEMMA in short LEXICON.
-Returns a string that is intended to be displayed in a tooltip.
-Uses short lexicon (e.g. StrongsRealHebrew or StrongsRealGreek)."
+  "Look up lexical entry for LEMMA in `short' LEXICON.
+Return a string that is intended to be displayed in a tooltip."
   (when (string-match "[0-9]+" lemma)
     (let ((string
-           (bible--remove-module-name 
-            lexicon 
-            ;; Get rid of unnecessary strongs codes at the beginning. This
-            ;; differs between lexicon versions.
-            (replace-regexp-in-string
-             ".*[0-9]+: [0-9]+. " ""
-             (bible--lex-query (concat (match-string 0 lemma)) lexicon)))))
+           (bible--remove-module-name
+            lexicon
+            ;; Get rid of unnecessary strongs codes at the beginning.
+            (substring (bible--lex-query (concat (match-string 0 lemma)) lexicon) 7))))
       string)))
 
 (defun bible--lookup-lex (lex)
@@ -1345,17 +1339,18 @@ Uses short lexicon (e.g. StrongsRealHebrew or StrongsRealGreek)."
 Return hash table entry if present in `lex-hash' cache, else look up in
 database and stash in cache."
   (when lex
-    (let* ((key (substring lex 7)) ; strip off "strong:" prefix.
-           (lex-text (gethash key lex-hash)))
-      (if lex-text
-          lex-text
-        (setq lex-text
+    (let* ((key (substring lex 7))  ; strip off "strong:" prefix.
+           (entry (gethash key lex-hash)))
+      (unless entry 
+        (setq entry 
               (cond ((string-prefix-p "G" key)
                      (bible--lookup-lemma-short key bible-greek-lexicon-short))
                     ((string-prefix-p "H" key)
                      (concat (string ?\x200e)
                              (bible--lookup-lemma-short key bible-hebrew-lexicon-short)))))
-        (puthash key (string-fill (bible--cleanup-lex-text lex-text) 75) lex-hash)))))
+        (puthash key (string-fill (bible--cleanup-lex-text entry) 75) lex-hash))
+      entry)))
+
 
 (defun bible--lookup-morph-entry (morph)
   "Look up entry for morphological item MORPH.
@@ -1381,8 +1376,8 @@ database and stash in cache."
 ;; Get string for tooltip display
 (defun bible--show-lex-morph (_window object pos)
   "Get text for tooltip display for OBJECT at POS in WINDOW.
-Includes both lex and morph definitions if text module has
-both tags, otherwise just get lex definition."
+Include both lex and morph definitions if text module has both tags,
+otherwise just get lex definition."
   (let* ((lex (get-text-property pos 'strong object))
          (lex-text (bible--lookup-lex lex))
          (morph (get-text-property pos 'morph object))
@@ -1869,7 +1864,6 @@ If optional argument VERSE is supplied, set cursor at verse."
     (pop-to-buffer (current-buffer) nil t)))
 
 
-
 
 ;;;; Bible Searching
 
@@ -1963,7 +1957,7 @@ installed."
 
 
 (defun bible--display-greek (term)
-  "Display Greek TERM..
+  "Display Greek TERM.
 This command is run by clicking on text, not directly by the user."
   (interactive (list (car (split-string (get-text-property (point) 'strong)))))
   ;; Remove "strong:G" prefix
@@ -1980,8 +1974,8 @@ This command is run by clicking on text, not directly by the user."
 ;; ;; REVIEW: Do something here? (FMG 5-Mar-2026)
 ;;  )
 
-(defun bible--fixup-lexicon-display (_termtype)
-  "Fixup the display of a lexical entry whose language is given by TERMTYPE."
+(defun bible--fixup-lexicon-display ()
+  "Fixup the display of a lexical entry."
   (let ((buffer-read-only nil))
     (goto-char (point-min))
     ;; This enables clicking on verse references.
@@ -2015,8 +2009,12 @@ This code is customized for the BDBGlosses_Strongs lexicon."
   (let ((buffer-read-only nil))
     (erase-buffer)
     ;; BDBGlosses_Strongs needs the prefixed `H'.
-    (insert (substring (bible--cleanup-lex-text (bible--lookup-lemma-hebrew (concat "H" lemma))) 7))
-    (bible--fixup-lexicon-display 'hebrew)))
+    (insert
+     (substring
+      (bible--cleanup-lex-text 
+       (bible--lookup-lemma-hebrew (concat "H" lemma)))
+      7))
+    (bible--fixup-lexicon-display)))
 
 (defun bible--open-term-greek (term)
   "Open a buffer of the Strong's Greek TERM definition."
@@ -2032,24 +2030,24 @@ This code is customized for the BDBGlosses_Strongs lexicon."
   (let ((buffer-read-only nil))
     (erase-buffer)
     (insert (bible--lookup-lemma-greek lemma))
-    (bible--fixup-lexicon-display 'greek)))
+    (bible--fixup-lexicon-display)))
 
 
 ;;;; Utilities
 
 (defun bible-new-line ()
-  "Ensure beginning of line.  Try to avoid redundant blank lines."
+  "Ensure beginning of line. Try to avoid redundant blank lines."
   (unless (bolp)
     (newline)))
 
 (defun bible--remove-module-name (module-name string)
   "Remove parenthesized MODULE-NAME from STRING.
-Also deals with bug where some versions of diatheke return string that
-is missing close parenthesis."
+Also deal with bug where some versions of diatheke return string that is
+missing close parenthesis."
   (replace-regexp-in-string (concat "^(" module-name ".*$") "" string))
 
 (defun bible--list-number-range (min max &optional prefix)
-  "Returns a list containing entries for each integer between MIN and MAX.
+  "Return a list containing entries for each integer between MIN and MAX.
 If PREFIX is supplied, prepend PREFIX to the entries.
 Used in tandem with `completing-read' for chapter selection."
   (let ((range-list nil))
@@ -2058,10 +2056,6 @@ Used in tandem with `completing-read' for chapter selection."
         (push (cons (concat prefix (number-to-string num)) num) range-list)))
     (nreverse range-list)))
 
-;;; Provides
-
 (provide 'bible)
 
-
-
 ;;; bible.el ends here.