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: Sat, 14 Jun 2014 09:52:05 -0700	[thread overview]
Message-ID: <CAOj2CQR93ennLO0QVMTF0M8bJNV_yFargei+6hWk2KBqrRw_=A@mail.gmail.com> (raw)
In-Reply-To: <1402728754759-324525.post@n5.nabble.com>

Hi klebsiella,

klebsiella <pierre.khoueiry@embl.de> 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 "<C-return>") '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)


  parent reply	other threads:[~2014-06-14 16:52 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
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 [this message]
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='CAOj2CQR93ennLO0QVMTF0M8bJNV_yFargei+6hWk2KBqrRw_=A@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.