emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Tak Kunihiro <tkk@misasa.okayama-u.ac.jp>
To: emacs-orgmode@gnu.org
Cc: tkk@misasa.okayama-u.ac.jp
Subject: Emacs speaks to spreadsheet
Date: Wed, 27 Aug 2014 21:51:16 +0900 (JST)	[thread overview]
Message-ID: <20140827.215116.31466034.tkk@misasa.okayama-u.ac.jp> (raw)

To make a plot using org-babel/R with org-table (as data.frame) is an
ultimate weapon for me.  Thank you for development.

Occasionally I want to transport datasets between Excel and org-table.
Two essential operations are (a) to copy cells from Excel and paste
into org-table and (b) other way around.  How those would work is
describe as below.

(a) Copy cells from Excel and paste into org-table
 - Datasets copied at Excel are stored in kill-ring as tabulated text.
   Tabulated text would be converted into org-table-clip.
 - A user selects a cell in org-table and pastes the datasets by
   (org-table-paste-rectangle)

(b) Copy cells from org-table and paste into Excel
 - A user selects cells by mouse drag on org-table and copies datasets
   by (org-table-copy-region).
 - The datasets are store as org-table-clip.  They would be converted
   into tabulated text and stored in kill-ring.
 - A user will paste the datasets in Excel

When those are assigned to (a) M-s-v and (b) M-s-c, Emacs speaks to
spreadsheet nicer than I expected.  I want to let you know!






Followings barely work.
--
(global-set-key (kbd "M-s-x") 'orgtbl-cell-cut)
(global-set-key (kbd "M-s-c") 'orgtbl-cell-copy)
(global-set-key (kbd "M-s-v") 'orgtbl-cell-paste)

(defun orgtbl-cell-copy ()
  "Copy cells in org-table and store as tabulated text in
kill-ring."
  (interactive)
  (if (org-at-table-p)
      (progn (call-interactively 'org-table-copy-region)
             (clip-orgtbl2tab)
             (exchange-point-and-mark))
    (call-interactively 'copy-rectangle-as-kill)
    (clip-rectangle2text)))

(defun orgtbl-cell-cut ()
  "Cut cells in org-table and store as tabulated text in
kill-ring."
  (interactive)
  (if (org-at-table-p)
      (progn (call-interactively 'org-table-cut-region)
             (clip-orgtbl2tab))
    (call-interactively 'kill-rectangle)
    (clip-rectangle2text)))

(defun orgtbl-cell-paste (&optional arg)
  "Paste tabulated text stored in kill-ring into org-table.  If point is not on org-table,
this creates a new org-table."
  (interactive "P")
  (cond
   (arg
    (org-table-paste-rectangle))
   ((org-at-table-p) ; (org-table-check-inside-data-field t)
    (clip-tab2orgtbl)
    (org-table-paste-rectangle))
   (t
    (let (tempclip-in-orgtbl)
      (with-temp-buffer
        (org-table-create "1x1")
        (goto-char (+ 1 (point)))
        (clip-tab2orgtbl)
        (org-table-paste-rectangle)
        (mark-whole-buffer)
        (setq tempclip-in-orgtbl (buffer-substring (point-min) (point-max))))
      (insert tempclip-in-orgtbl)))))

(defun clip-tab2orgtbl ()
  "Create org-table-clip from tabulated text stored in
kill-ring."
  (with-temp-buffer
    (org-mode)
    (yank)
    (orochi-tbl-tab2org (point-min) (point-max))
    (goto-char (+ 1 (point-min)))
    (org-table-align)
    (org-table-copy-region (+ 1 (point-min)) (- (point-max) 2))))

(defun clip-orgtbl2tab ()
  "Store tabulated text to kill-ring that was converted from
org-table-clip."
  (with-temp-buffer
    (org-mode)
    (org-table-create "1x1")
    (goto-char (+ 1 (point-min)))
    (org-table-paste-rectangle)
    (mark-whole-buffer)
    (call-interactively 'orochi-tbl-org2tab)
    (mark-whole-buffer)
    (call-interactively 'kill-region)))

(defun clip-rectangle2text ()
  "Convert killed-rectangle to normal text and store in
kill-ring."
  (with-temp-buffer
    (yank-rectangle)
    (mark-whole-buffer)
    (call-interactively 'kill-region)))

(defun orochi-tbl-org2tab (start end)
  "Convert orgtbl on region to tab delimited text"
  (interactive "r")
  (orochi-replace-regexps '(("^\s*\|\-.*\n" . "")
                            ("^\s*\|\s*"    . "")
                            ("\s*\|\s*$"    . "")
                            ("\s*\|\s*"     . "	"))
                          start end))

(defun orochi-replace-regexps (rep-list start end)
  "Find and replace region for a set of regexps"
  (save-excursion
    (save-restriction
      (narrow-to-region start end)
      (dolist (re-rep rep-list)
        (goto-char (point-min))
        (while (re-search-forward (car re-rep) nil t)
          (replace-match (cdr re-rep)))))))

                 reply	other threads:[~2014-08-27 12:51 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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

  List information: https://www.orgmode.org/

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

  git send-email \
    --in-reply-to=20140827.215116.31466034.tkk@misasa.okayama-u.ac.jp \
    --to=tkk@misasa.okayama-u.ac.jp \
    --cc=emacs-orgmode@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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).