How can I send a command to Emacs 24 from VBA in MS Office?

1

I want to invoke commands from within MS Office application which have a direct effect on my (open) Emacs application, for example

  • do a search for the currently selected text in Emacs

I wonder how I can do that. Is there a direct way to send commands to Emacs? I'm running Emacs 24.3.1 on Windows 7 with the server options

;;prevent error message of unsafe server (see http://stackoverflow.com/a/1313577/772434 )
(require 'server)
(and (>= emacs-major-version 23) (defun server-ensure-safe-dir (dir) "Noop" t))
(server-start)
;;(add-to-list 'load-path "~/path/to/org/protocol/")
(require 'org-protocol)

and I'm using org-protocol

MostlyHarmless

Posted 2014-07-31T08:25:08.747

Reputation: 1 708

Answers

2

You can send command to Emacs (running in server mode) using emacsclient.

For example :

emacsclient --eval "(org-search-view nil """pattern""")"

If the elisp becomes too complex, you might be better of wrapping it in a file that you can load. For example :

fun.el

(defun my/search (pattern)
  (with-current-buffer "BUFFER-NAME"
     (search-forward pattern)))

command-line

emacsclient --load fun.el --eval '(my/search "PATTERN")'

François Févotte

Posted 2014-07-31T08:25:08.747

Reputation: 676

thanks, that works so far. However, the output of the commnand is displayed in the terminal. How can I define the command, so that the command is executed in Emacs itself? I tried with emacsclient --eval (org-search-view "mysearchterm") This should create a list with search results and I'd like to have it displayed in an existing or new Emacs buffer. – MostlyHarmless – 2014-07-31T11:39:55.030

The value that your form evaluates to is displayed in your terminal. However, the elisp forms are executed in Emacs itself. For exemple, org-search-view evaluates to t, which is normally not shown anywhere when you run it interactively, but is displayed in your terminal when you run it using emacsclient. The useful results of your search are however displayed in an Org Agenda buffer, whether you call it interactively from the command loop, or using emacsclient. – François Févotte – 2014-07-31T12:16:29.247

1In your case, I think the problem rather comes from the way you call org-search-view non-interactively: the pattern should be the 2nd argument. See my edit and the documentation for org-search-view. – François Févotte – 2014-07-31T12:16:45.867

Thanks again for your help and the suggested changes! It still does not work for me - in the command line I get ERROR: End of file during parsing and there is no visible change in the open emacs buffers – MostlyHarmless – 2014-07-31T12:40:04.940

1Mmm, it looks like your emacs lisp form is incomplete. Either it is a silly copy/paste problem, or (more probably) a problem with quotes in the command line. Unfortunately, I'm no expert on the Windows command line... – François Févotte – 2014-07-31T14:43:46.337

quoting was not entirely correct - see my edit. Now it works fine, thanks again! :-) – MostlyHarmless – 2014-08-01T07:18:22.847