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: Sat, 14 Jun 2014 09:52:05 -0700 Message-ID: References: <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> <1402728754759-324525.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 1402764766 21868 80.91.229.3 (14 Jun 2014 16:52:46 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 14 Jun 2014 16:52:46 +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 18:52:40 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 1WvrC3-0006nG-Pu for geh-help-gnu-emacs@m.gmane.org; Sat, 14 Jun 2014 18:52:39 +0200 Original-Received: from localhost ([::1]:36054 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WvrC3-0005jH-E0 for geh-help-gnu-emacs@m.gmane.org; Sat, 14 Jun 2014 12:52:39 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54109) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WvrBr-0005iL-L8 for help-gnu-emacs@gnu.org; Sat, 14 Jun 2014 12:52:28 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WvrBq-0002pL-AF for help-gnu-emacs@gnu.org; Sat, 14 Jun 2014 12:52:27 -0400 Original-Received: from mail-ob0-x233.google.com ([2607:f8b0:4003:c01::233]:51504) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WvrBq-0002pA-2t for help-gnu-emacs@gnu.org; Sat, 14 Jun 2014 12:52:26 -0400 Original-Received: by mail-ob0-f179.google.com with SMTP id uz6so4088092obc.24 for ; Sat, 14 Jun 2014 09:52:25 -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=y8TmohGHX61Dgb9E3cXJGrAdozyJDQRbiaESDLa5hJ8=; b=yyvjWyzwVX8+tfecbKifKrM/aOJ75i1KVT2pvLerEtG3lJx1Zy1JjcLW1hgM/cZUqa +pP31DGFEX6MBgjXRmwATA2qEjidjXHvL5mXUEIo4T0U0PjIw9meuhYVATZEecQRl9e7 J79h+7NCwrcPQqZoXs1qUz/MLwTuYYDff4EENDzee4kbD+v0qYfhcpmw3L6BMKjyakuZ s+pNlxABt3v281QczK1v5YFyug9TUi63Nez5phkjJNFvShDbxEdMP6t/yzafLf4wySfh uk5FsBTqlGPvpHaAps9N1JSp+xrxJBjI2J8xDJZaeO6ZxSdLIuX3kXU2b+DdcxySwMk1 St5g== X-Received: by 10.60.103.173 with SMTP id fx13mr769565oeb.25.1402764745304; Sat, 14 Jun 2014 09:52:25 -0700 (PDT) Original-Received: by 10.76.166.234 with HTTP; Sat, 14 Jun 2014 09:52:05 -0700 (PDT) In-Reply-To: <1402728754759-324525.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:c01::233 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:98260 Archived-At: Hi klebsiella, klebsiella wrote: > It works like a charm ... very efficient, > > Though, one small improvement will be to send the whole current line, > instead of the symbol only, when no region is selected. [snip] > Also, but very minor, would be to strip/skip empty lines and > eventually move cursor to the next line of code (but this is really > not very important) All best and thanks again for the very good job You're welcome - I'm glad it worked. Here's a version with the changes you suggested. The "thing" to send when there's no active region (i.e. selection) is configurable, with the current line as the default. Let me know if it's what you have in mind. By the way - is this useful for others? If so I could release it on e.g. MELPA. Best, John ;;; iterm.el - send text to a running iTerm instance (require 'pcase) (require 'thingatpt) ;; To match SublimeText's key binding: ;; (global-set-key (kbd "") 'iterm-send-text) (defvar iterm-default-thing 'line "The \"thing\" to send if no region is active. Can be any symbol understood by `bounds-of-thing-at-point'.") (defvar iterm-empty-line-regexp "^[[:space:]]*$" "Regexp to match empty lines, which will not be sent to iTerm. Set to nil to disable removing empty lines.") (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) (char-equal (elt str (- length 1)) 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-maybe-remove-empty-lines (str) (if iterm-empty-line-regexp (let ((regexp iterm-empty-line-regexp) (lines (split-string str "\n"))) (mapconcat 'identity (delq nil (mapcar (lambda (line) (unless (string-match-p regexp line) line)) lines)) "\n")) str)) (defun iterm-send-string (str) "Send STR to a running iTerm instance." (let* ((str (iterm-maybe-remove-empty-lines str)) (str (iterm-handle-newline str)) (str (iterm-escape-string 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 iterm-default-thing)))) (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)) (forward-line 1)) (provide 'iterm)