emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Vladimir Alexiev <vladimir.alexiev@ontotext.com>
To: emacs-orgmode@gnu.org
Subject: Re: configure separator in org-table-insert-hline
Date: Fri, 21 Aug 2020 12:34:33 +0300	[thread overview]
Message-ID: <CAMv+wg4VfCo4Gn2gbdjoY4Fm4Ap06VpxkZ=0OeHDV-OoWxj9Yg@mail.gmail.com> (raw)
In-Reply-To: <87wo1scqer.fsf@nicolasgoaziou.fr>

[-- Attachment #1: Type: text/plain, Size: 4985 bytes --]

> That will not work without changing Org syntax. Markdown and Org table
are incompatible.

I guess you're right re support for formulas, table export, table
input/output from code, etc.
But I don't expect any of that of Markdown tables.
All I expect is that I can use table editing and alignment, and have proper
markdown hlines.

Here's some code that works for me.
It overwrites 2 org table functions, changing just one line (see "FIXED").
Would be nice if this can be adopted in org; of course with a proper option


;; Make org table minor mode (orgtbl-mode) support "|" as hline separator
in markdown

(add-hook 'orgtbl-mode-hook
          (defun my-orgtbl-mode-hook ()
            (set (make-local-variable 'org-table-hline-separator)
                 (if (memq major-mode '(markdown-mode gfm-mode))
                     "|" "+"))))

(eval-after-load "org" '(fset 'org-table-align 'my-org-table-align))
(eval-after-load "org-table" '(fset 'org-table-insert-hline

(defun my-org-table-align ()
  "Align the table at point by aligning all vertical bars."
  (let ((beg (org-table-begin))
(end (copy-marker (org-table-end))))
     ;; Make sure invisible characters in the table are at the right
     ;; place since column widths take them into account.
     (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)
      (let* ((indent (progn (looking-at "[ \t]*") (match-string 0)))
    ;; Table's rows as lists of fields.  Rules are replaced
    ;; by nil.  Trailing spaces are removed.
    (fields (mapcar
     (lambda (l)
(and (not (string-match-p org-table-hline-regexp l))
    (org-split-string l "[ \t]*|[ \t]*")))
     (split-string (buffer-substring beg end) "\n" t)))
    ;; Compute number of columns.  If the table contains no
    ;; field, create a default table and bail out.
     (if fields (apply #'max (mapcar #'length fields))
(kill-region beg end)
(org-table-create org-table-default-size)
(user-error "Empty table - created default table")))
    (widths nil)
    (alignments nil))
;; Compute alignment and width for each column.
(dotimes (i columns-number)
 (let* ((max-width 1)
(fixed-align? nil)
(numbers 0)
(non-empty 0))
   (dolist (row fields)
     (let ((cell (or (nth i row) "")))
(setq max-width (max max-width (org-string-width cell)))
(cond (fixed-align? nil)
     ((equal cell "") nil)
     ((string-match "\\`<\\([lrc]\\)[0-9]*>\\'" cell)
      (setq fixed-align? (match-string 1 cell)))
      (cl-incf non-empty)
      (when (string-match-p org-table-number-regexp cell)
(cl-incf numbers))))))
   (push max-width widths)
   (push (cond
  ((>= numbers (* org-table-number-fraction non-empty)) "r")
  (t "l"))
(setq widths (nreverse widths))
(setq alignments (nreverse alignments))
;; Store alignment of this table, for later editing of single
;; fields.
(setq org-table-last-alignment alignments)
(setq org-table-last-column-widths widths)
;; Build new table rows.  Only replace rows that actually
;; changed.
(dolist (row fields)
 (let ((previous (buffer-substring (point) (line-end-position)))
(format "%s|%s|"
(if (null row) ;horizontal rule
    (mapconcat (lambda (w) (make-string (+ 2 w) ?-))
org-table-hline-separator) ;; FIXED
  (let ((cells ;add missing fields
 (append row
 (make-list (- columns-number
(length row))
    (mapconcat #'identity
(cl-mapcar #'org-table--align-field
   (if (equal new previous)
     (insert new "\n")
     (delete-region (point) (line-beginning-position 2)))))
(set-marker end nil)
(when org-table-overlay-coordinates (org-table-overlay-coordinates))
(setq org-table-may-need-update nil))))))

(defun my-org-table-insert-hline (&optional above)
  "Insert a horizontal-line below the current line into the table.
With prefix ABOVE, insert above the current line."
  (interactive "P")
  (unless (org-at-table-p) (user-error "Not at a table"))
  (when (eobp) (save-excursion (insert "\n")))
  (unless (string-match-p "|[ \t]*$" (org-current-line-string))
   (let ((line (org-table-clean-line
(buffer-substring (point-at-bol) (point-at-eol))))
(col (current-column)))
     (while (string-match "|\\( +\\)|" line)
       (setq line (replace-match
  (concat org-table-hline-separator ;; FIXED
                           (make-string (- (match-end 1) (match-beginning
1)) ?-)
                           "|") t t line)))
     (and (string-match "\\+" line) (setq line (replace-match "|" t t
     (beginning-of-line (if above 1 2))
     (insert line "\n")
     (beginning-of-line (if above 1 -1))
     (org-move-to-column col)
     (when org-table-overlay-coordinates (org-table-align)))))

[-- Attachment #2: Type: text/html, Size: 6484 bytes --]

  reply	other threads:[~2020-08-21  9:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-21  9:07 configure separator in org-table-insert-hline Vladimir Alexiev
2020-08-21  9:13 ` Nicolas Goaziou
2020-08-21  9:34   ` Vladimir Alexiev [this message]
2020-08-21  9:49     ` Nicolas Goaziou
2020-08-21 10:12       ` Vladimir Alexiev
2020-08-21 21:23         ` Nicolas Goaziou
2020-08-22  7:16           ` Vladimir Alexiev
2020-08-24 16:07             ` Nick Dokos

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAMv+wg4VfCo4Gn2gbdjoY4Fm4Ap06VpxkZ=0OeHDV-OoWxj9Yg@mail.gmail.com' \
    --to=vladimir.alexiev@ontotext.com \
    --cc=emacs-orgmode@gnu.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


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).