Giter Club home page Giter Club logo

emacs-async's Introduction

License GPL 3 MELPA MELPA Stable

emacs-async

async.el is a module for doing asynchronous processing in Emacs. Some async applications are provided as well with this package:

  • Dired-async
  • smtp-mail-async
  • async-bytecomp

Install

You can install emacs-async package from ELPA or MELPA using package.el.

You can also install from sources, in this case you should install using make and make install to ensure emacs-async is intalled in a standard load-path destination where other packages can find it easily when compiling.

Install dired-async

Add to your .emacs.el:

(autoload 'dired-async-mode "dired-async.el" nil t)
(dired-async-mode 1)

This will allow you to run asynchronously the dired commands for copying, renaming and symlinking. If you are a helm user, this will allow you to copy, rename etc... asynchronously from helm. Note that with helm you can disable this by running the copy, rename etc... commands with a prefix argument.

If you don't want to make dired/helm asynchronous disable it with dired-async-mode.

Debian and Ubuntu

Users of Debian 9 or later or Ubuntu 16.04 or later may simply apt-get install elpa-async.

Enable asynchronous compilation of your (M)elpa packages

By default emacs package.el compile packages in its running emacs session. This is not a problem when installing a new package (which is not actually loaded in current emacs) but it may create errors and bad compilation when upgrading a package (old version of package is already loaded and running in current emacs). You can remedy to this by allowing async to compile your packages asynchronously, (helm and magit actually do this by default, so if you are using these packages they will compile asynchronously) to do this, add to your init file:

(async-bytecomp-package-mode 1)

You can control which packages will compile async with async-bytecomp-allowed-packages. Set it to '(all) to be sure you will compile all packages asynchronously.

Send mails asynchronously with smtp mail async

To enable this feature, ensure smtp-mail-async.el is loaded and use

(setq message-send-mail-function 'async-smtpmail-send-it).

WARNINGS:

  • When using recent emacs (25+) the network security manager maybe called interactively in child emacs and make async-smtpmail-send-it fail, so be sure to send email once synchronously before using async-smtpmail-send-it as your message-send-mail-function.

  • You may loose your sent mail if your network is down, so ensure to queue your mails if so. you can do this automatically, see issue #64.

Async usage

The interface is intended to be very easy to use:

async-start

async-start START-FUNC FINISH-FUNC

Execute START-FUNC (often a lambda) in a subordinate Emacs process. When done, the return value is passed to FINISH-FUNC. Example:

(async-start
   ;; What to do in the child process
   (lambda ()
     (message "This is a test")
     (sleep-for 3)
     222)

   ;; What to do when it finishes
   (lambda (result)
     (message "Async process done, result should be 222: %s" result)))

If FINISH-FUNC is nil or missing, a future is returned that can be inspected using async-get, blocking until the value is ready. Example:

(let ((proc (async-start
               ;; What to do in the child process
               (lambda ()
                 (message "This is a test")
                 (sleep-for 3)
                 222))))

    (message "I'm going to do some work here") ;; ....

    (message "Waiting on async process, result should be 222: %s"
             (async-get proc)))

If you don't want to use a callback, and you don't care about any return value from the child process, pass the 'ignore symbol as the second argument (if you don't, and never call async-get, it will leave *emacs* process buffers hanging around):

(async-start
 (lambda ()
   (delete-file "a remote file on a slow link" nil))
 'ignore)

Note: Even when FINISH-FUNC is present, a future is still returned except that it yields no value (since the value is passed to FINISH-FUNC). Calling async-get on such a future always returns nil. It can still be useful, however, as an argument to async-ready or async-wait.

async-start-process

async-start-process NAME PROGRAM FINISH-FUNC &rest PROGRAM-ARGS

Start the executable PROGRAM asynchronously. See async-start. PROGRAM is passed PROGRAM-ARGS, calling FINISH-FUNC with the process object when done. If FINISH-FUNC is nil, the future object will return the process object when the program is finished. Set DEFAULT-DIRECTORY to change PROGRAM's current working directory.

async-get

async-get FUTURE

Get the value from an asynchronously called function when it is ready. FUTURE is returned by async-start or async-start-process when its FINISH-FUNC is nil.

async-ready

async-ready FUTURE

Query a FUTURE to see if its function's value is ready -- i.e., if no blocking would result from a call to async-get on that FUTURE.

async-wait

async-wait FUTURE

Wait for FUTURE to become ready.

async-inject-variables

async-inject-variables INCLUDE-REGEXP &optional PREDICATE EXCLUDE-REGEXP

Return a setq form that replicates part of the calling environment. It sets the value for every variable matching INCLUDE-REGEXP and also PREDICATE. It will not perform injection for any variable matching EXCLUDE-REGEXP (if present). It is intended to be used as follows:

(async-start
   `(lambda ()
      (require 'smtpmail)
      (with-temp-buffer
        (insert ,(buffer-substring-no-properties (point-min) (point-max)))
        ;; Pass in the variable environment for smtpmail
        ,(async-inject-variables "\\`\\(smtpmail\\|\\(user-\\)?mail\\)-")
        (smtpmail-send-it)))
   'ignore)

async-let

async-let BINDINGS &rest FORMS

Allow to establish let bindings asynchronously. Each value of binding can refer to the symbols already bound in BINDINGS (like let*). FORMS are executed once BINDINGS have been evaluated, but without blocking emacs.

Examples:

(async-let ((x "hello")
            (y "world"))
  (message "%s %s" x y))
  
(async-let ((x (* 5 2))
            (y (+ x 4))
            (z (+ x y)))
  (message "%d + %d = %d" x y z))

Note that if you bind something to nil and set it afterward in body, the evaluation of this binding will NOT be asynchronous, but will happen in you current emacs, blocking it if the evaluation of this value is sufficiently important, e.g:

(async-let ((x "hello")
            (y "world")
            z)
  (setq z (+ 1 2)) ;; Huge calculation of Z will block emacs.
  (message "%s %s %d" x y z))

IOW if the calculation of Z is huge and you want it asynchronous evaluate it in BINDINGS but not in FORMS.

emacs-async's People

Contributors

darkfeline avatar darwinawardwinner avatar jwiegley avatar kolewu avatar michael-heerdegen avatar mneilsen avatar myuhe avatar philhudson avatar spwhitton avatar syohex avatar tarsius avatar thierryvolpiatto avatar trevoke avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.