From mboxrd@z Thu Jan 1 00:00:00 1970 From: Tak Kunihiro Subject: Emacs speaks to spreadsheet Date: Wed, 27 Aug 2014 21:51:16 +0900 (JST) Message-ID: <20140827.215116.31466034.tkk@misasa.okayama-u.ac.jp> Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:51901) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XMchP-0002Bm-Li for emacs-orgmode@gnu.org; Wed, 27 Aug 2014 08:51:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XMchH-0001FR-E0 for emacs-orgmode@gnu.org; Wed, 27 Aug 2014 08:51:39 -0400 Received: from scmgateway1.cc.okayama-u.ac.jp ([42.127.236.165]:16036) by eggs.gnu.org with smtp (Exim 4.71) (envelope-from ) id 1XMchG-0001Es-TR for emacs-orgmode@gnu.org; Wed, 27 Aug 2014 08:51:31 -0400 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Cc: tkk@misasa.okayama-u.ac.jp 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)))))))