I'm trying to create a formula that modifies overlays on an org-table. I need to figure out the position in the buffer of the cell so I can create an overlay that spans the length of the cell.

How can I figure out the start-pos and end-pos of the cell?


It is relatively easy to get the beginning and end position of a cell within a tbl-formula:

* First heading
  | first column | second column | third column |
  |            1 | 139,154       |              |
  |              | 189,204       |              |
  #+TBLFM: $2='(progn (org-table-goto-line  @#) (org-table-goto-column  $#) (format "%d,%d" (save-excursion (skip-chars-backward "^|") (point)) (save-excursion (skip-chars-forward "^|") (point))))

You can check that this delivers the right positions by:

M-: (overlay-put (make-overlay 189 204) 'face 'org-warning)

where you have to replace the pair of numbers 189 204 with a pair from the second column of the table (after re-calculation of the table).

But, I am afraid that this does not help you very much to put overlays on cells. At least the overlays are not very stable:

  1. You have to give the formula a return value and this return value will overwrite the content of the cell (or some cell). So the formula will effect the table aside of putting an overlay over some text.
  2. Any formatting of the table such as tab will overwrite your manually formatted text of the table.

The following table demonstrates the problems with creating overlays in formulas.

* First heading
  | first column | second column                   | third column              |
  |              | Example: cell start 201 end 234 | Superfluous modification. |
  |              | Example: cell start 282 end 315 | Superfluous modification. |
  |              | Example: cell start 363 end 396 | Superfluous modification. |
  #+TBLFM: $2='(save-excursion (org-table-goto-line  @#) (org-table-goto-column $#) (format "Example: cell start %d end %d" (progn (skip-chars-backward "^|") (point)) (progn (skip-chars-forward "^|") (point))))::$3='(save-excursion (org-table-goto-line  @#) (org-table-goto-column (1- $#)) (overlay-put (make-overlay (progn (skip-chars-backward "^|") (point)) (progn (skip-chars-forward "^|") (point))) 'face 'org-warning) "Superfluous modification.")

If you switch on formula-debugging and re-calculate the table you will see that the expected overlays are put onto the cells of the second column but removed again when the table is re-formatted at the end of the calculation.

| improve this answer | |
  • For overlays I guess you could use markers. Though, actually I think I remember that org-table is not marker friendly in the way tables are updated. Perhaps one could add-function on org-table-align and reapply overlays that way... – rasmus May 25 '15 at 10:43

Here's something to get you started

(defun rasmus/org-cell-pos (row column)
  "Return cons of and org table cell beginning and end.
ROW is the line of the cell and COLUMN is the column of cell."
  (if (org-at-table-p)
        (org-table-goto-line row)
        (org-table-goto-column column)
        (cons (save-excursion (search-backward "|")
                              (1+ (point)))
              (save-excursion (search-forward "|")
                              (1- (point)))))
    (error "Not at org table")))
| 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.