4

When I launch my emacs software (Aquamacs.app) I see a message

File mode specification error: (error ":END: line missing at position 23740")

Unfortunately, I do not know in which file this problem occurs. It must be an org-mode buffer, where I have a drawer like :PROPERTIES: or :LOGBOOK: where I accidentally deleted the corresponding :END: line.

I don't see how to find the corresponding place in my files.

Is there a way to check all open buffers for such inconsistency or get a clearer error message which also specifies the buffer where the problem occurs?

3

This appears to be coming from the function org-flag-drawer.

Use C-h f to find where this function is defined, and modify it to also give the buffer name:

(defun org-flag-drawer (flag)
  "When FLAG is non-nil, hide the drawer we are within.
    Otherwise make it visible."
  (save-excursion
    (beginning-of-line 1)
    (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
      (let ((b (match-end 0)))
        (if (re-search-forward
             "^[ \t]*:END:"
             (save-excursion (outline-next-heading) (point)) t)
            (outline-flag-region b (point-at-eol) flag)
          (user-error (concat (buffer-name) " :END: line missing at position %s" b)))))))
  • hm, I tried it but the error does not occur any more. :-) Seems, I have (unwillingly) already repaired the org file. – MostlyHarmless Nov 13 '14 at 11:30
2

The error message tells you where the error is: position 23740. You can go there with goto-char. Doing M-x goto-char 23740 will take you to the error. org-mode's folding may make it hard to see what's going on, so switching to fundamental-mode (with M-x fundamental-mode) first may help you see what's going on.

  • yes, I found that out already, but the error message does not tell me which org-file is the one to check and I have many of them open, they get loaded automatically in the background for the agenda – MostlyHarmless Nov 14 '14 at 6:16
1

I've had the same problem. My solution was to write a simple lint-type command.

This adds the command sh/org-lint which will iterate over all the buffers loaded from my agenda and check for this kind of problem. It lists the problems in a compilation buffer, which means that you can just press RETURN to jump to the problems.

It's pretty simplistic, and I keep meaning to improve it, but works well enough for me when I need to track down these kind of things.

Apologies in advance for my shocking elisp.

(defun sh/org-lint-buffer ()
  "Check for balanced BEGIN/END markers in the current Org-mode buffer.

 My edits (especially resolution of git merge conflicts) seem to
 sometimes mess up the balancing of the ':BEGIN:' and ':END:'
 markers in the file. Org-mode does report these as errors when
 it's parsing the file (you can see the messages in the
 '*Messages*' buffer), but it unhelpfully does not identify the
 buffer where the problem has occurred. This function searches
 through the current buffer and reports buffer and line positions
 of the errors, which is much more useful."

  ;; <START> markers are:
   ;;; LOGBOOK, PROPERTIES
  ;; Cases:
   ;;; <START> with no END before end of buffer
   ;;; <START> with no END before next <START>
   ;;; END before <START>

  (interactive)
  (save-excursion
    (save-restriction
      (widen)
      (goto-char (point-min))
      (let ((previous-point (point))
            (next-max-point)
            (end-marker-re "^:END:")
            (start-marker-re "^:LOGBOOK:\\|:PROPERTIES:"))

        ;; Before we move to the first start block, we're now outside
        ;; of a BEGIN/END. If we find another END before the next
        ;; block then that is clearly an orphan.
        (save-excursion
          (save-excursion
            (setq next-max-point
                  (or (re-search-forward start-marker-re (point-max) t)
                      (point-max))))
          (if (re-search-forward end-marker-re next-max-point t)
              (sh/org-lint-error (buffer-file-name) (line-number-at-pos) "orphaned END marker")))

        ;; Loop until there are no more start markers.
        (while (re-search-forward start-marker-re nil t)
          ;; Try to find the END marker that matches this start
          ;; position and move to there.
          (save-excursion
            (setq next-max-point
                  (or (re-search-forward start-marker-re (point-max) t)
                      (point-max))))
          (unless (re-search-forward end-marker-re next-max-point t)
            (sh/org-lint-error (buffer-file-name) (line-number-at-pos) "START without END"))

          ;; Move on.
          (setq previous-point (point))

          ;; We're now outside of a BEGIN/END; if we find another END
          ;; before the next block then that is clearly an orphan.
          (save-excursion
            (save-excursion
              (setq next-max-point
                    (or (re-search-forward start-marker-re (point-max) t)
                        (point-max))))
            (if (re-search-forward end-marker-re next-max-point t)
                (sh/org-lint-error (buffer-file-name) (line-number-at-pos) "orphaned END marker"))))))))

(defun sh/org-lint ()
  "Check for balanced BEGIN/END markers in all Org-mode buffers.

 Apply Org-mode file structure and style checks to all Org-mode
 buffers. See `sh/org-lint-buffer' for a description of the checks
 performed."
  (interactive)
  (save-excursion
    (with-current-buffer (get-buffer-create "*Org lint*")
      (read-only-mode 0)
      (erase-buffer)))
  (mapcar (lambda(buffer)
            (with-current-buffer buffer
              (if (eq major-mode 'org-mode)
                  (sh/org-lint-buffer))))
          (buffer-list))
  (when (buffer-size
         (with-current-buffer (get-buffer "*Org lint*")
           (compilation-mode)
           (current-buffer)))
    (switch-to-buffer-other-window "*Org lint*")))

(defun sh/org-lint-error (buffer-name line-number error-message)
  "Report an error when linting an Org-mode buffer.

 Reports ERROR-MESSAGE as being the problem reported for line
 LINE-NUMBER in the buffer called BUFFER-NAME."
  (save-excursion
    (with-current-buffer (get-buffer-create "*Org lint*")
      (insert
       (format "%s:%d: %s\n" buffer-name line-number error-message)))))
  • cool, thanks a lot! I'll try that out when the error occurs next time! – MostlyHarmless Dec 22 '14 at 20:25

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.