From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: John Mastro Newsgroups: gmane.emacs.help Subject: Re: send selection or current line to Terminal/iTerm Date: Fri, 13 Jun 2014 19:30:35 -0700 Message-ID: References: <1402261104327-324145.post@n5.nabble.com> <1402586029056-324374.post@n5.nabble.com> <87r42tvpkr.fsf@debian.uxu> <87k38lvp9r.fsf@debian.uxu> <87d2edvnog.fsf@debian.uxu> <1402609430389-324400.post@n5.nabble.com> <1402666776993-324448.post@n5.nabble.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: ger.gmane.org 1402713075 32350 80.91.229.3 (14 Jun 2014 02:31:15 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 14 Jun 2014 02:31:15 +0000 (UTC) To: "help-gnu-emacs@gnu.org" Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Jun 14 04:31:09 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WvdkL-0003PC-2B for geh-help-gnu-emacs@m.gmane.org; Sat, 14 Jun 2014 04:31:09 +0200 Original-Received: from localhost ([::1]:33943 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WvdkK-00014l-IM for geh-help-gnu-emacs@m.gmane.org; Fri, 13 Jun 2014 22:31:08 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:39881) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WvdkA-00014V-Dz for help-gnu-emacs@gnu.org; Fri, 13 Jun 2014 22:30:59 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wvdk9-0002eu-Eu for help-gnu-emacs@gnu.org; Fri, 13 Jun 2014 22:30:58 -0400 Original-Received: from mail-oa0-x22f.google.com ([2607:f8b0:4003:c02::22f]:45247) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wvdk9-0002eg-7w for help-gnu-emacs@gnu.org; Fri, 13 Jun 2014 22:30:57 -0400 Original-Received: by mail-oa0-f47.google.com with SMTP id n16so3703091oag.20 for ; Fri, 13 Jun 2014 19:30:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=JQbp3v+XgUGHkyoRGnsoqsS9/JwzHGb0xDr6M7pFHyA=; b=1AAA0CQeCMGs21hpsw9xbEQJIbkO2nnd2vg5QOvR1eTkmCgxmYyw84QR7KdoT8WHRC Vft0PsCcK83c0+RGPQpstEzqT5tWzMbnbdMfumwu1x8y17oIuAlvqB5mSZlICjs38S/i J4EA45cccHsuCEYfLtUvR/bE2KHb4my7GKY4Z0EbgDya5S9bKKVRehFAUgnhC+r/T1CJ iZmIEI57xvo4lgEJ7qPyUdkBppSj6MoKeU7elS0EFOEDpciJSs2UqGHhC0TnIbEfCLR4 W+u/8L9RVOhpRQBB2NO/ayyvmfJ2PRhisE+4UuGghzJJ1LrGrJB/hp7lUi6yoeXpmHxD cWng== X-Received: by 10.182.165.232 with SMTP id zb8mr6510796obb.47.1402713056059; Fri, 13 Jun 2014 19:30:56 -0700 (PDT) Original-Received: by 10.76.166.234 with HTTP; Fri, 13 Jun 2014 19:30:35 -0700 (PDT) In-Reply-To: <1402666776993-324448.post@n5.nabble.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4003:c02::22f X-Content-Filtered-By: Mailman/MimeDel 2.1.14 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:98231 Archived-At: Hi klebsiella, I have another version for you to try. It behaved as expected in a few simple tests on my machine. Let me know how it goes. Best, John ;;; Begin iterm.el ;; To match SublimeText's key binding: ;; (global-set-key (kbd "") 'iterm-send-text) (require 'pcase) (require 'thingatpt) (defun iterm-escape-string (str) (let* ((str (replace-regexp-in-string "\\\\" "\\\\" str nil t)) (str (replace-regexp-in-string "\"" "\\\"" str nil t))) str)) (defun iterm-last-char-p (str char) (let ((length (length str))) (and (> length 0) (string= (substring str (- length 1)) (string char))))) (defun iterm-chop-newline (str) (let ((length (length str))) (if (iterm-last-char-p str ?\n) (substring str 0 (- length 1)) str))) (defun iterm-maybe-add-newline (str) (if (iterm-last-char-p str ? ) (concat str "\n") str)) (defun iterm-handle-newline (str) (iterm-maybe-add-newline (iterm-chop-newline str))) (defun iterm-send-string (str) "Send STR to a running iTerm instance." (let ((str (iterm-escape-string (iterm-handle-newline str)))) (shell-command (concat "osascript -e 'tell app \"iTerm\"' " "-e 'set mysession to current session of current terminal' " (format "-e 'tell mysession to write text \"%s\"' " str) "-e 'end tell'")))) (defun iterm-text-bounds () (pcase-let ((`(,beg . ,end) (if (use-region-p) (cons (region-beginning) (region-end)) (bounds-of-thing-at-point 'symbol)))) (list beg end))) (defun iterm-send-text (beg end) "Send buffer text in region from BEG to END to iTerm. If called interactively without an active region, send the symbol at point instead." (interactive (iterm-text-bounds)) (let ((str (iterm-escape-string (buffer-substring-no-properties beg end)))) (iterm-send-string str))) (provide 'iterm)