Function: kotl-mode:exchange-cells

kotl-mode:exchange-cells is an interactive and byte-compiled function defined in kotl-mode.el.

Signature

(kotl-mode:exchange-cells CELL-REF-1 CELL-REF-2)

Documentation

Exchange CELL-REF-1 with CELL-REF-2 in current view. Don't move point.

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/hyperbole-20260414.325/kotl/kotl-mode.el
(defun kotl-mode:exchange-cells (cell-ref-1 cell-ref-2)
  "Exchange CELL-REF-1 with CELL-REF-2 in current view.  Don't move point."
  (interactive
   (hargs:iform-read
    '(interactive "*+KExchange cell: \n+KExchange cell <%s> with cell: ")
    (save-excursion
      (list (kcell-view:label)
	    (cond
	     ((kcell-view:previous t)
	      (kcell-view:label))
	     ((kcell-view:next t)
	      (kcell-view:label))
	     (t (error
		 "(kotl-mode:exchange-cells): No two visible cells available")))))))
  (unless (and (or (stringp cell-ref-1) (natnump cell-ref-1))
	       (or (stringp cell-ref-2) (natnump cell-ref-2)))
    (error "(kotl-mode:exchange-cells): Cell refs must be either strings or numbers >= 0, not: '%s' and '%s'"
	   cell-ref-1 cell-ref-2))
  (when (equal cell-ref-1 cell-ref-2)
    (error "(kotl-mode:exchange-cells): Cannot exchange as both cell refs are the same: '%s' and '%s'"
	   cell-ref-1 cell-ref-2))
  (save-excursion
    (let (kcell-1 contents-1 idstamp-1
	  kcell-2 contents-2 idstamp-2)
      ;;
      ;; Save cell-1 attributes
      (kotl-mode:goto-cell cell-ref-1 t)
      (setq kcell-1 (kcell-view:cell)
	    idstamp-1 (kcell-view:idstamp-integer)
	    contents-1 (kcell-view:contents))
      ;;
      ;; Save cell-2 attributes
      (kotl-mode:goto-cell cell-ref-2 t)
      (setq kcell-2 (cl-copy-list (kcell-view:cell))
	    idstamp-2 (kcell-view:idstamp-integer)
	    contents-2 (kcell-view:contents))

      ;; Substitute cell-1 contents into cell-2 location.
      (delete-region (kcell-view:start) (kcell-view:end-contents))
      (insert
       (replace-regexp-in-string
	"\\([\n\r]\\)"
	(concat "\\1" (make-string (kcell-view:indent) ?\ )) contents-1))
      (when kotl-mode:refill-flag
	(kotl-mode:fill-cell))

      ;; Substitute cell-2 contents into cell-1 location.
      (kotl-mode:goto-cell cell-ref-1 t)
      ;; Exchange cell contents.
      (delete-region (kcell-view:start) (kcell-view:end-contents))
      ;; Add indentation to all but first line.
      (insert
       (replace-regexp-in-string
	"\\([\n\r]\\)"
	(concat "\\1" (make-string (kcell-view:indent) ?\ )) contents-2))
      (when kotl-mode:refill-flag
	(kotl-mode:fill-cell))

      (save-excursion
	;;
	;; Substitute cell-1 attributes into cell-2 location.
	;;
	(kotl-mode:goto-cell cell-ref-2 t)
	;; Set kcell properties.
	(kcell-view:set-cell kcell-1 idstamp-1)
	;; If idstamp labels are on, then must exchange labels in view.
	(when (eq (kview:label-type kotl-kview) 'id)
          (klabel:set (format "0%d" idstamp-1))))

      ;;
      ;; Substitute cell-2 attributes into cell-1 location.
      ;;
      ;; Set kcell properties.
      (kcell-view:set-cell kcell-2 idstamp-2)
      ;; If idstamp labels are on, then must exchange labels in view.
      (when (eq (kview:label-type kotl-kview) 'id)
        (klabel:set (format "0%d" idstamp-2))))))