[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/scanner e8310f5 52/56: factor out non-needed globals
From: |
Stefan Monnier |
Subject: |
[elpa] externals/scanner e8310f5 52/56: factor out non-needed globals |
Date: |
Fri, 10 Apr 2020 13:56:07 -0400 (EDT) |
branch: externals/scanner
commit e8310f580cbabc08f6b839e3b30c2d5e263afb17
Author: Raffael Stocker <address@hidden>
Commit: Raffael Stocker <address@hidden>
factor out non-needed globals
---
Readme.org | 2 +
scanner-test.el | 84 ++++++++++++++++++++++++--------------
scanner.el | 122 ++++++++++++++++++++++++--------------------------------
3 files changed, 108 insertions(+), 100 deletions(-)
diff --git a/Readme.org b/Readme.org
index ccc469f..73c3a6d 100644
--- a/Readme.org
+++ b/Readme.org
@@ -40,3 +40,5 @@
- This package doesn't support document feeders yet.
- This package doesn't support authentication.
+ - If a new document scan is started while another is still running, the log
+ will be messed up a bit.
diff --git a/scanner-test.el b/scanner-test.el
index 7b4ab9b..6934ff4 100644
--- a/scanner-test.el
+++ b/scanner-test.el
@@ -73,37 +73,47 @@
(ert-deftest scanner-test-scanimage-args ()
"Test the argument list construction for scanimage."
;; minimum args list (no device-specific options are available)
- (let ((scanner--available-switches nil)
+ (let ((switches nil)
(scanner-image-format '(:image "fmt-img" :doc "fmt-doc"))
(scanner-device-name "devname")
(scanner-image-size '(200 250))
(-compare-fn #'string=))
;; known values are included with their switches
(should (-is-infix-p '("-d" "devname") (scanner--scanimage-args "file"
- :image)))
+ :image
+ switches)))
(should (-is-infix-p '("-o" "file") (scanner--scanimage-args "file"
- :image)))
- (should (-contains-p (scanner--scanimage-args "file" :image)
+ :image
+ switches)))
+ (should (-contains-p (scanner--scanimage-args "file" :image switches)
"--format=fmt-img"))
;; a provided image format overrides the default format
- (should (-contains-p (scanner--scanimage-args "file" :image "arg-fmt")
+ (should (-contains-p (scanner--scanimage-args "file" :image switches
+ "arg-fmt")
"--format=arg-fmt"))
;; device-specific options are not included in the argument list
- (should-not (-contains-p (scanner--scanimage-args "file" :image) "--mode"))
- (should-not (-contains-p (scanner--scanimage-args "file" :image)
+ (should-not (-contains-p (scanner--scanimage-args "file" :image switches)
+ "--mode"))
+ (should-not (-contains-p (scanner--scanimage-args "file" :image switches)
"--resolution"))
- (should-not (-contains-p (scanner--scanimage-args "file" :image) "-x"))
- (should-not (-contains-p (scanner--scanimage-args "file" :image) "-y"))
- (should-not (-contains-p (scanner--scanimage-args "file" :doc) "-x"))
- (should-not (-contains-p (scanner--scanimage-args "file" :doc) "-y"))
+ (should-not (-contains-p (scanner--scanimage-args "file" :image switches)
+ "-x"))
+ (should-not (-contains-p (scanner--scanimage-args "file" :image switches)
+ "-y"))
+ (should-not (-contains-p (scanner--scanimage-args "file" :doc switches)
+ "-x"))
+ (should-not (-contains-p (scanner--scanimage-args "file" :doc switches)
+ "-y"))
;; without format and device name, these are not in the args list
(let ((scanner-image-format nil)
(scanner-device-name nil))
- (should-not (-contains-p (scanner--scanimage-args "file" :image)
+ (should-not (-contains-p (scanner--scanimage-args "file" :image switches)
"--format="))
- (should-not (-contains-p (scanner--scanimage-args "file" :image) "-d"))))
+ (should-not (-contains-p (scanner--scanimage-args "file" :image
+ switches)
+ "-d"))))
;; image args list with device-specific args
- (let ((scanner--available-switches '("--resolution" "-x" "-y" "--mode"))
+ (let ((switches '("--resolution" "-x" "-y" "--mode"))
(scanner-image-format '(:image "fmt-img" :doc "fmt-doc"))
(scanner-resolution '(:doc 300 :image 600))
(scanner-scan-mode '(:image "Color" :doc "Gray"))
@@ -111,29 +121,43 @@
(scanner-paper-sizes '(:a4 (210 297)))
(scanner-image-size '(200 250))
(-compare-fn #'string=))
- (should (-is-infix-p '("-o" "file") (scanner--scanimage-args "file"
:image)))
- (should (-contains-p (scanner--scanimage-args "file" :image)
+ (should (-is-infix-p '("-o" "file") (scanner--scanimage-args "file" :image
+ switches)))
+ (should (-contains-p (scanner--scanimage-args "file" :image switches)
"--format=fmt-img"))
- (should (-contains-p (scanner--scanimage-args "file" :image "arg-fmt")
+ (should (-contains-p (scanner--scanimage-args "file" :image switches
+ "arg-fmt")
"--format=arg-fmt"))
- (should (-contains-p (scanner--scanimage-args "file" :image)
"--mode=Color"))
- (should (-contains-p (scanner--scanimage-args "file" :image)
+ (should (-contains-p (scanner--scanimage-args "file" :image switches)
+ "--mode=Color"))
+ (should (-contains-p (scanner--scanimage-args "file" :image switches)
"--resolution=600"))
- (should (-is-infix-p '("-x" "210") (scanner--scanimage-args "file" :doc)))
- (should (-is-infix-p '("-y" "297") (scanner--scanimage-args "file" :doc)))
- (should (-is-infix-p '("-x" "200") (scanner--scanimage-args "file"
:image)))
- (should (-is-infix-p '("-y" "250") (scanner--scanimage-args "file"
:image)))
- (should (-is-infix-p '("-o" "file") (scanner--scanimage-args "file" :doc)))
- (should (-contains-p (scanner--scanimage-args "file" :doc)
+ (should (-is-infix-p '("-x" "210") (scanner--scanimage-args "file" :doc
+ switches)))
+ (should (-is-infix-p '("-y" "297") (scanner--scanimage-args "file" :doc
+ switches)))
+ (should (-is-infix-p '("-x" "200") (scanner--scanimage-args "file" :image
+ switches)))
+ (should (-is-infix-p '("-y" "250") (scanner--scanimage-args "file" :image
+ switches)))
+ (should (-is-infix-p '("-o" "file") (scanner--scanimage-args "file" :doc
+ switches)))
+ (should (-contains-p (scanner--scanimage-args "file" :doc switches)
"--format=fmt-doc"))
- (should (-contains-p (scanner--scanimage-args "file" :doc "arg-fmt")
+ (should (-contains-p (scanner--scanimage-args "file" :doc switches
+ "arg-fmt")
"--format=arg-fmt"))
- (should (-contains-p (scanner--scanimage-args "file" :doc) "--mode=Gray"))
- (should (-contains-p (scanner--scanimage-args "file" :doc)
+ (should (-contains-p (scanner--scanimage-args "file" :doc switches)
+ "--mode=Gray"))
+ (should (-contains-p (scanner--scanimage-args "file" :doc switches)
"--resolution=300"))
(let ((scanner-image-size nil))
- (should-not (-contains-p (scanner--scanimage-args "file" :image) "-x"))
- (should-not (-contains-p (scanner--scanimage-args "file" :image)
"-y")))))
+ (should-not (-contains-p (scanner--scanimage-args "file" :image
+ switches)
+ "-x"))
+ (should-not (-contains-p (scanner--scanimage-args "file" :image
+ switches)
+ "-y")))))
(ert-deftest scanner-test-tesseract-args ()
diff --git a/scanner.el b/scanner.el
index 602eb61..f0f801d 100644
--- a/scanner.el
+++ b/scanner.el
@@ -277,41 +277,6 @@ These options are necessary for the full set of features
offered
by the scanner package. If one of these is missing, this is
ignored, but something may not work as expected."))
-(defconst scanner--device-option-re
- (eval-when-compile (regexp-opt scanner--device-specific-switches t)))
-
-(defvar scanner--available-switches
- nil
- "List of required options implemented by the device backend.")
-
-(defvar scanner--missing-switches
- nil
- "List of required options missing from the device backend.")
-
-(defun scanner--check-device-switches ()
- "Return available and missing options provided by the device.
-
-This function checks the SANE backend of the device selected by
-‘scanner-device-name’ against the required options. If
-‘scanner-device-name’ is nil, it attempts auto-detection. The
-return value is a list comprising a list of the available options
-and a list of the missing options. As a side effect, these
-results are cached in ‘scanner--available-switches’ and
-‘scanner--missing-switches’."
- (let ((-compare-fn #'string=)
- opts)
- (with-temp-buffer
- (apply #'call-process scanner-scanimage-program nil t nil "-A"
- (and scanner-device-name (list "-d" scanner-device-name)))
- (goto-char (point-min))
- (while (re-search-forward scanner--device-option-re nil t)
- (push (match-string 1) opts)))
- (setq scanner--available-switches (nreverse opts)
- scanner--missing-switches
- (-difference scanner--device-specific-switches
- scanner--available-switches))
- (list scanner--available-switches scanner--missing-switches)))
-
(defun scanner--detect-devices ()
"Return a list of auto-detected scanning devices.
@@ -324,10 +289,11 @@ name, the device type, and the vendor and model names."
(--filter (eql 3 (length it))
(mapcar (lambda (x) (split-string x "|")) scanners)))))
-(defun scanner--scanimage-args (outfile type &optional img-fmt)
+(defun scanner--scanimage-args (outfile type switches &optional img-fmt)
"Construct the argument list for scanimage(1).
-OUTFILE is the output filename and IMG-FMT is the output image
-format. TYPE is either ‘:image’ or ‘:doc’.
+OUTFILE is the output filename, TYPE is either ‘:image’ or
+‘:doc’, SWITCHES is a list of available device-dependent options
+and IMG-FMT is the output image format.
When scanning documents (type :doc), scanner uses the IMG-FMT
argument for the intermediate representation before conversion to
@@ -355,7 +321,7 @@ simply dropped."
(list "-x" (number-to-string (car size))))
((and "-y" (guard size))
(list "-y" (number-to-string (cadr size)))))
- scanner--available-switches)
+ switches)
scanner-scanimage-switches))))
(defun scanner--tesseract-args (input output-base)
@@ -370,41 +336,53 @@ extensions depending on the selected output options, see
scanner-tesseract-switches
scanner-tesseract-outputs)))
-(defconst scanner--image-extensions
- '(("jpeg" . "jpeg")
- ("jpg" . "jpeg")
- ("png" . "png")
- ("pnm" . "pnm")
- ("tiff" . "tiff")
- ("tif" . "tiff"))
- "List of known image filename extensions with aliases.")
-
(defun scanner--determine-image-format (extension)
"Determine image file format from EXTENSION.
If the extension is unknown, return the default format."
- (let ((ext (if extension (downcase extension) "")))
- (or (cdr (assoc ext scanner--image-extensions))
+ (let ((ext (if extension (downcase extension) ""))
+ (known-ext '(("jpeg" . "jpeg")
+ ("jpg" . "jpeg")
+ ("png" . "png")
+ ("pnm" . "pnm")
+ ("tiff" . "tiff")
+ ("tif" . "tiff"))))
+ (or (cdr (assoc ext known-ext))
(plist-get scanner-image-format :image))))
(defun scanner--ensure-init ()
"Ensure that scanning device is initialized.
If no scanning device has been configured or the configured
-device is not available, attempt auto-detection. Check for
-availability of required options."
- (unless (and scanner-device-name
- (eql 0 (call-process scanner-scanimage-program
- nil nil nil "-n"
- "-d" scanner-device-name)))
- (let ((num-devices (length (scanner--detect-devices))))
- (cond ((eql 0 num-devices)
- (user-error "No scanning device was found"))
- ((eql 1 num-devices)
- (setq scanner-device-name (caar scanner--detected-devices)))
- (t (call-interactively #'scanner-select-device)))))
- (scanner--check-device-switches)
- (when scanner--missing-switches
- (scanner--log "Some required options are not supported by the device: %S"
- scanner--missing-switches)))
+device is not available, attempt auto-detection and maybe ask for
+a device selection.
+
+This function checks the SANE backend of the selected device
+against the required options. The return value is a list of the
+available options."
+ (let ((-compare-fn #'string=)
+ (switches-re (eval-when-compile
+ (regexp-opt scanner--device-specific-switches t)))
+ opts)
+ (unless (and scanner-device-name
+ (eql 0 (call-process scanner-scanimage-program
+ nil nil nil "-n"
+ "-d" scanner-device-name)))
+ (let ((num-devices (length (scanner--detect-devices))))
+ (cond ((eql 0 num-devices)
+ (user-error "No scanning device was found"))
+ ((eql 1 num-devices)
+ (setq scanner-device-name (caar scanner--detected-devices)))
+ (t (call-interactively #'scanner-select-device)))))
+ (with-temp-buffer
+ (apply #'call-process scanner-scanimage-program nil t nil "-A"
+ (and scanner-device-name (list "-d" scanner-device-name)))
+ (goto-char (point-min))
+ (while (re-search-forward switches-re nil t)
+ (push (match-string 1) opts)))
+ (-when-let (missing (-difference scanner--device-specific-switches
+ opts))
+ (scanner--log "Some required options are not supported by the device: %S"
+ missing))
+ (nreverse opts)))
(defun scanner--log (msg &rest args)
"Write a log message MSG to the process log buffer.
@@ -504,17 +482,19 @@ If ‘scanner-device-name’ is nil or this device is
unavailable,
attempt auto-detection. If more than one scanning device is
available, ask for a selection interactively."
(interactive "P\nFDocument file name: ")
- (scanner--ensure-init)
(let ((doc-file (file-name-sans-extension filename))
(num-pages (prefix-numeric-value npages))
(fmt (plist-get scanner-image-format :doc))
+ (switches (scanner--ensure-init))
(file-list '())
(fl-file nil))
(cl-labels ((scanimage
()
(let* ((img-file (make-temp-file "scanner" nil (concat "."
fmt)))
(scanimage-args (scanner--scanimage-args img-file
- :doc fmt)))
+ :doc
+ switches
+ fmt)))
(push img-file file-list)
(make-process :name "Scanner (scanimage)"
:command `(,scanner-scanimage-program
@@ -590,13 +570,13 @@ If ‘scanner-device-name’ is nil or this device is
unavailable,
attempt auto-detection. If more than one scanning device is
available, ask for a selection interactively."
(interactive "P\nFImage file name: ")
- (scanner--ensure-init)
(let* ((fmt (scanner--determine-image-format filename))
(fname-base (file-name-sans-extension filename))
(fname-ext (if (file-name-extension filename)
(file-name-extension filename t)
(concat "." fmt)))
(num-scans (prefix-numeric-value nscans))
+ (switches (scanner--ensure-init))
(page-count 1))
(cl-labels ((scanimage
(multi-scan)
@@ -608,7 +588,9 @@ available, ask for a selection interactively."
(cl-incf page-count))
(concat fname-base fname-ext)))
(scanimage-args (scanner--scanimage-args img-file
- :image fmt)))
+ :image
+ switches
+ fmt)))
(scanner--log "Scanning image to file \"%s\"" img-file)
(make-process :name "Scanner (scanimage)"
:command `(,scanner-scanimage-program
- [elpa] externals/scanner eee1a77 54/56: remove useless type checks, (continued)
- [elpa] externals/scanner eee1a77 54/56: remove useless type checks, Stefan Monnier, 2020/04/10
- [elpa] externals/scanner c815ae0 42/56: make Makefile a bit more general, Stefan Monnier, 2020/04/10
- [elpa] externals/scanner 312b0f4 45/56: clean up scanner--scanimage-args, Stefan Monnier, 2020/04/10
- [elpa] externals/scanner b36caab 51/56: add menu entries and command for multiple scans, Stefan Monnier, 2020/04/10
- [elpa] externals/scanner af4923b 35/56: require menu-bar, Stefan Monnier, 2020/04/10
- [elpa] externals/scanner e9b14b5 39/56: clean up a few rather cosmetic issues, Stefan Monnier, 2020/04/10
- [elpa] externals/scanner f660947 50/56: add log warning about missing device options, Stefan Monnier, 2020/04/10
- [elpa] externals/scanner 4202783 49/56: add logging of process error outputs, Stefan Monnier, 2020/04/10
- [elpa] externals/scanner 56f13c7 53/56: clean up tesseract languages and outputs customization validation, Stefan Monnier, 2020/04/10
- [elpa] externals/scanner 8486db9 55/56: correct and clean up image scanning, Stefan Monnier, 2020/04/10
- [elpa] externals/scanner e8310f5 52/56: factor out non-needed globals,
Stefan Monnier <=
- [elpa] externals/scanner fff6573 38/56: implement page-reversal customization for document mode, Stefan Monnier, 2020/04/10