1

这是我对 dabbrev-expand 的扩展,以支持子字符串扩展。据我所知,它按预期工作。但是,如果它支持类似于 的行为的符号内扩展,我会发现它更加有用mdabbrev,顺便说一下,在符号字符和大小写调整支持方面是不完整的。然而,的pattern参数dabbrev-substring-search只是点之前的模式,但对于就地扩展,我们也需要点之后的模式。为什么这种模式在 hippie/dabbrev-expand 中不可用,是否有首选方法来查询它?

(defun dabbrev-substring-search (pattern &optional reverse limit syntax-context)
  "Expand dabbrev substring. See:
http://www.emacswiki.org/cgi-bin/wiki/HippieExpand#toc5"
  (let ((result ())
        (regpat (cond ((not hippie-epxand-dabbrev-as-symbol)
                       (concat (regexp-quote pattern) W*))
                      ;; ((eq (char-syntax (aref pattern 0)) ?_)
                      ;;  (concat (regexp-quote pattern)
                      ;;          "\\(\\sw\\|\\s_\\)*"))
                      (t
                       (concat "\\(?:"
                               Y<
                               "\\(" "\\(?:\\sw\\|\\s_\\)+" "\\)"
                               "\\(" (regexp-quote pattern) "\\)"
                               "\\(" "\\(?:\\sw\\|\\s_\\)*" "\\)"
                               Y>
                               "\\)"

                               "\\|"

                               "\\(?:"
                               Y<
                               "\\(" "\\(?:\\sw\\|\\s_\\)*" "\\)"
                               "\\(" (regexp-quote pattern) "\\)"
                               "\\(" "\\(?:\\sw\\|\\s_\\)+" "\\)"
                               Y>
                               "\\)"

                               )))))
    (while (and (not result)
                (if reverse
                    (re-search-backward regpat limit t)
                  (re-search-forward regpat limit t)))
      (setq result (buffer-substring-no-properties (save-excursion
                                                     (goto-char (match-beginning 0))
                                                     ;;(skip-syntax-backward "w_")
                                                     (point))
                                                   (match-end 0)))

      (if (he-string-member result he-tried-table t)
          (setq result nil)))     ; ignore if bad prefix or already in table

    (when nil
      (when result
        (let* ((p (point))
               (end3 (match-end 3))
               (beg2 (match-end 2))
               (end2 (match-end 2))
               (dummy (message "%s %s %s" end3 beg2 end2))
               (beg (- end3 beg2))       ;begin offset from point
               (end (- end3 end2)))      ;end offset from point
          (setq dabbrev-substring-match-region (cons beg end))
          (hictx-generic (- p beg) (- p end) nil 'match 3))))

    result))
;; Use: (dabbrev-substring-search "he")

(defun try-expand-dabbrev-substring-visible (old)
  "Like `try-expand-dabbrev' but for visible part of buffer."
  (interactive "P")
  (let ((old-fun (symbol-function 'he-dabbrev-search)))
    (fset 'he-dabbrev-search (symbol-function 'dabbrev-substring-search))
    (unwind-protect (try-expand-dabbrev-visible old)
      (fset 'he-dabbrev-search old-fun))))

(defun try-expand-dabbrev-substring (old)
  "Like `try-expand-dabbrev' but for substring match."
  (interactive "P")
  (let ((old-fun (symbol-function 'he-dabbrev-search)))
    (fset 'he-dabbrev-search (symbol-function 'dabbrev-substring-search))
    (unwind-protect (try-expand-dabbrev old)
      (fset 'he-dabbrev-search old-fun))))

(defun try-expand-dabbrev-substring-all-buffers (old)
  "Like `try-expand-dabbrev-all-buffers' but for substring match."
  (interactive "P")
  (let ((old-fun (symbol-function 'he-dabbrev-search)))
    (fset 'he-dabbrev-search (symbol-function 'dabbrev-substring-search))
    (unwind-protect (try-expand-dabbrev-all-buffers old)
      (fset 'he-dabbrev-search old-fun))))

例如使用

(setq hippie-expand-try-functions-list
        (append
         '(
           try-expand-dabbrev-substring-visible
           try-expand-dabbrev-substring
           try-expand-dabbrev-substring-all-buffers)))
4

1 回答 1

0

这可能有帮助,也可能没有帮助。与普通的 dabbrev 一样,它适用于点之前的文本,但候选匹配可以是子字符串、正则表达式或模糊(各种),除了前缀。 冰柱 - 动态缩写

于 2012-01-03T18:50:58.157 回答