From 4f5a8ae4eaed79b52b5f3529102611e06577e439 Mon Sep 17 00:00:00 2001 From: Chris Kauffman Date: Tue, 29 May 2018 14:37:24 -0500 Subject: [PATCH 6/8] Code changes for single-cell movement with notes. This version has some remaining contents and commented code. --- doc/org-manual.org | 24 ++++++++++ lisp/org-table.el | 94 +++++++++++++++++++++++---------------- testing/examples/pub/link | 1 - 3 files changed, 80 insertions(+), 39 deletions(-) delete mode 120000 testing/examples/pub/link diff --git a/doc/org-manual.org b/doc/org-manual.org index 51884ec99..40f542335 100644 --- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -1577,6 +1577,30 @@ you, configure the option ~org-table-auto-blank-field~. #+findex: org-table-kill-row Kill the current row or horizontal line. +- {{{kbd(S-UP)}}} (~org-table-move-single-cell-up~) :: + + #+kindex: S-UP + #+findex: org-table-move-single-cell-up + Move single cell up by swapping with adjacent cells. + +- {{{kbd(S-DOWN)}}} (~org-table-move-single-cell-down~) :: + + #+kindex: S-DOWN + #+findex: org-table-move-single-cell-down + Move single cell down by swapping with adjacent cells. + +- {{{kbd(S-LEFT)}}} (~org-table-move-single-cell-left~) :: + + #+kindex: S-LEFT + #+findex: org-table-move-single-cell-left + Move single cell left by swapping with adjacent cells. + +- {{{kbd(S-RIGHT)}}} (~org-table-move-single-cell-right~) :: + + #+kindex: S-RIGHT + #+findex: org-table-move-single-cell-right + Move single cell right by swapping with adjacent cells. + - {{{kbd(M-S-DOWN)}}} (~org-table-insert-row~) :: #+kindex: M-S-DOWN diff --git a/lisp/org-table.el b/lisp/org-table.el index e8898a1cb..009f50d19 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -1441,76 +1441,94 @@ non-nil, the one above is used." (t (setq min mean))))) (if above min max)))))) -;;;###autoload -(defun org-table-max-line-col () - "Return the maximum line and column of the current table as a -list of two numbers" - (when (not (org-at-table-p)) - (user-error "Not in an org-table")) - (let ((table-end (org-table-end))) - (save-mark-and-excursion - (goto-char table-end) - (org-table-previous-field) - (list (org-table-current-line) (org-table-current-column))))) - -;;;###autoload -(defun org-table-swap-cells (row1 col1 row2 col2) - "Swap two cells indicated by the coordinates provided" +;; replaced with call to org-table-analyze and use of variables set by it + +;; ;;;###autoload +;; (defun org-table-max-line-col () +;; "Return the maximum line and column of the current table as a +;; list of two numbers" +;; (when (not (org-at-table-p)) +;; (user-error "Not in an org-table")) +;; (let ((table-end (org-table-end))) +;; (save-mark-and-excursion +;; (goto-char table-end) +;; (org-table-previous-field) +;; (list (org-table-current-line) (org-table-current-column))))) + +; ;;;###autoload ;; told not to autoload by Nicolas Goaziou +(defun org-table--swap-cells (row1 col1 row2 col2) + "Swap two cells indicated by the coordinates provided. +row1, col1, row2, col2 are integers indicating the row/column +position of the two cells that will be swapped in the table." (let ((content1 (org-table-get row1 col1)) (content2 (org-table-get row2 col2))) (org-table-put row1 col1 content2) (org-table-put row2 col2 content1) - (org-table-align))) - -;;;###autoload -(defun org-table-move-single-cell (direction) - "Move the current cell in a cardinal direction according to the -parameter symbol: 'up 'down 'left 'right. Swaps contents of -adjacent cell with current one." - (unless (org-at-table-p) - (error "No table at point")) - (let ((drow 0) (dcol 0)) +; (org-table-align) ;; told by Nicolas Goaziou that this is "out of scope" + )) + +; ;;;###autoload ; told not to auto-load by Nicolas Goaziou, also reformatted comment +(defun org-table--move-single-cell (direction) + "Move the current cell in a cardinal direction. +Parameter direction is a symbol which mathes 'up 'down 'left or +'right. The contents the current cell are swapped with cell in +the indicated direction." +;removed check for in table; moved to public functions + (let ((drow 0) (dcol 0)) (cond ((equal direction 'up) (setq drow -1)) ((equal direction 'down) (setq drow +1)) ((equal direction 'left) (setq dcol -1)) ((equal direction 'right) (setq dcol +1)) (t (error "Not a valid direction, must be one of 'up 'down 'left 'right"))) + (org-table-analyze) ;to set org-table-current-ncol and org-table-dlines (let* ((row1 (org-table-current-line)) (col1 (org-table-current-column)) (row2 (+ row1 drow)) (col2 (+ col1 dcol)) - (max-row-col (org-table-max-line-col)) - (max-row (car max-row-col)) - (max-col (cadr max-row-col))) - (when (or (< col1 1) (< col2 1) (> col2 max-col) (< row2 1) (> row2 max-row)) + (org-table-current-nrow (- (length org-table-dlines) 1))) + (when (or (< col1 1) (< col2 1) (< row2 1) + (> col2 org-table-current-ncol) + (> row2 org-table-current-nrow)) (user-error "Cannot move cell further")) - (org-table-swap-cells row1 col1 row2 col2) + (org-table--swap-cells row1 col1 row2 col2) (org-table-goto-line row2) (org-table-goto-column col2)))) ;;;###autoload (defun org-table-move-single-cell-up () - "Move a single cell up in a table; swap with anything in target cell" + "Move a single cell up in a table; swap with anything in target cell." (interactive) - (org-table-move-single-cell 'up)) + (unless (org-table-check-inside-data-field) + (error "No table at point")) + (org-table--move-single-cell 'up) + (org-table-align)) ;;;###autoload (defun org-table-move-single-cell-down () - "Move a single cell down in a table; swap with anything in target cell" + "Move a single cell down in a table; swap with anything in target cell." (interactive) - (org-table-move-single-cell 'down)) + (unless (org-table-check-inside-data-field) + (error "No table at point")) + (org-table--move-single-cell 'down) + (org-table-align)) ;;;###autoload (defun org-table-move-single-cell-left () - "Move a single cell left in a table; swap with anything in target cell" + "Move a single cell left in a table; swap with anything in target cell." (interactive) - (org-table-move-single-cell 'left)) + (unless (org-table-check-inside-data-field) + (error "No table at point")) + (org-table--move-single-cell 'left) + (org-table-align)) ;;;###autoload (defun org-table-move-single-cell-right () - "Move a single cell right in a table; swap with anything in target cell" + "Move a single cell right in a table; swap with anything in target cell." (interactive) - (org-table-move-single-cell 'right)) + (unless (org-table-check-inside-data-field) + (error "No table at point")) + (org-table--move-single-cell 'right) + (org-table-align)) ;;;###autoload (defun org-table-delete-column () diff --git a/testing/examples/pub/link b/testing/examples/pub/link deleted file mode 120000 index 01bb51522..000000000 --- a/testing/examples/pub/link +++ /dev/null @@ -1 +0,0 @@ -../pub-symlink \ No newline at end of file -- 2.17.0