1

Is it there a easy way to fill the same data in the rectangle area from cell a to cell b (4x4 cells) in the org-mode table as shown below in one go?

|   |   |   |   |   |   |   |   |
|---+---+---+---+---+---+---+---|
|   |   |   |   |   |   |   |   |
|   |   | a |   |   |   |   |   |
|   |   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |   |
|   |   |   |   |   | b |   |   |
|   |   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |   |
  • Not too clear. Are you asking to copy the content of cell a to cell b, overwriting any content that might be there already? Or to move it? Or something else? (Usually "fill" in Emacs means filling text to fit lines with a fill-column, but I don't think you are asking about that.) – Drew Dec 23 '15 at 0:26
3

I assume that the following code does what you want. Select the rectangle from a to b in your table and call M-x org-table-copy-corner-to-rectangle. All fields inside the rectangle are filled with a. You can put the code into your init-file (e.g., ~/.emacs) and bind some key or menu-entry to it. Adding a menu item to the Tbl menu is already included at the end of the following code.

(require 'org-table)
(defun org-table-copy-corner-to-rectangle  ()
  "Copy the field at the start corner of the currently active rectangle to the full rectangle."
  (interactive)
  (unless (org-region-active-p)
    (error "Active region in org-table required"))
  (let* ((beg (region-beginning))
     (end (region-end)))
    (save-excursion
      (goto-char beg)
      (org-table-check-inside-data-field)
      (let* ((field (save-excursion (org-table-get-field)))
         (l01 (org-table-current-dline))
         (c01 (org-table-current-column))
         (l02 (progn (goto-char end)
             (org-table-check-inside-data-field)
             (org-table-current-dline)))
         (c02 (org-table-current-column))
         (cMin (min c01 c02))
         (cMax (max c01 c02))
         (nl (abs (- l01 l02))))
    (goto-char beg)
    (loop for row from 0 upto nl do
          (while (org-at-table-hline-p) (beginning-of-line 2))
          (if (not (org-at-table-p))
          (progn (end-of-line 0) (org-table-next-field)))
          (loop for col from cMin upto cMax do
            (org-table-goto-column col nil 'force)
            (org-table-get-field nil field))
          (forward-line)))
      (goto-char beg)
      (org-table-align))))

(easy-menu-add-item org-tbl-menu '("Rectangle") ["Copy start corner to rectangle" org-table-copy-corner-to-rectangle :active (org-at-table-p)])
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.