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.
(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
\'\'
--> \'<xsl:value-of select=\"/xml/lang/_MATCHED_STRING\" />\'.
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 "<xsl:value-of select=\"/xml/lang/_%s\" />")
(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))
;; <xsl:value-of select="/xml/lang/_BIG_TEXT" />
(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)