Elisp一天一函数(9)—— 自定义函数:select-text-between-quotations

今天来实现一个简单的函数,用于选中当前光标所在的一对双引号之间的字符串内容,如果当前光标不在一对双引号之间,则不选中任何内容。

函数代码如下:

;; 首先定义一个函数,用于判断指定的位置是否在双引号内部,使用了`syntax-ppss'函数提供的能力
(defun check-inside-quotations (position)
  "Check whether the indicated position inside a pair of quotations."
  (nth 3 (syntax-ppss position)))

;; 选中当前光标所在的两个双引号之间的字符串内容,如果当前光标不在一对双引号之间,则什么也不做
(defun select-text-between-quotations ()
  "Select text between two nearest quotation marks."
  (interactive)
  (if (check-inside-quotations (point))
      (progn
    ;; here should not use square brackets in regexp
    (let ((start) (skip-char "^\""))
      (skip-chars-backward skip-char)
      (setq start (point))
      (skip-chars-forward skip-char)
      (push-mark start)
      (setq mark-active t)))
    (message "not inside a pair of quotation marks!")))
;; 绑定快捷键到C-"
(global-set-key (kbd "C-\"") 'select-text-between-quotations)

执行上述代码,然后把光标放在一个字符串之间,按快捷键C-",就可以快速选中整个字符串。

猜你喜欢

转载自blog.csdn.net/weixin_34004576/article/details/87162839