tecosaur / emacs-everywhere

Mirror of https://git.tecosaur.net/tec/emacs-everywhere
GNU General Public License v3.0
551 stars 56 forks source link

Updating the clipboard externally #67

Open slayer152 opened 1 year ago

slayer152 commented 1 year ago

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 commented 1 year ago

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.