- (let ((num todos-categories-category-number))
- (save-excursion
- (forward-line 0)
- (skip-chars-forward " ")
- (setq num (number-at-point)))
- (when (and num (if lower
- (< num (length todos-categories))
- (> num 1)))
- (let* ((col (current-column))
- (beg (progn (forward-line (if lower 0 -1)) (point)))
- (num1 (progn (skip-chars-forward " ") (1- (number-at-point))))
- (num2 (1+ num1))
- (end (progn (forward-line 2) (point)))
- (catvec (vconcat todos-categories))
- (cat1-list (aref catvec num1))
- (cat2-list (aref catvec num2))
- (cat1 (car cat1-list))
- (cat2 (car cat2-list))
- buffer-read-only newcats)
- (delete-region beg end)
- (setq num1 (1+ num1))
- (setq num2 (1- num2))
- (setq num num2)
- (todos-insert-category-line cat2)
- (setq num num1)
- (todos-insert-category-line cat1)
- (aset catvec num2 (cons cat2 (cdr cat2-list)))
- (aset catvec num1 (cons cat1 (cdr cat1-list)))
- (setq todos-categories (append catvec nil))
- (setq newcats todos-categories)
- (with-current-buffer (get-file-buffer todos-current-todos-file)
- (setq todos-categories newcats)
- (todos-update-categories-sexp))
- (forward-line (if lower -1 -2))
- (forward-char col)))))
+ (save-excursion
+ (forward-line 0)
+ (skip-chars-forward " ")
+ (setq todos-categories-category-number (number-at-point)))
+ (when (if lower
+ (< todos-categories-category-number (length todos-categories))
+ (> todos-categories-category-number 1))
+ (let* ((col (current-column))
+ ;; The line we're raising to, or lowering from...
+ (beg (progn (forward-line (if lower 0 -1)) (point)))
+ ;; ...and its number.
+ (num1 (progn (skip-chars-forward " ") (1- (number-at-point))))
+ ;; The number of the line we're exchanging with.
+ (num2 (1+ num1))
+ ;; The start of the line below the one we're exchanging with.
+ (end (progn (forward-line 2) (point)))
+ (catvec (vconcat todos-categories))
+ ;; Category names and item counts of the two lines being exchanged.
+ (cat1-list (aref catvec num1))
+ (cat2-list (aref catvec num2))
+ (cat1 (car cat1-list))
+ (cat2 (car cat2-list))
+ buffer-read-only newcats)
+ (delete-region beg end)
+ (setq num1 (1+ num1))
+ (setq num2 (1- num2))
+ ;; Exchange the lines and rebuttonize them.
+ (setq todos-categories-category-number num2)
+ (todos-insert-category-line cat2)
+ (setq todos-categories-category-number num1)
+ (todos-insert-category-line cat1)
+ ;; Update todos-categories alist.
+ (aset catvec num2 (cons cat2 (cdr cat2-list)))
+ (aset catvec num1 (cons cat1 (cdr cat1-list)))
+ (setq todos-categories (append catvec nil))
+ (setq newcats todos-categories)
+ (with-current-buffer (get-file-buffer todos-current-todos-file)
+ (setq todos-categories newcats)
+ (todos-update-categories-sexp))
+ (forward-line (if lower -1 -2))
+ (forward-char col))))