Skip to content

Instantly share code, notes, and snippets.

@cbilson
Created July 29, 2018 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbilson/b4ddfd3c2ba7cf81bea1696cf14b25fd to your computer and use it in GitHub Desktop.
Save cbilson/b4ddfd3c2ba7cf81bea1696cf14b25fd to your computer and use it in GitHub Desktop.
elisp to send powershell to ConEmu for eval (... don't ask)
;;
;; ConEmu Functions
;;
(defvar cbilson/conemu-tab-id 0 "The current ConEmu PID or HWND.")
(defun cbilson/conemu-callback (process msg)
(message "conemu: %s" msg))
(defun cbilson/send-conemu-macro (macro &optional pid-or-hwnd)
"Send a command to ConEmu."
(interactive "p")
(let* ((pid-or-hwnd (or pid-or-hwnd cbilson/conemu-tab-id))
(conemuc-exe (concat cbilson/tools-dir "ConEmu/ConEmu/ConEmuC.exe"))
(command (format "%s -GuiMacro:%d %s" conemuc-exe pid-or-hwnd macro))
(buffer (get-buffer-create "*conemu*"))
(process (start-process-shell-command
"conemu"
buffer
command)))
(set-process-filter process 'cbilson/conemu-callback)))
;;
;; Powershell Specific
;;
(defun cbilson/send-powershell-to-conemu (powershell)
"Send a powershell command to ConEmu."
(let* ((file (make-temp-file "powershell" nil ".ps1" powershell)))
(message "file: %s" file)
(cbilson/send-conemu-macro (format "print: \". %s\\r\\n\"" file))))
(defun cbilson/send-powershell-region-to-conemu (start end)
"Sends a region to conemu to execute as powershell."
(interactive "r")
(let ((powershell (format "# from %s\r\n%s"
(buffer-file-name)
(buffer-substring-no-properties start end))))
(cbilson/send-powershell-to-conemu powershell)))
(defun cbilson/send-powershell-buffer-to-conemu ()
"Sends a region to conemu to execute as powershell."
(interactive)
; TODO: I think powershell has some kind of #pragma to set the line number
; for error messages. I should use that here so it says "line 242"
; instead of "line 1" when something breaks.
(let ((powershell (format "# from %s\r\n%s"
(buffer-file-name)
(buffer-substring-no-properties (point-min) (point-max)))))
(cbilson/send-powershell-to-conemu powershell)))
(defun cbilson/powershell-mode-hook ()
(setq powershell-indent 2
indent-tabs-mode nil)
; When I am in a PS1 buffer and C-c C-c, eval the buffer as a temp file in the active
; ConEmu tab.
(define-key powershell-mode-map (kbd "C-c C-c") 'cbilson/send-powershell-buffer-to-conemu))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment