Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Following on from this question the command works fine, but it doesn't like it when the parent folder has spaces in its name. How do I get these spaces to be automatically escaped?

I don't know whether this is a question about perl or about emacs, but since it follows from a SU question rather than a SO question, I've asked the followup here too.

Say the file I'm interested in word-counting is /home/seamus/Foo Bar/file.tex I'd get the following error: File not found or not readable: /home/seamus/Foo File not found or not readable: Bar/file.tex

[edited to make the error closer to the actual error I got]

share|improve this question
up vote 1 down vote accepted

How about using shell-quote-argument (to quote any possibly funky characters) on the result of expand-file-name (to expand the ~, before it is quoted).

(let ((path "~/Foo Bar/file.tex"))
  (shell-quote-argument (expand-file-name path)))

If the ~ is not already part of the string before it gets to you, you could add if after using shell-quote-argument so that the shell will expand ~ for you instead of Emacs:

(let ((path-in-home "Foo Bar/file.tex"))
  (concat "~/" (shell-quote-argument path-in-home)))

The answer to your referenced question shows using (buffer-file-name) to get the filename. In that case, just use (shell-quote-argument (buffer-file-name)) instead. There will not be a ~ involved.

Apparently your errors are coming from It does not properly handle spaces in the filenames it is given. Internally, it is using the given filenames as glob patterns instead of actual filenames. Unix shells normally handle globbing for the user so programs do not often include this functionality themselves. Maybe this bit of functionality was for Windows users (where the shell does not expand file patterns).

Anyway, you might be able to work around the problem by wrapping quotes around your filename:

(defun latex-word-count ()
  (shell-command (concat "/path/to/ "
                         (shell-quote-argument (concat "'" (buffer-file-name) "'")))))

Or you could rip the globbing functionality out of

diff --git i/ w/
index 96fac5c..aa96eb4 100755
--- i/
+++ w/
@@ -360,7 +360,7 @@ sub parse_options_output {
 sub parse_file_list {
   my @filelist=@_;
   my $listtotalcount=new_count("TOTAL COUNT");
-  for my $file (<@filelist>) {
+  for my $file (@filelist) {
     my $filetotalcount=parse_file($file);

And use more reasonable code on the elisp side:

(defun latex-word-count ()
  (shell-command (concat "/path/to/ "
                         (shell-quote-argument (buffer-file-name)))))
share|improve this answer
Thanks, I guess this answer is on the right track, but I still get the same error. The error doesn't include a ~, but has the whole path written out. I forgot this when writing my original question... – Seamus Jul 5 '10 at 13:28
It works for me when I take latex-word-count and replace (buffer-file-name) with (shell-quote-argument (buffer-file-name)). Maybe you should post the exact code you are using. – Chris Johnsen Jul 5 '10 at 20:46
Assuming you use shell-quote-argument, the Emacs part works fine. It is the script that is broken with respect to filenames with spaces in them. – Chris Johnsen Jul 5 '10 at 21:32
Thanks! I went for the concat quotes solution, because I don't want to play around with for now... – Seamus Jul 6 '10 at 16:44
See this question for a followup to the above.… – Seamus Jul 22 '10 at 15:49

You'll need to escape the spaces, so from your example, you'd write in e-lisp:

"~/Foo\\ Bar/ "

and you'd get:

"~/Foo\ Bar/ "

share|improve this answer
I understand this, the problem is that I'm getting the file name from the emacs buffer or some such, so I can't manually put in the escape characters – Seamus Jul 5 '10 at 14:41

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:

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

As has already been explained by Chris, 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.

Have put this reply both in this thread and in the original thread: not ment to spam, but for ease of finding.


share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .