1

Is there a command for moving only the value 11 to the right column in the following column?

| Name |    |   |
|------+----+---|
| a    | 11 |   |
| b    | 10 |   |

Something like org-table-move-column but applying to a single cell only.

1
  • There's no out of the box solution that I know of. Have a look at org-table-copy-down in org-table.el , It will give you the basis to write your function. your – manandearth Nov 10 '18 at 11:24
1

Here is a naive function that I put together in few minutes. It uses interactive commands, so there must be a more correct way of doing the same thing.

Org table commands behave in quite surprising ways. See command documentation to understand better what is happening here.

This function does not delete the cell content in the next cell, but prepends to it. It should be easy to modify it to remove the next cell content first.

 (defun org-table-prepend-cell-to-right ()
   "Cut table cell content and prepend it to cell contects on its right."
   (interactive)
   (when (org-at-table-p 'any)
     (goto-char (1+ (point)))
     (call-interactively 'org-table-beginning-of-field)
     (let ((start (point)) (string))
       (call-interactively 'org-table-end-of-field)
       (setq string (concat (delete-and-extract-region start (point)) " "))
       (org-table-next-field)
       (insert string)
       (org-ctrl-c-ctrl-c))))
9
  • This function exits at the call to org-backward-sentence with Wrong type argument: commandp, org-table-beginning-of-field. Does it work on your computer and which version of Emacs are you using? I'm using 26.1. – Raul Laasner Nov 10 '18 at 21:30
  • It works for me for almost all cells of a table, It does not take into account all edge cases. It does work if you put the cursor on string "11" in your example. I am using GNU Emacs 26.1, but more importantly, I am using Org mode version 9.1.14. – Heikki Nov 10 '18 at 22:42
  • I upgraded Org mode to 9.1.14 now. However, running org-backward-sentence still exits with the same error. This is strange because it is a basic org-mode command that has existed for some time. Do you have any suggestions on how to debug this? Otherwise I'll just open a ticket with org-mode maintainers. – Raul Laasner Nov 10 '18 at 23:20
  • Unfortunately I do not have any further suggestions. I tried replacing org-backward-sentence with org-table-beginning-of-field that the first function is calling, but I got an error progn: Wrong type argument: number-or-marker-p, nil. That does not make much sense to me,either. – Heikki Nov 11 '18 at 6:10
  • Are you sure you are running 9.1.4.? Depending how you upgraded, you might have to run org-reload first. – Heikki Nov 11 '18 at 6:13
0

Following advice from @manandearth, I modified the org-table-move-cell function from org-table.el by commenting out a few lines:

(defun org-table-move-cell (&optional left)
  "Move the current cell to the right.  With arg LEFT, move to the left."
  (interactive "P")
  (unless (org-at-table-p) (user-error "Not at a table"))
  (org-table-find-dataline)
  (org-table-check-inside-data-field)
  (let* ((col (org-table-current-column))
         (col1 (if left (1- col) col))
         (colpos (if left (1- col) (1+ col)))
         (beg (org-table-begin))
         (end (copy-marker (org-table-end))))
    (when (and left (= col 1))
      (user-error "Cannot move cell further left"))
    (when (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
      (user-error "Cannot move cell further right"))
    (org-table-save-field
     ;; (goto-char beg)
     ;; (while (< (point) end)
     (unless (org-at-table-hline-p)
       (org-table-goto-column col1 t)
       (when (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
         (transpose-regions
          (match-beginning 1) (match-end 1)
          (match-beginning 2) (match-end 2))
         )
       )
     ;; (forward-line)
     ;; )
     )
    (set-marker end nil)
    (org-table-goto-column colpos)
    (org-table-align)
    (when (or (not org-table-fix-formulas-confirm)
              (funcall org-table-fix-formulas-confirm "Fix formulas? "))
      (org-table-fix-formulas
       "$" (list (cons (number-to-string col) (number-to-string colpos))
                 (cons (number-to-string colpos) (number-to-string col))))
      (org-table-fix-formulas
       "$LR" (list (cons (number-to-string col) (number-to-string colpos))
                   (cons (number-to-string colpos) (number-to-string col))))
      )
    )
  )

The commented lines would otherwise have the effect of going to the beginning of table and then processing every line until the end of table is reached. Now only the current line is processed and thus only two cells are transposed.

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.