0

I know this question is similar to completion of a sentence rather than word but I ask anyway since I suggest another way of storing what is supposed to be auto-completed.

Suppose I stored some sentences in a file. How could I, in the current buffer, complete the sentences from this file just by typing the beginning (or, better, any part) of them?

Edit: To be more explicit, here is the context: I'm in the process of grading (maths) exams and most of the positive and negative remarks I have to express (and to write down) about my students works are the sames.

Hence, suppose I have the following sentences in a sentences.txt file:

Please, don't take your dreams for reality: (a + b)² ≠ a² + b².
Please, don't take your dreams for reality: cos(a + b) ≠ cos a + cos b.
You should revise the derivatives.
No, √(a²) ≠ a.
This only works for a positive x.

I'd like to be able in my current buffer (of a file stored, say, in the same directory as sentences.txt) to type e.g.

Please

and to be prompted with the first two sentences in a drop down list, just as does autocomplete for the (single) words of the current buffer.

4
  • 1
    Does this answer your question? completion of a sentence rather than word
    – Drew
    Commented Jun 2, 2020 at 17:16
  • This is a duplicate of the question you cite. If you read the answer, which says to consult (emacs) Abbrevs, and if you do that, you'll see how to create an abbreviations file that does what you want.
    – Drew
    Commented Jun 2, 2020 at 17:17
  • @Drew Almost a duplicate, I agree but, as I said, I ask how to get completions from an external file (e.g. with one sentence per line) which is maybe easier than getting completions from all the sentences of the current buffer. And the answers of the linked question doesn't help me: as the OP, I don't want abbreviation expansion but completion (hence 1st answer not answering) and expanding to the closest matching line (2nd answer) doesn't answer either. Commented Jun 2, 2020 at 19:14
  • Sorry, I misunderstood. I thought the file was for your abbreviation definitions. I've retracted my close vote. Thanks for the comment.
    – Drew
    Commented Jun 2, 2020 at 20:58

1 Answer 1

1

Since you don't need actual abbreviations, just call completing-read with your list of sentences. This will let you define the list of sentences however you want. Here's the simplest possible implementation:

(defun db48x/insert-canned-response ()
  (interactive)
  (let ((lines (with-temp-buffer
                 (insert-file-contents "~/temp/sentences.txt")
                 (split-string (buffer-string) "\n" t))))
    (insert (ido-completing-read "Response: " lines))))
(global-set-key (kbd "C-c k") 'db48x/insert-canned-response)

I actually used ido-completing-read because it's fancier than the default completing-read; it shows you a preview of what you're about to select, and it lets you narrow the choices by substring match rather than just by prefix match. For example, this will let you type 'cos' to select the second response from your examples, which has the same prefix as the first response.

This implementation has obvious deficiencies, but they probably don't matter in practice. If your list of responses gets large enough or your hard drive is slow enough that it becomes a problem, you can ask a new question about it.

2
  • OK, works nicely, though not as real autocompletion: whatever is already typed, invoking this function displays all the sentences in the same order. But, once they are displayed, typing part of them selects the relevant candidates. Commented Jun 4, 2020 at 12:59
  • 1
    Correct, I made no attempt to use what is already typed in the buffer to limit the choices. However, this is not hard. completing-read and ido-completing-read take an argument called initial-input which is designed specifically for this purpose. You can just pass in the final word or the final sentence in the buffer.
    – db48x
    Commented Jun 4, 2020 at 17:25

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.