From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id qK7SCCasn2AgdQEAgWs5BA (envelope-from ) for ; Sat, 15 May 2021 13:10:30 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id 8B2YBCasn2AiaAAA1q6Kng (envelope-from ) for ; Sat, 15 May 2021 11:10:30 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 5F29A22D5C for ; Sat, 15 May 2021 13:10:29 +0200 (CEST) Received: from localhost ([::1]:55710 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lhsBg-0002rR-9X for larch@yhetil.org; Sat, 15 May 2021 07:10:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42014) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lhsB4-0002r3-EI for emacs-orgmode@gnu.org; Sat, 15 May 2021 07:09:50 -0400 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]:41832) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lhsB2-0005xb-HB; Sat, 15 May 2021 07:09:50 -0400 Received: by mail-pl1-x62f.google.com with SMTP id e18so631471plh.8; Sat, 15 May 2021 04:09:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:in-reply-to:references:date:message-id :mime-version; bh=pJKixLXWitzAWcryxIY7LXdXKNYkm7TQwsoNEqAfTOk=; b=Uhy7s3/2/ooFugrJqUOOEV9KMT+Gq4gnq/NDiry2VDG0uxIsjpghqepcRRuBGJOXeV rfqhrzYy25RmXYpw169z9v4u9aXP7xq/TdUzJYbPNubxYVRfdNgPdYXJry6bKpucrQTY NKSi/Zqle0ErlQPThl6g3zRNW+4sB1rs8d6SyFM3MUhPeIjI2o6VRJUkLvb2IA0EMwOZ IvgEJVEbR8+f23AQTs3ETJjBKQqBp7y1qTx9kJmaT9ImH9MoAw2ss6UkPwXbOVkJt3R1 ZxNV89Smf7YlTdTVlZ6gc/GcWxGluUQHiGCXgkrG318WRcUHH75iF4P3Nrix69R/nEa9 Hbyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=pJKixLXWitzAWcryxIY7LXdXKNYkm7TQwsoNEqAfTOk=; b=rIt4GzoTPUGZDtVIx9RJSgp9CQeZkII9fjjzrK/waH0wLOX8HxGYjt9jxXhek0i59R Z+CesgJoDrli5jIIEtVEZsVbgtABkBvxKeQ4z7kuTyqms+mDy297SwW0LDB52OL8kUQf 7WnBvRtxCFH77nQpk5peLVVlEGitEKszB9zHf3Y0owYZiyoNRT/fmzHzIKmnlJ+kMWHj X4Lz/faNO02aeM5ChoCNyH330+pftWXCBtyk1v4jAz6wEeJemqS5cVnuwp9Sgm8ANy/o tCadQAjIxWFMKzhGko9Yyt0dRrQQ5HzM4yhN79eSc9npchLOx7Vudm9psA0VWZRlLwsl 0gsQ== X-Gm-Message-State: AOAM531FL3B7ex/kg4iGKRnSJpUlk546ZfFryAWChpH1Z5ukfxMk8LKn u5HFm4V06ea+mdjw7UTwZy79sU9oltQisg== X-Google-Smtp-Source: ABdhPJxy+tAQDoJ6F2I+9TpqTKrbNOUHnpVbBaVk3/x31QlNnNNe9KlgDicxUpvXAbYOnshKMhPwQA== X-Received: by 2002:a17:902:6901:b029:ee:e531:ca5f with SMTP id j1-20020a1709026901b02900eee531ca5fmr50499501plk.37.1621076986564; Sat, 15 May 2021 04:09:46 -0700 (PDT) Received: from localhost ([45.251.50.206]) by smtp.gmail.com with ESMTPSA id 66sm6504125pgj.9.2021.05.15.04.09.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 May 2021 04:09:45 -0700 (PDT) From: Utkarsh Singh To: Bastien Subject: Re: bug#47885: [PATCH] org-table-import: Make it more smarter for interactive use In-Reply-To: <87k0o0jnm5.fsf@gnu.org> References: <87czuq9958.fsf@gmail.com> <8735vmelfs.fsf@nicolasgoaziou.fr> <87k0oyfj4y.fsf@gmail.com> <87im4h9irn.fsf@nicolasgoaziou.fr> <87zgxpwqa7.fsf@gmail.com> <875z07jx6n.fsf@nicolasgoaziou.fr> <87tunqby9a.fsf@gmail.com> <875yzq77w8.fsf@gmail.com> <87o8dd74dv.fsf@gmail.com> <874kf49x7f.fsf@gnu.org> <87pmxse29o.fsf@gmail.com> <87k0o0jnm5.fsf@gnu.org> Date: Sat, 15 May 2021 16:39:54 +0530 Message-ID: <87im3kdzi5.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::62f; envelope-from=utkarsh190601@gmail.com; helo=mail-pl1-x62f.google.com X-Spam_score_int: 1 X-Spam_score: 0.1 X-Spam_bar: / X-Spam_report: (0.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, PDS_OTHER_BAD_TLD=1.997, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Maxim Nikulin , emacs-orgmode@gnu.org, mail@nicolasgoaziou.fr Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1621077029; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=pJKixLXWitzAWcryxIY7LXdXKNYkm7TQwsoNEqAfTOk=; b=hzL27rBxUOjo7FoGYp2E64iQOagSyBjsk6pO8HDDZT6utNAJMz5yb7R79Es7DIw5FQpp30 vPg/cgMBubVLYcQ/WnIM+QUT69tKzj7WMpnCN22Ifqr5qroYsnW37rIUIbUAr8sUM6qx5Y gJIQVE7p46MMGB9a4HTVJCaACgCCx9YGd5ZcLLgRYI4qgu7eTRZCgcZMdgQUVAzjH103U0 ppWtSHNuayKJZkIN5cwk/v+QSuf585vxx9fMGZ/r7Yrk9FsrzmWzJGfr2IkvbMvFBrZvn2 6uZDDKaFbvTFTyOnhYb3XHf9ICo/p00Z+rEq0ugCTf5PIMMCkxSggtdE5eomoA== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1621077029; a=rsa-sha256; cv=none; b=uGaEA4Tmrr2l0Ahv8o3bqQyxO0Rj4zxQI4cU2zYnh8/P4QmRX+1swf1yzOex+p6V+S+NSu OSSmMU0Asyj7dwvBnk8xw1S0vjxRh9m/sWVLWNRg+E+QP6nbyk4p1+CjVGO6+vHUOXbCXN pwlWOHLO/ng8t6aXkyzgYA5Cf7v3UtUrWUwgOg81i/5Lbd8UZ5Pv6mFbHBzpmWJNGyjAii hfrv4SpPc0DiGLEQNssbkep6kedAsDkFdAtdMTCWZ1CaGnulZWxWf9hw2Nc/X1S1Ov3mPs wVKUh4qVd9i3R6NQddZQ48ZM3/BgSZc/oBrPixCJM9B8gs3WCy4AJln0Hjve9A== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20161025 header.b="Uhy7s3/2"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Spam-Score: -3.15 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20161025 header.b="Uhy7s3/2"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Queue-Id: 5F29A22D5C X-Spam-Score: -3.15 X-Migadu-Scanner: scn0.migadu.com X-TUID: HErT9x5EWSTU --=-=-= Content-Type: text/plain On 2021-05-15, 12:30 +0200, Bastien wrote: > Hi Utkarsh, > > Utkarsh Singh writes: > >> For now can you review the patches I proposed earlier in this >> thread? > > Do these patches provide a complete and predictable solution? > If so, can you merge them into a single patch against master? > Here are the patches: I have separated out 'adding of prompt' to patch2 as it doesn't have to do anything with separator guessing part. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-org-table-convert-region-move-out-separator-guessing.patch Content-Description: patch1 >From 659e5e95bd8e024ac4730cf410a565b9e975b669 Mon Sep 17 00:00:00 2001 From: Utkarsh Singh Date: Sat, 15 May 2021 16:30:36 +0530 Subject: [PATCH 1/2] org-table-convert-region: move out separator-guessing 1. Move separator guessing code to org-table-guess-separator (new function). 2. Add semicolon, colon and SPACE to the list of know separator (separator which we can guess). --- lisp/org-table.el | 49 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/lisp/org-table.el b/lisp/org-table.el index cc69542..d3242da 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -837,6 +837,39 @@ SIZE is a string Columns x Rows like for example \"3x2\"." (goto-char pos)) (org-table-align))) +(defun org-table-guess-separator (beg0 end0) + "Guess separator for region BEG0 to END0. + +List of preferred separator (in order of preference): +comma, TAB, semicolon, colon or SPACE. + +Search for a line which doesn't contain a separator if found +search again using next preferred separator or else return +separator as string." + (let* ((beg (save-excursion + (goto-char (min beg0 end0)) + (skip-chars-forward " \t\n") + (if (eobp) (point) (line-beginning-position)))) + (end (save-excursion + (goto-char (max beg0 end0)) + (skip-chars-backward " \t\n" beg) + (if (= beg (point)) (point) (line-end-position)))) + (sep-regexp + (list (list "," (rx bol (1+ (not (or ?\n ?,))) eol)) + (list "\t" (rx bol (1+ (not (or ?\n ?\t))) eol)) + (list ";" (rx bol (1+ (not (or ?\n ?\;))) eol)) + (list ":" (rx bol (1+ (not (or ?\n ?:))) eol)) + (list " " (rx bol (1+ (not (or ?\n ?\s))) eol))))) + (unless (= beg end) + (save-excursion + (goto-char beg) + (catch :found + (pcase-dolist (`(,sep ,regexp) sep-regexp) + (save-excursion + (unless (re-search-forward regexp end t) + (throw :found sep)))) + nil))))) + ;;;###autoload (defun org-table-convert-region (beg0 end0 &optional separator) "Convert region to a table. @@ -853,10 +886,7 @@ following values: integer When a number, use that many spaces, or a TAB, as field separator regexp When a regular expression, use it to match the separator nil When nil, the command tries to be smart and figure out the - separator in the following way: - - when each line contains a TAB, assume TAB-separated material - - when each line contains a comma, assume CSV material - - else, assume one or more SPACE characters as separator." + separator using `org-table-guess-seperator'." (interactive "r\nP") (let* ((beg (min beg0 end0)) (end (max beg0 end0)) @@ -873,13 +903,10 @@ nil When nil, the command tries to be smart and figure out the (if (bolp) (backward-char 1) (end-of-line 1)) (setq end (point-marker)) ;; Get the right field separator - (unless separator - (goto-char beg) - (setq separator - (cond - ((not (re-search-forward "^[^\n\t]+$" end t)) '(16)) - ((not (re-search-forward "^[^\n,]+$" end t)) '(4)) - (t 1)))) + (when (and (not separator) + (not (setq separator + (org-table-guess-separator beg end)))) + (user-error "Failed to guess separator")) (goto-char beg) (if (equal separator '(4)) (while (< (point) end) -- 2.31.1 --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0002-org-table-import-add-file-prompt.patch Content-Description: patch2 >From 328d59dd2d2fc797e5819f43b40c96ffa45cd62f Mon Sep 17 00:00:00 2001 From: Utkarsh Singh Date: Sat, 15 May 2021 16:32:32 +0530 Subject: [PATCH 2/2] org-table-import: add file prompt --- lisp/org-table.el | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lisp/org-table.el b/lisp/org-table.el index d3242da..20144c9 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -954,7 +954,8 @@ lines. It can have the following values: - (64) Prompt for a regular expression as field separator. - integer When a number, use that many spaces, or a TAB, as field separator. - regexp When a regular expression, use it to match the separator." - (interactive "f\nP") + (interactive (list (read-file-name "Import file: ") + (prefix-numeric-value current-prefix-arg))) (when (and (called-interactively-p 'any) (not (string-match-p (rx "." (or "txt" "tsv" "csv") eos) file)) (not (yes-or-no-p "The file's extension is not .txt, .tsv or .csv. Import? "))) -- 2.31.1 --=-=-= Content-Type: text/plain -- Utkarsh Singh http://utkarshsingh.xyz --=-=-=--