Created
November 12, 2012 17:23
-
-
Save magnars/4060654 to your computer and use it in GitHub Desktop.
Hippie Expand: use closest matches first
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; To use, replace these values in your hippie-expand-try-functions-list: | |
;; | |
;; try-expand-dabbrev => try-expand-dabbrev-closest-first | |
;; try-expand-line => try-expand-line-closest-first | |
;; | |
(defvar he-search-loc-backward (make-marker)) | |
(defvar he-search-loc-forward (make-marker)) | |
(defun try-expand-dabbrev-closest-first (old) | |
"Try to expand word \"dynamically\", searching the current buffer. | |
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) | |
(unless old | |
(he-init-string (he-dabbrev-beg) (point)) | |
(set-marker he-search-loc-backward he-string-beg) | |
(set-marker he-search-loc-forward he-string-end)) | |
(if (not (equal he-search-string "")) | |
(save-excursion | |
(save-restriction | |
(if hippie-expand-no-restriction | |
(widen)) | |
(let (forward-point | |
backward-point | |
forward-distance | |
backward-distance | |
forward-expansion | |
backward-expansion | |
chosen) | |
;; search backward | |
(goto-char he-search-loc-backward) | |
(setq expansion (he-dabbrev-search he-search-string t)) | |
(when expansion | |
(setq backward-expansion expansion) | |
(setq backward-point (point)) | |
(setq backward-distance (- he-string-beg backward-point))) | |
;; search forward | |
(goto-char he-search-loc-forward) | |
(setq expansion (he-dabbrev-search he-search-string nil)) | |
(when expansion | |
(setq forward-expansion expansion) | |
(setq forward-point (point)) | |
(setq forward-distance (- forward-point he-string-beg))) | |
;; choose depending on distance | |
(setq chosen (cond | |
((and forward-point backward-point) | |
(if (< forward-distance backward-distance) :forward :backward)) | |
(forward-point :forward) | |
(backward-point :backward))) | |
(when (equal chosen :forward) | |
(setq expansion forward-expansion) | |
(set-marker he-search-loc-forward forward-point)) | |
(when (equal chosen :backward) | |
(setq expansion backward-expansion) | |
(set-marker he-search-loc-backward backward-point)) | |
)))) | |
(if (not expansion) | |
(progn | |
(if old (he-reset-string)) | |
nil) | |
(progn | |
(he-substitute-string expansion t) | |
t)))) | |
(defun try-expand-line-closest-first (old) | |
"Try to complete the current line to an entire line in the buffer. | |
The argument OLD has to be nil the first call of this function, and t | |
for subsequent calls (for further possible completions of the same | |
string). It returns t if a new completion is found, nil otherwise." | |
(let ((expansion ()) | |
(strip-prompt (and (get-buffer-process (current-buffer)) | |
comint-use-prompt-regexp | |
comint-prompt-regexp))) | |
(unless old | |
(he-init-string (he-line-beg strip-prompt) (point)) | |
(set-marker he-search-loc-backward he-string-beg) | |
(set-marker he-search-loc-forward he-string-end)) | |
(if (not (equal he-search-string "")) | |
(save-excursion | |
(save-restriction | |
(if hippie-expand-no-restriction | |
(widen)) | |
(let (forward-point | |
backward-point | |
forward-distance | |
backward-distance | |
forward-expansion | |
backward-expansion | |
chosen) | |
;; search backward | |
(goto-char he-search-loc-backward) | |
(setq expansion (he-line-search he-search-string | |
strip-prompt t)) | |
(when expansion | |
(setq backward-expansion expansion) | |
(setq backward-point (point)) | |
(setq backward-distance (- he-string-beg backward-point))) | |
;; search forward | |
(goto-char he-search-loc-forward) | |
(setq expansion (he-line-search he-search-string | |
strip-prompt nil)) | |
(when expansion | |
(setq forward-expansion expansion) | |
(setq forward-point (point)) | |
(setq forward-distance (- forward-point he-string-beg))) | |
;; choose depending on distance | |
(setq chosen (cond | |
((and forward-point backward-point) | |
(if (< forward-distance backward-distance) :forward :backward)) | |
(forward-point :forward) | |
(backward-point :backward))) | |
(when (equal chosen :forward) | |
(setq expansion forward-expansion) | |
(set-marker he-search-loc-forward forward-point)) | |
(when (equal chosen :backward) | |
(setq expansion backward-expansion) | |
(set-marker he-search-loc-backward backward-point)) | |
)))) | |
(if (not expansion) | |
(progn | |
(if old (he-reset-string)) | |
()) | |
(progn | |
(he-substitute-string expansion t) | |
t)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment