Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

3 Answers

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 texcount.pl. 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 ()
  (interactive)
  (shell-command (concat "/path/to/texcount.pl "
                         (shell-quote-argument (concat "'" (buffer-file-name) "'")))))

Or you could rip the globbing functionality out of texcount.pl:

diff --git i/texcount.pl w/texcount.pl
index 96fac5c..aa96eb4 100755
--- i/texcount.pl
+++ w/texcount.pl
@@ -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);
     add_count($listtotalcount,$filetotalcount);
   }

And use more reasonable code on the elisp side:

(defun latex-word-count ()
  (interactive)
  (shell-command (concat "/path/to/texcount.pl "
                         (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 texcount.pl 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 texcount.pl for now... –  Seamus Jul 6 '10 at 16:44
    
See this question for a followup to the above. stackoverflow.com/questions/3281539/… –  Seamus Jul 22 '10 at 15:49
add comment

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

"~/Foo\\ Bar/file.text.pl "

and you'd get:

"~/Foo\ Bar/file.text.pl "

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
add comment

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.

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.

Einar

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.