Hi all,

[Breif summary of this message]
Is it acceptable to change a little the implicit specification of
EXTENSION argument of `TeX-search-files-kpathsea'?

[Detail]
I'm currently considering to adjust
`LaTeX-includegraphics-read-file-TeX' in style/graphicx.el to use
`TeX-search-files-by-type'.  It now uses only `TeX-search-files', so it
doesn't benefit from kpathsea library.

At first I thought it is a simple job of replacing `TeX-search-files'
with `TeX-search-files-by-type' at a glance of
`LaTeX-search-files-type-alist' having "graphics" entry:
----------------------------------------------------------------------
(defcustom LaTeX-search-files-type-alist
  '((texinputs "${TEXINPUTS.latex}" ("tex/generic/" "tex/latex/")
               TeX-file-extensions)
    (docs "${TEXDOCS}" ("doc/") TeX-doc-extensions)
    (graphics "${TEXINPUTS}" ("tex/") LaTeX-includegraphics-extensions)
...
----------------------------------------------------------------------
However, it turned out not so easy because of small conflicts in
treatment of EXTENSIONS argument between `TeX-search-files' and
`TeX-search-files-kpathsea'.

(1) `TeX-search-files-kpathsea' treats EXTENSIONS as list of _literal_
extensions according to this line:
----------------------------------------------------------------------
           (setq extensions (concat "\\." (regexp-opt extensions t) "\\'")
----------------------------------------------------------------------
Thus we cannot supply regexps in EXTENSIONS.

(2) `TeX-search-files' hands over EXTENSIONS to `TeX-match-extension'
and `TeX-strip-extension'.  The latter hands over EXTENSIONS to the
former, thus it is `TeX-match-extension' that determines the treatment
of EXTENSIONS.  Since it does
----------------------------------------------------------------------
  (let ((regexp (concat "\\.\\("
                        (mapconcat 'identity extensions "\\|")
                        "\\)$"))
----------------------------------------------------------------------
, EXTENSIONS is regarded as a list of _regexps_.

`TeX-search-files-by-type' first tries `TeX-search-files-kpathsea' and
fallbacks on `TeX-search-files'.  It just gives the EXTENSION argument
to both functions as-is.  This doesn't cause much problem when EXTENSION
doesn't contain regexp meta chars.  For example, the default value of
`TeX-file-extensions' is ("tex" "sty" "cls" "ltx" "texi" "txi" "texinfo"
"dtx"), which is totally valid as both the literal extensions and the
regular expressions.

However, this is not the case for `LaTeX-includegraphics-extensions',
whose typical value contains "jpe?g".  Therefore, if
`LaTeX-includegraphics-extensions' is given as EXTENSION argument of
`TeX-search-files-kapthsea', the function never searches for files with
"jpeg" or "jpg" as their extension but instead files with _literal_
"jpe?g" extensions.

My current idea is to modify `TeX-search-files-kapthsea' so that it
treats EXTENSIONS argument as a list of regexps, not of literal
extensions.  The attached patch #1 implements this idea.  It also
adjusts related functions and :type arguments of relavant defcustom's.

Are these changes acceptable?  Or should I make out a more sophisticated
approach which preserves the default behavior of
`TeX-search-files-kapthsea'?  If OK, the attached patch #2 implements
actual change of using `TeX-search-files-by-type' in
`LaTeX-includegraphics-read-file-TeX' and #3 adds support for dvipdfmx
in graphicx.el.

N.B.  The patch #2 assumes the policy to drop support for older emacsen
so that it gives a list, not alist, as a completion table for
`completing-read'.

Best,
Ikumi Keita

>From [email protected] Tue Oct 03 18:27:13 2017
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: [PATCH] Resolve conflict of argument spec between regexp and literal
X-Mercurial-Node: 8e548b10f4be3a981513f53756cd28fca758826a
X-Mercurial-Series-Index: 1
X-Mercurial-Series-Total: 1
Message-Id: <[email protected]>
X-Mercurial-Series-Id: <[email protected]>
User-Agent: Mercurial-patchbomb/4.3.1
Date: Tue, 03 Oct 2017 18:27:13 +0900
From: Ikumi Keita <[email protected]>
To: dummy

* tex.el (TeX-search-files-kpathsea): Treat EXTENSIONS argument as a
list of regexps in accordance with other parts of AUCTeX.
(TeX-ispell-document): Treat `TeX-file-extensions' as a list of
regexps in accordance with other parts of AUCTeX.
(TeX-file-extensions,TeX-Biber-file-extensions):
(BibTeX-file-extensions,BibLaTeX-style-extensions):
(BibTeX-style-extensions): Change custom type to regexp from string.
(TeX-doc-extensions): Turn into list of regexps.
* latex.el (LaTeX-split-bibs): Treat `TeX-Biber-file-extensions' as a
list of regexps in accordance with other parts of AUCTeX.

diff --git a/latex.el b/latex.el
--- a/latex.el
+++ b/latex.el
@@ -1626,7 +1626,7 @@
     (dolist (bib bibs)
       (LaTeX-add-bibliographies (TeX-replace-regexp-in-string
 				 (concat "\\(?:\\."
-					 (mapconcat #'regexp-quote
+					 (mapconcat #'identity
 						    TeX-Biber-file-extensions
 						    "\\|\\.")
 					 "\\)")
diff --git a/tex.el b/tex.el
--- a/tex.el
+++ b/tex.el
@@ -4463,7 +4463,7 @@
 (defcustom TeX-file-extensions '("tex" "sty" "cls" "ltx" "texi" "txi" "texinfo" "dtx")
   "*File extensions used by manually generated TeX files."
   :group 'TeX-file-extension
-  :type '(repeat (string :format "%v")))
+  :type '(repeat (regexp :format "%v")))
 
 (defcustom TeX-all-extensions '("[^.\n]+")
   "All possible file extensions."
@@ -4478,8 +4478,8 @@
   (make-variable-buffer-local 'TeX-default-extension)
 
 (defvar TeX-doc-extensions
-  '("dvi" "pdf" "ps" "txt" "html" "dvi.gz" "pdf.gz" "ps.gz" "txt.gz" "html.gz"
-    "dvi.bz2" "pdf.bz2" "ps.bz2" "txt.bz2" "html.bz2")
+  '("dvi" "pdf" "ps" "txt" "html" "dvi\\.gz" "pdf\\.gz" "ps\\.gz" "txt\\.gz"
+    "html\\.gz" "dvi\\.bz2" "pdf\\.bz2" "ps\\.bz2" "txt\\.bz2" "html\\.bz2")
   "File extensions of documentation files.")
 
 (defcustom docTeX-default-extension "dtx"
@@ -4498,22 +4498,22 @@
 (defcustom TeX-Biber-file-extensions '("bib" "ris" "xml")
   "Valid file extensions for Biber files."
   :group 'TeX-file-extension
-  :type '(repeat (string :format "%v")))
+  :type '(repeat (regexp :format "%v")))
 
 (defcustom BibTeX-file-extensions '("bib")
   "Valid file extensions for BibTeX files."
   :group 'TeX-file-extension
-  :type '(repeat (string :format "%v")))
+  :type '(repeat (regexp :format "%v")))
 
 (defcustom BibLaTeX-style-extensions '("bbx")
   "Valid file extensions for BibLaTeX styles."
   :group 'TeX-file-extension
-  :type '(repeat (string :format "%v")))
+  :type '(repeat (regexp :format "%v")))
 
 (defcustom BibTeX-style-extensions '("bst")
   "Valid file extensions for BibTeX styles."
   :group 'TeX-file-extension
-  :type '(repeat (string :format "%v")))
+  :type '(repeat (regexp :format "%v")))
 
 (defun TeX-match-extension (file &optional extensions)
   "Return non-nil if FILE has one of EXTENSIONS.
@@ -4591,7 +4591,9 @@
 	  result)
       (if (eq scope 'global)
 	  (setq dirs (delete "./" dirs)))
-      (setq extensions (concat "\\." (regexp-opt extensions t) "\\'")
+      (setq extensions (concat "\\.\\(?:"
+			       (mapconcat #'identity extensions "\\|")
+			       "\\)\\'")
 	    result (apply #'append (mapcar (lambda (x)
 					     (when (file-readable-p x)
 					       (directory-files
@@ -6594,7 +6596,7 @@
 				   (cons (file-name-nondirectory name)
 					 (TeX-style-list)) "\\|")
 			"\\)\\.\\("
-			(mapconcat 'regexp-quote TeX-file-extensions "\\|")
+			(mapconcat #'identity TeX-file-extensions "\\|")
 			"\\)\\'"))
 	(buffers (buffer-list)))
     (while buffers


>From [email protected] Tue Oct 03 18:27:18 2017
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: [PATCH] Use `TeX-search-files-by-type' to assist input
 \includegraphics
X-Mercurial-Node: 9b9492ece766243fe64ba0a9f8ad1a948f7c9eca
X-Mercurial-Series-Index: 1
X-Mercurial-Series-Total: 1
Message-Id: <[email protected]>
X-Mercurial-Series-Id: <[email protected]>
User-Agent: Mercurial-patchbomb/4.3.1
Date: Tue, 03 Oct 2017 18:27:18 +0900
From: Ikumi Keita <[email protected]>
To: dummy

* style/graphicx.el (LaTeX-includegraphics-extensions-list): New
function.
(LaTeX-includegraphics-extensions): Delegate most of its role to the
above new function and become its wrapper.
(LaTeX-includegraphics-global-files): New variable.
(LaTeX-includegraphics-read-file-TeX): Use `TeX-search-files-by-type'
rather than `TeX-search-files' and cache global value in the above
new variable.
Reflect the option `LaTeX-includegraphics-strip-extension-flag'.
* tex.el (TeX-search-files-type-alist): Remove `graphics' entry,
which is supplied in latex-mode by `LaTeX-search-files-type-alist'.
(TeX-normal-mode): Add `LaTeX-includegraphics-global-files' to list of
variables to be cleared.

diff --git a/style/graphicx.el b/style/graphicx.el
--- a/style/graphicx.el
+++ b/style/graphicx.el
@@ -105,65 +105,84 @@
 			 LaTeX-graphicx-key-val-options))
      optional)))
 
+(defun LaTeX-includegraphics-extensions-list ()
+  "Return appropriate extensions for input files to \\includegraphics.
+Return value is a list of regexps."
+  (let ((temp (copy-sequence LaTeX-includegraphics-extensions)))
+    (cond (;; 'default TeX-engine:
+	   (eq TeX-engine 'default)
+	   (if ;; we want to produce a pdf
+	       (if TeX-PDF-mode
+		   ;; Return t if default compiler produces PDF,
+		   ;; nil for "Dvips" or "Dvipdfmx"
+		   (not (TeX-PDF-from-DVI))
+		 ;; t if pdftex is used in dvi-mode
+		 TeX-DVI-via-PDFTeX)
+	       ;; We're using pdflatex in pdf-mode
+	       (delete-dups
+		(append LaTeX-includegraphics-pdftex-extensions
+			temp))
+	     ;; We're generating a .dvi to process with dvips or dvipdfmx
+	     (progn
+	       (dolist (x '("jpe?g" "pdf" "png"))
+		 (setq temp (delete x temp)))
+	       (delete-dups
+		(append LaTeX-includegraphics-dvips-extensions
+			temp)))))
+	  ;; Running luatex in pdf or dvi-mode:
+	  ((eq TeX-engine 'luatex)
+	   (if TeX-PDF-mode
+	       (delete-dups
+		(append LaTeX-includegraphics-pdftex-extensions
+			temp))
+	     (progn
+	       (dolist (x '("jpe?g" "pdf" "png"))
+		 (setq temp (delete x temp)))
+	       (delete-dups
+		(append LaTeX-includegraphics-dvips-extensions
+			temp)))))
+	  ;; Running xetex in any mode:
+	  ((eq TeX-engine 'xetex)
+	   (delete-dups (append LaTeX-includegraphics-xetex-extensions
+				temp)))
+	  ;; For anything else
+	  (t
+	   temp))))
+
 (defun LaTeX-includegraphics-extensions (&optional list)
-  "Return appropriate extensions for input files to \\includegraphics."
-  (let* ((temp (copy-sequence LaTeX-includegraphics-extensions))
-	 (LaTeX-includegraphics-extensions
-	  (cond (;; 'default TeX-engine:
-		 (eq TeX-engine 'default)
-		 (if ;; we want to produce a pdf
-		     (if TeX-PDF-mode
-			 ;; Return t if default compiler produces PDF,
-			 ;; nil for "Dvips" or "Dvipdfmx"
-			 (not (TeX-PDF-from-DVI))
-		       ;; t if pdftex is used in dvi-mode
-		       TeX-DVI-via-PDFTeX)
-		     ;; We're using pdflatex in pdf-mode
-		     (delete-dups
-		      (append LaTeX-includegraphics-pdftex-extensions
-			      temp))
-		   ;; We're generating a .dvi to process with dvips or dvipdfmx
-		   (progn
-		     (dolist (x '("jpe?g" "pdf" "png"))
-		       (setq temp (delete x temp)))
-		     (delete-dups
-		      (append LaTeX-includegraphics-dvips-extensions
-			      temp)))))
-		;; Running luatex in pdf or dvi-mode:
-		((eq TeX-engine 'luatex)
-		 (if TeX-PDF-mode
-		     (delete-dups
-		      (append LaTeX-includegraphics-pdftex-extensions
-			      temp))
-		   (progn
-		     (dolist (x '("jpe?g" "pdf" "png"))
-		       (setq temp (delete x temp)))
-		     (delete-dups
-		      (append LaTeX-includegraphics-dvips-extensions
-			      temp)))))
-		;; Running xetex in any mode:
-		((eq TeX-engine 'xetex)
-		 (delete-dups (append LaTeX-includegraphics-xetex-extensions
-				      temp)))
-		;; For anything else
-		(t
-		 temp))))
-    (concat "\\."
-	    (mapconcat 'identity
-		       (or list LaTeX-includegraphics-extensions)
-		       "$\\|\\.")
-	    "$")))
+  "Return appropriate extensions for input files to \\includegraphics.
+Return value is a single regexp.
+Optional argument LIST if non-nil is used as list of regexps of
+extensions to be matched."
+  (unless list
+    (setq list (LaTeX-includegraphics-extensions-list)))
+  (concat "\\." (mapconcat #'identity list "$\\|\\.") "$"))
+
+(defvar LaTeX-includegraphics-global-files nil
+  "List of the non-local TeX input files.
+Initialized once at the first time you prompt for an input file.
+May be reset with `\\[universal-argument] \\[TeX-normal-mode]'.")
 
 (defun LaTeX-includegraphics-read-file-TeX ()
   "Read image file for \\includegraphics.
 Offers all graphic files found in the TeX search path.  See
 `LaTeX-includegraphics-read-file' for more."
-  (completing-read
-   "Image file: "
-   (TeX-delete-dups-by-car
-    (mapcar 'list
-	    (TeX-search-files nil LaTeX-includegraphics-extensions t t)))
-   nil nil nil))
+  (let ((LaTeX-includegraphics-extensions
+	 (LaTeX-includegraphics-extensions-list)))
+    (unless LaTeX-includegraphics-global-files
+      (message "Searching for graphic files...")
+      (setq LaTeX-includegraphics-global-files
+	    (TeX-search-files-by-type
+	     'graphics 'global t
+	     LaTeX-includegraphics-strip-extension-flag))
+      (message "Searching for graphic files...done"))
+    (completing-read
+     "Image file: "
+     (append
+      (TeX-search-files-by-type 'graphics 'local t
+				LaTeX-includegraphics-strip-extension-flag)
+      LaTeX-includegraphics-global-files)
+     nil nil nil)))
 
 (defun LaTeX-includegraphics-read-file-relative ()
   "Read image file for \\includegraphics.
diff --git a/tex.el b/tex.el
--- a/tex.el
+++ b/tex.el
@@ -4656,7 +4656,6 @@
 (defvar TeX-search-files-type-alist
   '((texinputs "${TEXINPUTS}" ("tex/") TeX-file-extensions)
     (docs "${TEXDOCS}" ("doc/") TeX-doc-extensions)
-    (graphics "${TEXINPUTS}" ("tex/") LaTeX-includegraphics-extensions)
     (bibinputs "${BIBINPUTS}" ("bibtex/bib/") BibTeX-file-extensions)
     (bstinputs "${BSTINPUTS}" ("bibtex/bst/") BibTeX-style-extensions))
   "Alist of filetypes with locations and file extensions.
@@ -6153,7 +6152,8 @@
 	    BibLaTeX-global-style-files nil
 	    TeX-Biber-global-files nil
 	    TeX-global-input-files nil
-	    LaTeX-global-class-files nil))
+	    LaTeX-global-class-files nil
+	    LaTeX-includegraphics-global-files nil))
   (let ((TeX-auto-save t))
     (if (buffer-modified-p)
 	(save-buffer)


>From [email protected] Tue Oct 03 18:27:22 2017
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: [PATCH] Add support for dvipdfmx to \includegraphics
X-Mercurial-Node: 0ff83704f9a2cb533f04d5429fd62959085263db
X-Mercurial-Series-Index: 1
X-Mercurial-Series-Total: 1
Message-Id: <[email protected]>
X-Mercurial-Series-Id: <[email protected]>
User-Agent: Mercurial-patchbomb/4.3.1
Date: Tue, 03 Oct 2017 18:27:22 +0900
From: Ikumi Keita <[email protected]>
To: dummy

* style/graphicx.el (LaTeX-includegraphics-extensions-list): Add
support for dvipdfmx.
* tests/latex/latex-test.el: New file to check correct extensions are
generated.

diff --git a/style/graphicx.el b/style/graphicx.el
--- a/style/graphicx.el
+++ b/style/graphicx.el
@@ -124,8 +124,11 @@
 			temp))
 	     ;; We're generating a .dvi to process with dvips or dvipdfmx
 	     (progn
-	       (dolist (x '("jpe?g" "pdf" "png"))
-		 (setq temp (delete x temp)))
+	       ;; dvipdfmx can handle jpeg, pdf and png for image formats.
+	       (unless (and TeX-PDF-mode
+			    (string= (TeX-PDF-from-DVI) "Dvipdfmx"))
+		 (dolist (x '("jpe?g" "pdf" "png"))
+		   (setq temp (delete x temp))))
 	       (delete-dups
 		(append LaTeX-includegraphics-dvips-extensions
 			temp)))))
@@ -147,7 +150,12 @@
 				temp)))
 	  ;; For anything else
 	  (t
-	   temp))))
+	   (if (and TeX-PDF-mode
+		    (string= (TeX-PDF-from-DVI) "Dvipdfmx"))
+	       ;; dvipdfmx can handle the same image formats as dvips.
+	       (delete-dups (append LaTeX-includegraphics-dvips-extensions
+				    temp))
+	     temp)))))
 
 (defun LaTeX-includegraphics-extensions (&optional list)
   "Return appropriate extensions for input files to \\includegraphics.
diff --git a/tests/latex/latex-test.el b/tests/latex/latex-test.el
--- a/tests/latex/latex-test.el
+++ b/tests/latex/latex-test.el
@@ -181,4 +181,89 @@
     (should (LaTeX-provided-package-options-member
 	     "biblatex" "backend=biber"))))
 
+(ert-deftest LaTeX-includegraphics-extensions ()
+  "Check correct extensions are generated accoding to `TeX-engine'."
+  (with-temp-buffer
+    (LaTeX-mode)
+    (TeX-load-style "graphicx")
+    (let (TeX-engine TeX-PDF-mode TeX-PDF-from-DVI
+		     TeX-PDF-via-dvips-ps2pdf TeX-DVI-via-PDFTeX)
+      ;; tests for default engine
+      (setq TeX-engine 'default)
+      ;; default 1
+      (setq TeX-PDF-mode t
+	    TeX-PDF-from-DVI nil
+	    TeX-DVI-via-PDFTeX nil)
+      (should
+       (equal (LaTeX-includegraphics-extensions-list)
+	      '("png" "pdf" "jpe?g" "jbig2" "jb2" "mps"
+		"PNG" "PDF" "JPE?G" "JBIG2" "JB2" "eps")))
+      ;; default 2
+      (setq TeX-PDF-mode t
+	    TeX-PDF-from-DVI "Dvips"
+	    TeX-DVI-via-PDFTeX nil)
+      (should
+       (equal (LaTeX-includegraphics-extensions-list)
+	      '("eps" "mps" "EPS")))
+      ;; default 3
+      (setq TeX-PDF-mode nil
+	    TeX-PDF-from-DVI nil
+	    TeX-DVI-via-PDFTeX nil)
+      (should
+       (equal (LaTeX-includegraphics-extensions-list)
+	      '("eps" "mps" "EPS")))
+      ;; default 4
+      (setq TeX-PDF-mode nil
+	    TeX-PDF-from-DVI nil
+	    TeX-DVI-via-PDFTeX t)
+      (should
+       (equal (LaTeX-includegraphics-extensions-list)
+	      '("png" "pdf" "jpe?g" "jbig2" "jb2" "mps"
+		"PNG" "PDF" "JPE?G" "JBIG2" "JB2" "eps")))
+      ;; default 5
+      (setq TeX-PDF-mode t
+	    TeX-PDF-from-DVI "Dvipdfmx"
+	    TeX-DVI-via-PDFTeX nil)
+      (should
+       (equal (LaTeX-includegraphics-extensions-list)
+	      '("eps" "mps" "EPS" "jpe?g" "pdf" "png")))
+
+      ;; tests for luatex engine
+      (setq TeX-engine 'luatex)
+      ;; luatex 1
+      (setq TeX-PDF-mode t)
+      (should
+       (equal (LaTeX-includegraphics-extensions-list)
+	      '("png" "pdf" "jpe?g" "jbig2" "jb2" "mps"
+		"PNG" "PDF" "JPE?G" "JBIG2" "JB2" "eps")))
+      ;; luatex 2
+      (setq TeX-PDF-mode nil)
+      (should
+       (equal (LaTeX-includegraphics-extensions-list)
+	      '("eps" "mps" "EPS")))
+
+      ;; test for xetex engine
+      (setq TeX-engine 'xetex)
+      (should
+       (equal (LaTeX-includegraphics-extensions-list)
+	      '("pdf" "eps" "mps" "ps" "png" "jpe?g" "jp2" "jpf"
+		"PDF" "EPS" "MPS" "PS" "PNG" "JPE?G" "JP2" "JPF"
+		"bmp" "pict" "psd" "mac" "tga" "gif" "tif" "tiff"
+		"BMP" "PICT" "PSD" "MAC" "TGA" "GIF" "TIF" "TIFF")))
+
+      ;; test for other engine
+      (setq TeX-engine 'omega)
+      ;; other 1
+      (setq TeX-PDF-mode t
+	    TeX-PDF-from-DVI "Dvipdfmx")
+      (should
+       (equal (LaTeX-includegraphics-extensions-list)
+	      '("eps" "mps" "EPS" "jpe?g" "pdf" "png")))
+      ;; other 2
+      (setq TeX-PDF-mode nil
+	    TeX-PDF-from-DVI nil)
+      (should
+       (equal (LaTeX-includegraphics-extensions-list)
+	      '("eps" "jpe?g" "pdf" "png"))))))
+
 ;;; latex-test.el ends here


_______________________________________________
auctex-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/auctex-devel

Reply via email to