org-make-toc icon indicating copy to clipboard operation
org-make-toc copied to clipboard

make org-make-toc error message less intrusive

Open VladimirAlexiev opened this issue 4 years ago • 5 comments

For most of my docs I use this mode so I set

(add-hook 'org-mode-hook #'org-make-toc-mode)

But not for all... In that case I get this error on every save, which is annoying:

"org-make-toc: No TOC node found."

Is there a way to make org-make-toc check whether there's a :CONTENTS: drawer at its start and if not, give up silently?

VladimirAlexiev avatar May 24 '20 19:05 VladimirAlexiev

Is there a way to make org-make-toc check whether there's a :CONTENTS: drawer at its start and if not, give up silently?

At what's start?

If the user calls org-make-toc, it's expected that the user intends for a TOC to be created, and therefore if the document is not properly configured, the user should be informed.

For that reason, I don't recommend adding the mode to org-mode-hook for all documents. I add it to the after-save-hook in documents I want TOCs created automatically in.

If you prefer to do it the other way, you could modify or advise the relevant code in various ways in your personal Emacs configuration.

alphapapa avatar May 24 '20 19:05 alphapapa

I mean a :CONTENTS: drawer somewhere in the doc.

I think the presence of such drawer is enough indication that I'd like the TOC to be updated, and vice versa: if there's no such drawer then there's nothing to update.

add it to the after-save-hook

  • Shouldn't this be before-save-hook?
  • What's the easiest way for a user to add that for some docs? The best would be if org-make-toc does such addition automatically

I have some code to insert meaningful anchors (CUSTOM_ID) to headings. It uses the in-buffer #+OPTIONS: anchor:* to record whether it should do its stuff. Let me know if I should share the code.

Cheers!

VladimirAlexiev avatar Jun 09 '20 16:06 VladimirAlexiev

I think the presence of such drawer is enough indication that I'd like the TOC to be updated, and vice versa: if there's no such drawer then there's nothing to update.

As I said, if the user calls org-make-toc, it's expected that the user intends for a TOC to be created, and therefore if the document is not properly configured, the user should be informed. If you don't want a TOC to be created, it's intended that you don't call that command.

Shouldn't this be before-save-hook?

Yes. See https://github.com/alphapapa/org-make-toc#automatically-update-on-save

What's the easiest way for a user to add that for some docs? The best would be if org-make-toc does such addition automatically

See https://github.com/alphapapa/org-make-toc#automatically-update-on-save

I have some code to insert meaningful anchors (CUSTOM_ID) to headings. It uses the in-buffer #+OPTIONS: anchor:* to record whether it should do its stuff. Let me know if I should share the code.

I don't understand what you mean.

alphapapa avatar Jun 10 '20 01:06 alphapapa

Or, you may activate it in all Org buffers like this: (add-hook 'org-mode-hook #'org-make-toc-mode)

That's what I've done and I get the error org-make-toc: No TOC node found on every save of non-org file, or org file without TOC. I haven't run org-make-toc myself.

IMHO when called from before-save-hook, org-make-toc should not presume that it's in an org buffer and that the buffer has TOC.

I don't see an easy way to fix it with defadvice, so I currently use this code:

(require 'org-make-toc)
(remove-hook 'org-mode-hook 'org-make-toc-mode)
(remove-hook 'before-save-hook 'org-make-toc)
(add-hook 'before-save-hook 'va/org-make-toc)

(defun va/org-make-toc ()
  "Make or update table of contents in current buffer."
  (interactive)
  (when (eq major-mode 'org-mode)
    (save-excursion
      (goto-char (point-min))
      (cl-loop for pos = (org-make-toc--next-toc-position)
               while pos
               do (progn
                    (goto-char pos)
                    (org-make-toc--update-toc-at-point))))))

(defun va/org-toc-insert ()
  "Make TOC at current position. org-make-toc-insert asks too many questions"
  (interactive)
  (unless (org-find-exact-headline-in-buffer "Table of Contents")
    (org-first-headline-recenter)
    (beginning-of-line)
    (insert "* Table of Contents                                 :TOC:noexport:
:PROPERTIES:
:TOC:      :include all
:END:

:CONTENTS:

:END:

")))

VladimirAlexiev avatar Jun 11 '20 10:06 VladimirAlexiev

That's what I've done and I get the error org-make-toc: No TOC node found on every save of non-org file, or org file without TOC. I haven't run org-make-toc myself.

You get the message when saving any non-Org file? Do you mean that you added it to your global before-save-hook?

IMHO when called from before-save-hook, org-make-toc should not presume that it's in an org buffer and that the buffer has TOC.

You are not supposed to add the function to your global before-save-hook, only to that hook in Org buffers.

alphapapa avatar Jun 11 '20 21:06 alphapapa