7

I have an org-mode file with more than 12000 footnotes (maybe less), I was cleaning it and found that after the conversion some footnotes weren't converted to org-mode footnotes, so I had to manually make them footnotes.

The thing is, since that addition the reference number inside the square brackets must be re-done otherwise I will end with repeated numbers, like: [12] ... [12] or [10678] ... [10678]. Is there a way to build a regexp that look for all numbers inside square brackets and replace the text inside them with their respective coincidence number (?) for instance: [12] is the coincidence number 10 and this other [12] is coincidence number 11 so [12] ... [12] becomes [10] ... [11]?

especial petition: if you are going to post an elisp snippet that solves this problem, please, please, please, comment it on detail because I want to learn elisp too!

shackra
  • 2,782
  • 19
  • 49
  • They are references, so must we expect that the number references to somewhere? I.e. in your situation, there are 4 occurrences of [12], two of them (reference + footnote) should be renamed [10] and the other two should be renamed [11]? That makes the question much more difficult than the way it is currently phrased! – T. Verron Dec 04 '14 at 07:45
  • 1
    How many of your references are duplicates? If not too many, I'd suggest de-duplicating them by hand, using a different namespace (e.g. [12-2]) to avoid the numbering issue. – T. Verron Dec 04 '14 at 07:46
  • 3
    Using the [fn:name] syntax rather than hand numbering is a better long term solution. – Andrew Swann Dec 04 '14 at 07:47
  • The references will be worked on separate files to make things easy. They aren't many, though I'm worried about how will [12-2] looks after exporting the document ... – shackra Dec 04 '14 at 07:56
  • You're right, [12-2] is not recognised as a footnote, my bad. Andrew Swann's solution is the best, and the notes should be auto-numbered by the org exporter in this case. – T. Verron Dec 04 '14 at 08:06
  • 1
    Also, if you drop your requirement about learning Elisp, you can probably employ keyboard macros together with the keyboard macro counter (if that is what it's called, see the manual) and `C-u C-s' within a macro. – mbork Dec 04 '14 at 18:13

1 Answers1

9

To re-number all footnotes starting from 1 you can use M-x query-replace-regexp (or, if you're feeling confident, just M-x replace-regexp) and type in

\[[0-9]+\] as the regexp to replace and

[\,(1+ \#)] as the replacement.

Here \# is the match count starting from zero, so I used \, and a lisp form which adds one to it.

To get (most of) this information, press C-h f replace-regexp and hit RETURN.

Constantine
  • 9,122
  • 1
  • 35
  • 50
  • Wow! This works perfectly! Is just a matter of separating the footnotes and putting it on a different file before using replace-regexp! – shackra Dec 04 '14 at 18:31