forked from magnars/.emacs.d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-hippie.el
170 lines (140 loc) · 6.2 KB
/
setup-hippie.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
(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))))
;; Hippie expand: sometimes too hip
(setq hippie-expand-try-functions-list '(try-expand-dabbrev-closest-first
try-complete-file-name
try-expand-dabbrev-all-buffers
try-expand-dabbrev-from-kill
try-expand-all-abbrevs
try-complete-lisp-symbol-partially
try-complete-lisp-symbol))
;; Create own function to expand lines (C-S-.)
(defun hippie-expand-lines ()
(interactive)
(let ((hippie-expand-try-functions-list '(try-expand-line-closest-first
try-expand-line-all-buffers)))
(end-of-line)
(hippie-expand nil)))
;; Don't case-fold when expanding with hippe
(defun hippie-expand-no-case-fold ()
(interactive)
(let ((case-fold-search nil))
(hippie-expand nil)))
(provide 'setup-hippie)