Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Emacs how do I mark a string when inside the same string?

Tags:

emacs

Let's say I have an output which contains:

{"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.", "A happy family is but an earlier heaven.", "Heaven has no rage like love to hatred turned, Nor hell a fury like a woman scorned."}

And my cursor is inside one of the string (after the word 'fought'):

"I know not with what weapons World War III will be fought|, but World War IV will be fought with sticks and stones."

And I would like to copy that whole string. Generally what I do is, I go to the start of the of the string, move one character back to the " and press "C-M-SPC" and select the string.

But I find that cumbersome. Is there a way to select the string within the string directly?

Also is it possible to select a string if it has escaped double quotes like:

"And she said \"Learning is what most adults| will do for a living in the 21st century.\" that yesterday"

In above if my cursor is after 'adults' it should be able to select the outer string properly.

Thanks.

like image 658
firesofmay Avatar asked Jun 07 '13 10:06

firesofmay


3 Answers

expand-region is what you're after. Screencast and project

like image 134
event_jr Avatar answered Nov 05 '22 13:11

event_jr


Here is the function:

(defun copy-quoted-string ()
  (interactive)
  "Copies the quoted text, ignoring the escaped quotes"
  (save-excursion
     (search-backward-regexp "[^\\]\"")
     (forward-char)
     (mark-sexp)
     (kill-ring-save (point) (mark))))

;this is for testing
(global-set-key [f2] 'copy-quoted-string)

For testing I used the following string:

"text text", "text \"quoted text\" text"

When I press F2, when the cursor is located inside "text text", this string is copied into clipboard. When I am in "text \"quoted text\" text" - this string is copied.

like image 1
user4035 Avatar answered Nov 05 '22 14:11

user4035


I found this another alternative: Thanks to Vedang.

Reference:

;;; Function to mark complete word, and expand to sentence etc.
;;; by Nikolaj Schumacher, 2008-10-20. Released under GPL.
(defun semnav-up (arg)
  (interactive "p")
  (when (nth 3 (syntax-ppss))
    (if (> arg 0)
        (progn
          (skip-syntax-forward "^\"")
          (goto-char (1+ (point)))
          (decf arg))
      (skip-syntax-backward "^\"")
      (goto-char (1- (point)))
      (incf arg)))
  (up-list arg))


;;; by Nikolaj Schumacher, 2008-10-20. Released under GPL.
(defun extend-selection (arg &optional incremental)
  "Select the current word.
Subsequent calls expands the selection to larger semantic unit."
  (interactive (list (prefix-numeric-value current-prefix-arg)
                     (or (and transient-mark-mode mark-active)
                         (eq last-command this-command))))
  (if incremental
      (progn
        (semnav-up (- arg))
        (forward-sexp)
        (mark-sexp -1))
    (if (> arg 1)
        (extend-selection (1- arg) t)
      (if (looking-at "\\=\\(\\s_\\|\\sw\\)*\\_>")
          (goto-char (match-end 0))
        (unless (memq (char-before) '(?\) ?\"))
          (forward-sexp)))
      (mark-sexp -1))))

(global-set-key (kbd "C-=") 'extend-selection)
like image 1
firesofmay Avatar answered Nov 05 '22 12:11

firesofmay