3

I am using Emacs to write a long thesis in LaTeX. I have no regrets, so far (about the Emacs/LaTeX combination, that is). One of the last things I need to do before I can submit the darn thing is to compile a list of abbreviations. Of course, I should have kept tabs on the abbrevs I have used, but I haven't.

Now, is there some way to employ the power of Emacs to find abbreviations, ideally even to have them displayed in a separate buffer?

Obviously, this is a good use-case for regular expressions. Abbreviations could be defined as at least three consecutive upper case letters delimited by whitespace. My regex skills are very limited, though. I tried to search (isearch.el) for \b[A-Z]{2,4}\b, but all I got was Failing regexp Isearch.

What am I doing wrong? Maybe someone has already written a useful script for this purpose. (Couldn't find anything on (M)ELPA.)

  • 2
    \b[A-Z]\{2,4\}\b will do; you need to escape the brackets. – Juancho Jul 10 '15 at 16:37
  • 1
    You can use occur to get a browseable list of matches. – Juancho Jul 10 '15 at 16:37
  • 1
    For at least 3 consecutive upper case letters, you need \b[A-Z]\{3,\}\b – Kaushal Modi Jul 10 '15 at 18:07
4

Here's a couple of functions that will do what you want (make sure to (require 'cl-lib) first). They will gather all the abbreviations of 3 capital letters or more, along with the point location that you first used them. They will then create a new buffer *Abbreviations* that will list them and their locations. (Despite the uncreative function names, they have nothing to do with Emacs' abbrev facility.)

Basically, evaluate these two functions and then M-x display-abbrevs.

(defun gather-abbrevs ()
  "Gather all abbreviations (3 or more capital letters) in the
buffer.  Return an alist of all of them and the point location of
their initial use."
  (let (case-fold-search abbrevs)
    (save-restriction
      (save-excursion
        (widen)
        (goto-char (point-min))
        (while (re-search-forward "\\b[A-Z]\\{3,\\}\\b" nil t)
          (push (list (match-string 0)
                      (- (point) (length (match-string 0))))
                abbrevs))
        (nreverse (cl-delete-duplicates abbrevs
                                        :key  #'car
                                        :test #'equal))))))

(defun display-abbrevs ()
  "Gather all abbreviations (3 or more capital letters) in the
current buffer.  Display them and their initial point locations
in the *Abbreviations* buffer."
  (interactive)
  (let ((buf (buffer-name))
        (new (get-buffer-create "*Abbreviations*"))
        (abs (gather-abbrevs)))
    (with-current-buffer new
      (delete-region (point-min) (point-max))
      (insert (concat "Abbreviations in the <" buf "> buffer\n\n"))
      (insert "Abbrev.\t\tLocation\n")
      (cl-dolist (a abs)
        (insert (concat (car a) "\t\t"
                        (number-to-string (cadr a)) "\n")))
      (pop-to-buffer new))))

Note that I've only tested them very briefly, so there may be bugs.

  • It works! Thank you for this brilliant solution. You should put it on some repository, because it is a very useful tool for writers. – henning -- reinstate Monica Jul 14 '15 at 10:29
6

If you are comfortable with occur, then try this:

M-x occur RET \b[A-Z]\{2,4\}\b

You can play with 2,4 values to adjust to your needs, perhaps different values for different parts of your thesis. Results are shown in a new buffer.

Since occur is available on default installations of Emacs, you don't have to add new libraries or dependencies.

  • +1: This solution has the advantage of being quick and easy to use. The main disadvantage is that it lists ALL lines with the abbreviations. However, if you use the abbreviations repeatedly throughout the text (which you probably do, or you wouldn't have bothered with an abbreviation in the first place), you'll get many, many more lines in the occur buffer than unique abbreviations. Your task would then be to go through the latter buffer and find the unique abbreviations, which could be error prone. – Dan Jul 10 '15 at 20:32
  • Works like a charm. Thanks for explaining the useful occur and pointing out the correct syntax. – henning -- reinstate Monica Jul 14 '15 at 10:32

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.