Note that there are some explanatory texts on larger screens.

plurals
  1. POHow could I improve this solution to the "create index entries" exercise of Emacs Lisp Intro?
    primarykey
    data
    text
    <p>I've programmed this solution for the exercise from section 11.4 (Looping Exercise):</p> <pre><code>(defun texinfo-index-dfns-in-par () "Create an index entry at the beginning of the paragraph for every '@dfn'." (interactive) (save-excursion (forward-paragraph) (let ((bound (point))) (backward-paragraph) (let ((insert-here (point))) (while (search-forward "@dfn{" bound t) (let* ((start (point)) (end (1- (search-forward "}" bound))) (dfn (buffer-substring start end))) (save-excursion (goto-char insert-here) (newline) (setq insert-here (point)) (insert "@cindex " dfn) (while (&lt; insert-here (line-beginning-position)) (join-line)) (end-of-line) (setq insert-here (point)) (forward-paragraph) (setq bound (point))))))))) </code></pre> <p>Though it's working, it feels much to convoluted to me. I'd like to know how this code could be simplified. I'm also interested in other possible improvements.</p> <p><strong>Edit:</strong></p> <p>Tyler's answer was great. With narrowing I could write a much shorter and cleaner version:</p> <pre><code>(defun texinfo-index-dfns-in-par () "Create an index entry at the beginning of the paragraph for every '@dfn'." (interactive) (save-excursion (mark-paragraph) (save-restriction (narrow-to-region (point) (mark)) (while (search-forward "@dfn{" nil t) (let ((start (point)) (end (1- (search-forward "}")))) (save-excursion (goto-char (point-min)) (insert "\n@cindex " (buffer-substring start end)) (while (&gt; (line-number-at-pos) 2) (join-line)) (narrow-to-region (line-end-position) (point-max)))))))) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload