Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Quick hack to make Emacs' hippie-expand the web page I'm looking at.
;; hippie-expand-the-web.el
;; Quick hack to make Emacs' hippie-expand look at the web page on the front page of my browser.
;; —Shane Celis @shanecelis
(defun load-web-frontpage ()
"Load the frontpage on your browser (currently assuming that's
Safari) into the buffer *web-frontpage*."
;; XXX Might want to keep appending pages to this buffer, like a web
;; short-term memory.
(with-current-buffer (get-buffer-create "*web-frontpage*")
;; erase the buffer
(start-process "web-frontpage" "*web-frontpage*"
"bash" "-c"
"links -dump $(osascript -e 'tell application \"Safari\" to URL of front document')"))
;; This is try-expand-dabbrev-all-buffers with a modification to have
;; it only search thru the new buffer we load from the web.
;; You must add this to your hippie-expand-try-functions-list. I
;; suggest adding it to the end.
(defun try-expand-dabbrev-web-frontpage (old)
"Try to expand word \"dynamically\", searching the
*web-frontpage* buffer after it has been refreshed. The argument
OLD has to be nil the first call of this function, and t for
subsequent calls (for further possible expansions of the same
string). It returns t if a new expansion is found, nil
(let ((expansion ())
(buf (current-buffer))
(orig-case-fold-search case-fold-search))
(if (not old)
(he-init-string (he-dabbrev-beg) (point))
(setq he-search-bufs (list (get-buffer "*web-frontpage*")))
(setq he-searched-n-bufs 0)
(set-marker he-search-loc 1 (car he-search-bufs))))
(if (not (equal he-search-string ""))
(while (and he-search-bufs
(not expansion)
(or (not hippie-expand-max-buffers)
(< he-searched-n-bufs hippie-expand-max-buffers)))
(set-buffer (car he-search-bufs))
(if (and (not (eq (current-buffer) buf))
(if hippie-expand-only-buffers
(he-buffer-member hippie-expand-only-buffers)
(not (he-buffer-member hippie-expand-ignore-buffers))))
(if hippie-expand-no-restriction
(goto-char he-search-loc)
(setq expansion
(let ((case-fold-search orig-case-fold-search))
(he-dabbrev-search he-search-string nil)))
(set-marker he-search-loc (point))
(if (not expansion)
(setq he-search-bufs (cdr he-search-bufs))
(setq he-searched-n-bufs (1+ he-searched-n-bufs))
(set-marker he-search-loc 1 (car he-search-bufs))))))
(setq he-search-bufs (cdr he-search-bufs))
(set-marker he-search-loc 1 (car he-search-bufs)))))
(set-buffer buf)
(if (not expansion)
(if old (he-reset-string))
(he-substitute-string expansion t)
Copy link

jasonm23 commented Oct 25, 2016

(title append: On OSX when using Safari.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment