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

Updating the clipboard externally #67

Open
slayer152 opened this issue Nov 15, 2022 · 1 comment
Open

Updating the clipboard externally #67

slayer152 opened this issue Nov 15, 2022 · 1 comment

Comments

@slayer152
Copy link

slayer152 commented Nov 15, 2022

Use case:
I use MS Word, Powerpoint a lot and want to use emacs-everywhere to help edit the text in Emacs. This works fine but if use, say bold, italics in org-mode and call emacs-everywhere-finish, the resulting text pasted into the application doesn't have the formatting. May not be for everyone but here's how I made it work (and can send a patch if interested).

  1. Define a function that converts the org buffer text into a format that preserves the formatting (In macos, I use textutil) and send it directly to clipboard - Add this function to emacs-everywhere-final-hooks
  2. Update emacs-everywhere-finish to not copy the buffer based on the value returned by another function (this other function is a user-option)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; emacs-everywhere.el 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defcustom emacs-everywhere-dont-copy-buffer-function #'always-false
  "Function that returns non-nil if `emacs-everywhere-finish' should
not copy buffer text.
This is useful if you want to populate the
clipboard through an external command.

The options are

1. Never (default), i.e., always copy the buffer text
2. Any user-defined function. This function should take no arguments and
return non-nil if buffer text should not be copied."
  :type '(radio
          (function-item :tag "never" always-false)
          (function-item :tag "User-defined function")))

(defun always-false()
  nil)

(defun emacs-everywhere-finish (&optional abort)
...
(unless abort
(run-hooks 'emacs-everywhere-final-hooks)
(unless (funcall emacs-everywhere-dont-copy-buffer-function)
        (gui-select-text (buffer-string))
        ....)))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; init.el
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  (add-hook 'emacs-everywhere-final-hooks #'my/formatted-copy)
  (setq emacs-everywhere-dont-copy-buffer-function 'my/emacs-everywhere-dont-copy)
  (defun my/emacs-everywhere-dont-copy(&optional buffer-name)
    (string-match-p ".*-Microsoft Word$" (or buffer-name (buffer-name))))
  (defun my/formatted-copy()
    (when (and (eq major-mode 'org-mode)
               (not (emacs-everywhere-markdown-p))
               (executable-find "textutil"))
      (let ((orig-buffer-name (buffer-name))
            (org-export-show-temporary-export-buffer))
        (org-export-to-buffer 'html (current-buffer) nil nil t t)
        (shell-command-on-region (point-min) (point-max)
                                 (concat "textutil -stdin -format html -stdout"
                                         (if (my/emacs-everywhere-dont-copy orig-buffer-name)
                                             " -convert rtf | pbcopy"
                                           " -convert txt"))
                                 (current-buffer) t))))
@tecosaur
Copy link
Owner

This is cool too see, but I don't think should necessarily be included with the package. Perhaps there's a need for a "community wiki of snippets"-type thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants