New thing-at-point target 'list-or-string' used in context-menu-region

* lisp/mouse.el (context-menu-region): Rearrange the order to
All>Defun>List>Line>Symbol.  Show title either "List" or "String"
depending on syntax-ppss, then use thing 'list-or-string' (bug#9054).

* lisp/thingatpt.el (thing-at-point-bounds-of-list-at-point): Add optional
args 'escape-strings' and 'no-syntax-crossing' like in 'up-list'.
(list-or-string): New target 'list-or-string'.
(thing-at-point-bounds-of-list-or-string-at-point): New function.
This commit is contained in:
Juri Linkov 2021-09-23 19:32:36 +03:00
parent da50b7ec13
commit 5d96fad278
3 changed files with 27 additions and 13 deletions

View file

@ -2586,6 +2586,9 @@ This new command (bound to 'C-c C-l') regenerates the current hunk.
This is like 'filename', but is a full path, and is nil if the file
doesn't exist.
*** New 'thing-at-point' target: 'list-or-string'.
This is like 'list', but also prefers to find of any enclosing string.
+++
*** New variable 'thing-at-point-provider-alist'.
This allows mode-specific alterations to how 'thing-at-point' works.

View file

@ -455,10 +455,11 @@ Some context functions add menu items below the separator."
`(menu-item "Paste" mouse-yank-at-click
:help "Paste (yank) text most recently cut/copied")))
(when (and (cdr yank-menu) (not buffer-read-only))
(let ((submenu (make-sparse-keymap (propertize "Paste from Kill Menu"))))
(dolist (item yank-menu)
(let ((submenu (make-sparse-keymap (propertize "Paste from Kill Menu")))
(i 0))
(dolist (item (reverse yank-menu))
(when (consp item)
(define-key-after submenu (vector (car item))
(define-key submenu (vector (setq i (1+ i)))
`(menu-item ,(cadr item)
,(lambda () (interactive)
(mouse-yank-from-menu click (car item)))))))
@ -477,18 +478,19 @@ Some context functions add menu items below the separator."
`(menu-item "All"
,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'buffer))
:help "Mark the whole buffer for a subsequent cut/copy"))
(define-key-after submenu [mark-line]
`(menu-item "Line"
,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'line))
:help "Mark the line at click for a subsequent cut/copy"))
(define-key-after submenu [mark-defun]
`(menu-item "Defun"
,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'defun))
:help "Mark the defun at click for a subsequent cut/copy"))
(define-key-after submenu [mark-list]
`(menu-item "List"
,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'list))
:help "Mark the list at click for a subsequent cut/copy"))
(define-key-after submenu [mark-list-or-string]
`(menu-item ,(if (nth 8 (syntax-ppss (posn-point (event-end click))))
"String" "List")
,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'list-or-string))
:help "Mark list or string at click for a subsequent cut/copy"))
(define-key-after submenu [mark-line]
`(menu-item "Line"
,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'line))
:help "Mark the line at click for a subsequent cut/copy"))
(define-key-after submenu [mark-symbol]
`(menu-item "Symbol"
,(lambda (e) (interactive "e") (mark-thing-at-mouse e 'symbol))

View file

@ -284,18 +284,27 @@ The bounds of THING are determined by `bounds-of-thing-at-point'."
(put 'list 'bounds-of-thing-at-point 'thing-at-point-bounds-of-list-at-point)
(defun thing-at-point-bounds-of-list-at-point ()
(defun thing-at-point-bounds-of-list-at-point (&optional escape-strings no-syntax-crossing)
"Return the bounds of the list at point.
Prefer the enclosing list with fallback on sexp at point.
\[Internal function used by `bounds-of-thing-at-point'.]"
(save-excursion
(if (ignore-errors (up-list -1))
(if (ignore-errors (up-list -1 escape-strings no-syntax-crossing))
(ignore-errors (cons (point) (progn (forward-sexp) (point))))
(let ((bound (bounds-of-thing-at-point 'sexp)))
(and bound
(<= (car bound) (point)) (< (point) (cdr bound))
bound)))))
(put 'list-or-string 'bounds-of-thing-at-point
'thing-at-point-bounds-of-list-or-string-at-point)
(defun thing-at-point-bounds-of-list-or-string-at-point ()
"Return the bounds of the list or string at point.
Like `thing-at-point-bounds-of-list-at-point', but also
prefer to find of any enclosing string."
(thing-at-point-bounds-of-list-at-point t t))
;; Defuns
(put 'defun 'beginning-op 'beginning-of-defun)