I want to define a function org-move-subtree-top that calls org-move-subtree-up until it returns a user-error (in this case "Cannot move past superior level or buffer limit"). How can I do this in elisp?

1 Answer 1


Write an infinite loop, and catch the error outside the loop.

(condition-case nil
    (while t
  (error nil))

Org mode 7.8.11 throws a plain error for this message. If you have a more recent version that calls user-error instead of error, replace error by user-error.

This hides all errors. It would be safer to catch only the specific error that you intended to happen, but that requires hard-coding the error message.

(condition-case err
    (while t
  (error (or (equal (cdr err) "Cannot move past superior level or buffer limit")
             (signal (car err) (cdr err)))))

Alternatively, you could implement this without calling org-move-subtree-up: find the boundaries of the subtree, find the target position, and move it there. Untested.

(defun org-move-subtree-top ()
  "Move the current subtree just after the heading of its parent."
  (interactive "*")
  (let* ((target (save-excursion
        (beg (progn
        (end (progn
        (text (buffer-substring beg end)))
    (delete-region text)
    (goto-char target)
    (insert text)))
  • The first snippet is essentially just ignore-errors. in order to only catch user errors (as the OP asked) you can replace the (error nil) with (user-error nil).
    – Malabarba
    Commented Mar 29, 2015 at 19:06
  • @Malabarba The Org mode version I have on my machine calls (error "Cannot move past superior level or buffer limit"). If newer versions call user-error then indeed error should be replaced by user-error in my snippet. Commented Mar 29, 2015 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.