From mboxrd@z Thu Jan 1 00:00:00 1970 From: Marcelo de Moraes Serpa Subject: Re: [OT] Encoding error when calling a ruby script from Emacs using shell-command Date: Fri, 31 Aug 2012 17:18:02 -0500 Message-ID: References: <7522.1346442688@alphaville> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=14dae934098f583eee04c8972a23 Return-path: Received: from eggs.gnu.org ([208.118.235.92]:38413) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T7ZXR-0007vI-PJ for emacs-orgmode@gnu.org; Fri, 31 Aug 2012 18:18:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T7ZXQ-0002MT-A3 for emacs-orgmode@gnu.org; Fri, 31 Aug 2012 18:18:05 -0400 Received: from mail-iy0-f169.google.com ([209.85.210.169]:53358) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T7ZXQ-0002M9-3a for emacs-orgmode@gnu.org; Fri, 31 Aug 2012 18:18:04 -0400 Received: by iagk10 with SMTP id k10so6459789iag.0 for ; Fri, 31 Aug 2012 15:18:03 -0700 (PDT) In-Reply-To: <7522.1346442688@alphaville> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: nicholas.dokos@hp.com Cc: Org Mode --14dae934098f583eee04c8972a23 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Alright, I solved it. The problem is that emacs' shell-command doesn't use the same environment, so it wasn't picking up the value of those three vars: =E2=9C=97 export | grep UTF LANG=3Den_US.UTF-8 LC_ALL=3Den_US.UTF-8 LC_CTYPE=3Den_US.UTF-8 So, I did this: (defun test () (setenv "LANG" "en_US.UTF-8") (setenv "LC_ALL" "en_US.UTF-8") (setenv "LC_CTYPE" "en_US.UTF-8") (shell-command "/Users/myself/.rvm/bin/rvm ruby-1.9.3-p194 do /usr/bin/rubyscript") ) And now it works fine. Cheers, - Marcelo. On Fri, Aug 31, 2012 at 2:51 PM, Nick Dokos wrote: > Marcelo de Moraes Serpa wrote: > > > Hey list, > > > > I've tried posting on help-gnu-emacs mailing list first, but not luck s= o > far, so I thought I'd try here, as I know there are many savvy emacs user= s > around. > > > > I have a small Ruby CLI program that I want to call from emacs. This > script simply opens an emacs orgmode file from a specific location in my > hard drive, and does some text processing. When I call it from the termin= al > directly, it works fine. When I call it from emacs, the > > script fails with an encoding error. > > > > I'm using this elisp to call it from emacs after a buffer is saved: > > > > (defun test () > > (let ((universal-coding-system-argument 'utf-8-unix)) > > (shell-command "/Users/myself/.rvm/bin/rvm ruby-1.9.3-p194 > do /usr/bin/myrubyscript") > > )) > > (add-hook 'after-save-hook 'test) > > > > NOTE: The (let ((universal-coding-system-argument 'utf-8-unix)) was an > attempt to fix it, but it made no difference whatsoever. > > > > Probably wrong, but who knows? it may work by some miracle: > > (let ((coding-system-for-read 'utf-8-unix) > (coding-system-for-write 'utf-8-unix)) > (shell-command "/Users/myself/.rvm/bin/rvm ruby-1.9.3-p194 do > /usr/bin/myrubyscript") > > > > After I save a buffer, the shell-command function is fired, but I get > the following output in the "*Shell Command Output*" buffer: > > > > F, [2012-08-30T01:59:18.688827 #94004] FATAL -- : invalid byte > sequence in US-ASCII (ArgumentError) > > > /Users/myself/.rvm/gems/ruby-1.9.3-p194/gems/org-ruby-0.6.3/lib/org-ruby/= parser.rb:89:in > `split' > > > /Users/myself/.rvm/gems/ruby-1.9.3-p194/gems/org-ruby-0.6.3/lib/org-ruby/= parser.rb:89:in > `initia > > > > But this looks like ruby is expecting ASCII and is getting something else > (probably UTF-8). > What does the output of the command, when executed from a terminal, look > like? Redirect it into > a file and then use od to look at bytes. > > Also, you can try adding an output buffer as argument to the shell-comman= d > and then eyeballing the > output in that buffer to see if it matches the terminal output. > > Nick > > > The strange thing is that the file that this script opens *is* > accessible, and is the same file it would open if it were fired up from t= he > terminal. For some reason, Emacs is getting in the way, but I have no ide= a > what that could be. Am I missing something? If someone could > > enlighten me here, I'd be really grateful! > > > > Thanks in advance, > > > > - Marcelo. > > > > > > ---------------------------------------------------- > > Alternatives: > > > > ---------------------------------------------------- > --14dae934098f583eee04c8972a23 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Alright, I solved it.

The problem is that emacs' she= ll-command doesn't use the same environment, so it wasn't picking u= p the value of those three vars:

=E2=9C=97 ex= port | grep UTF
=C2=A0 =C2=A0 LANG=3Den_US.UTF-8=C2=A0
=C2=A0 =C2=A0 LC_ALL= =3Den_US.UTF-8
=C2=A0 =C2=A0 LC_CTYPE=3Den_US.UTF-8
So, I did this:

(defun test ()
=C2=A0 (setenv "LANG" "en_US.UTF-8")
=C2=A0 (setenv "LC_ALL" "en_US.UTF-8")
= =C2=A0 (setenv "LC_CTYPE" "en_US.UTF-8")
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 (shell-command =C2=A0"/Users/myself/.rvm/bin/= rvm ruby-1.9.3-p194 do /usr/bin/rubyscript")
=C2=A0 =C2=A0 =C2=A0 =C2=A0 )

And now i= t works fine.

Cheers,

- M= arcelo.




On Fri, Aug 31, 2012 at 2:51 PM, Nick Dokos <nicholas.do= kos@hp.com> wrote:
Marcelo de Moraes Serpa &l= t;celoserpa@gmail.com> wrote:=

> Hey list,
>
> I've tried posting on help-gnu-emacs mailing list first, but not l= uck so far, so I thought I'd try here, as I know there are many savvy e= macs users around.
>
> I have a small Ruby CLI program that I want to call from emacs.=C2=A0T= his script simply opens an emacs orgmode file from a specific location in m= y hard drive, and does some text processing.=C2=A0When I call it from the t= erminal directly, it works fine. When I call it from emacs, the
> script fails with an encoding error.
>
> I'm using this elisp to call it from emacs after a buffer is saved= :
>
> =C2=A0 =C2=A0 (defun test ()
> =C2=A0 =C2=A0 =C2=A0 (let ((universal-coding-system-argument 'utf-= 8-unix))
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (shell-command =C2=A0"/= Users/myself/.rvm/bin/rvm ruby-1.9.3-p194 do /usr/bin/myrubyscript") > =C2=A0 =C2=A0 =C2=A0 =C2=A0 ))
> =C2=A0 =C2=A0 (add-hook 'after-save-hook 'test)
>
> NOTE: The (let ((universal-coding-system-argument 'utf-8-unix)) wa= s an attempt to fix it, but it made no difference whatsoever.
>

Probably wrong, but who knows? it may work by some miracle:

(let ((coding-system-for-read 'utf-8-unix)
=C2=A0 =C2=A0 =C2=A0 (coding-system-for-write 'utf-8-unix))
=C2=A0 (shell-command "/Users/myself/.rvm/bin/rvm ru= by-1.9.3-p194 do /usr/bin/myrubyscript")


> After I save a buffer, the shell-command funct= ion is fired, but I get the following output in the "*Shell Command Ou= tput*" buffer:
>
> =C2=A0 =C2=A0 F, [2012-08-30T01:59:18.688827 #94004] FATAL -- : invali= d byte sequence in US-ASCII (ArgumentError)
> =C2=A0 =C2=A0 /Users/myself/.rvm/gems/ruby-1.9.3-p194/gems/org-ruby-0.= 6.3/lib/org-ruby/parser.rb:89:in `split'
> =C2=A0 =C2=A0 /Users/myself/.rvm/gems/ruby-1.9.3-p194/gems/org-ruby-0.= 6.3/lib/org-ruby/parser.rb:89:in `initia
>

But this looks like ruby is expecting ASCII and is getting something = else (probably UTF-8).
What does the output of the command, when executed from a terminal, look li= ke? Redirect it into
a file and then use od to look at bytes.

Also, you can try adding an output buffer as argument to the shell-command = and then eyeballing the
output in that buffer to see if it matches the terminal output.

Nick

> The strange thing is that the file that this script opens *is* accessi= ble, and is the same file it would open if it were fired up from the termin= al. For some reason, Emacs is getting in the way, but I have no idea what t= hat could be. Am I missing something? If someone could
> enlighten me here, I'd be really grateful!
>
> Thanks in advance,
>
> - Marcelo.
>
>
> ----------------------------------------------------
> Alternatives:
>
> ----------------------------------------------------

--14dae934098f583eee04c8972a23--