emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] Emacs speaks to spreadsheet


From: Tak Kunihiro
Subject: [O] Emacs speaks to spreadsheet
Date: Wed, 27 Aug 2014 21:51:16 +0900 (JST)

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 via email to

[Prev in Thread] Current Thread [Next in Thread]