Skip to content

pipe shell command

Detlef Groth edited this page Jan 7, 2025 · 5 revisions

With the pipe-shell-command you can call command line programs and use their output directly from within MicroEmacs.

Pasting the Primary Selection

Here an example on how to insert the primary selection into a running GUI version of MicroEmacs (mew or mews) even if you selected Clipboard only in User Setup -> System Clipboard:

; to be placed in USERNAME.emf
; replace ISO-8859-15 with your prefered ISO encoding used from within ME
define-macro insert-primary-selection
  0x400 pipe-shell-command "xclip -o -selection primary | iconv -f UTF-8 -t ISO-8859-15" #l0
  insert-string #l0
!emacro

!if &not &band $system 0x01
  ; GUI - non-terminal mode
  global-bind-key "insert-primary-selection" "S-insert"
!endif

A sightly improved version uses the current character encoding (defaults to Windows-CP1252) instead of hard-coding the ISO8859-15 encoding.

define-macro insert-primary-selection
  set-variable #l0 &reg &spr "/history/%s/char-set" %platform "CP1252"  set-variable #l0 &spr "%s" &rep &rep #l0 "iso" "ISO-" "cp" "CP"
  0x400 pipe-shell-command &spr "xclip -o -selection primary | iconv -f UTF-8 -t %s" #l0 #l1
  insert-string #l1
!emacro

This is simple solution which has a 2048 byte limite for the variable content length and hardcodes the ISO conversion independent from the current character setting. Steve suggest a more general approach which has not these limitations. here his implementation:

define-macro insert-primary-selection
  !if &seq &which "xclip" "ERROR"
     ml-write "Error: xclip is not installed, please install!"
     !return
  !endif  
  set-position "\x82"
  !force 0 delete-buffer "*ips-tmp*"
  0x40 pipe-shell-command "xclip -o -selection primary" "*ips-tmp*"
  buffer-mode "view"
  buffer-is-utf8 #l1
  !iif #l1  4 change-buffer-charset "utf8" "display"
  beginning-of-buffer
  set-mark
  end-of-buffer
  !force backward-char
  copy-region
  goto-position "\x82"
  !force 0 delete-buffer "*ips-tmp*"
  yank
!emacro
Clone this wiki locally