7

I am editing a document with lots of URLs in it, and I would like to automatically fold them down to just the last part of the URL so that they take up less room on the line, similar to the way hideshow folds up blocks of code. For example:

<http://www.foo.bar/baz.html>

To:

<baz.html>

I would appreciate any suggestions on how to do this, preferably in a way that is compatible with goto-address. I'm also wondering if it's possible to include a +/- symbol next to the URL like in hideshowvis.

castle-bravo
  • 283
  • 1
  • 3

1 Answers1

6

The URL parsing could probably be included, so take this just as an example, but the general idea is like this:

(defun my/minify-urls (beg end)
  (interactive
   (if (region-active-p)
       (list (region-beginning) (region-end))
     (list (point-min) (point-max))))
  (save-excursion
    (goto-char beg)
    (while (re-search-forward "<\\w+:\\/\\/\\(:?[^>\\/]+\\/\\)*\\([^>\\/]+\\)>" end t)
      (message "matched")
      (let* ((all (match-string 0))
             (match (match-string 1))
             (ibeg (- (point) (length all) -1))
             (iend (- (point) (length match) -3)))
        (make-text-button iend (1- (point))
                          'len (- iend ibeg)
                          'state nil
                          'action (lambda (button)
                                    (let ((state (button-get button 'state))
                                          (len (button-get button 'len))
                                          (pos (button-start button)))
                                      (add-text-properties
                                       (- pos len) pos
                                       (if state '(invisible t) '(invisible nil)))
                                      (button-put button 'state (not state)))))
        (add-text-properties ibeg iend '(invisible t))))))

RET on the URLs to toggle expanded/minified state.

wvxvw
  • 11,222
  • 2
  • 30
  • 55
  • 2
    This looks to be a useful update to the built-in `goto-address-mode`. Please consider contributing. – abo-abo Feb 17 '16 at 12:57