From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Daniel Sousa Newsgroups: gmane.emacs.help Subject: Re: Help with emacs scripting Date: Mon, 16 Apr 2012 01:10:38 +0100 Message-ID: References: <495248DFDEA08C469BBDED2D4AA6C614425873@DAKIYA1.pegasus.local> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=0016e6d58f00cc234404bdc0a717 X-Trace: dough.gmane.org 1334535072 20949 80.91.229.3 (16 Apr 2012 00:11:12 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 16 Apr 2012 00:11:12 +0000 (UTC) Cc: "help-gnu-emacs@gnu.org" To: Doug Lewan Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Apr 16 02:11:11 2012 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1SJZXB-0004zH-Ei for geh-help-gnu-emacs@m.gmane.org; Mon, 16 Apr 2012 02:11:09 +0200 Original-Received: from localhost ([::1]:53213 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SJZXA-0002ma-Eh for geh-help-gnu-emacs@m.gmane.org; Sun, 15 Apr 2012 20:11:08 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:52510) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SJZX2-0002mJ-VM for help-gnu-emacs@gnu.org; Sun, 15 Apr 2012 20:11:03 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SJZWz-0004wt-Mq for help-gnu-emacs@gnu.org; Sun, 15 Apr 2012 20:11:00 -0400 Original-Received: from sousa.cc ([46.51.181.120]:54446) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SJZWz-0004vz-9x for help-gnu-emacs@gnu.org; Sun, 15 Apr 2012 20:10:57 -0400 Original-Received: from mail-wi0-f177.google.com (mail-wi0-f177.google.com [209.85.212.177]) by sousa.cc (Postfix) with ESMTPSA id 18AC020D85 for ; Mon, 16 Apr 2012 00:10:53 +0000 (UTC) Original-Received: by wibhj13 with SMTP id hj13so3771269wib.12 for ; Sun, 15 Apr 2012 17:10:53 -0700 (PDT) Original-Received: by 10.216.139.67 with SMTP id b45mr6233147wej.0.1334535053386; Sun, 15 Apr 2012 17:10:53 -0700 (PDT) Original-Received: by 10.223.63.70 with HTTP; Sun, 15 Apr 2012 17:10:38 -0700 (PDT) In-Reply-To: <495248DFDEA08C469BBDED2D4AA6C614425873@DAKIYA1.pegasus.local> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 46.51.181.120 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:84474 Archived-At: --0016e6d58f00cc234404bdc0a717 Content-Type: text/plain; charset=ISO-8859-1 Thank you very much, that does almost what I wanted. I made a few changes to make the script work and then I changed it to check if the string was already in the .php files and if it is, it uses it. Since I don't know regular expressions and it's the first time I'm using lisp, I guess this is a terrible code, but it works Once again, thank you for your help. I left here the final code I'll be using: (defvar *ds-languages-dir* "languages") (defvar *ds-php-file* (format "%s/pt.php" *ds-languages-dir*)) (defvar *ds-php-buffer* ()) (defun ds-create-lang-entries () "Change the selection in the current buffer and create a PHP definition in the PHP file. The change to make is \'\' --> \'\'. The addition to the PHP file is made at the end of the file; it is \'DEFINE(\"_LANG_MATCHED_STRING\", \"%s\");\'." (interactive) (ensure-php-buffer) (let ((xsl-format "") (php-format "DEFINE(\"_LANG_%s\", \"%s\");\n") (oldbuf (current-buffer)) (the-string (buffer-substring-no-properties (point) (mark)))) (save-current-buffer (set-buffer *ds-php-buffer*) (if (search-backward the-string nil t) (progn (let ((endpoint (- (point) 4))) (search-backward "DEFINE" nil t) (let ((the-constant (buffer-substring-no-properties (search-forward "_LANG_") endpoint))) ;; Change the selected text to the appropriate XSL (set-buffer oldbuf) (delete-region (point) (mark)) (goto-char (mark)) (insert (format xsl-format the-constant))))) (progn (set-buffer oldbuf) (let* ((the-constant (replace-regexp-in-string " " "_" (upcase (read-from-minibuffer "constant: "))))) ;; Change the selected text to the appropriate XSL (delete-region (point) (mark)) (goto-char (mark)) (insert (format xsl-format the-constant)) ;; (save-excursion (set-buffer *ds-php-buffer*) (goto-char (point-max)) (insert (format php-format the-constant the-string))))))))) (defun ensure-php-buffer () "Make sure that everything about the PHP buffer is good." (unless (file-directory-p *ds-languages-dir*) (make-directory *ds-languages-dir* t)) (unless *ds-php-buffer* (setq *ds-php-buffer* (find-file-noselect *ds-php-file*)))) (local-set-key "\C-c\C-c" 'ds-create-lang-entries) On Fri, Apr 13, 2012 at 2:56 PM, Doug Lewan wrote: > A concrete example always makes vague requirements clearer. > Thank you. > > I think the following will do what you want. > The process is this: > 1. Go through the XSL buffer(s), > 2. sweeping the strings of interest and > 3. type C-cC-c to process. > 4. Move on to the next string and repeat. > > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; > > (defvar *ds-languages-dir* "languages") > (defvar *ds-php-file* (format "%s/pt.php" *ds-languages-dir*)) > (defvar *ds-php-buffer* ()) > > (defun ds-create-lang-entries () > "Change the selection in the current buffer and create a PHP definition > in the PHP file. > The change to make is > \'MATCHED_STRING\' > --> \'\'. > The addition to the PHP file is made at the end of the file; > it is \'DEFINE(\"_LANG_MATCHED_STRING\", \"bar\");\'." > (interactive) > (ensure-php-buffer) > (let* ((xsl-format "") > (php-format "DEFINE(\"_LANG_%s\", \"bar\");\n") > (the-string (buffer-substring-no-properties (point) (mark))) > (the-STRING (upcase the-string))) > ;; Change the selected text to the appropriate XSL > (delete-region (point) (mark)) > (goto-char (mark)) > (insert (format xsl-format the-string)) > ;; Now add a PHP definition at the end of the PHP file. > (save-excursion > (set-buffer *ds-php-buffer*) > (goto-char (point-max)) > (insert (format php-format the-STRING))))) > > (defun ensure-php-buffer () > "Make sure that everything about the PHP buffer is good." > (unless (file-directory-p *ds-languages-dir*) (make-directory > *ds-languages-dir* t)) > (unless *ds-php-buffer* (setq *ds-php-buffer* (find-file-noselect > *ds-php-file*)))) > > (local-set-key "\C-c\C-c" 'ds-create-lang-entries) > > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; > > I hope this helps. > > ,Doug > > > > From: help-gnu-emacs-bounces+dougl=shubertticketing.com@gnu.org [mailto: > help-gnu-emacs-bounces+dougl=shubertticketing.com@gnu.org] On Behalf Of > Daniel Sousa > Sent: Thursday, 2012 April 12 12:10 > To: help-gnu-emacs@gnu.org > Subject: Re: Help with emacs scripting > > I'm majoring in applied math and computation, I know pretty well that > computers are dumb xD > > I think neither of you have understood what I wanted, so I'll give you an > example. > I have a lot of .xsl that generate pages and I want to make those pages > translatable. I migth have the file, for exemple, welcome.xsl: > > > > > >

Bem-vindo

>
>
    > >
  • Coisa
  • >
    >
>
>
> And I wanted to make this translatable, so I change this to > > > > > >

>
>
    > >
  • select="name" />
  • >
    >
>
>
> And I create a languages/pt.php with: > define('_LANG_WELCOME','Bem-vindo'); > define('_LANG_STUFF','Coisa'); > > I have a piece of php code that puts in the xml all the constants that > begin with "_LANG". > > I have a lot of these .xsl and I need to go manually through them all to > find the strings that I want to make translatable. What I want this script > to do is, on my input put that /> thing on the place I select and append to the language file the > repective define();. > --0016e6d58f00cc234404bdc0a717 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thank you very much, that does almost what I wanted. I made a few changes t= o make the script work and then I changed it to check if the string was alr= eady in the .php files and if it is, it uses it.
Since I don't know= regular expressions and it's the first time I'm using lisp, I gues= s this is a terrible code, but it works

Once again, thank you for your help.
I left h= ere the final code I'll be using:
(defvar *ds-languages-= dir* "languages")
(defvar *ds-php-file* (format "%= s/pt.php" *ds-languages-dir*))
(defvar *ds-php-buffer* ())

(defun =A0ds-crea= te-lang-entries ()
=A0"Change the selection in the current b= uffer and create a PHP definition in the PHP file.
The change to = make is
=A0 =A0\'\'
=A0 =A0 =A0 =A0 --> \'<xsl:val= ue-of select=3D\"/xml/lang/_MATCHED_STRING\" />\'.
The addition to the PHP file is made at the end of the file;
i= t is \'DEFINE(\"_LANG_MATCHED_STRING\", \"%s\");\&#= 39;."
=A0(interactive)
=A0(ensure-php-buffer)
=A0(let ((= xsl-format "<xsl:value-of select=3D\"/xml/lang/_%s\" />= ;")
=A0 =A0 =A0 =A0(php-format "DEFINE(\"_LANG_%s\= ", \"%s\");\n")
=A0 =A0 =A0 =A0(oldbuf (current-buffer))
=A0 =A0 =A0 =A0(the= -string (buffer-substring-no-properties (point) (mark))))
=A0 =A0= (save-current-buffer
=A0 =A0 =A0 (set-buffer *ds-php-buffer*)
=A0 =A0 =A0 (if (search-backward the-string nil t)
=A0 =A0 =A0 =A0 (progn
=A0(let ((endpoint (- (point) 4)))
= =A0 =A0(s= earch-backward "DEFINE" nil t)
=A0 = =A0(let ((the-constant (buffer-substring-no-properties (search-forward &quo= t;_LANG_") endpoint)))
=A0 =A0 =A0 =A0 =A0 =A0 =A0 ;; Change= the selected text to the appropriate XSL
=A0 = =A0 =A0(set-buffer oldbuf)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 (delete-re= gion (point) (mark))
=A0 =A0 =A0 =A0 =A0 =A0 =A0 (goto-char (mark= ))
=A0 =A0 =A0 =A0 =A0 =A0 =A0 (insert (format xsl-format the-con= stant)))))
(prog= n
=A0(set-buffer oldbuf)
=A0 =A0 =A0 =A0 =A0 =A0 (let* ((the-con= stant (replace-regexp-in-string " " "_" (upcase (read-f= rom-minibuffer "constant: ")))))
=A0 =A0 =A0 =A0 =A0 =A0 =A0 ;; Change the selected text to the appropr= iate XSL
=A0 =A0 =A0 =A0 =A0 =A0 =A0 (delete-region (point) (mark= ))
=A0 =A0 =A0 =A0 =A0 =A0 =A0 (goto-char (mark))
=A0 = =A0 =A0 =A0 =A0 =A0 =A0 (insert (format xsl-format the-constant))
=A0 =A0 =A0 =A0 =A0 =A0 =A0 ;; <xsl:value-of select=3D"/xml/la= ng/_BIG_TEXT" />
=A0 =A0 =A0 =A0 =A0 =A0 =A0 (save-excurs= ion
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (set-buffer *ds-php-buffer*)<= /div>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (goto-char (point-max))
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (insert (format php-format the-constan= t the-string)))))))))

(defun ensure-php-buffer ()<= /div>
=A0"Make sure that everything about the PHP buffer is good.&= quot;
=A0(unless (file-directory-p *ds-languages-dir*) (make-directory *ds-langua= ges-dir* t))
=A0(unless *ds-php-buffer* (setq *ds-php-buffer* (fi= nd-file-noselect *ds-php-file*))))

(local-set-key = "\C-c\C-c" 'ds-create-lang-entries)

On Fri, Apr 13, 2012 at 2:56 PM, = Doug Lewan <dougl@shubertticketing.com> wrote:
A concrete example always makes vague requirements clearer.
Thank you.

I think the following will do what you want.
The process is this:
1. Go through the XSL buffer(s),
2. sweeping the strings of interest and
3. type C-cC-c to process.
4. Move on to the next string and repeat.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar *ds-languages-dir* "languages")
(defvar *ds-php-file* (format "%s/pt.php" *ds-languages-dir*))
(defvar *ds-php-buffer* ())

(defun =A0ds-create-lang-entries ()
=A0"Change the selection in the current buffer and create a PHP= definition in the PHP file.
The change to make is
=A0 =A0\'MATCHED_STRING\'
=A0 =A0 =A0 =A0 --> \'<xsl:value-of select=3D\"/xml/lang/MA= TCHED_STRING\" />\'.
The addition to the PHP file is made at the end of the file;
it is \'DEFINE(\"_LANG_MATCHED_STRING\", \"bar\");\= '."
=A0(interactive)
=A0(ensure-php-buffer)
=A0(let* ((xsl-format "<xsl:value-of select=3D\&= quot;/xml/lang/%s\" />")
=A0 =A0 =A0 =A0 (php-format "DEFINE(\"_LANG_%s\", \"ba= r\");\n")
=A0 =A0 =A0 =A0 (the-string (buffer-substring-no-properties (point) = (mark)))
=A0 =A0 =A0 =A0 (the-STRING (upcase the-string)))
=A0 =A0;; Change the selected text to the appropriate XSL
=A0 =A0(delete-region (point) (mark))
=A0 =A0(goto-char (mark))
=A0 =A0(insert (format xsl-format the-string))
=A0 =A0;; Now add a PHP definition at the end of the PHP= file.
=A0 =A0(save-excursion
=A0 =A0 =A0(set-buffer *ds-php-buffer*)
=A0 =A0 =A0(goto-char (point-max))
=A0 =A0 =A0(insert (format php-format the-STRING)))))

(defun ensure-php-buffer ()
=A0"Make sure that everything about the PHP buffer is good."
=A0(unless (file-directory-p *ds-languages-dir*) (make-directory *ds-langu= ages-dir* t))
=A0(unless *ds-php-buffer* (setq *ds-php-buffer* (find-file-noselect *ds-p= hp-file*))))

(local-set-key "\C-c\C-c" 'ds-create-lang-entries)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

I hope this helps.

,Doug



From: help-gnu-emacs-bounces+dougl=3Dshubertticketing.com@gnu.org [mailto:help-gnu-emacs-bounces+dougl=3Dshubertticketing.com@gnu.org] On Beha= lf Of Daniel Sousa
Sent: Thursday, 2012 April 12 12:10
To: help-gnu-emacs@gnu.org
Subject: Re: Help with emacs scripting

I'm majoring in applied m= ath and computation, I know pretty well that computers are dumb xD

I think neither of you have understood what I wanted, so I'll give you = an example.
I have a lot of .xsl that generate pages and I want to make those pages tra= nslatable. I migth have the file, for exemple, welcome.xsl:
<?xml version=3D"1.0" encoding=3D"ISO-8859-1" ?><= br> <xsl:stylesheet version=3D"1.0" xmlns:xsl=3D"http://www.w3.org/19= 99/XSL/Transform">=A0
<xsl:output method=3D"html" indent=3D"yes" encoding= =3D"iso-8859-1" />
<xsl:template match=3D"/">
<xsl:if test=3D"welcome">
<p>Bem-vindo</p>
</xsl:if>
<ul>
<xsl:for-each select=3D"elements">
<li>Coisa <xsl:value-of select=3D"name" /></li>=
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
And I wanted to make this translatable, so I change this to
<?xml version=3D"1.0" encoding=3D"ISO-8859-1" ?><= br> <xsl:stylesheet version=3D"1.0" xmlns:xsl=3D"http://www.w3.org/19= 99/XSL/Transform">=A0
<xsl:output method=3D"html" indent=3D"yes" encoding= =3D"iso-8859-1" />
<xsl:template match=3D"/xml">
<xsl:if test=3D"welcome">
<p><xsl:value-of select=3D"/xml/lang/_WELCOME" /><= /p>
</xsl:if>
<ul>
<xsl:for-each select=3D"elements">
<li> <xsl:value-of select=3D"/xml/lang/_STUFF" />=A0= =A0<xsl:value-of select=3D"name" /></li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
And I create a languages/pt.php with:
=A0define('_LANG_WELCOME','Bem-vindo');
=A0define('_LANG_STUFF','Coisa');

I have a piece of php code that puts in the xml all the constants that begi= n with "_LANG".

I have a lot of these .xsl and I need to go manually through them all to fi= nd the strings that I want to make translatable. What I want this script to= do is, on my input put that=A0<xsl:value-of select=3D"/xml/lang/_S= TUFF" />=A0thing on the place I select and append to the language f= ile the repective define();.

--0016e6d58f00cc234404bdc0a717--