I have this LaTeX code:
%% test.tex
\begin{wrapfig}
\includegraphics[width=1cm]{fig1.eps}\end{wrapfig}
\includegraphics[width=1cm]{fig2.eps}
I have a script called fix-files-ext.sh that fixes the extensions of graphic files (in this example it will change .eps to .pdf).
My script is this:
## fix-files-ext.sh
#!/usr/bin/emacs --script
emacs "$1" --batch -l ~/fix-files-ext.el ## &> /dev/null
where fix-files-ext.el contains this code:
;; fix-files-ext.el
(save-excursion
(let* ((case-fold-search nil))
(goto-char (point-min))
(while (re-search-forward "\\\\includegraphics" nil t)
(save-excursion
(let* ((pos1 (copy-marker (progn
(when (looking-at "\\(?:[ \n]+\\)?\\[")
(forward-sexp))
(point))))
(pos2 (copy-marker (progn
(forward-sexp)
(point)))))
(if (re-search-backward "\\.\\(e?ps\\|E?PS\\)}" pos1 t)
(replace-match ".pdf}" t)
(if (re-search-backward "\\.\\(JBIG2\\|JPEG\\|JP2\\|JPG\\|JPX\\|PDF\\|PNG\\)}" pos1 t)
(replace-match (downcase (match-string 0)) t)
(if (re-search-backward "\\.\\(tiff?\\|TIFF?\\)}" pos1 t)
(replace-match ".png}" t)))))))
(save-buffer)))
Here is the output I get calling my script:
$ bash fix-files-ext.sh test.tex
Loading /etc/emacs/site-start.d/00debian.el (source)...
Loading /etc/emacs/site-start.d/50dictionaries-common.el (source)...
Loading debian-ispell...
Loading /var/cache/dictionaries-common/emacsen-ispell-default.el (source)...
Loading /var/cache/dictionaries-common/emacsen-ispell-dicts.el (source)...
Scan error: "Containing expression ends prematurely", 54, 67
The Scan error refers to the second forward-sexp. But if I open test.tex with emacs and load fix-files-ext.el with load-file, it works perfectly. Why does this happen?
Remarks
In reference to $ bash fix-files-ext.sh test.tex:
If I drop
\end{wrapfig}, it works.If I leave
\end{wrapfig}and change the secondforward-sexptoforward-list, it works.
For me it is preferable to use forward-sexp instead of forward-list because if there were unbalanced brackets in the file names of the figures, forward-list would fail unlike forward-sexp (I am a professional typesetter and sometimes it happens that the names of the figures are a bit exotic).
Beyond that, I would like to understand why the same code called first by bash and then by emacs doesn't behave the same way.