From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Simen =?iso-8859-1?q?Heggest=F8yl?= Newsgroups: gmane.emacs.devel Subject: [ELPA] New package: cycle-quotes.el Date: Sat, 04 Jun 2016 13:00:23 +0200 Message-ID: <1465038023.18228.0@smtp.gmail.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-Tb1NYsqD/e+eus91n0OQ" X-Trace: ger.gmane.org 1465038071 5613 80.91.229.3 (4 Jun 2016 11:01:11 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 4 Jun 2016 11:01:11 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Jun 04 13:01:11 2016 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1b99KI-0006ES-AV for ged-emacs-devel@m.gmane.org; Sat, 04 Jun 2016 13:01:10 +0200 Original-Received: from localhost ([::1]:60302 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b99KH-0004ra-Dz for ged-emacs-devel@m.gmane.org; Sat, 04 Jun 2016 07:01:09 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:39968) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b99Jg-0004qZ-TJ for emacs-devel@gnu.org; Sat, 04 Jun 2016 07:00:34 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b99Jb-0000qD-T2 for emacs-devel@gnu.org; Sat, 04 Jun 2016 07:00:32 -0400 Original-Received: from mail-lf0-x244.google.com ([2a00:1450:4010:c07::244]:35544) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b99Ja-0000ph-Nx for emacs-devel@gnu.org; Sat, 04 Jun 2016 07:00:27 -0400 Original-Received: by mail-lf0-x244.google.com with SMTP id s186so3183329lfs.2 for ; Sat, 04 Jun 2016 04:00:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:subject:to:message-id:mime-version; bh=b9kR6DI7ZWx7ZCXa62jjzaDymioLwI4SQJh2TpUZ4jU=; b=HP1DvSePbyRBHGyNfaO9NwsDyHsslU1uab3i7+CI2Y+I0IB231BwNqVNJ9xP214mk0 6xFBRBXIa3ozTCNfmAXzZ4l5YBZKxMdP9n4ZMGEG1NnkDwydyyWUUWsNAAsETbbUteDQ CcG5wMISpLx3WOqv16orE3V1ov4GBFzMXB2HDKRcIOwPy78wZ5efjF5LckQRax/jYcOE 7DR0uGSLtH+wDRMefsrNNpj+3aivtANPDngje7FBlQUAjWBWaUqcCkhTDeAcuTRBN+rE KTCHd5jPu4UqRLoBF6Rl7bsE9JDlOGWxQ4iysBi/vmOzhnw8p8BA+ZdYINEghMasyNgB +MyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:subject:to:message-id:mime-version; bh=b9kR6DI7ZWx7ZCXa62jjzaDymioLwI4SQJh2TpUZ4jU=; b=OZHkHrJjHN5t8VW0jPj2WWXgOBce088NHPo91b3yXCRhRaIwlkjKZYxFWZskKOKgEr S8KMAF4LunoqvwpMue83vr0bbxTsg4zd1ZGT0sA/kEft8EMpkrJSYbnSmuXJxpKC/bOK iQxNtNDyfIgbSB+Ys98nRnP2bxpDUTAenjtPmNhkoBH+PfuAMfHe7Zk0tT53VPYCuKNb 08nzQ3zc7ghcb6/lPp59Itng5lQqmvjjMHFUei0q/4yHU5W6MMAbgJC6F0ZE4pnKvM9q 1LCQzkafXEk+5X1uODaNpFrySOrm6x8a3t4EVvVzzBwQziXiDFmPtneYCckSkhqMlziJ bUaA== X-Gm-Message-State: ALyK8tL36HLaUY3mOnFEj4kVb/Ktva+lz9VhvdTjESjZrvrq6pxPkxSREUC6S1hqgs2qkg== X-Received: by 10.25.24.65 with SMTP id o62mr1842859lfi.23.1465038025427; Sat, 04 Jun 2016 04:00:25 -0700 (PDT) Original-Received: from [192.168.100.5] (cm-84.210.143.4.getinternet.no. [84.210.143.4]) by smtp.gmail.com with ESMTPSA id f6sm939963lbt.31.2016.06.04.04.00.24 for (version=TLSv1/SSLv3 cipher=OTHER); Sat, 04 Jun 2016 04:00:24 -0700 (PDT) X-Mailer: geary/0.11.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:4010:c07::244 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:204190 Archived-At: --=-Tb1NYsqD/e+eus91n0OQ Content-Type: text/plain; charset=utf-8; format=flowed Hello. I'd like to offer a package I've written, cycle-quotes.el, for GNU ELPA. It's a convenience package providing a command to cycle between different string quoting styles. For instance, in JavaScript, there's three string quote characters: ", ` and '. In a JavaScript buffer, with point located someplace within the string, the `cycle-quotes' command will cycle between these quote styles each time the command is called: --> "Hi, it's me!" --> `Hi, it's me!` --> 'Hi, it\'s me!' -- | | ------------------------------------------------------------ As seen in the above example, it handles escaping and unescaping of quote characters inside the string. I've been using the package privately for some time now and found it useful, so maybe others will find it useful too. There is a similar package available from MELPA, toggle-quotes.el, but it works differently. toggle-quotes.el toggles between the " and ' quotes, while cycle-quotes.el uses the current mode's syntax table to determine which quote characters to cycle between (so it's able to support an arbitrary number of quote styles). -- Simen --=-Tb1NYsqD/e+eus91n0OQ Content-Type: multipart/mixed; boundary="=-jfonigdHrVtvXIT1amzB" --=-jfonigdHrVtvXIT1amzB Content-Type: text/x-emacs-lisp Content-Disposition: attachment; filename=cycle-quotes-test.el Content-Transfer-Encoding: quoted-printable ;;; cycle-quotes-test.el --- Tests for cycle-quotes.el -*- lexical-binding= : t; -*- ;; Copyright (C) 2015-2016 Simen Heggest=C3=B8yl ;; Author: Simen Heggest=C3=B8yl ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;;; Commentary: ;;; Code: (require 'cycle-quotes) (require 'ert) ;; For testing triple quotes (require 'python) (ert-deftest test-cycle-quotes--set-quote-chars () (with-temp-buffer (let ((st (make-syntax-table))) (set-syntax-table st) (modify-syntax-entry ?a "\"") (modify-syntax-entry ?b "\"") (cycle-quotes--set-quote-chars) (should (=3D (length cycle-quotes--quote-chars) 3)) (should (memq ?a cycle-quotes--quote-chars)) (should (memq ?b cycle-quotes--quote-chars)) (should (memq ?\" cycle-quotes--quote-chars))))) (ert-deftest test-cycle-quotes--next-quote-char () (let ((cycle-quotes--quote-chars '(?a))) (should (=3D (cycle-quotes--next-quote-char ?a) ?a))) (let ((cycle-quotes--quote-chars '(?a ?b))) (should (=3D (cycle-quotes--next-quote-char ?a) ?b))) (let ((cycle-quotes--quote-chars '(?a ?b ?c))) (should (=3D (cycle-quotes--next-quote-char ?c) ?a)))) (ert-deftest test-cycle-quotes--fix-escapes () (with-temp-buffer (insert "b\\baabc\\b") (cycle-quotes--fix-escapes (point-min) (point-max) ?a ?b) (should (equal (buffer-string) "bb\\a\\abcb")))) (ert-deftest test-cycle-quotes () (with-temp-buffer (let ((st (make-syntax-table))) (set-syntax-table st) (modify-syntax-entry ?' "\"") (modify-syntax-entry ?` "\"") (insert "\"Hi, it's me!\"") (goto-char 5) (cycle-quotes) (should (equal (buffer-string) "`Hi, it's me!`")) (cycle-quotes) (should (equal (buffer-string) "'Hi, it\\'s me!'")) (cycle-quotes) (should (equal (buffer-string) "\"Hi, it's me!\""))))) (ert-deftest test-cycle-quotes-triple-quotes () (with-temp-buffer (python-mode) (insert "'''Triple quotes, as found in Python.'''") (goto-char 5) (cycle-quotes) (should (equal (buffer-string) "\"\"\"Triple quotes, as found in Python.\"\"\"")) (cycle-quotes) (should (equal (buffer-string) "'''Triple quotes, as found in Python.'''")))) (provide 'cycle-quotes-test) ;;; cycle-quotes-test.el ends here = --=-jfonigdHrVtvXIT1amzB Content-Type: text/x-emacs-lisp Content-Disposition: attachment; filename=cycle-quotes.el Content-Transfer-Encoding: quoted-printable ;;; cycle-quotes.el --- Cycle between quote styles -*- lexical-binding: t;= -*- ;; Copyright (C) 2015-2016 Simen Heggest=C3=B8yl ;; Author: Simen Heggest=C3=B8yl ;; Keywords: convenience ;; Version: 0.1 ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;;; Commentary: ;; This package provides the `cycle-quotes' function to cycle between ;; different string quote styles. For instance, in JavaScript, there's ;; three string quote characters: ", ` and '. In a JavaScript buffer, ;; with point located someplace within the string, `cycle-quotes' will ;; cycle between the following quote styles each time it's called: ;; ;; --> "Hi, it's me!" --> `Hi, it's me!` --> 'Hi, it\'s me!' -- ;; | | ;; ------------------------------------------------------------ ;; ;; As seen in the above example, `cycle-quotes' tries to escape and ;; unescape quote characters intelligently. ;;; Code: (defvar-local cycle-quotes--quote-chars '() "A list of string quote characters for the current mode. Set the first time `cycle-quotes' is called in a buffer.") (defvar-local cycle-quotes--quote-chars-mode nil "The latest mode that the quote char list was last computed for. If this is different from the current mode, the quote chars need to be recomputed.") (defun cycle-quotes--set-quote-chars () "Set the quote chars for the current syntax table." (let ((syntax-table (syntax-table))) (while syntax-table (map-char-table (lambda (char-code-or-range syntax) (when (equal syntax (string-to-syntax "\"")) (if (consp char-code-or-range) (let ((from (car char-code-or-range)) (to (cdr char-code-or-range))) (dolist (char-code (number-sequence from to)) (add-to-list 'cycle-quotes--quote-chars char-code))) (add-to-list 'cycle-quotes--quote-chars char-code-or-range)))) syntax-table) (setq syntax-table (char-table-parent syntax-table))) (setq-local cycle-quotes--quote-chars-mode major-mode))) (defun cycle-quotes--next-quote-char (char) "Return quote char after CHAR." (let ((list-from-char (member char cycle-quotes--quote-chars))) (when list-from-char (if (=3D (length list-from-char) 1) (car cycle-quotes--quote-chars) (cadr list-from-char))))) (defun cycle-quotes--fix-escapes (beg end escape-char unescape-char) "Fix character escapes between BEG and END. Instances of ESCAPE-CHAR will be escaped by `\', while instances where UNESCAPE-CHAR are escaped by `\' will have their escape character removed." (let ((escape-string (string escape-char)) (unescape-string (string unescape-char))) (save-excursion (goto-char end) (while (search-backward (concat "\\" unescape-string) beg t) (replace-match unescape-string nil t))) (save-excursion (goto-char end) (while (search-backward escape-string beg t) (replace-match (concat "\\" escape-string) nil t) (forward-char -1))))) ;;;###autoload (defun cycle-quotes () "Cycle between string quote styles." (interactive) (unless (eq major-mode cycle-quotes--quote-chars-mode) (cycle-quotes--set-quote-chars)) (if (< (length cycle-quotes--quote-chars) 2) (message "The current mode has no alternative quote syntax") (let ((quote-char (nth 3 (syntax-ppss)))) (if (not quote-char) (message "Not inside a string") (let ((inside-generic-string (eq quote-char t)) ;; Can't use `save-excursion', because the marker will get ;; deleted if point is at the beginning of the string. (start-pos (point))) (when inside-generic-string (skip-syntax-backward "^|") (forward-char -1) (setq quote-char (char-after))) (let ((new-quote-char (cycle-quotes--next-quote-char (if inside-generic-string (char-after) quote-char)))) (unless inside-generic-string (search-backward-regexp (concat "\\([^\\]" (string quote-char) "\\)\\|" "^" (string quote-char))) (when (match-beginning 1) (forward-char))) (let ((repeat ;; Handle multiple quotes, such as Python's triple ;; quotes. (save-excursion (search-forward-regexp (format "%c+" quote-char)) (- (match-end 0) (match-beginning 0))))) (save-excursion (let ((beg (point))) (forward-sexp) ;; `forward-sexp' fails to jump to the matching quote ;; in some modes, for instance `js2-mode'. (skip-syntax-backward "^\"|") (cycle-quotes--fix-escapes (+ beg 1) (+ (point) 1) new-quote-char quote-char)) (delete-char (- repeat)) (dotimes (_ repeat) (insert new-quote-char))) (delete-char repeat) (dotimes (_ repeat) (insert new-quote-char)))) (goto-char start-pos)))))) (provide 'cycle-quotes) ;;; cycle-quotes.el ends here = --=-jfonigdHrVtvXIT1amzB-- --=-Tb1NYsqD/e+eus91n0OQ--