Skip to content

Instantly share code, notes, and snippets.

@shanecelis
Last active October 25, 2016 00:34
Show Gist options
  • Save shanecelis/5dfd74216bdc268fb19b421ae4152ce8 to your computer and use it in GitHub Desktop.
Save shanecelis/5dfd74216bdc268fb19b421ae4152ce8 to your computer and use it in GitHub Desktop.
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*."
(interactive)
;; 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
(erase-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
otherwise."
(let ((expansion ())
(buf (current-buffer))
(orig-case-fold-search case-fold-search))
(if (not old)
(progn
(he-init-string (he-dabbrev-beg) (point))
(load-web-frontpage)
(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))))
(save-excursion
(save-restriction
(if hippie-expand-no-restriction
(widen))
(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)
(progn
(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)
(progn
(if old (he-reset-string))
())
(progn
(he-substitute-string expansion t)
t))))
@jasonm23
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