* configure separator in org-table-insert-hline
@ 2020-08-21 9:07 Vladimir Alexiev
2020-08-21 9:13 ` Nicolas Goaziou
0 siblings, 1 reply; 8+ messages in thread
From: Vladimir Alexiev @ 2020-08-21 9:07 UTC (permalink / raw)
To: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 259 bytes --]
org-table-insert-hline (org-table.el) inserts lines like this
|----+----+----|
But I often use orgtbl-mode in markdown-mode,
and in markdown the hlines should be like this:
|----|----|----|
Perhaps all that's needed is to make this conditional:
(concat "+"
[-- Attachment #2: Type: text/html, Size: 656 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: configure separator in org-table-insert-hline
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
0 siblings, 1 reply; 8+ messages in thread
From: Nicolas Goaziou @ 2020-08-21 9:13 UTC (permalink / raw)
To: Vladimir Alexiev; +Cc: emacs-orgmode
Hello,
Vladimir Alexiev <vladimir.alexiev@ontotext.com> writes:
> org-table-insert-hline (org-table.el) inserts lines like this
> |----+----+----|
>
> But I often use orgtbl-mode in markdown-mode,
> and in markdown the hlines should be like this:
> |----|----|----|
>
> Perhaps all that's needed is to make this conditional:
> (concat "+"
That will not work without changing Org syntax. Markdown and Org table
are incompatible.
Regards,
--
Nicolas Goaziou
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: configure separator in org-table-insert-hline
2020-08-21 9:13 ` Nicolas Goaziou
@ 2020-08-21 9:34 ` Vladimir Alexiev
2020-08-21 9:49 ` Nicolas Goaziou
0 siblings, 1 reply; 8+ messages in thread
From: Vladimir Alexiev @ 2020-08-21 9:34 UTC (permalink / raw)
To: emacs-orgmode
[-- 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
etc.
Cheers!
;; 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
'my-org-table-insert-hline))
(defun my-org-table-align ()
"Align the table at point by aligning all vertical bars."
(interactive)
(let ((beg (org-table-begin))
(end (copy-marker (org-table-end))))
(org-table-save-field
;; 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)
(org-table-with-shrunk-columns
(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.
(columns-number
(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)))
(t
(cl-incf non-empty)
(when (string-match-p org-table-number-regexp cell)
(cl-incf numbers))))))
(push max-width widths)
(push (cond
(fixed-align?)
((>= numbers (* org-table-number-fraction non-empty)) "r")
(t "l"))
alignments)))
(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)))
(new
(format "%s|%s|"
indent
(if (null row) ;horizontal rule
(mapconcat (lambda (w) (make-string (+ 2 w) ?-))
widths
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
cells
widths
alignments)
"|"))))))
(if (equal new previous)
(forward-line)
(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))
(org-table-align))
(org-table-with-shrunk-columns
(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
line)))
(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 --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: configure separator in org-table-insert-hline
2020-08-21 9:34 ` Vladimir Alexiev
@ 2020-08-21 9:49 ` Nicolas Goaziou
2020-08-21 10:12 ` Vladimir Alexiev
0 siblings, 1 reply; 8+ messages in thread
From: Nicolas Goaziou @ 2020-08-21 9:49 UTC (permalink / raw)
To: Vladimir Alexiev; +Cc: emacs-orgmode
Vladimir Alexiev <vladimir.alexiev@ontotext.com> writes:
> 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
> etc.
You may suggest it to Markdown mode. I think this is a terrible idea for
Org mode: syntax should not be optional.
Regards,
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: configure separator in org-table-insert-hline
2020-08-21 9:49 ` Nicolas Goaziou
@ 2020-08-21 10:12 ` Vladimir Alexiev
2020-08-21 21:23 ` Nicolas Goaziou
0 siblings, 1 reply; 8+ messages in thread
From: Vladimir Alexiev @ 2020-08-21 10:12 UTC (permalink / raw)
To: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 529 bytes --]
>
> You may suggest it to Markdown mode. I think this is a terrible idea for
> Org mode: syntax should not be optional.
>
But I'm not talking about org-mode !
I'm talking about orgtbl minor mode.
That minor mode is supposed to work in other modes (that's the whole
purpose of its existence),
and so it should be able to adapt to the table syntax of those
modes/notations.
Yes, markdown-mode may need to do something to use orgtbl-mode.
The problem is that the two functions are not adaptable because "+" is
hard-coded in them.
[-- Attachment #2: Type: text/html, Size: 1054 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: configure separator in org-table-insert-hline
2020-08-21 10:12 ` Vladimir Alexiev
@ 2020-08-21 21:23 ` Nicolas Goaziou
2020-08-22 7:16 ` Vladimir Alexiev
0 siblings, 1 reply; 8+ messages in thread
From: Nicolas Goaziou @ 2020-08-21 21:23 UTC (permalink / raw)
To: Vladimir Alexiev; +Cc: emacs-orgmode
Hello,
Vladimir Alexiev <vladimir.alexiev@ontotext.com> writes:
> But I'm not talking about org-mode !
> I'm talking about orgtbl minor mode.
Of course you are talking about Org mode. You modified org-table-align,
which is clearly an Org function. Also, Orgtbl minor mode is a part of
Org.
> That minor mode is supposed to work in other modes (that's the whole
> purpose of its existence),
> and so it should be able to adapt to the table syntax of those
> modes/notations.
Not at all. It is supposed to bring Org tables into other major modes.
Not the other way. Do you think Orgtbl minor mode adapts to the syntax
in LaTeX?
You may use radio tables to transform an Org table into a Markdown table
(whatever that means, vanilla Markdown doesn't support tables).
Regards,
--
Nicolas Goaziou
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: configure separator in org-table-insert-hline
2020-08-21 21:23 ` Nicolas Goaziou
@ 2020-08-22 7:16 ` Vladimir Alexiev
2020-08-24 16:07 ` Nick Dokos
0 siblings, 1 reply; 8+ messages in thread
From: Vladimir Alexiev @ 2020-08-22 7:16 UTC (permalink / raw)
To: Vladimir Alexiev, emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 37 bytes --]
How can I use radio tables for that?
[-- Attachment #2: Type: text/html, Size: 59 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: configure separator in org-table-insert-hline
2020-08-22 7:16 ` Vladimir Alexiev
@ 2020-08-24 16:07 ` Nick Dokos
0 siblings, 0 replies; 8+ messages in thread
From: Nick Dokos @ 2020-08-24 16:07 UTC (permalink / raw)
To: emacs-orgmode
Vladimir Alexiev <vladimir.alexiev@ontotext.com> writes:
> How can I use radio tables for that?
>
By writing a translation function, similar to orgtbl-to-{csv,latex,html,texinfon,unicode,orgtbl}.
See
(info "(org) Tables in Arbitrary Syntax")
--
Nick
"There are only two hard problems in computer science: cache
invalidation, naming things, and off-by-one errors." -Martin Fowler
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2020-08-24 16:08 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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
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).