获取光标所在字符串的方法?

比如:

(defvar string "This is a (光标位置) string")

如果光标在“光标位置”,怎样获取到"This is a string"?最好能不依赖于第三方如smartparens之类。

参考 thing-at-point

thing-at-point还能取string?

String也是sexp,用(thing-at-point 'sexp)就可以获取

string里面可以有 (xxx) 这样的内容

有空格不行了

一直苦于这个问题。希望看到比较好的办法。

(nth 3 (syntax-ppss)) 在string里们的时候不为nil。 移动光标然后每次判断,但是比较慢。 如果字符串非常大就不行了。

性能好的方法是从buffer开头自己parse的样子。但是要处理\

(defun my/string-atpt ()
  (save-excursion
    ;; Already in string
    (when (nth 3 (syntax-ppss))
      ;; In Elisp, string are always surrounded by quote.
      ;; But there may be escaped quote in string, so a (nth 3 (syntax-ppss))
      ;; is necessary to ensure our search has arrived at the bos.

      ;; And we can not use `re-search-forward' to jump to the EOS.
      ;; Consider following situation: "String"symbol
      ;; (thing-at-point 'sexp) will return "symbol" instead of "\"String\""
      (while (and (re-search-backward "\"" nil t)
                  (nth 3 (syntax-ppss))))
      (thing-at-point 'sexp))))
;;; Example: "This is a (|) string"
;;; char | indicates cursor pos

(my/string-atpt)

;; => "This is a () string"

(thing-at-point ‘string)

在我写的 thing-edit.el 里面的 thing-copy-parentheses 函数是你要找的东西。

(defun thing-copy-parentheses (kill-conditional)
  "Copy content in match parentheses.
If `KILL-CONDITIONAL' is non-nil, kill object,
otherwise copy object."
  (interactive "P")
  (save-excursion
    (if (thing-edit-in-string-p)
        (thing-edit-internal
         (1+ (car (thing-edit-string-start+end-points)))
         (cdr (thing-edit-string-start+end-points))
         kill-conditional)
      (thing-edit-internal
       (progn
         (backward-up-list)
         (forward-char +1)
         (point))
       (progn
         (up-list)
         (forward-char -1)
         (point))
       kill-conditional))))
2 个赞

本人耗尽毕生所学EmacsLisp,还有TG群的大佬指导写出来的函数,希望对你有帮助吧!可以获取当前光标下的字符串并且将其添加到kill环中。

(defun get-string (current-point)
  "获取当前光标下的字符串,并
CURRENT-POINT 是当前光标位点
BEFORE-QUOTATION 是前一个双引号的位置 LATER-QUOTAION则是后一个的位置"
  (interactive "d")
  (save-excursion
  (let ((before-quotation current-point)
        (later-quotation current-point))
    (if (search-forward "\"" nil t -1)
        (progn
          (setq before-quotation (point))
          (search-forward "\"" nil t 2)
          (setq later-quotation (point))
          (copy-region-as-kill before-quotation later-quotation))))))

get-string

emacs 28.0.5 26.2在emacs -Q的情况下,用你的代码不能获取光标下的字符串(总是返回nil)。

如果需要前置的操作条件,还请仔细说明一下较好。

因为根本就没有 string 类型。

这样没什么问题:

(defun bounds-of-string-at-point ()
  (save-excursion
    (let ((p (point))
          end)
      (while (nth 3 (syntax-ppss))
        (skip-syntax-forward "^\"|")
        (forward-char))
      (setq end (point))
      (goto-char p)
      (while (nth 3 (syntax-ppss))
        (skip-syntax-backward "^\"|")
        (backward-char))
      (cons (point) end))))

UI挺好看,顶

我 错 了 。

可以设置forward-string函数 等别人来吧

这个是doom-peacock主题。

一个不靠谱的思路:用 font-lock-mode 已经算好的信息

(不过在注释里面的 “string” 就读不出来了 233)

这个不行,emacs会随着语法变化改变颜色。

其实 thing-edit.el 完全就是楼主要的东西,我都稳定用了十几年了。

1 个赞

evil的话yi"