From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Chunyang Xu Newsgroups: gmane.emacs.help Subject: Re: [el-search] How to search string excluding docstring? Date: Tue, 26 Dec 2017 00:58:06 +0800 Message-ID: References: <87608u501d.fsf@web.de> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1514221021 30787 195.159.176.226 (25 Dec 2017 16:57:01 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 25 Dec 2017 16:57:01 +0000 (UTC) User-Agent: mu4e 0.9.18; emacs 27.0.50 Cc: help-gnu-emacs@gnu.org To: Michael Heerdegen Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Dec 25 17:56:57 2017 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eTW3a-0007Nh-F8 for geh-help-gnu-emacs@m.gmane.org; Mon, 25 Dec 2017 17:56:54 +0100 Original-Received: from localhost ([::1]:54851 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eTW5X-0002Vm-Hb for geh-help-gnu-emacs@m.gmane.org; Mon, 25 Dec 2017 11:58:55 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:34434) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eTW54-0002VF-Vj for help-gnu-emacs@gnu.org; Mon, 25 Dec 2017 11:58:28 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eTW51-0007ND-SH for help-gnu-emacs@gnu.org; Mon, 25 Dec 2017 11:58:27 -0500 Original-Received: from forward100o.mail.yandex.net ([37.140.190.180]:50301) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eTW51-0007MD-Eb for help-gnu-emacs@gnu.org; Mon, 25 Dec 2017 11:58:23 -0500 Original-Received: from mxback2j.mail.yandex.net (mxback2j.mail.yandex.net [IPv6:2a02:6b8:0:1619::10b]) by forward100o.mail.yandex.net (Yandex) with ESMTP id 4D9242A202BF; Mon, 25 Dec 2017 19:58:20 +0300 (MSK) Original-Received: from smtp2p.mail.yandex.net (smtp2p.mail.yandex.net [2a02:6b8:0:1472:2741:0:8b6:7]) by mxback2j.mail.yandex.net (nwsmtp/Yandex) with ESMTP id gjji5CEjJR-wKaCTqOY; Mon, 25 Dec 2017 19:58:20 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=xuchunyang.me; s=mail; t=1514221100; bh=apwy/n8uHdJqD3HQkzxiy2lmXb/ZuNIy/1QGLQuEER8=; h=References:From:To:Cc:Subject:In-reply-to:Date:Message-ID; b=ySTQUprKly/M4YVTIXxcC3xvBjyAEV4pm/HPM9GK6ln1F+nz467jEwg8/UMUqrddi P50gW/5o4sCBZ7iXnuPMfsl7UwmrDvoTME03G+yJYdIxaR4KeeKP+b4vh++83L0OwB i2oNs0EvYvDCwkcUm4z/pRD3JTBkxaGgequMjbHY= Original-Received: by smtp2p.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id 4t8vzgVBQ3-wBYagw0n; Mon, 25 Dec 2017 19:58:12 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client certificate not present) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=xuchunyang.me; s=mail; t=1514221093; bh=apwy/n8uHdJqD3HQkzxiy2lmXb/ZuNIy/1QGLQuEER8=; h=References:From:To:Cc:Subject:In-reply-to:Date:Message-ID; b=ubXt20lmXG306whOLFcLl8fqmm4UJQMN/m0O55+wghHVLgh8LW7QXTJ/rzmFKGma3 rpJZO/AFRcZ6mivS1oegpCMoytAZyTa8IF6N7D29ykpAVN5iXMMO1Ow7/bgMM/hZsW nvskg4Xz5yPLy05zNq9+kA7SyjXsAhLD1tasaUvM= Authentication-Results: smtp2p.mail.yandex.net; dkim=pass header.i=@xuchunyang.me In-reply-to: <87608u501d.fsf@web.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 37.140.190.180 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:115452 Archived-At: Michael Heerdegen writes: > Chunyang Xu writes: > >> Hi Michael Heerdegen and other Emacs users, >> >> For example, with the following contents in a buffer >> >> (defun foo () >> "foo docstring" >> (message "foo string")) >> >> (string "foo") matches both "foo docstring" and "foo string", I want a >> way to exclude the docstring, by "docstring", I just mean the string >> being checking should not be the fourth element in >> >> (defun _ _ docstring . _) >> >> I am thinking a pattern like >> >> (and (pred stringp) (guard (not (docstring-p))) (string "foo")) >> >> but I have no idea how to define 'docstring-p'. > > The general task is context-sensitive matching. You have no chance with > the current version of el-search, because all patterns there are not > context aware, as you have noticed. > > I want to add that, but because there are some pitfalls, I've not yet > uploaded something like this. > > Here is what I currently have: > > #+begin_src emacs-lisp > (defun el-search--try-find-parent-beg () > ;; try to find beg of parent exp heuristically > (unless (looking-at "^(") > (let ((opoint (point))) > (or (and (search-backward-regexp "[;\(\)\"]" (max (- (point) 300) (point-min))) > (looking-at "\(") > (and (or (eobp)) (not (= (char-before) ?\\))) > (not (looking-back ";" (line-beginning-position)))) > (progn (goto-char opoint) > nil))))) > > (el-search-defpattern parent (pattern &optional n) > "Matches when PATTERN matches the (Nth) parent of the current expression. > N defaults to 1. Slow." > (let ((parent-end (make-symbol "parent-end")) > (counter (make-symbol "ctr"))) > `(and > (let ,counter (or ,n 1)) > (let (,'\` ((,'\, ,pattern))) > (save-excursion > (condition-case nil > (progn > (while (>= (cl-decf ,counter) 0) > (or > (el-search--try-find-parent-beg) > (cond > ((eq (char-before) ?`) (backward-char 1)) > ((eq (char-before) ?,) (backward-char 1)) > ((and (eq (char-before (1- (point))) ?,) > (eq (char-before) ?@)) > (backward-char 2)) > ((eq (char-before) ?') (backward-char 1)) > ((and (eq (char-before (1- (point))) ?#) > (eq (char-before) ?')) > (backward-char 2)) > (t (when-let* ((,parent-end (scan-lists (point) 1 1))) > (goto-char ,parent-end) > (backward-list)))))) > (list (read (current-buffer)))) > (scan-error nil))))))) > #+end_src > > That should enable you to solve your task. Note it's experimental, and > I only had a quick look again now - want to go hiking! > > One thing you can definitely _not_ do with this is to use it recursively > - i.e. (parent (parent PATTERN)) won't work (that's why I added the > optional N argument) - `parent' is only valid when applied to the > current expression. > > Please tell me if it works for you. It does work for me, via (and (string "foo") (not (parent `(defun . ,_)))) I also realize another way, which is not general at all but seeming working. To check if the current string is docstring (save-excursion (backward-up-list) (pcase (read (current-buffer)) (`(defun ,_ ,_ ,(and (pred stringp) docstring) . ,_) (string= docstring string)))) then to search docstring (and (string) string (guard ...)) It has a bug, for example, when search the following, it matches both the first and the second "foo" (the second is not wanted) (defun hello () "foo" "foo" (message "foo")) But it can be ignored because the second "foo" is useless so we don't write code like this. So my current approach of searching string excluding docstring is (defun el-search--docstring-p (string) (save-excursion (backward-up-list) (pcase (read (current-buffer)) (`(defun ,_ ,_ ,(and (pred stringp) docstring) . ,_) (string= docstring string))))) (and (string "foo") s (guard (not (el-search--docstring-p s)))) I plan to support defvar/defcustom etc later. > BTW, another not yet uploaded pattern for matching strings you may find > useful is > > #+begin_src emacs-lisp > (el-search-defpattern string-lines (pattern) > "Match any string whose line number is matched by PATTERN. > > Examples: (string-lines 1) matches one-line strings. > \(string-lines (pred (>= 5))\) matches strings consisting of not > more than 5 lines." > (let ((string (make-symbol "string"))) > `(and (string) > ,string > (let ,pattern > (with-temp-buffer > (insert ,string) > (count-lines (point-min) (point-max))))))) > #+end_src > > I don't know if it's useful enough to upload it. Thanks. I don't have such a need yet but it shows me a simple example of using 'el-search-defpattern'. > > Regards, > > Michael.