views:

99

answers:

3

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?

+2  A: 

Your second routine should work whether there are spaces in the file name or not. For example, I created this little command:

(defun ls-l ()
  (interactive)
  (shell-command (concat "ls -l "
                         (shell-quote-argument
                          (buffer-file-name)))))

It works when I invoke it while editing a file called foo.txt and when editing a file called foo bar.txt.

Sean
Yup. I can't see any reason for the single-quotes in the first routine; shell-quote-argument will do everything that you intend those single-quotes to do.
offby1
But it doesn't work with texcount (I tried). See the questions I linked in my original question, for a possible explanation as to why. (I don't quite understand the reason why, myself at the moment...)
Seamus
+1  A: 

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))))
Ivan Andrus
That seems to work. I tried typing out the command myself to try and work out what the command was doing. I obviously got the parentheses wrong or something, since it kept giving me errors. But when I just yanked this code into my .emacs it worked fine. Thanks!
Seamus
+1  A: 

Hi.

I'm the developer of TeXcount and came across this posting just a little while ago.

As is pointed out, the problem is with TeXcount, so the best solution is to fix TeXcount rather than hack some other solution. I have an update available on the TeXcount web page in which I hope the problem is solved: http://folk.uio.no/einarro/TeXcount/download.html

NB: This is the temporary version of the new web pages, and may move later on if I decide to new address for TeXcount.

The problem came about because, in order to allow wildcards in file names under Windows, I had used <@files> to get all the files, and this didn't like the spaces. In Linux, you could just use @files without the glob (<...>), but I'd like TeXcount to work in Windows too, so a better solution was to escape the spaces before passing them to the glob.

Hope this helps, and if it doesn't please contact me and I'll see if I can help...I'm not a regular here, so I might not notice questions if posted as replies.

Einar

Einar