all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: John Mastro <john.b.mastro@gmail.com>
To: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Subject: Re: send selection or current line to Terminal/iTerm
Date: Fri, 13 Jun 2014 19:30:35 -0700	[thread overview]
Message-ID: <CAOj2CQTtY7KGbGi6GQLtYiT-1JVjyQTZtxSjKy=FHoaH60jymw@mail.gmail.com> (raw)
In-Reply-To: <1402666776993-324448.post@n5.nabble.com>

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 "<C-return>") '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)


  parent reply	other threads:[~2014-06-14  2:30 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-08 20:58 send selection or current line to Terminal/iTerm klebsiella
2014-06-12 15:13 ` klebsiella
2014-06-12 15:49   ` Jai Dayal
2014-06-12 16:00     ` klebsiella
2014-06-12 16:02       ` Jai Dayal
     [not found]   ` <mailman.3500.1402588211.1147.help-gnu-emacs@gnu.org>
2014-06-12 20:29     ` Emanuel Berg
2014-06-12 20:31       ` Jai Dayal
     [not found]       ` <mailman.3510.1402605133.1147.help-gnu-emacs@gnu.org>
2014-06-12 20:36         ` Emanuel Berg
2014-06-12 20:43           ` Jai Dayal
2014-06-12 21:28             ` John Mastro
     [not found]           ` <mailman.3511.1402605838.1147.help-gnu-emacs@gnu.org>
2014-06-12 21:10             ` Emanuel Berg
2014-06-12 21:17               ` Jai Dayal
2014-06-12 21:43                 ` klebsiella
2014-06-12 23:59                   ` Jai Dayal
     [not found]                     ` <CAOj2CQRaRpKuTTgseGjfhk504BLRJ=J+9C76nj+W6Px2iL9X1Q@mail.gmail.com>
2014-06-13  0:55                       ` John Mastro
2014-06-13 13:39                         ` klebsiella
2014-06-13 16:51                           ` John Mastro
2014-06-14  2:30                           ` John Mastro [this message]
2014-06-14  6:52                             ` klebsiella
2014-06-14 14:22                               ` Robert Thorpe
2014-06-14 15:09                                 ` klebsiella
2014-06-14 18:59                                   ` Robert Thorpe
     [not found]                                 ` <mailman.3652.1402758599.1147.help-gnu-emacs@gnu.org>
2014-06-17 14:11                                   ` Rusi
2014-06-17 15:58                                     ` klebsiella
2014-06-17 18:46                                     ` klebsiella
2014-06-17 19:55                                       ` John Mastro
2014-06-18  1:11                                       ` Robert Thorpe
2014-06-18 11:30                                         ` klebsiella
2014-06-14 16:52                               ` John Mastro
2014-06-14 18:09                                 ` klebsiella
2014-06-14 18:36                                   ` John Mastro
2014-06-14 19:01                                     ` klebsiella
2014-06-14 19:24                                       ` John Mastro
2014-06-14 19:53                                         ` klebsiella
2014-06-14 15:31                     ` Alan Schmitt
     [not found]               ` <mailman.3513.1402607827.1147.help-gnu-emacs@gnu.org>
2014-06-12 21:35                 ` Emanuel Berg
2014-06-12 22:50   ` Robert Thorpe
     [not found] ` <mailman.3499.1402586049.1147.help-gnu-emacs@gnu.org>
2014-06-12 15:47   ` Emanuel Berg
2014-06-12 21:26   ` Hans BKK

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAOj2CQTtY7KGbGi6GQLtYiT-1JVjyQTZtxSjKy=FHoaH60jymw@mail.gmail.com' \
    --to=john.b.mastro@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.