;;;; Tersify and untersify text. ;;;; (defun tersify-region (beg end) "Don't say anything twice when you could [6 words back] it once." (interactive "r") (save-excursion ;; First make sure end floats, since we'll be changing the length ;; of the text between BEG and END. (if (not (markerp end)) (let ((marky-mark (make-marker))) (set-marker marky-mark end) (setq end marky-mark))) ;; Go to the right place, with appropriate fuzz factor: (goto-char beg) (forward-word 1) (forward-word -1) (if (< (point) beg) (forward-word 1)) ;; Tersify: (let ((seen (make-vector 1721 nil)) (count 0)) (while (< (point) end) (let* ((word (current-word)) (sym (intern-soft word seen)) ;; ... heh heh (last-pos (and sym (symbol-value sym))) (sym (intern word seen))) ;; ... how d'ya like _that_? (if last-pos (progn (kill-word 1) (insert "[" (number-to-string (- count last-pos)) " words back]") (forward-word -1))) (set sym count) (setq count (1+ count)) (forward-word 1) (forward-word 1) (if (< (point) end) (forward-word -1)) ))))) ;;; The following courtesy of Jim Blandy : (defun verbosify-region (beg end) "An uncompression function that usually makes things smaller. NOTA BENE: you probably want to put the buffer in text-mode before you run this. The current buffer must have the same concept of `word' as the one you ran tersify-region in." (interactive "r") (save-excursion ;; First make sure end floats, since we'll be changing the length ;; of the text between BEG and END. (if (not (markerp end)) (let ((marky-mark (make-marker))) (set-marker marky-mark end) (setq end marky-mark))) ;; Verbosify: (while (re-search-forward "\\[\\([0-9]+\\)[ \t\n]words[ \t\n]back\\]" end t) (let ((index (string-to-number (buffer-substring (match-beginning 1) (match-end 1))))) (delete-region (match-beginning 0) (match-end 0)) (insert (save-excursion (forward-word (- index)) (current-word))))) ;; Fill the region again afterwards. (goto-char beg) (beginning-of-line) (setq beg (point)) (goto-char end) (or (bolp) (forward-line 1)) (fill-region beg (point)))) ;; Test text: ;; ;; this is a test of something a test of something yes indeed we are ;; looking at a test of something of what? Of something ;; ;; this is a test of something [4 words back] [4 words back] [4 words ;; back] [4 words back] yes indeed we are looking at [10 words back] [10 ;; words back] [10 words back] [10 words back] [2 words back] what? Of ;; [4 words back]