emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [RFC] [PATCH] org-table: introduce an upper bound on the number of lines `org-table-convert-region-max-lines' will attempt.
@ 2014-08-23  7:44 Aaron Ecay
  2014-12-01 15:46 ` Bastien
  0 siblings, 1 reply; 3+ messages in thread
From: Aaron Ecay @ 2014-08-23  7:44 UTC (permalink / raw)
  To: emacs-orgmode

* lisp/org-table.el (org-table-convert-region-max-lines): New
defcustom.
(org-table-convert-region): Use it.

This is useful primarily for babel results.  If a large table-like
object is returned by a code block, this function will become bogged
down in trying to read it, and hang emacs (necessitating a C-g).  This
situation most commonly arises when a :results none header has been
ommitted.  With the patch, the user will not experience a hang, but
rather an error message.
---
 lisp/org-table.el | 95 +++++++++++++++++++++++++++++++------------------------
 1 file changed, 54 insertions(+), 41 deletions(-)

diff --git a/lisp/org-table.el b/lisp/org-table.el
index 06a1008..bf88d11 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -366,6 +366,16 @@ available parameters."
   :group 'org-table-import-export
   :type 'string)

+(defcustom org-table-convert-region-max-lines 999
+  "Max lines that `org-table-convert-region' will attempt to process.
+
+The function can be slow on larger regions; this safety feature
+prevents it from hanging emacs."
+  :group 'org-table-import-export
+  :type 'integer
+  :version "24.5"
+  :package-version '(Org . "8.4"))
+
 (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
   "Detects a table line marked for automatic recalculation.")
 (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
@@ -566,49 +576,52 @@ nil      When nil, the command tries to be smart and figure out the
   (let* ((beg (min beg0 end0))
 	 (end (max beg0 end0))
 	 re)
-    (if (equal separator '(64))
-	(setq separator (read-regexp "Regexp for field separator")))
-    (goto-char beg)
-    (beginning-of-line 1)
-    (setq beg (point-marker))
-    (goto-char end)
-    (if (bolp) (backward-char 1) (end-of-line 1))
-    (setq end (point-marker))
-    ;; Get the right field separator
-    (unless separator
+    (if (> (count-lines beg end) org-table-convert-region-max-lines)
+	(user-error "Region is longer than `org-table-convert-region-max-lines' (%s) lines; not converting"
+		    org-table-convert-region-max-lines)
+      (if (equal separator '(64))
+	  (setq separator (read-regexp "Regexp for field separator")))
       (goto-char beg)
-      (setq separator
+      (beginning-of-line 1)
+      (setq beg (point-marker))
+      (goto-char end)
+      (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))))
+      (goto-char beg)
+      (if (equal separator '(4))
+	  (while (< (point) end)
+	    ;; parse the csv stuff
 	    (cond
-	     ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
-	     ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
-	     (t 1))))
-    (goto-char beg)
-    (if (equal separator '(4))
-	(while (< (point) end)
-	  ;; parse the csv stuff
-	  (cond
-	   ((looking-at "^") (insert "| "))
-	   ((looking-at "[ \t]*$") (replace-match " |") (beginning-of-line 2))
-	   ((looking-at "[ \t]*\"\\([^\"\n]*\\)\"")
-	    (replace-match "\\1")
-	    (if (looking-at "\"") (insert "\"")))
-	   ((looking-at "[^,\n]+") (goto-char (match-end 0)))
-	   ((looking-at "[ \t]*,") (replace-match " | "))
-	   (t (beginning-of-line 2))))
-      (setq re (cond
-		((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
-		((equal separator '(16)) "^\\|\t")
-		((integerp separator)
-		 (if (< separator 1)
-		     (user-error "Number of spaces in separator must be >= 1")
-		   (format "^ *\\| *\t *\\| \\{%d,\\}" separator)))
-		((stringp separator)
-		 (format "^ *\\|%s" separator))
-		(t (error "This should not happen"))))
-      (while (re-search-forward re end t)
-	(replace-match "| " t t)))
-    (goto-char beg)
-    (org-table-align)))
+	     ((looking-at "^") (insert "| "))
+	     ((looking-at "[ \t]*$") (replace-match " |") (beginning-of-line 2))
+	     ((looking-at "[ \t]*\"\\([^\"\n]*\\)\"")
+	      (replace-match "\\1")
+	      (if (looking-at "\"") (insert "\"")))
+	     ((looking-at "[^,\n]+") (goto-char (match-end 0)))
+	     ((looking-at "[ \t]*,") (replace-match " | "))
+	     (t (beginning-of-line 2))))
+	(setq re (cond
+		  ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
+		  ((equal separator '(16)) "^\\|\t")
+		  ((integerp separator)
+		   (if (< separator 1)
+		       (user-error "Number of spaces in separator must be >= 1")
+		     (format "^ *\\| *\t *\\| \\{%d,\\}" separator)))
+		  ((stringp separator)
+		   (format "^ *\\|%s" separator))
+		  (t (error "This should not happen"))))
+	(while (re-search-forward re end t)
+	  (replace-match "| " t t)))
+      (goto-char beg)
+      (org-table-align))))

 ;;;###autoload
 (defun org-table-import (file arg)
--
2.0.4

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [RFC] [PATCH] org-table: introduce an upper bound on the number of lines `org-table-convert-region-max-lines' will attempt.
  2014-08-23  7:44 [RFC] [PATCH] org-table: introduce an upper bound on the number of lines `org-table-convert-region-max-lines' will attempt Aaron Ecay
@ 2014-12-01 15:46 ` Bastien
  2014-12-12  4:56   ` Aaron Ecay
  0 siblings, 1 reply; 3+ messages in thread
From: Bastien @ 2014-12-01 15:46 UTC (permalink / raw)
  To: Aaron Ecay; +Cc: emacs-orgmode

Hi Aaron,

Aaron Ecay <aaronecay@gmail.com> writes:

> * lisp/org-table.el (org-table-convert-region-max-lines): New
> defcustom.
> (org-table-convert-region): Use it.

Looks good, feel free to apply this.

-- 
 Bastien

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [RFC] [PATCH] org-table: introduce an upper bound on the number of lines `org-table-convert-region-max-lines' will attempt.
  2014-12-01 15:46 ` Bastien
@ 2014-12-12  4:56   ` Aaron Ecay
  0 siblings, 0 replies; 3+ messages in thread
From: Aaron Ecay @ 2014-12-12  4:56 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode

Hi Bastien,

2014ko abenudak 1an, Bastien-ek idatzi zuen:
> 
> Hi Aaron,
> 
> Aaron Ecay <aaronecay@gmail.com> writes:
> 
>> * lisp/org-table.el (org-table-convert-region-max-lines): New
>> defcustom.
>> (org-table-convert-region): Use it.
> 
> Looks good, feel free to apply this.

Thanks, done.

-- 
Aaron Ecay

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-12-12  4:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-23  7:44 [RFC] [PATCH] org-table: introduce an upper bound on the number of lines `org-table-convert-region-max-lines' will attempt Aaron Ecay
2014-12-01 15:46 ` Bastien
2014-12-12  4:56   ` Aaron Ecay

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).