0
0
Fork 1
mirror of https://git.savannah.gnu.org/git/emacs/org-mode.git synced 2024-07-15 19:16:28 +00:00

org-table: introduce an upper bound on `org-table-convert-region-max-lines'.

* 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
omitted.  With the patch, the user will not experience a hang, but
rather an error message.
This commit is contained in:
Aaron Ecay 2014-12-11 23:51:57 -05:00
parent 7c86747881
commit b8d790a076

View file

@ -380,6 +380,16 @@ available parameters."
:group 'org-table-import-export :group 'org-table-import-export
:type 'string) :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 "25.1"
:package-version '(Org . "8.3"))
(defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)" (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
"Detects a table line marked for automatic recalculation.") "Detects a table line marked for automatic recalculation.")
(defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)" (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
@ -546,49 +556,52 @@ nil When nil, the command tries to be smart and figure out the
(let* ((beg (min beg0 end0)) (let* ((beg (min beg0 end0))
(end (max beg0 end0)) (end (max beg0 end0))
re) re)
(if (equal separator '(64)) (if (> (count-lines beg end) org-table-convert-region-max-lines)
(setq separator (read-regexp "Regexp for field separator"))) (user-error "Region is longer than `org-table-convert-region-max-lines' (%s) lines; not converting"
(goto-char beg) org-table-convert-region-max-lines)
(beginning-of-line 1) (if (equal separator '(64))
(setq beg (point-marker)) (setq separator (read-regexp "Regexp for field separator")))
(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) (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 (cond
((not (re-search-forward "^[^\n\t]+$" end t)) '(16)) ((looking-at "^") (insert "| "))
((not (re-search-forward "^[^\n,]+$" end t)) '(4)) ((looking-at "[ \t]*$") (replace-match " |") (beginning-of-line 2))
(t 1)))) ((looking-at "[ \t]*\"\\([^\"\n]*\\)\"")
(goto-char beg) (replace-match "\\1")
(if (equal separator '(4)) (if (looking-at "\"") (insert "\"")))
(while (< (point) end) ((looking-at "[^,\n]+") (goto-char (match-end 0)))
;; parse the csv stuff ((looking-at "[ \t]*,") (replace-match " | "))
(cond (t (beginning-of-line 2))))
((looking-at "^") (insert "| ")) (setq re (cond
((looking-at "[ \t]*$") (replace-match " |") (beginning-of-line 2)) ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
((looking-at "[ \t]*\"\\([^\"\n]*\\)\"") ((equal separator '(16)) "^\\|\t")
(replace-match "\\1") ((integerp separator)
(if (looking-at "\"") (insert "\""))) (if (< separator 1)
((looking-at "[^,\n]+") (goto-char (match-end 0))) (user-error "Number of spaces in separator must be >= 1")
((looking-at "[ \t]*,") (replace-match " | ")) (format "^ *\\| *\t *\\| \\{%d,\\}" separator)))
(t (beginning-of-line 2)))) ((stringp separator)
(setq re (cond (format "^ *\\|%s" separator))
((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?") (t (error "This should not happen"))))
((equal separator '(16)) "^\\|\t") (while (re-search-forward re end t)
((integerp separator) (replace-match "| " t t)))
(if (< separator 1) (goto-char beg)
(user-error "Number of spaces in separator must be >= 1") (org-table-align))))
(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 ;;;###autoload
(defun org-table-import (file arg) (defun org-table-import (file arg)