conf-hippie.el 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ;;; Code:
  2. (defvar he-search-loc-backward (make-marker))
  3. (defvar he-search-loc-forward (make-marker))
  4. ;; Functions
  5. (defun distopico:try-expand-line-closest-first (old)
  6. "Try to complete the current line to an entire line in the buffer.
  7. The argument OLD has to be nil the first call of this function, and t
  8. for subsequent calls (for further possible completions of the same
  9. string). It returns t if a new completion is found, nil otherwise.
  10. from: https://gist.github.com/magnars/4060654"
  11. (let ((expansion ())
  12. (strip-prompt (and (get-buffer-process (current-buffer))
  13. comint-use-prompt-regexp
  14. comint-prompt-regexp)))
  15. (unless old
  16. (he-init-string (he-line-beg strip-prompt) (point))
  17. (set-marker he-search-loc-backward he-string-beg)
  18. (set-marker he-search-loc-forward he-string-end))
  19. (if (not (equal he-search-string ""))
  20. (save-excursion
  21. (save-restriction
  22. (if hippie-expand-no-restriction
  23. (widen))
  24. (let (forward-point
  25. backward-point
  26. forward-distance
  27. backward-distance
  28. forward-expansion
  29. backward-expansion
  30. chosen)
  31. ;; search backward
  32. (goto-char he-search-loc-backward)
  33. (setq expansion (he-line-search he-search-string
  34. strip-prompt t))
  35. (when expansion
  36. (setq backward-expansion expansion)
  37. (setq backward-point (point))
  38. (setq backward-distance (- he-string-beg backward-point)))
  39. ;; search forward
  40. (goto-char he-search-loc-forward)
  41. (setq expansion (he-line-search he-search-string
  42. strip-prompt nil))
  43. (when expansion
  44. (setq forward-expansion expansion)
  45. (setq forward-point (point))
  46. (setq forward-distance (- forward-point he-string-beg)))
  47. ;; choose depending on distance
  48. (setq chosen (cond
  49. ((and forward-point backward-point)
  50. (if (< forward-distance backward-distance) :forward :backward))
  51. (forward-point :forward)
  52. (backward-point :backward)))
  53. (when (equal chosen :forward)
  54. (setq expansion forward-expansion)
  55. (set-marker he-search-loc-forward forward-point))
  56. (when (equal chosen :backward)
  57. (setq expansion backward-expansion)
  58. (set-marker he-search-loc-backward backward-point))
  59. ))))
  60. (if (not expansion)
  61. (progn
  62. (if old (he-reset-string))
  63. ())
  64. (progn
  65. (he-substitute-string expansion t)
  66. t))))
  67. (defun distopico:hippie-expand-lines ()
  68. "Create own function to expand lines."
  69. (interactive)
  70. (let ((hippie-expand-try-functions-list '(try-expand-line-closest-first
  71. try-expand-line-all-buffers)))
  72. (end-of-line)
  73. (hippie-expand nil)))
  74. (defun distopico:hippie-expand-no-case-fold ()
  75. "Don't case-fold when expanding with `hippe-mode'."
  76. (interactive)
  77. (let ((case-fold-search nil))
  78. (hippie-expand nil)))
  79. (provide 'conf-hippie)
  80. ;;; conf-hippie.el ends here