Skip to content

Walheimat/parallel

Repository files navigation

parallel

Coverage Status

parallel allows you to create a command comprising two commands. The first function, the default, consumes normal prefix arguments, the second requires a numeric argument to activate and consumes numeric prefix arguments.

It grew out of my configuration and therefore is currently still heavily geared towards my usage.

Installation

If you use straight or quelpa, you know what to do.

If you’re on Emacs >29, I recommend using package-vc-install.

Alternatively, provided you have Cask, you can install the package with make package-install.

Usage

Say you have two related commands but too many key-bindings already. You also want those two commands do consume prefix arguments.

(require 'parallel)

(defun say-yes (&optional no)
  "Say 'yes', unless NO is t, then say 'no'."
  (interactive "P")

  (if no
      (message "no")
    (message "yes")))

(defun say-hello (&optional goodbye)
  "Say 'hello', unless GOODBYE is t, then say 'goodbye'."
  (interactive "P")

  (if goodbye
      (message "goodbye")
    (message "hello")))

(parallel say-yes say-hello)

This will create a new command say-yes||hello. Let’s take a look at the docstring:

;; Call `say-yes' or `say-hello' depending on prefix argument.

;; No argument means: call the prior. Numeric prefix 0 means: call the latter.

;; For all other prefix values: numeric prefixes call the latter,
;; `universal-argument' prefixes call the prior.

If you bind this command now, it will call say-yes and call message with “yes”. If you call it with the universal-argument, it will branch as expected and call message with “no”.

If you want to call say-hello, you need to call the new command with a numeric prefix argument. C-u 0 will call say-hello normally as if no prefix argument had been provided, and will call message with “hello”; all other numeric prefix arguments will be passed and therefore the function will call message with “goodbye= for C-u 1, for example.

If you need your second command to sometimes behave like a normal command because it uses the prefix argument to branch (take a look at org-refile for example), you can pass the key :universalize t.

(parallel say-yes say-hello :universalize t)

This means that a C-u 4 for example will result in prefix argument =’(4)= like a normal C-u would.

Configuration

You can configure the separator used to construct the new command name by setting parallel-separator. The default is the string =”||”=.

If you want to avoid automatic normalizing you can set parallel-naming-function to parallel--concatenate or your own function accepting the two function symbols to return a new symbol.

(setq parallel-naming-function 'parallel--concatenate)

(parallel say-yes say-hello) ;; Now creates command `say-yes||say-hello'.

If you have a custom namespace for your functions, say my/, you can set parallel-custom-namespace to =”my/”= to ensure normalizing works.

(parallel my/say-hello say-yes) ;; Would yield `my/say-hello||say-yes'.

(setq parallel-custom-namespace "my/")

(parallel my/say-hello say-yes) ;; Now yields expected `say-hello||say-yes'.