Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #79 try to support output to multiple destinations #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 57 additions & 46 deletions google-translate-core-ui.el
Original file line number Diff line number Diff line change
Expand Up @@ -647,11 +647,15 @@ clicked."
(apply 'call-process google-translate-listen-program nil nil nil
(google-translate-format-listen-urls text language)))))

(defun google-translate-translate (source-language target-language text &optional output-destination)
(defun google-translate-translate (source-language target-language text &optional output-destinations)
"Translate TEXT from SOURCE-LANGUAGE to TARGET-LANGUAGE.

About the OUTPUT-DESTINATION, check out option
`google-translate-output-destination'.
About the OUTPUT-DESTINATIONS, All output destination elements in
this list will be invoked. If you just want one output
destination, then use a list that only contains one element of
that output destination.

More info check out option `google-translate-output-destination'.

To deal with multi-line regions, sequences of white space
are replaced with a single space. If the region contains not text, a
Expand All @@ -661,49 +665,56 @@ message is printed."
text)))
(if (null json)
(message "Nothing to translate.")
(let* ((detailed-translation
(google-translate-json-detailed-translation json))
(detailed-definition
(google-translate-json-detailed-definition json))
(gtos
(make-gtos
:source-language source-language
:target-language target-language
:auto-detected-language (aref json 2)
:text text
:text-phonetic (google-translate-json-text-phonetic json)
:translation (google-translate-json-translation json)
:translation-phonetic (google-translate-json-translation-phonetic json)
:detailed-translation detailed-translation
:detailed-definition detailed-definition
:suggestion (when (null detailed-translation)
(google-translate-json-suggestion json))))
(output-destination (if (null output-destination)
google-translate-output-destination
output-destination)))
(cond
((null output-destination)
(google-translate-buffer-output-translation gtos))
((equal output-destination 'echo-area)
(google-translate-echo-area-output-translation gtos))
((equal output-destination 'popup)
(google-translate-popup-output-translation gtos))
((equal output-destination 'kill-ring)
(google-translate-kill-ring-output-translation gtos))
((equal output-destination 'current-buffer)
(google-translate-current-buffer-output-translation gtos))
((equal output-destination 'help)
(let ((describe-func
(function
(lambda (gtos)
(google-translate-help-buffer-output-translation gtos)))))
(help-setup-xref (list 'google-translate-translate source-language target-language text) nil)
(with-help-window (help-buffer)
(funcall describe-func gtos))))
((equal output-destination 'paragraph-overlay)
(google-translate-paragraph-overlay-output-translation gtos))
((equal output-destination 'paragraph-insert)
(google-translate-paragraph-insert-output-translation gtos)))))))
(cl-flet* ((output-dispatcher
(output-destination gtos)
(cond
((null output-destination)
(google-translate-buffer-output-translation gtos))
((equal output-destination 'echo-area)
(google-translate-echo-area-output-translation gtos))
((equal output-destination 'popup)
(google-translate-popup-output-translation gtos))
((equal output-destination 'kill-ring)
(google-translate-kill-ring-output-translation gtos))
((equal output-destination 'current-buffer)
(google-translate-current-buffer-output-translation gtos))
((equal output-destination 'help)
(let ((describe-func
(function
(lambda (gtos)
(google-translate-help-buffer-output-translation gtos)))))
(help-setup-xref
(list 'google-translate-translate source-language target-language text) nil)
(with-help-window (help-buffer)
(funcall describe-func gtos))))
((equal output-destination 'paragraph-overlay)
(google-translate-paragraph-overlay-output-translation gtos))
((equal output-destination 'paragraph-insert)
(google-translate-paragraph-insert-output-translation gtos)))))
(let* ((detailed-translation
(google-translate-json-detailed-translation json))
(detailed-definition
(google-translate-json-detailed-definition json))
(gtos
(make-gtos
:source-language source-language
:target-language target-language
:auto-detected-language (aref json 2)
:text text
:text-phonetic (google-translate-json-text-phonetic json)
:translation (google-translate-json-translation json)
:translation-phonetic (google-translate-json-translation-phonetic json)
:detailed-translation detailed-translation
:detailed-definition detailed-definition
:suggestion (when (null detailed-translation)
(google-translate-json-suggestion json))))
(output-destinations (if (null output-destinations)
google-translate-output-destination
output-destinations)))
(if (seq-empty-p output-destinations)
(output-dispatcher nil gtos)
(mapc (lambda (output-destination) (output-dispatcher output-destination gtos))
output-destinations)))))))

(defun google-translate-popup-output-translation (gtos)
"Output translation to the popup tooltip using `popup'
Expand Down
4 changes: 2 additions & 2 deletions google-translate-default-ui.el
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ This command also specificly support org-mode."
(save-excursion
(mark-paragraph)
(buffer-substring-no-properties (region-beginning) (region-end)))
'paragraph-overlay)
'(paragraph-overlay))
(deactivate-mark)
(forward-paragraph)
(next-line)))))
Expand All @@ -348,7 +348,7 @@ This command does NOT support document format like org-mode."
(save-excursion
(mark-paragraph)
(buffer-substring-no-properties (region-beginning) (region-end)))
'paragraph-insert)
'(paragraph-insert))
(deactivate-mark)
(next-line))))

Expand Down