Re:Bug: ODT export of Chinese text inserts spaces for line breaks

2021-06-28 Thread tumashu
You can try the below config :-)





(defun eh-org-wash-text (text backend _info)
  "导出 org file 时,删除中文之间不必要的空格。"
  (when (or (org-export-derived-backend-p backend 'html)
(org-export-derived-backend-p backend 'odt))
(let ((regexp "[[:multibyte:]]")
  (string text))
  ;; org-mode 默认将一个换行符转换为空格,但中文不需要这个空格,删除。
  (setq string
(replace-regexp-in-string
 (format "\\(%s\\) *\n *\\(%s\\)" regexp regexp)
 "\\1\\2" string))
  ;; 删除粗体之后的空格
  (dolist (str '("" "" "" ""))
(setq string
  (replace-regexp-in-string
   (format "\\(%s\\)\\(%s\\)[ ]+\\(%s\\)" regexp str regexp)
   "\\1\\2\\3" string)))
  ;; 删除粗体之前的空格
  (dolist (str '("" "" "" "" ""))
(setq string
  (replace-regexp-in-string
   (format "\\(%s\\)[ ]+\\(%s\\)\\(%s\\)" regexp str regexp)
   "\\1\\2\\3" string)))
  string)))

(add-hook 'org-export-filter-headline-functions #'eh-org-wash-text)
(add-hook 'org-export-filter-paragraph-functions #'eh-org-wash-text)













在 2021-06-29 11:47:06,"James Harkins"  写道:
>Consider the following org document.
>
>* Test
>1本人不想亲自拿到学历学位证书、急于离校者,可书面委托他人代领学历学位证
>书,29日起即可离校;2本人想亲自领取学历学位证书者,按学校规定的程序及有关
>要求办理离校手续,领取相关证书后离校;
>
>This was produced by pasting in a single, long line, and then using alt-Q (a 
>normal thing to do, and good for readability, because org-mode doesn't wrap 
>lines by default).
>
>Exporting to ODT produces the following (body text, omitting titles, headers 
>and such).
>
>1本人不想亲自拿到学历学位证书、急于离校者,可书面委托他人代领学历学位证 书,29日起即可离校;2本人想亲自领取学历学位证书者,按学校规定的程序及有关 
>要求办理离校手续,领取相关证书后离校;
>
>Between 证 and 书, and between 关 and 要, there is a space. Chinese typography 
>does not allow for spaces mid-sentence.
>
>So, it would make sense to add a rule to the exporter: if one of the 
>characters before or after a source-text line break is a Chinese, Japanese or 
>Korean character, do not add a space. (The space is valid, of course, if the 
>characters on either side of the line breaks are Roman or [I would guess] 
>Cyrillic as well.)
>
>(Side note: Exporting to a LaTeX buffer shows that the line breaks have been 
>copied into the .tex document as is -- but, provided that you have a 
>`usepackage{xeCJK}` in the preamble, LaTeX produces correct, space-free 
>output. So -- Org "gets away with it" because of LaTeX's handling of CJK text. 
>It seems for ODT, Org needs to handle the spacing within its own logic.)
>
>This is org 9.1.9... bit old, I know, but I'm gonna take a wild guess that 
>this has not been a high-visibility issue.
>
>hjh


Bug: ODT export of Chinese text inserts spaces for line breaks

2021-06-28 Thread James Harkins
Consider the following org document.

* Test
1本人不想亲自拿到学历学位证书、急于离校者,可书面委托他人代领学历学位证
书,29日起即可离校;2本人想亲自领取学历学位证书者,按学校规定的程序及有关
要求办理离校手续,领取相关证书后离校;

This was produced by pasting in a single, long line, and then using alt-Q (a 
normal thing to do, and good for readability, because org-mode doesn't wrap 
lines by default).

Exporting to ODT produces the following (body text, omitting titles, headers 
and such).

1本人不想亲自拿到学历学位证书、急于离校者,可书面委托他人代领学历学位证 书,29日起即可离校;2本人想亲自领取学历学位证书者,按学校规定的程序及有关 
要求办理离校手续,领取相关证书后离校;

Between 证 and 书, and between 关 and 要, there is a space. Chinese typography does 
not allow for spaces mid-sentence.

So, it would make sense to add a rule to the exporter: if one of the characters 
before or after a source-text line break is a Chinese, Japanese or Korean 
character, do not add a space. (The space is valid, of course, if the 
characters on either side of the line breaks are Roman or [I would guess] 
Cyrillic as well.)

(Side note: Exporting to a LaTeX buffer shows that the line breaks have been 
copied into the .tex document as is -- but, provided that you have a 
`usepackage{xeCJK}` in the preamble, LaTeX produces correct, space-free output. 
So -- Org "gets away with it" because of LaTeX's handling of CJK text. It seems 
for ODT, Org needs to handle the spacing within its own logic.)

This is org 9.1.9... bit old, I know, but I'm gonna take a wild guess that this 
has not been a high-visibility issue.

hjh



Re: [PATCH] ob-R output file with graphics parameter

2021-06-28 Thread Jeremie Juste
Hello,


Sorry for the delayed reply.

On Sunday, 27 Jun 2021 at 10:12, Jack Kamm wrote:

@Colin, thanks for your feedback. I have to confess that I didn't know about 
the second file
parameter at all until I provided the patch. I just discovered main branch 
works with the second :file
specification,  by chance just before your mail. I have been led into the wrong 
direction trying to get back the
old specification for graphics link.

#+BEGIN_SRC R :results output graphics :file test.pdf
plot(1)
#+end_src


The documentation about this part is poor and I must have missed these
dicussions on the mailing list. So all this time I lived without the graphics 
link. ;-|.

Now that I am back on track, I will check how well the current version works 
particularly for
remote connection. If it does the patch will be redundant.



> However, I tested Jeremie's example on latest org master and it also
> worked fine for me, also on remote sessions.

@Jack thanks for your feedback. I'll improve my style.
>
> The requirement for a second file parameter was added in Org 9.3 to
> support the use case in this thread:
>
> https://orgmode.org/list/3ac2f42a-8ff2-1464-fa36-451e2ef0e...@pressure.to/
>
> But this syntax is annoyingly verbose for ob-R users, and also broke
> lots of ob-R examples prior to Org 9.3.
>
> A simple fix might be to have the "graphics" flag implicitly add the
> "file" flag as well. But we would need to first check that this doesn't
> break other use cases.

I do agree with this solution. If the current specification works, we
could make it easy for ob-R user by implicitly adding a file flag. But
as far as I understand, the change will have to be made in ob-core.el.

>
>> Subject: [PATCH 1/4] ob-R.el: Remove redundant argument to function
>
> I think it would be better to squash these changes into a single commit.
>
Thanks again for the feedback.

Best regards,

-- 
Jeremie Juste



Re: Bug: Duplicate logbook entry for repeated tasks [9.4.6 (9.4.6-gab9f2a @ /home/gustavo/.emacs.d/elpa/org-9.4.6/)]

2021-06-28 Thread Bhavin Gandhi
On Sun, 27 Jun 2021 at 12:01, Bhavin Gandhi wrote:
>
> Till now I found that the function `org-add-log-setup' is getting
> called twice. Why that's happening, I'm not sure yet. I think running
> edebug will help here, I'm going to try that.
>

I found the reason for this behavior with the help of edebug.

`org-todo' calls `org-add-log-setup' to log the state change, and then
it calls `org-auto-repeat-maybe'.

The `org-auto-repeat-maybe' then calls the `org-add-log-setup' again, I
think this is for logging something related to repeated entries.

Before the change `c670379ad', `org-add-log-setup' used to add
`org-add-log-note' to post-command-hook instead of calling it directly.

If I have understood the post-command-hooks correctly, it used to keep
only one entry of `org-add-log-note'. This is because `add-hook' function
makes sure that there are no duplicate entries in the hooks.

I'm not sure how this can be fixed, I'm going to read the
`org-auto-repeat-maybe' to understand what all it is doing.



Re: org-agenda no longer clocks out then in

2021-06-28 Thread Marco Wahl
Tory, Dave D  write:

>...> issues with org-log-note-clock-out...

> this commit
> https://code.orgmode.org/bzg/org-mode/commit/8e3e2f667f0b28b85845204b708c3f0aebc9152b
> probably fixes the issue. Could you perhaps give it a test?

Yes, please!

I also guess that the commit by Nicolas is a fix for the issue.

Thanks Nicolas, Dave and Tory.


Ciao,
-- 
Marco



Re: [patch] add :url and :doi optional entries for export to BiBTeX

2021-06-28 Thread Eric S Fraga
I introduced an error in the previous patch.  Please use this one.
Thank you,
eric

-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.4.6-571-gc591be
: Latest paper written in org: https://arxiv.org/abs/2106.05096
>From ddad85139bd508a06db6e98ddef0131f7cc9946e Mon Sep 17 00:00:00 2001
From: Eric S Fraga 
Date: Mon, 28 Jun 2021 13:35:34 +0100
Subject: [PATCH] ol-bibtex: Added url and doi as optional entries for BiBTeX
 export

* lisp/ol-bibtex.el: :doi and :url entries added to optional list of
entries to export to BiBTeX when present in the properties.

* etc/ORG-NEWS: added news item noting this change.
---
 etc/ORG-NEWS  |  3 +++
 lisp/ol-bibtex.el | 31 +--
 2 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 956f1cf19..3f3971961 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -349,6 +349,9 @@ argument is present.  You can also set =:async no= to force it off
 Async evaluation is disabled during export.
 
 ** Miscellaneous
+*** =org-bibtex= includes =doi= and =url= entries when exporting to BiBTeX
+=doi= and =url= entries have been made optional for some publication
+types and will be exported if present for those types.
 *** Missing or empty placeholders in "eval" macros are now =nil=
 They used to be the empty string.
 *** =org-goto-first-child= now works before first heading
diff --git a/lisp/ol-bibtex.el b/lisp/ol-bibtex.el
index 57b240d92..bad4c7e54 100644
--- a/lisp/ol-bibtex.el
+++ b/lisp/ol-bibtex.el
@@ -145,59 +145,59 @@
   '((:article
  (:description . "An article from a journal or magazine")
  (:required :author :title :journal :year)
- (:optional :volume :number :pages :month :note))
+ (:optional :volume :number :pages :month :note :doi))
 (:book
  (:description . "A book with an explicit publisher")
  (:required (:editor :author) :title :publisher :year)
- (:optional (:volume :number) :series :address :edition :month :note))
+ (:optional (:volume :number) :series :address :edition :month :note :doi))
 (:booklet
  (:description . "A work that is printed and bound, but without a named publisher or sponsoring institution.")
  (:required :title)
- (:optional :author :howpublished :address :month :year :note))
+ (:optional :author :howpublished :address :month :year :note :doi :url))
 (:conference
  (:description . "")
  (:required :author :title :booktitle :year)
- (:optional :editor :pages :organization :publisher :address :month :note))
+ (:optional :editor :pages :organization :publisher :address :month :note :doi :url))
 (:inbook
  (:description . "A part of a book, which may be a chapter (or section or whatever) and/or a range of pages.")
  (:required (:author :editor) :title (:chapter :pages) :publisher :year)
- (:optional :crossref (:volume :number) :series :type :address :edition :month :note))
+ (:optional :crossref (:volume :number) :series :type :address :edition :month :note :doi))
 (:incollection
  (:description . "A part of a book having its own title.")
  (:required :author :title :booktitle :publisher :year)
- (:optional :crossref :editor (:volume :number) :series :type :chapter :pages :address :edition :month :note))
+ (:optional :crossref :editor (:volume :number) :series :type :chapter :pages :address :edition :month :note :doi))
 (:inproceedings
  (:description . "An article in a conference proceedings")
  (:required :author :title :booktitle :year)
- (:optional :crossref :editor (:volume :number) :series :pages :address :month :organization :publisher :note))
+ (:optional :crossref :editor (:volume :number) :series :pages :address :month :organization :publisher :note :doi))
 (:manual
  (:description . "Technical documentation.")
  (:required :title)
- (:optional :author :organization :address :edition :month :year :note))
+ (:optional :author :organization :address :edition :month :year :note :doi :url))
 (:mastersthesis
  (:description . "A Master’s thesis.")
  (:required :author :title :school :year)
- (:optional :type :address :month :note))
+ (:optional :type :address :month :note :doi :url))
 (:misc
  (:description . "Use this type when nothing else fits.")
  (:required)
- (:optional :author :title :howpublished :month :year :note))
+ (:optional :author :title :howpublished :month :year :note :doi :url))
 (:phdthesis
  (:description . "A PhD thesis.")
  (:required :author :title :school :year)
- (:optional :type :address :month :note))
+ (:optional :type :address :month :note :doi :url))
 (:proceedings
  (:description . "The proceedings of a conference.")
  (:required :title :year)
- (:optional :editor (:volume :number) :series :address :month :organization :publisher :note))
+ (:optional :editor (:volume :number) :series :address :month :organization 

Re: [wip-cite-new] Where is the development branch ?

2021-06-28 Thread Eric S Fraga
On Monday, 28 Jun 2021 at 08:26, Bruce D'Arcus wrote:
> Oh, for sure; I agree it'd be good to get this merged for those of us
> that need to get academic work done now!

Especially now that (at least for me) all teaching activities are mostly
over and done with and I can concentrate on my research. :-)

-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.4.6-571-gc591be
: Latest paper written in org: https://arxiv.org/abs/2106.05096



[patch] add :url and :doi optional entries for export to BiBTeX

2021-06-28 Thread Eric S Fraga
Hello all,

increasingly, I need URL and DOI BiBTeX entries to cite particular types
of publications.  Some BiBTeX styles now recognise these but the current
BiBTeX exporter in org does not export these.  I have added :url and
:doi entries to the list of optional entries to export when present to a
selection of the publication types.

I think this is not a breaking change but who knows...

-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.4.6-571-gc591be
: Latest paper written in org: https://arxiv.org/abs/2106.05096
>From ddad85139bd508a06db6e98ddef0131f7cc9946e Mon Sep 17 00:00:00 2001
From: Eric S Fraga 
Date: Mon, 28 Jun 2021 13:35:34 +0100
Subject: [PATCH] ol-bibtex: Added url and doi as optional entries for BiBTeX
 export

* lisp/ol-bibtex.el: :doi and :url entries added to optional list of
entries to export to BiBTeX when present in the properties.

* etc/ORG-NEWS: added news item noting this change.
---
 etc/ORG-NEWS  |  3 +++
 lisp/ol-bibtex.el | 31 +--
 2 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 956f1cf19..3f3971961 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -349,6 +349,9 @@ argument is present.  You can also set =:async no= to force it off
 Async evaluation is disabled during export.
 
 ** Miscellaneous
+*** =org-bibtex= includes =doi= and =url= entries when exporting to BiBTeX
+=doi= and =url= entries have been made optional for some publication
+types and will be exported if present for those types.
 *** Missing or empty placeholders in "eval" macros are now =nil=
 They used to be the empty string.
 *** =org-goto-first-child= now works before first heading
diff --git a/lisp/ol-bibtex.el b/lisp/ol-bibtex.el
index 57b240d92..bad4c7e54 100644
--- a/lisp/ol-bibtex.el
+++ b/lisp/ol-bibtex.el
@@ -145,59 +145,59 @@
   '((:article
  (:description . "An article from a journal or magazine")
  (:required :author :title :journal :year)
- (:optional :volume :number :pages :month :note))
+ (:optional :volume :number :pages :month :note :doi))
 (:book
  (:description . "A book with an explicit publisher")
  (:required (:editor :author) :title :publisher :year)
- (:optional (:volume :number) :series :address :edition :month :note))
+ (:optional (:volume :number) :series :address :edition :month :note :doi))
 (:booklet
  (:description . "A work that is printed and bound, but without a named publisher or sponsoring institution.")
  (:required :title)
- (:optional :author :howpublished :address :month :year :note))
+ (:optional :author :howpublished :address :month :year :note :doi :url))
 (:conference
  (:description . "")
  (:required :author :title :booktitle :year)
- (:optional :editor :pages :organization :publisher :address :month :note))
+ (:optional :editor :pages :organization :publisher :address :month :note :doi :url))
 (:inbook
  (:description . "A part of a book, which may be a chapter (or section or whatever) and/or a range of pages.")
  (:required (:author :editor) :title (:chapter :pages) :publisher :year)
- (:optional :crossref (:volume :number) :series :type :address :edition :month :note))
+ (:optional :crossref (:volume :number) :series :type :address :edition :month :note :doi))
 (:incollection
  (:description . "A part of a book having its own title.")
  (:required :author :title :booktitle :publisher :year)
- (:optional :crossref :editor (:volume :number) :series :type :chapter :pages :address :edition :month :note))
+ (:optional :crossref :editor (:volume :number) :series :type :chapter :pages :address :edition :month :note :doi))
 (:inproceedings
  (:description . "An article in a conference proceedings")
  (:required :author :title :booktitle :year)
- (:optional :crossref :editor (:volume :number) :series :pages :address :month :organization :publisher :note))
+ (:optional :crossref :editor (:volume :number) :series :pages :address :month :organization :publisher :note :doi))
 (:manual
  (:description . "Technical documentation.")
  (:required :title)
- (:optional :author :organization :address :edition :month :year :note))
+ (:optional :author :organization :address :edition :month :year :note :doi :url))
 (:mastersthesis
  (:description . "A Master’s thesis.")
  (:required :author :title :school :year)
- (:optional :type :address :month :note))
+ (:optional :type :address :month :note :doi :url))
 (:misc
  (:description . "Use this type when nothing else fits.")
  (:required)
- (:optional :author :title :howpublished :month :year :note))
+ (:optional :author :title :howpublished :month :year :note :doi :url))
 (:phdthesis
  (:description . "A PhD thesis.")
  (:required :author :title :school :year)
- (:optional :type :address :month :note))
+ (:optional :type :address :month 

Re: [wip-cite-new] Where is the development branch ?

2021-06-28 Thread Bruce D'Arcus
On Mon, Jun 28, 2021 at 8:22 AM Eric S Fraga  wrote:
>
> On Monday, 28 Jun 2021 at 08:09, Bruce D'Arcus wrote:
> > FWIW, I added a function to format org-cite here, which you can use
> > with ivy-bibtex also.
>
> Thank you.  Very comprehensive.  Mine is much simpler!  But I have two
> versions, one for the master branch and one for the wip-cite-new branch
> and I have to keep re-evaluating the code when I switch org branches
> which is why I'm keen to have the branches merged. ;-)

Oh, for sure; I agree it'd be good to get this merged for those of us
that need to get academic work done now!

Bruce



Re: [wip-cite-new] Where is the development branch ?

2021-06-28 Thread Eric S Fraga
On Monday, 28 Jun 2021 at 08:09, Bruce D'Arcus wrote:
> FWIW, I added a function to format org-cite here, which you can use
> with ivy-bibtex also.

Thank you.  Very comprehensive.  Mine is much simpler!  But I have two
versions, one for the master branch and one for the wip-cite-new branch
and I have to keep re-evaluating the code when I switch org branches
which is why I'm keen to have the branches merged. ;-)

-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.4.6-571-gc591be
: Latest paper written in org: https://arxiv.org/abs/2106.05096



Re: [wip-cite-new] Where is the development branch ?

2021-06-28 Thread Bruce D'Arcus
On Mon, Jun 28, 2021 at 7:07 AM Eric S Fraga  wrote:

> It would be nice to have this merged with master so that I can have the
> rest of my workflow at hand; e.g. I have ivy-bibtex inserting links to
> citations automatically but I have to change the format of what it
> inserts depending on which version of org I am using...

FWIW, I added a function to format org-cite here, which you can use
with ivy-bibtex also.

https://github.com/bdarcus/bibtex-actions/blob/main/bibtex-actions.el#L200

There were a few UI decisions I made that will need real world testing
to see if they were right.

Bruce



Re: [wip-cite-new] Where is the development branch ?

2021-06-28 Thread Eric S Fraga
On Saturday, 26 Jun 2021 at 10:35, Nicolas Goaziou wrote:
> Emmanuel Charpentier  writes:
>> Related question : is there an ETA of these new abilities in =Org= ?
>
> I would appreciate some alpha-testing first, but I would say the code is
> already good enough for inclusion at this point. I.e., there are
> a couple of points open for discussion, but no blocker.

As a data point, I can say that I have been using the cite branch for
writing a couple of documents the past week or two, documents that had
to be exported to ODT (yech) and needed citations.  It seems to be
working well.  Thank you!

It would be nice to have this merged with master so that I can have the
rest of my workflow at hand; e.g. I have ivy-bibtex inserting links to
citations automatically but I have to change the format of what it
inserts depending on which version of org I am using...

Thank you,
eric

PS -I still do not understand why the syntax is not [[cite:...]] (which
is what I use in master).  But I don't want to re-open the
discussion. ;-)

-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.4.6-571-gc591be
: Latest paper written in org: https://arxiv.org/abs/2106.05096



Re: Large source block causes org-mode to be unusable

2021-06-28 Thread Eric S Fraga
On Saturday, 26 Jun 2021 at 16:10, Léo Ackermann wrote:
> @EricSFrada, would you mind sharing your code for your proof sections ?
> If you've got ideas for this module, or/and want to participate to its
> development, please let me know :)

I have no code for proofs.  Sorry!  I was simply illustrating that you
can put in LaTeX code directly should special blocks be too large.

-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.4.6-571-gc591be
: Latest paper written in org: https://arxiv.org/abs/2106.05096



Re: Large source block causes org-mode to be unusable

2021-06-28 Thread Eric S Fraga
On Monday, 28 Jun 2021 at 08:28, Sébastien Miquel wrote:
> Léo Ackermann writes:
>> @EricSFrada, would you mind sharing your code for your proof sections ?
> This functionality is now built-in: headings with an `ignore' tag do
> not get exported (their contents do). For very large proof, this seems
> like the right thing to do.

Ah, maybe this is what Leo was referring to.  I may have misunderstood.

Yes, it seems that org now does process :ignore: tags, I guess.  I've
been using my own code to ignore headings for so long that I did not
realise this!

-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.4.6-571-gc591be
: Latest paper written in org: https://arxiv.org/abs/2106.05096



Re: [PATCH] ob-R output file with graphics parameter

2021-06-28 Thread Colin Baxter
> Jack Kamm  writes:

> Hi all,
>> I obviously missing something. The above works for me without the
>> patch. Unfortunately, I can't trace back the thread in order to
>> understand the context.

> I think this is a followup from this mail:

> https://orgmode.org/list/87zgxc42qg@gmail.com/

> wherein Jeremie states:

>> The current patch have been tested for remote connections as well
>> and AFAIK, nothing breaks.
>> 
>> But I'm afraid that the graphical output is broken and has long
>> been even before the path. The test for graphical output is
>> compromised and does not do the right test. I will suggest new
>> ones.

> So I think it's to do with graphical outputs in remote R sessions.

Thank you for explaining.

Best wishes



Re: Large source block causes org-mode to be unusable

2021-06-28 Thread Sébastien Miquel

Hi,

Léo Ackermann writes:

@EricSFrada, would you mind sharing your code for your proof sections ?

This functionality is now built-in: headings with an `ignore' tag do not get
exported (their contents do). For very large proof, this seems like the 
right

thing to do.

In small to moderate sized blocks, the delay can still be noticeable and 
ought

to be fixed. Attached is a patch that seems to resolve this issue. I haven't
noticed any drawbacks so far.

Regards,

--
Sébastien Miquel

>From d843bdc5887a6e50a57e349128ebbe032086dc17 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= 
Date: Sun, 27 Jun 2021 16:24:22 +0200
Subject: [PATCH] WIP : do not refontify special blocks

---
 lisp/org.el | 99 ++---
 1 file changed, 64 insertions(+), 35 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 1bd9e02eb..9fd3f8514 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -5265,22 +5265,32 @@ by a #."
   (org-fontify-meta-lines-and-blocks-1 limit)
 (error (message "Org mode fontification error in %S at %d"
 		(current-buffer)
-		(line-number-at-pos)
+		(line-number-at-pos
+  nil)
 
 (defun org-fontify-meta-lines-and-blocks-1 (limit)
   "Fontify #+ lines and blocks."
-  (let ((case-fold-search t))
-(when (re-search-forward
-	   (rx bol (group (zero-or-more (any " \t")) "#"
-			  (group (group (or (seq "+" (one-or-more (any "a-zA-Z")) (optional ":"))
-	(any " \t")
-	eol))
- (optional (group "_" (group (one-or-more (any "a-zA-Z"))
-			  (zero-or-more (any " \t"))
-			  (group (group (zero-or-more (not (any " \t\n"
- (zero-or-more (any " \t"))
- (group (zero-or-more any)
-	   limit t)
+  (let* ((case-fold-search t)
+ (fl-beg (point))
+ (fl-end limit)
+ (fbeg (when (and (> fl-beg (point-min))
+  (get-text-property (1- fl-beg) 'font-lock-multiline-block))
+ (or (previous-single-property-change
+  fl-beg 'font-lock-multiline-block)
+ (point-min)
+(when fbeg (goto-char fbeg))
+(while (and (< (point) limit)
+(re-search-forward
+	 (rx bol (group (zero-or-more (any " \t")) "#"
+			(group (group (or (seq "+" (one-or-more (any "a-zA-Z")) (optional ":"))
+	  (any " \t")
+	  eol))
+   (optional (group "_" (group (one-or-more (any "a-zA-Z"))
+			(zero-or-more (any " \t"))
+			(group (group (zero-or-more (not (any " \t\n"
+   (zero-or-more (any " \t"))
+   (group (zero-or-more any)
+	 limit t))
   (let ((beg (match-beginning 0))
 	(end-of-beginline (match-end 0))
 	;; Including \n at end of #+begin line will include \n
@@ -5318,7 +5328,7 @@ by a #."
 	  (remove-text-properties beg end-of-endline
   '(display t invisible t intangible t)))
 	(add-text-properties
-	 beg end-of-endline '(font-lock-fontified t font-lock-multiline t))
+	 beg end-of-endline '(font-lock-fontified t font-lock-multiline-block t))
 	(org-remove-flyspell-overlays-in beg bol-after-beginline)
 	(org-remove-flyspell-overlays-in nl-before-endline end-of-endline)
 	(cond
@@ -5327,7 +5337,8 @@ by a #."
 	  (add-text-properties bol-after-beginline block-end '(src-block t)))
 	 (quoting
 	  (add-text-properties
-	   bol-after-beginline beg-of-endline
+	   (max bol-after-beginline (or fl-beg bol-after-beginline))
+   (min beg-of-endline (or fl-end beg-of-endline))
 	   (list 'face
 		 (list :inherit
 			   (let ((face-name
@@ -5426,26 +5437,41 @@ by a #."
 	(add-text-properties closing-start end '(invisible t)))
 	  t)
 
-(defun org-fontify-extend-region (beg end _old-len)
-  (let ((begin-re "\\(\\[\\|\\(#\\+begin_\\|begin{\\)\\S-+\\)")
-	(end-re "\\(\\]\\|\\(#\\+end_\\|end{\\)\\S-+\\)")
-	(extend
- (lambda (r1 r2 dir)
-	   (let ((re (replace-regexp-in-string
-  "\\(begin\\|end\\)" r1
-		  (replace-regexp-in-string
-   "[][]" r2
-		   (match-string-no-properties 0)
-	 (re-search-forward (regexp-quote re) nil t dir)
-(save-match-data
-  (save-excursion
-	(goto-char beg)
-	(back-to-indentation)
-	(cond ((looking-at end-re)
-	   (cons (or (funcall extend "begin" "[" -1) beg) end))
-	  ((looking-at begin-re)
-	   (cons beg (or (funcall extend "end" "]" 1) end)))
-	  (t (cons beg end)))
+(defun org-fontify-extend-region (bego endo _old-len)
+  (let* ((beg bego) (end endo)
+(bol (save-excursion (goto-char beg) (point-at-bol)))
+(eol (save-excursion (goto-char end) (point-at-eol
+(let ((before-multi (and (> bol (point-min))
+ (get-text-property (1- bol) 'font-lock-multiline-block)))
+  (after-multi (get-text-property eol