Search code examples
perlemacslatexauctex

Escaping spaces when calling (buffer-file-name) in emacs


So, I had a question about getting word count to work properly in emacs LaTeX mode (auctex, actually, but never mind.) That was answered fine. Then I found I had trouble when the (buffer-file-name) included spaces. This made it mess up. This problem was got around too. Now the problem is that the solution breaks when there AREN'T any spaces.

So currently I have two emacs commands:

(defun latex-word-count ()
  (interactive)
  (shell-command (concat "/usr/local/bin/texcount.pl "
                         "-inc "
                     (shell-quote-argument (concat "'" (buffer-file-name) "'")))))

this works when there is a space in the containing folder.

(defun latex-word-c-nospace ()
  (interactive)
  (shell-command (concat "/usr/local/bin/texcount.pl "
             "-inc "
             (shell-quote-argument (buffer-file-name)))))

This works when there is no space in the containing folder name. (OK so the indenting is a little screwey, but whatever)

My question: is there some way to have the same function work in both cases? This answer suggests the problem is with texcount rather than emacs. Is there a way to do this without messing about with texcount.pl? Or is my best bet to poke texcount.pl in the way Chris Johnsen suggested on SU?


Solution

  • You always have the option of having emacs determine if the file name has a space:

    (defun latex-word-count ()
      (interactive)
      (let* ((has-space (string-match " " buffer-file-name))
             (quoted-name (shell-quote-argument
                           (if has-space
                               (concat "'" buffer-file-name "'")
                             buffer-file-name))))
        (shell-command (concat "/usr/local/bin/texcount.pl "
                               "-inc "
                               quoted-name))))