emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/ebdb 26ee1cb 330/350: Refactor snarfing


From: Eric Abrahamsen
Subject: [elpa] externals/ebdb 26ee1cb 330/350: Refactor snarfing
Date: Mon, 14 Aug 2017 11:47:05 -0400 (EDT)

branch: externals/ebdb
commit 26ee1cb834c49d88aec568ecf9ce71b260f06e74
Author: Eric Abrahamsen <address@hidden>
Commit: Eric Abrahamsen <address@hidden>

    Refactor snarfing
    
    * ebdb-snarf.el (ebdb-snarf): Don't set up the temp buffer here. Also,
      add additional argument determining whether to display or return the
      records found.
      (ebdb-snarf-collect): Do the temp buffer setup here.
    * ebdb-mua.el (ebdb-mua-snarf-article): Use new calling convention of
      ebdb-snarf to return and combine snarfed records from both the
      article body and the signature. (ebdb-mua-article-body,
      ebdb-mua-article-signature): The default return value of these two
      methods should be nil, not an empty string, that's unneccesarily
      complicated.
    * ebdb-test.el (ebdb-snarf-mail-and-name): Alter test accordingly.
---
 ebdb-mua.el   |  31 ++++++----
 ebdb-snarf.el | 178 ++++++++++++++++++++++++++++++----------------------------
 ebdb-test.el  |  25 ++++-----
 3 files changed, 124 insertions(+), 110 deletions(-)

diff --git a/ebdb-mua.el b/ebdb-mua.el
index a6f7d64..776a6f8 100644
--- a/ebdb-mua.el
+++ b/ebdb-mua.el
@@ -953,13 +953,17 @@ Dispatches on the value of major-mode."
 This method should NOT return the message headers, only the
 article text.  This is typically used for snarfing.")
 
