Question

I use LaTeX with Emacs and want to be able to pass words of what I write to a shell command. (As example I use xargs echo.)

For this purpose I defined an interactive elisp function:

(defun echo () 
  (interactive)
  (let ((bnds  (bounds-of-thing-at-point 'word)))
    (shell-command-on-region (car bnds) (cdr bnds) "xargs echo")))

This works well for most buffers, but not for LaTeX buffers, using AUCTeX font locking. Most words again work fine, but not words with quotation marks at their word boundary.

A minimal example:

\documentclass{article}
\usepackage{german}

\begin{document}
test "`Test"'
\end{document}

First, just as proof of concept: If point is on test in my LaTeX buffer and I try my command (echo) I get test as result in the minibuffer. (As expected.)

Now the problem: If point is on Test instead and I use my command xargs complains about unmatched double quotes.

The reason for this is that shell-command-on-region doesn't pass Test to the shell command but Test"'. This I can verify by using in my elisp code xargs -0. I then get as result in the minibuffer:

Test"'

The problem comes from AUCTeXs font locking. (Which I want to use!) If I set font-latex-quotes to nil, my command works fine (it returns Test in the minibuffer), but of course quoted content will not be fontified ...

Any ideas how I could either change my elisp code to work also with AUCTeX, or to customize AUCTeX so that it fontifies quoted content but preserves correct word boundaries?

Was it helpful?

Solution

(defun echo ()
  (interactive)
  (let ((bnds (bounds-of-thing-at-point 'word)))
    (shell-command-on-region (car bnds) (cdr bnds) "xargs echo")
    (display-buffer "*Shell Command Output*")))

WRT your comment, change syntax-table temporarily with function modify-syntax-entry or use another one like this:

(defun echo ()
  (interactive)
  (with-syntax-table text-mode-syntax-table
    (let ((bnds (bounds-of-thing-at-point 'word)))
      (shell-command-on-region (car bnds) (cdr bnds) "xargs echo")
      (display-buffer "*Shell Command Output*"))))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top