From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: =?utf-8?Q?Simen_Heggest=C3=B8yl?= Newsgroups: gmane.emacs.devel Subject: Re: [ELPA] New package: cycle-quotes.el Date: Sat, 11 Jun 2016 19:23:40 +0200 Message-ID: <87y46babc3.fsf@gmail.com> References: <1465185956771-399665.post@n5.nabble.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1465665843 27574 80.91.229.3 (11 Jun 2016 17:24:03 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 11 Jun 2016 17:24:03 +0000 (UTC) Cc: Stefan Monnier , emacs-devel@gnu.org To: Michael Mauger Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Jun 11 19:23:58 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 1bBmdY-0007zM-Gl for ged-emacs-devel@m.gmane.org; Sat, 11 Jun 2016 19:23:56 +0200 Original-Received: from localhost ([::1]:47903 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bBmdX-0007vZ-6O for ged-emacs-devel@m.gmane.org; Sat, 11 Jun 2016 13:23:55 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54389) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bBmdQ-0007vQ-VC for emacs-devel@gnu.org; Sat, 11 Jun 2016 13:23:50 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bBmdL-00084r-T4 for emacs-devel@gnu.org; Sat, 11 Jun 2016 13:23:47 -0400 Original-Received: from mail-lf0-x234.google.com ([2a00:1450:4010:c07::234]:33246) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bBmdL-00083U-Ex for emacs-devel@gnu.org; Sat, 11 Jun 2016 13:23:43 -0400 Original-Received: by mail-lf0-x234.google.com with SMTP id f6so41858037lfg.0 for ; Sat, 11 Jun 2016 10:23:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:in-reply-to:date:message-id:mime-version; bh=+o2Vrdp3+P9TKhBrASsGLoRu03gHgtNJ9gEzVtILcY0=; b=tTnCw6Zljp874VI2p/cQPlSnn05x2E9nljlm0fb5Z6+1brAMMie0SNZpH3xoUx05fr WSN44kx4eAzPR8a0usaxuoOiSDyecF9mGfGpcqIMOR/1l0vrG64s9gcqLqK4GwxHLAeh IhEmmDT5N4+wtZjsio2orWBBLucS7C9V8V/zPmuFV+FtIxqS777Lm7qUYzNDeHqznNNO Nip0dGjJrCwSNIWqF/G39Fn6Rf8lo/NLjHTgk2SU+udsRcHDzXT42Ylfx97Lp2noF5xB nUO3I6jQwK+LNuRBavBBZP1YOFdEzkL6Qd+8DZgnmzAV2FGxEW+TCQmO5vOh0DiugY+E o8Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:in-reply-to:date:message-id :mime-version; bh=+o2Vrdp3+P9TKhBrASsGLoRu03gHgtNJ9gEzVtILcY0=; b=TrUc8QlMpyL/dFPvBnfeUdXgSC05pcLobhpnmXHY39kYpp4FFqe7xU05V7BLBLKXGQ MpYqHBMtImFDlPkq2FOmbsv0Wlz/6YL5Ad77qJl3nauICCZyH7dUMJQkhUJ88cZOw0p6 9IHaNXrtsYJ9RQFX5QBqEvbV3Mw/mAzXlWuLeRUoXJgnGiHhBH3G1++pYCgW5/TOc5Qh VJA0FXOtZLI7+S/lt8QDo4821a3yDuIGbL5hz/jtSOqi3XW1/TdzZZOPHTz1Feg29iqR +96HSN6r2sthOj8g1RXtmQnx/kPGCgd4wKGGIJRLwYlrR7Cdfe2XdAeQKs6Gp3XQdcaI +G0Q== X-Gm-Message-State: ALyK8tKxcKN1Lf2B5BhYmf6/kQ1vbVg3NYjJl6+omNs93CGK2UAKeBnMs9OerNlpohfC0Q== X-Received: by 10.46.32.158 with SMTP id g30mr1972765lji.28.1465665822470; Sat, 11 Jun 2016 10:23:42 -0700 (PDT) Original-Received: from x240 (cm-84.210.143.4.getinternet.no. [84.210.143.4]) by smtp.gmail.com with ESMTPSA id o137sm967101lfe.2.2016.06.11.10.23.41 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 11 Jun 2016 10:23:41 -0700 (PDT) In-Reply-To: <1465185956771-399665.post@n5.nabble.com> (message from Michael Mauger on Sun, 5 Jun 2016 21:05:56 -0700 (MST)) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:4010:c07::234 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:204288 Archived-At: --=-=-= Content-Type: text/plain Michael Mauger writes: > The functionality you provide is useful, but making it so that mode-specific > hooks can provide the intelligent details, while your module provides the > engine for cycling and escaping might be a future direction. Michael, Stefan, will something like this provide enough flexibility for you? -- Simen --=-=-= Content-Type: text/x-diff; charset=utf-8 Content-Disposition: inline; filename=cycle-quotes-hooks.diff Content-Transfer-Encoding: quoted-printable diff --git a/cycle-quotes.el b/cycle-quotes.el index faac0cb..075ab98 100644 --- a/cycle-quotes.el +++ b/cycle-quotes.el @@ -45,9 +45,13 @@ Set the first time `cycle-quotes' is called in a buffer.= ") If this is different from the current mode, the quote chars need to be recomputed.") =20 -(defun cycle-quotes--set-quote-chars () - "Set the quote chars for the current syntax table." - (let ((syntax-table (syntax-table))) +(defun cycle-quotes-quote-chars () + "Return a list of quote characters for the current mode. +This is the default value of `cycle-quotes-quote-chars-function'. +It uses the syntax table for the current mode to determine which +characters should be regarded as quote characters." + (let ((syntax-table (syntax-table)) + (quote-chars '())) (while syntax-table (map-char-table (lambda (char-code-or-range syntax) @@ -56,12 +60,50 @@ to be recomputed.") (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)))) + (add-to-list 'quote-chars char-code))) + (add-to-list '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))) + quote-chars)) + +(defun cycle-quotes-surrounding-quote () + "Return the surrounding quote character if point is inside a string. +This is the default value of +`cycle-quotes-surrounding-quote-function'." + (nth 3 (syntax-ppss))) + +(defun cycle-quotes-forward-string () + "Move point across the string directly after point. +This is the default value of +`cycle-quotes-forward-string-function'." + (forward-sexp) + ;; `forward-sexp' fails to jump to the matching quote in some modes, + ;; for instance `js2-mode'. + (skip-syntax-backward "^\"|")) + +(defvar-local cycle-quotes-quote-chars-function + #'cycle-quotes-quote-chars + "Function returning quote characters for the current mode.") + +(defvar-local cycle-quotes-surrounding-quote-function + #'cycle-quotes-surrounding-quote + "Function returning the quote character surrounding point. +It should return the surrounding quote character if point is +inside a string. If not, it should return nil.") + +(defvar-local cycle-quotes-forward-string-function + #'cycle-quotes-forward-string + "Function moving point across a string directly after point. +For instance, if this function is called when point is at `=E2=98=85': + + =E2=98=85'Let\'s go!' + +Point should end up at the end up after the last quote: + + 'Let\'s go!'=E2=98=85 + +Just calling `forward-sexp' should work for most modes, where +quote characters have been given string quote syntax.") =20 (defun cycle-quotes--next-quote-char (char) "Return quote char after CHAR." @@ -93,10 +135,13 @@ character removed." "Cycle between string quote styles." (interactive) (unless (eq major-mode cycle-quotes--quote-chars-mode) - (cycle-quotes--set-quote-chars)) + (let ((quote-chars (funcall cycle-quotes-quote-chars-function))) + (setq-local cycle-quotes--quote-chars quote-chars)) + (setq-local cycle-quotes--quote-chars-mode major-mode)) (if (< (length cycle-quotes--quote-chars) 2) (message "The current mode has no alternative quote syntax") - (let ((quote-char (nth 3 (syntax-ppss)))) + (let ((quote-char + (funcall cycle-quotes-surrounding-quote-function))) (if (not quote-char) (message "Not inside a string") (let ((inside-generic-string (eq quote-char t)) @@ -127,10 +172,7 @@ character removed." (- (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 "^\"|") + (funcall cycle-quotes-forward-string-function) (cycle-quotes--fix-escapes (+ beg 1) (+ (point) 1) new-quote-char quote-char)) (delete-char (- repeat)) --=-=-=--