How to kill quoted string at emacs point?

I would like to kill the quoted line in the source file without having to mark the beginning of the line and the kill area, but just placing a point somewhere inside the specified line and clicking on the shortcut.

I tried to write a function in elisp for this, but I realized that the file would need to be analyzed from the beginning to the point to determine if this point is inside the specified line and find the bounds of the quoted line (also process \ ") ...

But the file is already parsed using lock-lock. So now I can find out if I am inside the quoted string:

(defun inside-quoted-string? () (interactive) (print (find 'font-lock-doc-face (text-properties-at (point))))) 

But how do I get the line bounds? font-lock knows about this since it nicely highlights it in blue, but how do I get it?

Edit: Thanks for the answers. I came up with this code that does exactly what I wanted - to move the code without selecting a region or even go to the beginning of the code.

 (defun kill-at-point () "Kill the quoted string or the list that includes the point" (interactive) (let ((p (nth 8 (syntax-ppss)))) (if (eq (char-after p) ?\") (progn (goto-char p) (kill-sexp)) (progn (up-list) (let ((beg (point))) (backward-list) (kill-region beg (point))))))) (global-set-key (kbd "C-,") 'kill-at-point) 

Any suggestions for improving it are welcome.

+7
source share
3 answers

Instead of relying on blocking fonts, you can use the data from a basic analyzer. The beginning of the line around the point (if any) is available as (nth 8 (syntax-ppss)) . Then you can use (forward-sexp 1) to jump over the line to find its end.

+5
source

You can find property borders with previous-property-change and next-property-change . For example:

 (defun kill-by-property (arg) (interactive "d") (kill-region (previous-property-change arg nil (point-min)) (next-property-change arg nil (point-max)))) 
+3
source

Having compiled Stefan’s suggestion on using syntax-ppss , the following should do the trick

 (defun kill-string () (interactive) (let ((string-start (nth 8 (syntax-ppss)))) (goto-char string-start) (kill-sexp))) 

It uses (nth 8 (syntax-ppss)) to find the beginning of the line, jumps there, and then uses the built-in kill-sexp to kill the s-expression at the point (in this case, the line we want to delete). There is no need at all for any calculations of the region on your part.

+1
source

All Articles