+(cl-defmethod ebdb-mua-article-body ()
+  "Default version returns nil."
+  nil)
+
 (cl-defgeneric ebdb-mua-article-signature (major-mode)
   "Return the text of the signature of the current article.")
 
 ;; At the moment this is only implemented for Gnus.
 (cl-defmethod ebdb-mua-article-signature ()
-  "Default version returns nothing."
-  "")
+  "Default version returns nil."
+  nil)
 
 ;;;###autoload
 (defun ebdb-mua-update-records (&optional header-class all)
@@ -1093,22 +1097,29 @@ associate field information in it with the article 
sender.
 
 With a prefix arg, only snarf the signature."
   (interactive "P")
+  (ebdb-mua-prepare-article)
   (condition-case nil
       ;; If the MUA has already popped up a buffer, assume the records
       ;; displayed there are relevant to the article snarf.
-      (let* ((buf (get-buffer (ebdb-make-buffer-name)))
-            (all-recs (ebdb-update-records
+      (let* ((all-recs (ebdb-update-records
                        (ebdb-get-address-components)
                        'existing))
             (sender (ebdb-update-records
                      (ebdb-get-address-components 'sender)
                      'existing))
-            (signature (ebdb-mua-article-signature)))
-       (ebdb-mua-prepare-article)
-       (unless (or (null (stringp signature)) (string-blank-p signature))
-         (ebdb-snarf signature nil nil sender))
-       (unless arg
-         (ebdb-snarf (ebdb-mua-article-body) nil nil all-recs)))
+            (body (ebdb-mua-article-body))
+            (signature (ebdb-mua-article-signature))
+            (records
+             (delete-dups
+              (append
+               (when signature
+                 (ebdb-snarf signature nil nil sender t))
+               (when (and  body (null arg))
+                 (ebdb-snarf body nil nil all-recs t))))))
+
+       (if records
+           (ebdb-display-records records nil t nil (ebdb-popup-window))
+         (message "No snarfable data found")))
     (cl-no-applicable-method
      (message "Article snarfing doesn't work in this context."))))
 
diff --git a/ebdb-snarf.el b/ebdb-snarf.el
index 1bb9204..2ef98fb 100644
--- a/ebdb-snarf.el
+++ b/ebdb-snarf.el
@@ -79,7 +79,7 @@ groups."
   :type 'list)
 
 ;;;###autoload
-(defun ebdb-snarf (&optional string start end recs)
+(defun ebdb-snarf (&optional string start end recs ret)
   "Snarf text and attempt to display/update/create a record from it.
 
 If STRING is given, snarf the string.  If START and END are given
@@ -89,32 +89,35 @@ buffer positions, and snarf the region between.  If all 
three
 arguments are nil, snarf the entire current buffer.
 
 If RECORDS is present, it is a list of records that we assume may
-be relevant to snarfed field data."
-  (interactive)
-  (let ((str
-        (cond ((use-region-p)
-               (buffer-substring-no-properties
-                (region-beginning) (region-end)))
-              ((and (or start end) string)
-               (substring string start end))
-              ((and start end (null string))
-               (buffer-substring-no-properties start end))
-              (string
-               string)
-              (t
-               (buffer-string))))
-       records)
-    (with-temp-buffer
-      (insert (string-trim str))
-      (goto-char (point-min))
-      (setq records (ebdb-snarf-query
-                    (ebdb-snarf-collapse
-                     (ebdb-snarf-collect recs)))))
-    (when records
-      (ebdb-display-records records nil t nil (list (selected-window))))))
+be relevant to snarfed field data.
 
-(defun ebdb-snarf-collect (&optional records)
-  "Collect EBDB record information from the text of the current buffer.
+If RET is non-nil, return the records.  Otherwise display them."
+  (interactive)
+  (let* ((str
+         (cond ((use-region-p)
+                (buffer-substring-no-properties
+                 (region-beginning) (region-end)))
+               ((and (or start end) string)
+                (substring string start end))
+               ((and start end (null string))
+                (buffer-substring-no-properties start end))
+               (string
+                string)
+               (t
+                (buffer-string))))
+        (records
+         (ebdb-snarf-query
+          (ebdb-snarf-collapse
+           (ebdb-snarf-collect str recs)))))
+
+    (if (null ret)
+       (if records
+           (ebdb-display-records records nil t nil (list (selected-window)))
+         (message "No snarfable data found"))
+      records)))
+
+(defun ebdb-snarf-collect (str &optional records)
+  "Collect EBDB record information from string STR.
 
 This function will find everything that looks like field
 information, and do its best to organize it into likely groups.
@@ -169,66 +172,69 @@ list of other field instances.  Any element can be nil."
          "\\)"))
        bundle block name)
 
-    (while (re-search-forward big-re nil t)
-      (goto-char (match-beginning 0))
-      (setq block (= (point) (point-at-bol)))
-      (when (setq name
-                 (save-excursion
-                   (when (re-search-backward
-                          (concat
-                           "\\("
-                           (mapconcat #'identity
-                                      ebdb-snarf-name-re "\\|")
-                           "\\)")
-                          (save-excursion
-                            (if block
-                                (progn (forward-line -1)
-                                       (line-beginning-position))
-                              (point-at-bol)))
-                          t)
-                     ;; If something goes wrong with the
-                     ;; name, don't worry about it.
-                     (ignore-errors
-                       (ebdb-parse
-                        'ebdb-field-name
-                        (string-trim (match-string-no-properties 0)))))))
-       ;; If NAME matches one of the records that are already in
-       ;; BUNDLES, then assume we should be working with that record.
-       (dolist (b bundles)
-         (when (and (aref b 0)
-                    (string-match-p (ebdb-string name)
-                                    (ebdb-string (aref b 0))))
-           (setq bundle b))))
-
-      (unless bundle
-       (setq bundle (make-vector 3 nil))
-       (when name
-         (push name (aref bundle 1))))
-
-      (dolist (class ebdb-snarf-routines)
-       (dolist (re (cdr class))
-         (while (re-search-forward re (if block
-                                          (save-excursion
-                                            (forward-line)
-                                            (line-end-position))
-                                        (point-at-eol))
-                                   t)
-           (condition-case nil
-               (push (ebdb-parse
-                      (car class)
-                      (match-string-no-properties 1))
-                     (aref bundle 2))
-
-             ;; If a regular expression matches but the result is
-             ;; unparseable, that means the regexp is bad and should be
-             ;; changed.  Later, report these errors if `ebdb-debug' is
-             ;; true.
-             (ebdb-unparseable nil)))))
-      (when bundle
-       (push bundle bundles)
-       (setq bundle nil))
-      (when block
-       (beginning-of-line 2)))
+    (with-temp-buffer
+      (insert str)
+      (goto-char (point-min))
+      (while (re-search-forward big-re nil t)
+       (goto-char (match-beginning 0))
+       (setq block (= (point) (point-at-bol)))
+       (when (setq name
+                   (save-excursion
+                     (when (re-search-backward
+                            (concat
+                             "\\("
+                             (mapconcat #'identity
+                                        ebdb-snarf-name-re "\\|")
+                             "\\)")
+                            (save-excursion
+                              (if block
+                                  (progn (forward-line -1)
+                                         (line-beginning-position))
+                                (point-at-bol)))
+                            t)
+                       ;; If something goes wrong with the
+                       ;; name, don't worry about it.
+                       (ignore-errors
+                         (ebdb-parse
+                          'ebdb-field-name
+                          (string-trim (match-string-no-properties 0)))))))
+         ;; If NAME matches one of the records that are already in
+         ;; BUNDLES, then assume we should be working with that record.
+         (dolist (b bundles)
+           (when (and (aref b 0)
+                      (string-match-p (ebdb-string name)
+                                      (ebdb-string (aref b 0))))
+             (setq bundle b))))
+
+       (unless bundle
+         (setq bundle (make-vector 3 nil))
+         (when name
+           (push name (aref bundle 1))))
+
+       (dolist (class ebdb-snarf-routines)
+         (dolist (re (cdr class))
+           (while (re-search-forward re (if block
+                                            (save-excursion
+                                              (forward-line)
+                                              (line-end-position))
+                                          (point-at-eol))
+                                     t)
+             (condition-case nil
+                 (push (ebdb-parse
+                        (car class)
+                        (match-string-no-properties 1))
+                       (aref bundle 2))
+
+               ;; If a regular expression matches but the result is
+               ;; unparseable, that means the regexp is bad and should be
+               ;; changed.  Later, report these errors if `ebdb-debug' is
+               ;; true.
+               (ebdb-unparseable nil)))))
+       (when bundle
+         (push bundle bundles)
+         (setq bundle nil))
+       (when block
+         (beginning-of-line 2))))
     bundles))
 
 (defun ebdb-snarf-collapse (input)
diff --git a/ebdb-test.el b/ebdb-test.el
index ff862fa..3acea4f 100644
--- a/ebdb-test.el
+++ b/ebdb-test.el
@@ -282,20 +282,17 @@
           "Eric Abrahamsen can't hold his drink\n<address@hidden> is where you 
can write and tell him so."))
        result)
     (dolist (text test-texts)
-      (with-temp-buffer
-       (insert text)
-       (goto-char (point-min))
-       (setq result (car (ebdb-snarf-collect)))
-       (pcase result
-         (`[nil (,name) (,mail)]
-          (unless (string= (ebdb-string name) "Eric Abrahamsen")
-            (ert-fail (list (format "Parsing \"%s\" resulted in name %s"
-                                    text (ebdb-string name)))))
-          (unless (string= (ebdb-string mail) "address@hidden")
-            (ert-fail (list (format "Parsing \"%s\" resulted in mail %s"
-                                    text (ebdb-string mail))))))
-         (_ (ert-fail (list (format "Parsing \"%s\" resulted in %s"
-                                    text result)))))))))
+      (setq result (car (ebdb-snarf-collect text)))
+      (pcase result
+       (`[nil (,name) (,mail)]
+        (unless (string= (ebdb-string name) "Eric Abrahamsen")
+          (ert-fail (list (format "Parsing \"%s\" resulted in name %s"
+                                  text (ebdb-string name)))))
+        (unless (string= (ebdb-string mail) "address@hidden")
+          (ert-fail (list (format "Parsing \"%s\" resulted in mail %s"
+                                  text (ebdb-string mail))))))
+       (_ (ert-fail (list (format "Parsing \"%s\" resulted in %s"
+                                  text result))))))))
 
 ;; Search testing.
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]