Emacs: fixed itranslate-lines
parent
0d4d8723bf
commit
7858f72185
|
@ -24,7 +24,7 @@ Requires `call-process-to-string' from `functions'."
|
||||||
(if (not (executable-find itranslate-program))
|
(if (not (executable-find itranslate-program))
|
||||||
(error "Program `%s' not found in path" itranslate-program)
|
(error "Program `%s' not found in path" itranslate-program)
|
||||||
(replace-regexp-in-string
|
(replace-regexp-in-string
|
||||||
"\n$" ""
|
" *\n$" ""
|
||||||
(call-process-to-string
|
(call-process-to-string
|
||||||
itranslate-program
|
itranslate-program
|
||||||
"-i"
|
"-i"
|
||||||
|
@ -42,49 +42,36 @@ respectively."
|
||||||
(equal current-prefix-arg '(4))))
|
(equal current-prefix-arg '(4))))
|
||||||
(when (called-interactively-p 'any) (itranslate-init))
|
(when (called-interactively-p 'any) (itranslate-init))
|
||||||
(let ((lang-in (or in itranslate-lang-input))
|
(let ((lang-in (or in itranslate-lang-input))
|
||||||
(lang-out (or out itranslate-lang-input)))
|
(lang-out (or out itranslate-lang-output)))
|
||||||
(if insert
|
(if insert
|
||||||
(insert (itranslate-string str lang-in lang-out))
|
(insert (itranslate-string str lang-in lang-out))
|
||||||
(message (itranslate-string str lang-in lang-out)))))
|
(message (itranslate-string str lang-in lang-out)))))
|
||||||
|
|
||||||
;; TODO: rewrite this one.
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun itranslate-lines (&option in out)
|
(defun itranslate-lines (beg end &optional in out)
|
||||||
"Translate current line or lines in region.
|
"Translate lines between BEG and END.
|
||||||
This calls the `itranslate' function. Output result at the end
|
Interactively, lines in region are used, or current line if no
|
||||||
after an ' = ' separtor."
|
region. This calls the `itranslate' function. Output result at
|
||||||
(interactive)
|
the end after an ' = ' separtor."
|
||||||
(unless itranslate-lang-p
|
(interactive
|
||||||
(itranslate-set-language)
|
(list (line-number-at-pos (point))
|
||||||
(set (make-local-variable 'itranslate-lang-p) t))
|
(line-number-at-pos (if mark-active (mark) (point))) nil nil))
|
||||||
|
(when (called-interactively-p 'any) (itranslate-init))
|
||||||
(let ((line)
|
(when (> beg end)
|
||||||
(cmd (concat itranslate-program " "
|
(setq beg end)
|
||||||
(unless (string= itranslate-lang-input "")
|
(setq end (line-number-at-pos (point))))
|
||||||
(concat "-i " itranslate-lang-input))
|
(save-excursion
|
||||||
" " itranslate-lang-output))
|
;; forward-line will remain on the same line if EOF has been reached. Need
|
||||||
(beg (line-number-at-pos (point)))
|
;; to check for it.
|
||||||
(end (line-number-at-pos (if mark-active (mark) (point)))))
|
(let ((lang-in (or in itranslate-lang-input))
|
||||||
|
(lang-out (or out itranslate-lang-output)))
|
||||||
;; Mark is assumed to specify the end. If it not not the case, we switch
|
|
||||||
;; the values.
|
|
||||||
(when (> beg end)
|
|
||||||
(setq beg (line-number-at-pos (mark)))
|
|
||||||
(setq end (line-number-at-pos (point))))
|
|
||||||
|
|
||||||
(save-excursion
|
|
||||||
;; forward-line will remain on the same line if EOF has been reached. Need
|
|
||||||
;; to check for it.
|
|
||||||
(while (<= beg end)
|
(while (<= beg end)
|
||||||
(goto-line beg)
|
(goto-line beg)
|
||||||
(setq line (buffer-substring-no-properties
|
(setq line (buffer-substring-no-properties
|
||||||
(line-beginning-position) (line-end-position)))
|
(line-beginning-position) (line-end-position)))
|
||||||
(unless (string-match "^ *$" line)
|
(unless (string-match "^ *$" line)
|
||||||
(end-of-line)
|
(end-of-line)
|
||||||
(insert " = " (shell-command-to-string
|
(insert " = " (itranslate line in out)))
|
||||||
(concat cmd " '" line "'")))
|
|
||||||
;; Shell commands usually output an EOL. We should remove it.
|
|
||||||
(delete-char -1))
|
|
||||||
(setq beg (1+ beg))))))
|
(setq beg (1+ beg))))))
|
||||||
|
|
||||||
(defun itranslate-init ()
|
(defun itranslate-init ()
|
||||||
|
@ -100,9 +87,10 @@ These languages are used by `itranslate'. Leave input empty for
|
||||||
auto-detect. Empty output defaults to English."
|
auto-detect. Empty output defaults to English."
|
||||||
(interactive)
|
(interactive)
|
||||||
(set (make-local-variable 'itranslate-lang-input)
|
(set (make-local-variable 'itranslate-lang-input)
|
||||||
(read-from-minibuffer "Input language: "))
|
(read-from-minibuffer
|
||||||
|
"Input language (leave empty for auto-detect): " itranslate-lang-input))
|
||||||
(set (make-local-variable 'itranslate-lang-output)
|
(set (make-local-variable 'itranslate-lang-output)
|
||||||
(read-string "Output language: " nil nil "en")))
|
(read-string "Output language: " itranslate-lang-output)))
|
||||||
|
|
||||||
(provide 'tool-itranslate)
|
(provide 'tool-itranslate)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue