Browse Source

org-table: Allow to shrink tables upon opening a document

* lisp/org-table.el (org-table-shrink): New function.
* lisp/org.el (org-startup-align-all-tables): Update docstring.
(org-startup-shrink-all-tables): New variable.
(org-mode): Use new function and new variable.
Nicolas Goaziou 7 years ago
parent
commit
73bf9b8887
2 changed files with 42 additions and 5 deletions
  1. 22 1
      lisp/org-table.el
  2. 20 4
      lisp/org.el

+ 22 - 1
lisp/org-table.el

@@ -781,7 +781,7 @@ When nil, simply write \"#ERROR\" in corrupted fields.")
     (org-table-save-field
      ;; Make sure invisible characters in the table are at the right
      ;; place since column widths take them into account.
-     (font-lock-fontify-region beg end)
+     (org-font-lock-ensure beg end)
      (move-marker org-table-aligned-begin-marker beg)
      (move-marker org-table-aligned-end-marker end)
      (goto-char beg)
@@ -4061,6 +4061,27 @@ prefix, expand all columns."
       (let ((o (org-table--shrunk-field)))
 	(when o (goto-char (overlay-start o)))))))
 
+;;;###autoload
+(defun org-table-shrink ()
+  "Shrink all columns with a width cookie in the table at point.
+Columns without a width cookie are expanded."
+  (interactive)
+  (unless (org-at-table-p) (user-error "Not at a table"))
+  (org-with-wide-buffer
+   (let ((begin (org-table-begin))
+	 (end (org-table-end))
+	 (regexp "|[ \t]*<[lrc]?[0-9]+>[ \t]*\\(|\\|$\\)")
+	 (columns))
+     (goto-char begin)
+     (while (re-search-forward regexp end t)
+       (goto-char (match-beginning 1))
+       (cl-pushnew (org-table-current-column) columns))
+     (org-table--expand-all-columns begin end)
+     ;; Make sure invisible characters in the table are at the right
+     ;; place since column widths take them into account.
+     (org-font-lock-ensure begin end)
+     (org-table--shrink-columns (sort columns #'<) begin end))))
+
 
 
 ;;; Formula editing

+ 20 - 4
lisp/org.el

@@ -1028,8 +1028,6 @@ the following lines anywhere in the buffer:
 
 (defcustom org-startup-align-all-tables nil
   "Non-nil means align all tables when visiting a file.
-This is useful when the column width in tables is forced with <N> cookies
-in table fields.  Such tables will look correct only after the first re-align.
 This can also be configured on a per-file basis by adding one of
 the following lines anywhere in the buffer:
    #+STARTUP: align
@@ -1037,6 +1035,17 @@ the following lines anywhere in the buffer:
   :group 'org-startup
   :type 'boolean)
 
+(defcustom org-startup-shrink-all-tables nil
+  "Non-nil means shrink all table columns with a width cookie.
+This can also be configured on a per-file basis by adding one of
+the following lines anywhere in the buffer:
+   #+STARTUP: shrink"
+  :group 'org-startup
+  :type 'boolean
+  :version "26.1"
+  :package-version '(Org . "9.1")
+  :safe #'booleanp)
+
 (defcustom org-startup-with-inline-images nil
   "Non-nil means show inline images when loading a new Org file.
 This can also be configured on a per-file basis by adding one of
@@ -4826,6 +4835,7 @@ After a match, the following groups carry important information:
     ("oddeven" org-odd-levels-only nil)
     ("align" org-startup-align-all-tables t)
     ("noalign" org-startup-align-all-tables nil)
+    ("shrink" org-startup-shrink-all-tables t)
     ("inlineimages" org-startup-with-inline-images t)
     ("noinlineimages" org-startup-with-inline-images nil)
     ("latexpreview" org-startup-with-latex-preview t)
@@ -5536,8 +5546,14 @@ The following commands are available:
   (unless org-inhibit-startup
     (org-unmodified
      (when org-startup-with-beamer-mode (org-beamer-mode))
-     (when org-startup-align-all-tables
-       (org-table-map-tables #'org-table-align t))
+     (when (or org-startup-align-all-tables org-startup-shrink-all-tables)
+       (org-table-map-tables
+	(cond ((and org-startup-align-all-tables
+		    org-startup-shrink-all-tables)
+	       (lambda () (org-table-align) (org-table-shrink)))
+	      (org-startup-align-all-tables #'org-table-align)
+	      (t #'org-table-shrink))
+	t))
      (when org-startup-with-inline-images (org-display-inline-images))
      (when org-startup-with-latex-preview (org-toggle-latex-fragment '(16)))
      (unless org-inhibit-startup-visibility-stuff (org-set-startup-visibility))