* Re: [PATCH] Accept more :tangle-mode specification forms
@ 2021-10-05 14:45 93% ` Timothy
2021-11-18 10:20 93% ` Timothy
0 siblings, 2 replies; 74+ results
From: Timothy @ 2021-10-05 14:45 UTC (permalink / raw)
To: Org Mode List
[-- Attachment #1: Type: text/plain, Size: 841 bytes --]
Hi Everyone,
It feels like we’re near a patch that would be good to merge. I would very much
like to get feedback on what I proposed in my reply to Tom though (see below).
>> That said, reducing the number of forms as Eric suggests would
>> be a happy medium.
>
> Indeed, I’ve basically supported every form I could think of. I’m currently
> inclined to cut it down to:
> • 755
> • “rwxrw-r–” (`ls -l’ style)
> • chmod style with `org-babel-tangle-default-mode’ and `file-modes-symbolic-to-number’
>
> Maybe with (if anybody says they would like this)
> • #o755 (elisp octal)
> • 0755 (C octal)
> • “rwx” = user perm, bit-or’d with `org-babel-tangle-default-mode’ for the rest
> (i.e. `org-babel-tangle-default-mode’, but not exceeding the user perm)
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Accept more :tangle-mode specification forms
@ 2021-10-05 16:13 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-10-05 16:13 UTC (permalink / raw)
To: unknown@email.com; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 326 bytes --]
Hi Jeremy,
> I think this is a good idea and don’t see any problems (or other
> suggestions) with the proposed formats.
>
> The existing (identity #o0755) will still function, correct?
> i.e. backward compatibility.
It should yes. I’ll double check before I actually push the commit.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Rename headline to heading
@ 2021-10-15 9:56 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-10-15 9:56 UTC (permalink / raw)
To: André A. Gomes; +Cc: Bastien, emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 382 bytes --]
Hi André,
I haven’t gone through the patches you’ve sent, but it looks like you’ve put a
tremendous amount of effort into this! It’s fantastic to see. I think I can
speak for the Org community when I say thank you.
André A. Gomes <andremegafone@gmail.com> writes:
> Hi Bastien and All,
>
> This is a first attempt towards the goal.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Prevent displayed images from being re-scaled
@ 2021-10-25 13:49 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-10-25 13:49 UTC (permalink / raw)
To: Org-Mode Mailing List
[-- Attachment #1: Type: text/plain, Size: 70 bytes --]
It’s been quite a few days, so I’ve just pushed this as 9dc08c9.
^ permalink raw reply [relevance 93%]
* Re: Subject: [PATCH] Fix DISPLAY error on exporting org with plantuml to html
@ 2021-11-18 7:38 92% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-18 7:38 UTC (permalink / raw)
To: Sun Lin; +Cc: emacs-orgmode
Hi Sun,
> [snipped patch description]
> Please help review the patch. Thanks.
> [2. text/x-patch; 0001-Fix-DISPLAY-error-on-exporting-org-with-plantuml-to-.patch]...
Thanks for your patience. This looks quite straight-forward. I don't use
or have plantuml so I'm in a poor position to test.
However, I can use google, and it seems that -Djava.awt.headless=true
just uses Java for some graphics operations which are normally delegated
to the system graphics stack.
This seems rather safe to me, and so I'd be inclined to take your word
that this is a beneficial change and merge the patch.
All the best,
Timothy
^ permalink raw reply [relevance 92%]
* Re: [PATCH] org-manual.org: org-cite additions
@ 2021-11-18 8:08 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-11-18 8:08 UTC (permalink / raw)
To: Bruce D'Arcus; +Cc: emacs-orgmode
Hi Bruce,
Thanks for this patch. I've had a read through and it all looks good to
me. I've just applied this with minor tweaks as 30dbfc6.
> This is a minor patch to address the most obvious missing pieces that
> have confused people.
--
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Accept more :tangle-mode specification forms
2021-10-05 14:45 93% ` Timothy
@ 2021-11-18 10:20 93% ` Timothy
2021-11-18 17:22 87% ` Timothy
1 sibling, 1 reply; 74+ results
From: Timothy @ 2021-11-18 10:20 UTC (permalink / raw)
To: Org Mode List
Hi All,
This has just been pushed as described in fa97f9a39.
See some tests I performed before pushing below.
#+begin_src text :tangle-mode (identity #o345) :tangle t1.txt
this works
#+end_src
#+begin_src text :tangle-mode 433 :tangle t2.txt
this works
#+end_src
#+begin_src text :tangle-mode u+x :tangle t3.txt
this works
#+end_src
#+begin_src text :tangle-mode rw-r--r-- :tangle t4.txt
this works
#+end_src
#+begin_src text :tangle-mode hey :tangle t5.txt
invalid, error message given
#+end_src
--
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] ob-R async evaluation
@ 2021-11-18 10:36 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-11-18 10:36 UTC (permalink / raw)
To: Jeremie Juste; +Cc: emacs-orgmode
Hi Jeremie,
Sorry it's taken a while to get to this. I've just gone through your
patch and it looks quite reasonable. I also took the liberty of
fixing the indentation in a few spots and tweaking your commit message.
This has just been pushed as 58db28d.
> I'm attaching a patch that clean up a bit of non conventional coding
> practices (mainly because of my ignorance), in ob-R. These were
> initially generating warning during the compilation of org-mode.
By the way, just letting you know that your mail client declared your
.patch to be a PDF file 😛.
> [2. application/pdf; 0001-ob-R.el-Patch-4-async-evaluation.patch]...
--
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Treat :tangle-mode as an octal value not integer
@ 2021-11-18 12:04 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-11-18 12:04 UTC (permalink / raw)
To: emacs-orgmode
Just removing this from updates.orgmode.org.
^ permalink raw reply [relevance 93%]
* Re: [PATCH] org.el (org-display-inline-image--width): Small fix
@ 2021-11-18 12:06 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-11-18 12:06 UTC (permalink / raw)
To: emacs-orgmode
Just marking this patch as applied for updates.orgmode.org.
Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:
> Hello,
>
> Sébastien Miquel <sebastien.miquel@posteo.eu> writes:
>
>> Subject: [PATCH] org.el (org-display-inline-image--width): Small fix
>
> I expounded the commit message and applied your patch. Thank you.
>
> Regards,
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Accept more :tangle-mode specification forms
2021-11-18 10:20 93% ` Timothy
@ 2021-11-18 17:22 87% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-18 17:22 UTC (permalink / raw)
To: Org Mode List
[-- Attachment #1: Type: text/plain, Size: 842 bytes --]
Hi All,
I thought I’d checked for this, but I’ve just noticed that :tangle-mode 755
doesn’t actually work as expected. I assumed 755 would be passed as a string but
org-babel-parse-header-arguments actually turns it into an integer, just like
(identity #o755). Obviously 755 != #o755 and so this causes issues.
As it stands “755” works, but that isn’t great (most importantly, it’s easy to
confuse). Since it’s easier to add than remove things like this, we could just
get rid of this for now, but a convenient octal notation was a large chunk of
the motivation here IIRC.
We could also change the implementation to handle :tangle-mode o755, which will
make org-babel-parse-header-arguments parse the argument as a string.
I’m be keen to hear other people’s thoughts on this.
All the best,
Timothy
^ permalink raw reply [relevance 87%]
* Re: [PATCH] Accept more :tangle-mode specification forms
@ 2021-11-20 8:08 60% ` Timothy
0 siblings, 2 replies; 74+ results
From: Timothy @ 2021-11-20 8:08 UTC (permalink / raw)
To: Tim Cross; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 3195 bytes --]
Hi Tom, Tim, Thomas, and Greg,
Thank you all for your thoughts. I’ll try to respond to all the main points
raised below.
First off, in case it wasn’t clear in my earlier email when I said “#o555 works”
or the like I was being lazy and really meaning “(identity #o555)” works.
The parsing of “555” to the integer 555 is done by
org-babel-parse-header-arguments, and so can’t really be changed. For a simple
octal notation our best bet is adding non-digit characters so it is left as a
string, e.g. “o555”.
┌────
│ 1 -> (org-babel-parse-header-arguments ":tangle-mode 555" light)
│ 1 <- org-babel-parse-header-arguments: ((:tangle-mode . 555))
│ ======================================================================
│ 1 -> (org-babel-parse-header-arguments ":tangle-mode o555" light)
│ 1 <- org-babel-parse-header-arguments: ((:tangle-mode . "o555"))
└────
So while I’d like it if we could easily apply Tom’s suggestion to “change the
radix for bare integers to 8 when reading that particular header”, I don’t think
that’s very feasible, unfortunately.
Giving errors when a base 10 value has been given by accident would be a nice
idea, but in practice isn’t easy as many base 10 values are valid octal values,
e.g. #o555 = 365. Likewise, I don’t think Tim’s suggestion of disallowing base
10 values is feasible.
Regarding the concern that “we are making a rod for our back by trying to make
this overly clever” and that the change makes it too complex — I disagree.
Having had this discussion earlier we’ve ended up with,
• a shorthand for octal
• ls-style
• chmod-style
I think this small collection of distinct and simple input methods isn’t overly
clever or complex, and feel that it strikes the right balance between too many
options and too little flexibility.
Octal is great, for those that are familiar with it. Likewise, chmod-style is
quite natural for those who are used to chmod. There’s little added complexity
to the Org code base as we simply pass this to the Emacs function
file-modes-symbolic-to-number. I think the ls-style is quite valuable for two
reasons. It’s well-established thanks to ls, and is particularly good for
understanding permissions at a glance. For reading Org files I think this is
advantageous compared to the other styles. I’m don’t find assertions that this
is non-typical or unpredictable well-founded. Each style/syntax is well-defined,
simple, distinct, and taken from very common/wide spread usage.
Tim suggested that invalid forms should cause tangling to fail, but I feel this
may be a bit much. Personally I’m inclined to either
• warn, and don’t touch the file permissions (this is what currently occurs)
• use a very conservative file permission (e.g. rw——-).
So, as I see it the main decision that needs to be made is how to handle the
octal shorthand, now that it’s clear that the original plan is flawed? I’m
thinking either “o555” or “#o555” would be a good improvement over “(identity
#o555), but am open to other suggestions.
All the best,
Timothy
^ permalink raw reply [relevance 60%]
* Re: [PATCH] Accept more :tangle-mode specification forms
@ 2021-11-20 14:50 93% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-20 14:50 UTC (permalink / raw)
To: tomas; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 1006 bytes --]
Hi Thomas (& co.),
>> […] I’m thinking either “o555” or “#o555” would be a good improvement over
>> “(identity #o555), but am open to other suggestions.
>
> That’s reasonable. I’d even tend to disallow decimal. In usage, it’s too
> exotic and the potential for someone entering “just a number” expecting
> for it to be read as octal is too high. Is it worth the risk?
I’ve just pushed three commits that
1. Add “o555” as an octal shorthand
2. Perform a simple check that integer modes are valid*
3. Make the ls-style regex stricter
> Timothy, I really admire your patience, and your incredibly friendly way
> of doing things :)
Thanks. It helps that this list is fairly friendly to begin with :)
All the best,
Timothy
* For example, “:tangle-mode 755” will now produce the warning:
“1363 is not a valid file mode octal. Did you give the decimal value 755 by
mistake?”. Maybe it would be worth adding “if so try o755” or similar?
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Accept more :tangle-mode specification forms
@ 2021-11-21 4:02 67% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-21 4:02 UTC (permalink / raw)
To: Tim Cross; +Cc: emacs-orgmode
Hi Tim,
>> The parsing of “555” to the integer 555 is done by
>> org-babel-parse-header-arguments, and so can’t really be changed.
>
> I don't understand this. Why can't it be changed?
Well, it can't be changed without changing
org-babel-parse-header-arguments, which is quite a major change and I
suspect may be a breaking change.
Maybe it's fine? I just don't feel confident about this.
> When I said disable base 10 values, what I meant was generate an error
> if the value looks like it is base 10 i.e. does not have a leading o or
> #o. This makes it clear it has to be specified as an octal value and
> will alert users to this fact.
This is all well and good, but the point I'm making is that since due to
the current behaviour of org-babel-parse-header-arguments we can't
distinguish between :tangle-mode (identity #o555) and :tangle-mode 365
putting this idea /into practice/ doesn't look like it will be easy.
It's also worth noting that with regard to the recent changes that have
been made, this is not a new issue.
>> I think the ls-style is quite valuable for two reasons. It’s
>> well-established thanks to ls, and is particularly good for
>> understanding permissions at a glance.
>
> I would agree it is an established way to display the permissions
> associated with a filesystem object, but disagree it is a well
> established way to set such values - I know of no other tool which uses
> this format.
The driving motivation here is that while the tools which you mention do
not use this for setting permissions (e.g. chmod), they are only used
for /setting/ permissions. Since Org files aren't write-only, and
occasionally we go back and look at what we've written :P I think
allowing a format that is optimised for ease-of-reading instead of
ease-of-writing makes some sense. It is in this respect that I think
ls -l style is a good idea.
> It is also not the same as the ls -l display (no object
> type indicator). The ls -l also displays sticky bit, uid and gid. Does
> your format also support setting these values (something which can be
> done with the octal or symbolic formats) i.e. support s, S, t and T for
> the 'executable' bit for user/group?
Ah, I'm afraid that I'm not that up-together on sticky bits. I suspect
that it's not just the ls -l style that will need tweaking. I'll read up
on this in the next few days and update the ML.
> Personally, I prefer the symbolic form as it is shorter and clear. I
> find the ls -l form too easy to get wrong (especially with getting the
> number of '-' correct).
Isn't choice a great thing? :D In seriousness, this is exactly why I
think it's worth providing these options. At least thinking back to when
I started using Linux a few years ago I would have said the same thing
about the octal form, and was completely unfamiliar with chmod. I expect
different people to have different preferences with these three options,
but for one of them to be something most people are happy with.
>> Tim suggested that invalid forms should cause tangling to fail, but I feel this
>> may be a bit much. Personally I’m inclined to either
>> • warn, and don’t touch the file permissions (this is what currently occurs)
>> • use a very conservative file permission (e.g. rw——-).
>
> I'm unsure on this. My concern is people may not notice the warning and
> then be surprised later. Given the potential security issues, a later
> surprise is something to be avoided even if it is inconvenient. With
> respect to the default action to take, I would suggest we also need to
> look at the default umask setting for the user and if that is more
> restrictive, use that rather than some value like rw------- For all we
> know, the user has set a specific umask for a valid reason and will be
> surprised if emacs just ignores that to do what it wants (another
> surprise to be avoided).
> The user is not required to specify a mode. However, if they do and if
> we cannot interpret what they have specified without ambiguity, we
> should throw an error and cease processing. Making a guess as to what
> they intended in this situation is IMO a big mistake.
I don't see how using the default permissions is a potential security
risk? Could it surprise the user, yes, but that seems unavoidable when
the user is doing something invalid.
> My only preference for "#o" over just "o" prefix is to clearly signal to
> the user that it is an octal value and avoid the situation where you
> might glance a value and see the leading 'o' as a '0', missing the point
> it is an octal value not a decimal one. However, this seems like a low
> risk, so I'm happy either way.
Well, I've got "o" for now but that's not set in stone. With a lower
case "o" I feel the risk for confusion with "0" is quite low (besides
which a "0" prefix seems to be the C-style for octal anyway).
All the best,
Timothy
^ permalink raw reply [relevance 67%]
* Re: [PATCH] Accept more :tangle-mode specification forms
@ 2021-11-21 4:27 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-11-21 4:27 UTC (permalink / raw)
To: Greg Minshall; +Cc: tomas, emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 508 bytes --]
Hi Greg,
> i’d push back, even here, on allowing decimal. file modes are bit
> masks. to me, offering a way to set a bit mask via a *decimal* value
> seems a mistake.
Just a quick note (see my long recent reply to Tim where I expand on this more),
but this isn’t new behaviour (isn’t actually affected by my recent changes) and
my concerns are with the viability of the necessary changes rather than whether
this would be good (we are of the same mind I think).
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] org-src: Reset buffer-modified-p after fontifying
@ 2021-11-21 6:58 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-11-21 6:58 UTC (permalink / raw)
To: Clément Pit-Claudel; +Cc: emacs-orgmode
Hi Clément,
I've just had a look at your patch, and it seems very reasonable to me.
Hence, I've just pushed it as ebd06c1 :)
> Hi all,
>
> * lisp/org-src.el (org-src-font-lock-fontify-block): Reset the
> modification flag of the temporary fontification buffer after
> fontifying.
>
> Without this some modes cause Emacs to prompt about unsaved buffers
> when exiting: `Save buffer *org-src-fontification:...-mode*?'
>
> (I'm seeing this with Proof General's coq-mode, specifically.)
>
> Clément.
>
> [2. text/x-patch; 0001-org-src-Reset-buffer-modified-p-after-fontifying.patch]...
--
Timothy
^ permalink raw reply [relevance 93%]
* Dodgy Worg publishing? (was: [patch] Fix link to Library of Babel)
@ 2021-11-21 7:04 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-11-21 7:04 UTC (permalink / raw)
To: Thomas S. Dye; +Cc: Bastien, emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 381 bytes --]
Hi Thomas,
Thanks for catching this problem. To me this looks more like a problem with the
worg publishing than an outdated link. I’ve cc’d Bastien as he’s much more
familiar with the Worg publish process than I am.
“Thomas S. Dye” <tsd@tsdye.online> writes:
> The attached patch changes a 404 link to the working link used on Worg.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Fontification for inline src blocks
@ 2021-11-21 14:09 49% ` Timothy
2021-11-22 11:52 45% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-21 14:09 UTC (permalink / raw)
To: org-mode-email
[-- Attachment #1: Type: text/plain, Size: 477 bytes --]
Hi All,
Since the contentious component of my previous patches has been the
`{{{results()}}}' prettification, I’ve prepared an alternate version that I’m
hoping everybody will be fairly happy with (fingers crossed!) that tosses out
the results prettification for now.
I think Protesilaos’ comments on making some new faces deserve consideration,
but could easily be done subsequently, as I’m quite keen to get this merged for
now.
All the best,
Timothy
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-src-Implement-native-inline-src-fontification.patch --]
[-- Type: text/x-patch, Size: 5149 bytes --]
From 53d2851e248c4f4e4076878a999cbf647b401578 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Tue, 13 Jul 2021 02:43:29 +0800
Subject: [PATCH] org-src: Implement native inline src fontification
* lisp/org-src.el (org-fontify-inline-src-blocks,
org-fontify-inline-src-blocks-1): Create a function to search the buffer
up to a limit for inline src blocks. Light fontification is applied to
matched inline src blocks. When `org-src-fontify-natively' is
set, `org-src-font-lock-fontify-block' is applied to the content.
* lisp/org.el (org-set-font-lock-defaults): Add
`org-fontify-inline-src-blocks' to `org-font-lock-extra-keywords', which
is locally bound inside `org-set-font-lock-defaults'.
(org-inline-src-fontify-max-length): Create a variable to limit the
maximum length of an inline-src block fontified, to protect from lag
spikes (e.g. when typing out src_lang{ and half of the buffer is
fontified).
---
lisp/org-src.el | 46 ++++++++++++++++++++++++++++++++++++++++++++++
lisp/org.el | 18 ++++++++++++++++++
2 files changed, 64 insertions(+)
diff --git a/lisp/org-src.el b/lisp/org-src.el
index 51dde602d..c3a6a40bc 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -654,6 +654,52 @@ (defun org-src-font-lock-fontify-block (lang start end)
'(font-lock-fontified t fontified t font-lock-multiline t))
(set-buffer-modified-p modified)))))
+(defun org-fontify-inline-src-blocks (limit)
+ "Try to apply `org-fontify-inline-src-blocks-1'."
+ (condition-case nil
+ (progn
+ (org-fontify-inline-src-blocks-1 limit)
+ (org-fontify-inline-src-results limit))
+ (error (message "Org mode fontification error in %S at %d"
+ (current-buffer)
+ (line-number-at-pos)))))
+
+(defun org-fontify-inline-src-blocks-1 (limit)
+ "Fontify inline src_LANG blocks, from `point' up to LIMIT."
+ (let ((case-fold-search t)
+ (initial-point (point)))
+ (when (re-search-forward "\\_<src_\\([^ \t\n[{]+\\)[{[]?" limit t) ; copied from `org-element-inline-src-block-parser'
+ (let ((beg (match-beginning 0))
+ (lang-beg (match-beginning 1))
+ (lang-end (match-end 1))
+ pt)
+ (font-lock-append-text-property lang-beg lang-end 'face 'org-meta-line)
+ (font-lock-append-text-property beg lang-beg 'face 'shadow)
+ (font-lock-append-text-property beg lang-end 'face 'org-block)
+ (setq pt (goto-char lang-end))
+ ;; `org-element--parse-paired-brackets' doesn't take a limit, so to
+ ;; prevent it searching the entire rest of the buffer we temporarily
+ ;; narrow the active region.
+ (save-restriction
+ (narrow-to-region beg (min (point-max)
+ limit
+ (+ lang-end org-inline-src-fontify-max-length)))
+ (when (ignore-errors (org-element--parse-paired-brackets ?\[))
+ (font-lock-append-text-property pt (point) 'face 'org-block)
+ (setq pt (point)))
+ (when (ignore-errors (org-element--parse-paired-brackets ?\{))
+ (remove-text-properties pt (point) '(face nil))
+ (font-lock-append-text-property pt (1+ pt) 'face '(org-block shadow))
+ (unless (= (1+ pt) (1- (point)))
+ (if org-src-fontify-natively
+ (org-src-font-lock-fontify-block
+ (buffer-substring-no-properties lang-beg lang-end)
+ (1+ pt) (1- (point)))
+ (font-lock-append-text-property (1+ pt) (1- (point)) 'face 'org-block)))
+ (font-lock-append-text-property (1- (point)) (point)'face '(org-block shadow))
+ (setq pt (point)))))
+ t)))
+
\f
;;; Escape contents
diff --git a/lisp/org.el b/lisp/org.el
index eeefb4af3..6a424991c 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -5279,6 +5279,23 @@ (defcustom org-allow-promoting-top-level-subtree nil
:version "24.1"
:group 'org-appearance)
+(defcustom org-inline-src-fontify-max-length 200
+ "Maximum content length of an inline src block that will be fontified.
+This is only relevant when `org-src-fontify-natively' is t."
+ :type 'integer
+ :package-version '(Org . "9.5")
+ :group 'org-appearance
+ :group 'org-babel)
+
+(defcustom org-inline-src-prettify-results t
+ "Whether to use (ab)use prettify-symbols-mode on {{{results(...)}}}.
+Either t or a cons cell of strings which are used as substitutions
+for the start and end of inline results, respectively."
+ :type '(choice boolean (cons string string))
+ :package-version '(Org . "9.5")
+ :group 'org-appearance
+ :group 'org-babel)
+
(defun org-fontify-meta-lines-and-blocks (limit)
(condition-case nil
(org-fontify-meta-lines-and-blocks-1 limit)
@@ -5785,6 +5802,7 @@ (defun org-set-font-lock-defaults ()
'(9 'org-special-keyword t))
;; Blocks and meta lines
'(org-fontify-meta-lines-and-blocks)
+ '(org-fontify-inline-src-blocks)
;; Citations
'(org-cite-activate))))
(setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
--
2.33.1
^ permalink raw reply related [relevance 49%]
* Re: [PATCH] Accept more :tangle-mode specification forms
@ 2021-11-21 14:33 64% ` Timothy
2021-11-29 18:57 91% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-21 14:33 UTC (permalink / raw)
To: Tim Cross; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 6101 bytes --]
Hi Tim,
Thanks for the way in which you’ve responded to my comments. I appreciate the
effort you’ve gone to to explain your views as opposed to simply saying you
disagree with some of my current thoughts :)
Tim Cross <theophilusx@gmail.com> writes:
> My suggestion is simply to look at the value and if it is not a string, reject
> it outright. […] This is not a change in org-babel-parse-arguments,
> but rather a change in how we interpret those arguments.
Ah I see, I did not think you were suggesting this. I’m quite wary of this as it
would break all current `:tangle-mode (identity #oXYZ)' headers, which as I
understand it are /the/ way tangle-mode has been previously set.
Perhaps this is a good argument for deprecation? I think we’d need to give a
decently long transition period though (a few years).
> Allowing base 10 mode specifications is just a bad idea. The fact it has been
> permitted in the past is likely just an oversight rather than intentional
> behaviour.
I think we are of the same mind regarding base10 permissions specifications
being bad, but are niggling over what to do about this.
> Given the potential security issues involved
> here, I think we can legitimately obsolete such features (using
> acceptable change management approaches with suitable notification for
> users etc).
I’m certainly open to deprecation, though I’d like to hear from some of the
other “main Org people” (e.g. Bastien, Nicolas) before doing anything along
these lines. Perhaps this would be worth making a second thread for?
> I understand your motivation. I dispute your claim it is an established
> and common convention.
Ok, cool. Since we should have a while till the next Org release, nothing is set
in stone yet and hopefully this will give us time to see if the ls -l format is
fit for purpose or not. I think it will be, but correctness is of course more
important than niceness.
>> [sticky bits and the ls -l format]
>
> This is very difficult Timothy. I really do appreciate all the work you
> have done here and your patience, which makes what I’m going to say all
> the more difficult. My big concern here is that you don’t understand
> this area with sufficient depth of understanding to really appreciate
> some of the subtleties involved here. This makes me concerned that any
> implementation of a new way to specify mode settings may have some
> unexpected side effects or hidden issues which could be exploited in
> unexpected ways (worse case) or simply have unexpected bugs which result
> in mode settings which are not what the user intended. As someone who
> has used Unix since 1980 and Linux since 1995, I’ve experienced my share
> of issues relating to file modes and their interaction with different
> filesystems, NFS, different platforms, character sets, locales etc.
> Implementing a new approach as you are proposing is something I would be
> extremely wary about.
>
> If you do plan to go forward with this approach, please ensure you are
> very confident in your understanding of the sticky bit, setuid/setgid
> and associated interaction with execute etc.
Thanks for explaining your position thoroughly here. I don’t need any convincing
that a solid understanding is necessary for a good implementation. I had a quick
peek and estimated the time I’d need to properly come to terms with sticky bits —
hence my estimate of a few days before I look at implementing this.
> I suspect you do have the skills to implement correctly given sufficient
> time, testing and motivation. However, I am concerned your initial stab
> at this may be somewhat naive and requires more consideration. My main
> concern is that once you take all the subtleties into consideration, the
> complexity of your ls -l approach will exceed its utility and
> maintainability. I could also be completely wrong.
I’ll let you (/the ML) know when I’ve taken a look at stiky bits, and whether I
think I’m able to create something that works. My intuition is that if ls -l can
properly represent sticky bits (and my rudimentary understanding is that it can)
it should be fine for specifying them too. We’ll see.
> Choice is good. However, as I mentioned in a previous post, the problem
> with choice is complexity and the biggest source of security problems is
> complexity. Keep in mind that what you do now is something which will
> need to be maintained by others in the future. It needs to be clear,
> concise and as simple as possible (but of course, no simpler!).
I like to think that the current implementation is fairly short and clean. If I
can’t add sticky bits “nicely”, I’ll reconsider things.
> Consider this simple scenario. The tangled output is being written to a
> directory which requires specific permissions for security reasons -
> perhaps it is some type of ftp server where permissions will determine
> what can be seen by whom. The data might contain sensitive information.
> The user specifies what they thing is the correct mode specification,
> but the7y get it wrong. Org comes along and looks at what they specified
> and decides it cannot interpret what the user has specified, so applies
> a default (and potentially completely wrong) mode, exposing sensitive
> data to others who should not have had access.
>
> Use a default if the user does not specify a mode, but if they specify a
> mode and org cannot interpret what they specified, then do nothing -
> don’t generate the tangled output at all. This is the only safe
> approach.
Thanks for the example. This sounds reasonable to me, and has prompted me to
test what’s actually happening at the moment.
Seems like the user-error I raise actually causes the tangling for that file
/and all subsequent files/ to fail, i.e. no content is written. It looks like we
actually want to loosen the current behaviour :P
Alright, that’s it for now. You can expect an update in a few days on sticky
bits.
All the best,
Timothy
^ permalink raw reply [relevance 64%]
* Re: [PATCH] Fix window width when line numbers present
@ 2021-11-21 19:14 92% ` Timothy
2021-11-21 19:19 93% ` Timothy
1 sibling, 1 reply; 74+ results
From: Timothy @ 2021-11-21 19:14 UTC (permalink / raw)
To: Matt Huszagh; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 916 bytes --]
Hi Matt,
> This is a very small patch that I believe fixes a previous, incorrect
> fix for computing the window text area when line numbers are
> present. The previous fix seemed to assume that
> display-line-numbers-width being nil meant that this means line numbers
> are not present. However, it can also mean (as described in the
> variable documentation) that the line number width is computed
> dynamically.
Thanks for this patch. I wasn’t aware of `line-number-display-width' when I wrote
that, but it looks like a better fit than my `(or ... 0)' statement. As such, I’ve
just applied your patch as cd3e138, tweaking the commit message to no longer go
over the maximum length :)
Looking at the docstring for `line-number-display-width', it seems that the
pixelwise mode may be more useful for our purposes, but that improvement can be
in a subsequent patch.
All the best,
Timothy
^ permalink raw reply [relevance 92%]
* Re: [PATCH] Fix window width when line numbers present
2021-11-21 19:14 92% ` Timothy
@ 2021-11-21 19:19 93% ` Timothy
1 sibling, 0 replies; 74+ results
From: Timothy @ 2021-11-21 19:19 UTC (permalink / raw)
To: Matt Huszagh; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 92 bytes --]
Oops, I forgot to mark this patch as applied for updates.orgmode.org.
Done in this message.
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Fix regex for determining image width from attribute
@ 2021-11-21 19:20 81% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-21 19:20 UTC (permalink / raw)
To: Matt Huszagh; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 1444 bytes --]
Hi Matt,
> A recent patch started computing the inline image width from any attr_
> line. This is incorrect, as it matches settings like attr_latex, or
> attr_html. We only want to look for settings specifically for the org
> buffer. This patch fixes that.
Once again, thank you for the patch. The fact that the current regexp matches
`#+attr_latex' and `#+attr_html' is in fact by design though*. This is because I
consider it safe to assume that a `#+attr_*' which gives non-integer width between
0 and 2 can be safely assumed to be that proportion of the page width. e.g.
`#+attr_latex: :width 0.7\linewidth' or `#+attr_html: :width 70%'.
This way, a good guess can be made without having do have both an
`#+attr_latex/html' /and/ an `#+attr_org' line for the width. Should this assumption
be incorrect, a subsequent `#+attr_org' line will override the other `#+attr_*'.
Should there be edge-cases where this assumption doesn’t hold, I’d be interested
in patches that improves the logic here. As long as this holds more often than
not though, this should be a net positive for user experience as I see it.
Do please let me know if there are any good examples of unintended /
counter-intuitive behaviour you’re aware of.
All the best,
Timothy
* Well, it’s worked this way for a while, and I made a deliberate choice to keep
this behaviour when expanding the width to recognise proportional values.
^ permalink raw reply [relevance 81%]
* Re: [PATCH] Fix window width when line numbers present
@ 2021-11-22 3:16 93% ` Timothy
1 sibling, 0 replies; 74+ results
From: Timothy @ 2021-11-22 3:16 UTC (permalink / raw)
To: Nicolas Goaziou; +Cc: Matt Huszagh, emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 617 bytes --]
Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:
> `line-number-display-width’ is Emacs 26+. So I guess it is unfortunately
> not acceptable on main branch.
Ooops, thanks for picking that up Nicolas. I see we actually have
┌────
│ (if (fboundp 'line-number-display-width)
│ (defalias 'org-line-number-display-width 'line-number-display-width)
│ (defun org-line-number-display-width (&rest _) 0))
└────
in org-compat.el, but I think that fallback definition could be improved — even
just `(or display-line-numbers-width 0)' would be better.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Fix window width when line numbers present
@ 2021-11-22 5:31 93% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-22 5:31 UTC (permalink / raw)
To: Bastien; +Cc: Matt Huszagh, emacs-orgmode, Nicolas Goaziou
[-- Attachment #1: Type: text/plain, Size: 397 bytes --]
Hi Bastien,
> We can safely assume that Org 9.6 will be released after Emacs 28, so
> let’s use `line-number-display-width’ on main.
So I take this to mean that we will now assume that Org 9.6+ will target Emacs
29? This seems reasonable to me, but doesn’t quite accord with how I read
<https://orgmode.org/worg/org-maintenance.html#emacs-compatibility>.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Supported Emacs version (was: [PATCH] Fix window width when line numbers present)
@ 2021-11-22 5:51 90% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-11-22 5:51 UTC (permalink / raw)
To: Bastien; +Cc: Matt Huszagh, emacs-orgmode, Nicolas Goaziou
[-- Attachment #1: Type: text/plain, Size: 817 bytes --]
Hi Bastien,
> Our commitment is that the latest Org version is compatible with the
> last three stable versions of Emacs.
>
> So when Emacs 28 and Org 9.6 are both out, we guarantee that Org is
> compatible with Emacs 28, 27 and 26.
>
> Does that explain it better?
Thanks for clarifying. That’s perfectly clear, and re-reading the content of
<https://orgmode.org/worg/org-maintenance.html#emacs-compatibility> I think that
is too and I just must have read it strangely the first time??
On the topic of Emacs version compatibility, I’ve just taken a peek at
org-compat.el and notice that we still have code for Emacs < 24.4 compatibility.
I’m tempted to make some patches to remove all the Emacs < 26 compatibility code
if we no longer need it. What do you think?
All the best,
Timothy
^ permalink raw reply [relevance 90%]
* Re: [PATCH] Fix regex for determining image width from attribute
@ 2021-11-22 8:29 91% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-22 8:29 UTC (permalink / raw)
To: Matt Huszagh; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 1641 bytes --]
Hi Matt,
> Unfortunately, I think this makes a valid use case
> impossible. Specifically, I like to be able to set an image width
> explicitly with #+attr_org (or some other attr_ for the corresponding
> export) and fall back to the actual image width when this isn’t
> specified. This includes the ability to use the actual image width in an
> org buffer, but an explicitly-set image width for export. For example,
> for my blog I often have attr_html set, but I want the image to use its
> actual width when displayed in org.
Thanks for explaining a use case! That’s most helpful.
> I don’t see how this is possible with the current implementation.But, it
> works naturally with the implementation I have in mind
Actually, it’s almost possible with the current implementation. Consider this
example:
┌────
│ #+attr_org: :width t
│ #+attr_html: :width 20%
│ [[file:image.png]]
└────
At the moment Org tries to interpret `t' as a number (and obviously fails),
however with a small tweak that I think would be very reasonable to make, this
would cause the behaviour you describe.
What do you think?
> (IIRC this is how it previously worked, but I could be mistaken).
You are mistaken. The previous implementation looked for `#+attr_*' too, but
didn’t recognise proportional values.
> Maybe a solution to accomplish all goals would be to add an #+attr_fallback
> (or attr_default, attr_any, attr_all, etc.) that is used for any backend
> unless a specific setting is made for that backend.
Hmmm, I’m not sure this is called for.
All the best,
Timothy
^ permalink raw reply [relevance 91%]
* Re: [PATCH] Fontification for inline src blocks
2021-11-21 14:09 49% ` Timothy
@ 2021-11-22 11:52 45% ` Timothy
0 siblings, 2 replies; 74+ results
From: Timothy @ 2021-11-22 11:52 UTC (permalink / raw)
To: org-mode-email
[-- Attachment #1: Type: text/plain, Size: 439 bytes --]
Hi Everyone,
I think there’s room in the future to add more faces for this, but I it occurs
to me that the main complaint raised in this thread can be resolved by adding
one new face: `org-inline-src-block' (which inherits from `org-block') by default.
See attached for a patch which just adds this face. I feel like this might be
the final version of this patch so I’d appreciate thoughts on this.
All the best,
Timothy
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-src-Implement-native-inline-src-fontification.patch --]
[-- Type: text/x-patch, Size: 5915 bytes --]
From 133b7a90853f7f9062bae40af2efc8fd22781125 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Tue, 13 Jul 2021 02:43:29 +0800
Subject: [PATCH] org-src: Implement native inline src fontification
* lisp/org-src.el (org-fontify-inline-src-blocks,
org-fontify-inline-src-blocks-1): Create a function to search the buffer
up to a limit for inline src blocks. Light fontification is applied to
matched inline src blocks. When `org-src-fontify-natively' is
set, `org-src-font-lock-fontify-block' is applied to the content.
* lisp/org.el (org-set-font-lock-defaults): Add
`org-fontify-inline-src-blocks' to `org-font-lock-extra-keywords', which
is locally bound inside `org-set-font-lock-defaults'.
(org-inline-src-fontify-max-length): Create a variable to limit the
maximum length of an inline-src block fontified, to protect from lag
spikes (e.g. when typing out src_lang{ and half of the buffer is
fontified).
* lisp/org-faces.el: Introduce a new face `org-inline-src-block' which
inherits from `org-block' by default.
---
lisp/org-faces.el | 4 ++++
lisp/org-src.el | 46 ++++++++++++++++++++++++++++++++++++++++++++++
lisp/org.el | 18 ++++++++++++++++++
3 files changed, 68 insertions(+)
diff --git a/lisp/org-faces.el b/lisp/org-faces.el
index b151045a9..272762789 100644
--- a/lisp/org-faces.el
+++ b/lisp/org-faces.el
@@ -459,6 +459,10 @@ (defface org-block-end-line '((t (:inherit org-block-begin-line)))
"Face used for the line delimiting the end of source blocks."
:group 'org-faces)
+(defface org-inline-src-block '((t (:inherit org-block)))
+ "Face used for inline source blocks as a whole."
+ :group 'org-faces)
+
(defface org-verbatim '((t (:inherit (fixed-pitch shadow))))
"Face for fixed-with text like code snippets."
:group 'org-faces
diff --git a/lisp/org-src.el b/lisp/org-src.el
index 51dde602d..f2aff1f43 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -654,6 +654,52 @@ (defun org-src-font-lock-fontify-block (lang start end)
'(font-lock-fontified t fontified t font-lock-multiline t))
(set-buffer-modified-p modified)))))
+(defun org-fontify-inline-src-blocks (limit)
+ "Try to apply `org-fontify-inline-src-blocks-1'."
+ (condition-case nil
+ (progn
+ (org-fontify-inline-src-blocks-1 limit)
+ (org-fontify-inline-src-results limit))
+ (error (message "Org mode fontification error in %S at %d"
+ (current-buffer)
+ (line-number-at-pos)))))
+
+(defun org-fontify-inline-src-blocks-1 (limit)
+ "Fontify inline src_LANG blocks, from `point' up to LIMIT."
+ (let ((case-fold-search t)
+ (initial-point (point)))
+ (while (re-search-forward "\\_<src_\\([^ \t\n[{]+\\)[{[]?" limit t) ; copied from `org-element-inline-src-block-parser'
+ (let ((beg (match-beginning 0))
+ (lang-beg (match-beginning 1))
+ (lang-end (match-end 1))
+ pt)
+ (font-lock-append-text-property lang-beg lang-end 'face 'org-meta-line)
+ (font-lock-append-text-property beg lang-beg 'face 'shadow)
+ (font-lock-append-text-property beg lang-end 'face 'org-inline-src-block)
+ (setq pt (goto-char lang-end))
+ ;; `org-element--parse-paired-brackets' doesn't take a limit, so to
+ ;; prevent it searching the entire rest of the buffer we temporarily
+ ;; narrow the active region.
+ (save-restriction
+ (narrow-to-region beg (min (point-max)
+ limit
+ (+ lang-end org-inline-src-fontify-max-length)))
+ (when (ignore-errors (org-element--parse-paired-brackets ?\[))
+ (font-lock-append-text-property pt (point) 'face 'org-inline-src-block)
+ (setq pt (point)))
+ (when (ignore-errors (org-element--parse-paired-brackets ?\{))
+ (remove-text-properties pt (point) '(face nil))
+ (font-lock-append-text-property pt (1+ pt) 'face '(org-block shadow))
+ (unless (= (1+ pt) (1- (point)))
+ (if org-src-fontify-natively
+ (org-src-font-lock-fontify-block
+ (buffer-substring-no-properties lang-beg lang-end)
+ (1+ pt) (1- (point)))
+ (font-lock-append-text-property (1+ pt) (1- (point)) 'face 'org-inline-src-block)))
+ (font-lock-append-text-property (1- (point)) (point)'face '(org-inline-src-block shadow))
+ (setq pt (point)))))
+ t)))
+
\f
;;; Escape contents
diff --git a/lisp/org.el b/lisp/org.el
index 331bd9f65..fc2ec622f 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -5279,6 +5279,23 @@ (defcustom org-allow-promoting-top-level-subtree nil
:version "24.1"
:group 'org-appearance)
+(defcustom org-inline-src-fontify-max-length 200
+ "Maximum content length of an inline src block that will be fontified.
+This is only relevant when `org-src-fontify-natively' is t."
+ :type 'integer
+ :package-version '(Org . "9.5")
+ :group 'org-appearance
+ :group 'org-babel)
+
+(defcustom org-inline-src-prettify-results t
+ "Whether to use (ab)use prettify-symbols-mode on {{{results(...)}}}.
+Either t or a cons cell of strings which are used as substitutions
+for the start and end of inline results, respectively."
+ :type '(choice boolean (cons string string))
+ :package-version '(Org . "9.5")
+ :group 'org-appearance
+ :group 'org-babel)
+
(defun org-fontify-meta-lines-and-blocks (limit)
(condition-case nil
(org-fontify-meta-lines-and-blocks-1 limit)
@@ -5785,6 +5802,7 @@ (defun org-set-font-lock-defaults ()
'(9 'org-special-keyword t))
;; Blocks and meta lines
'(org-fontify-meta-lines-and-blocks)
+ '(org-fontify-inline-src-blocks)
;; Citations
'(org-cite-activate))))
(setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
--
2.33.1
^ permalink raw reply related [relevance 45%]
* Re: [PATCH] Fontification for inline src blocks
@ 2021-11-22 13:43 44% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-22 13:43 UTC (permalink / raw)
To: Ihor Radchenko; +Cc: org-mode-email
[-- Attachment #1: Type: text/plain, Size: 3725 bytes --]
Hi Ihor,
> I have some comments. See below.
Thanks for going through this, and for all your help previously.
I appreciate it :)
>> (org-inline-src-fontify-max-length): Create a variable to limit the
>> maximum length of an inline-src block fontified, to protect from lag
>> spikes (e.g. when typing out src_lang{ and half of the buffer is
>> fontified).
>
> I do not like this. Even with this variable, some part of buffer may be
> spuriously fontified. Maybe you just verify that you are at actual
> inline-src block by examining org-element-context?
The description may need updating, as that’s a tad inaccurate. That value
actually limits how far forwards a paired paren is searched for. There’s no
spurious fontification.
>> +(defun org-fontify-inline-src-blocks (limit)
>> + “Try to apply `org-fontify-inline-src-blocks-1’.”
>> + (condition-case nil
>> + (progn
>> + (org-fontify-inline-src-blocks-1 limit)
>> + (org-fontify-inline-src-results limit))
>
> org-fontify-inline-src-results is not defined in this patch.
Ah. Thanks for catching this!
>> + (font-lock-append-text-property lang-beg lang-end ’face ’org-meta-line)
>> + (font-lock-append-text-property beg lang-beg ’face ’shadow)
>> + (font-lock-append-text-property beg lang-end ’face ’org-inline-src-block)
>
> Is there some special reason why you apply both ’shadow and
> ’org-inline-src-block? What about ’org-meta-line face? Maybe
> ’org-meta-line should not be hard-coded?
I think there’s an argument for more faces that can be made because of element
like this, but it comes down to the idea that in a `src_lang[options]{content}'
construct `src_' is effectively visual noise, particularly once fortification
occurs and it’s obvious that it’s inline code even without it. So, I find it
nicest if it’s faded, which `shadow' does. Also applying `org-inline-src-block'
allows for a consistent background colour across the whole construct.
>> + (setq pt (goto-char lang-end))
>> + ;; `org-element–parse-paired-brackets’ doesn’t take a limit, so to
>> + ;; prevent it searching the entire rest of the buffer we temporarily
>> + ;; narrow the active region.
>> + (save-restriction
>> + (narrow-to-region beg (min (point-max)
>> + limit
>> + (+ lang-end org-inline-src-fontify-max-length)))
>> + (when (ignore-errors (org-element–parse-paired-brackets ?\[))
>> + (font-lock-append-text-property pt (point) ’face ’org-inline-src-block)
>> + (setq pt (point)))
>> + (when (ignore-errors (org-element–parse-paired-brackets ?\{))
>
> It looks like you are repeating the job of org-element-context here. Why
> don’t you just get the proper object?
IIRC `org-element-context' doesn’t separate out the `src_', `lang', `[options]', and
`{content}' of an inline source code block (which we want).
>> + (remove-text-properties pt (point) ’(face nil))
>> + (font-lock-append-text-property pt (1+ pt) ’face ’(org-block shadow))
>
> Do you really intend to use ’org-block and ’shadow faces here? Not
> ’org-inline-src-block?
Ah, that was an oversight. Thanks for catching that, changed to
`(org-inline-src-block shadow)'.
>> +(defcustom org-inline-src-prettify-results t
>
> Looks like a stray defcustom. At least, it is not mentioned in the
> commit message and not used within the patch.
Removed.
> Best,
> Ihor
Thanks again! See an updated patch attached.
All the best,
Timothy
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-src-Implement-native-inline-src-fontification.patch --]
[-- Type: text/x-patch, Size: 5474 bytes --]
From 08fff19eb6242339f9fe5549de0bc54520a3d603 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Tue, 13 Jul 2021 02:43:29 +0800
Subject: [PATCH] org-src: Implement native inline src fontification
* lisp/org-src.el (org-fontify-inline-src-blocks,
org-fontify-inline-src-blocks-1): Create a function to search the buffer
up to a limit for inline src blocks. Light fontification is applied to
matched inline src blocks. When `org-src-fontify-natively' is
set, `org-src-font-lock-fontify-block' is applied to the content.
* lisp/org.el (org-set-font-lock-defaults): Add
`org-fontify-inline-src-blocks' to `org-font-lock-extra-keywords', which
is locally bound inside `org-set-font-lock-defaults'.
(org-inline-src-fontify-max-length): Create a variable to limit the
maximum length of an inline-src block fontified, to protect from lag
spikes (e.g. when typing out src_lang{ and half of the buffer is
fontified).
* lisp/org-faces.el: Introduce a new face `org-inline-src-block' which
inherits from `org-block' by default.
---
lisp/org-faces.el | 4 ++++
lisp/org-src.el | 44 ++++++++++++++++++++++++++++++++++++++++++++
lisp/org.el | 9 +++++++++
3 files changed, 57 insertions(+)
diff --git a/lisp/org-faces.el b/lisp/org-faces.el
index b151045a9..272762789 100644
--- a/lisp/org-faces.el
+++ b/lisp/org-faces.el
@@ -459,6 +459,10 @@ (defface org-block-end-line '((t (:inherit org-block-begin-line)))
"Face used for the line delimiting the end of source blocks."
:group 'org-faces)
+(defface org-inline-src-block '((t (:inherit org-block)))
+ "Face used for inline source blocks as a whole."
+ :group 'org-faces)
+
(defface org-verbatim '((t (:inherit (fixed-pitch shadow))))
"Face for fixed-with text like code snippets."
:group 'org-faces
diff --git a/lisp/org-src.el b/lisp/org-src.el
index 51dde602d..fc9ddc27e 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -654,6 +654,50 @@ (defun org-src-font-lock-fontify-block (lang start end)
'(font-lock-fontified t fontified t font-lock-multiline t))
(set-buffer-modified-p modified)))))
+(defun org-fontify-inline-src-blocks (limit)
+ "Try to apply `org-fontify-inline-src-blocks-1'."
+ (condition-case nil
+ (org-fontify-inline-src-blocks-1 limit)
+ (error (message "Org mode fontification error in %S at %d"
+ (current-buffer)
+ (line-number-at-pos)))))
+
+(defun org-fontify-inline-src-blocks-1 (limit)
+ "Fontify inline src_LANG blocks, from `point' up to LIMIT."
+ (let ((case-fold-search t)
+ (initial-point (point)))
+ (while (re-search-forward "\\_<src_\\([^ \t\n[{]+\\)[{[]?" limit t) ; copied from `org-element-inline-src-block-parser'
+ (let ((beg (match-beginning 0))
+ (lang-beg (match-beginning 1))
+ (lang-end (match-end 1))
+ pt)
+ (font-lock-append-text-property lang-beg lang-end 'face 'org-meta-line)
+ (font-lock-append-text-property beg lang-beg 'face 'shadow)
+ (font-lock-append-text-property beg lang-end 'face 'org-inline-src-block)
+ (setq pt (goto-char lang-end))
+ ;; `org-element--parse-paired-brackets' doesn't take a limit, so to
+ ;; prevent it searching the entire rest of the buffer we temporarily
+ ;; narrow the active region.
+ (save-restriction
+ (narrow-to-region beg (min (point-max)
+ limit
+ (+ lang-end org-inline-src-fontify-max-length)))
+ (when (ignore-errors (org-element--parse-paired-brackets ?\[))
+ (font-lock-append-text-property pt (point) 'face 'org-inline-src-block)
+ (setq pt (point)))
+ (when (ignore-errors (org-element--parse-paired-brackets ?\{))
+ (remove-text-properties pt (point) '(face nil))
+ (font-lock-append-text-property pt (1+ pt) 'face '(org-inline-src-block shadow))
+ (unless (= (1+ pt) (1- (point)))
+ (if org-src-fontify-natively
+ (org-src-font-lock-fontify-block
+ (buffer-substring-no-properties lang-beg lang-end)
+ (1+ pt) (1- (point)))
+ (font-lock-append-text-property (1+ pt) (1- (point)) 'face 'org-inline-src-block)))
+ (font-lock-append-text-property (1- (point)) (point)'face '(org-inline-src-block shadow))
+ (setq pt (point)))))
+ t)))
+
\f
;;; Escape contents
diff --git a/lisp/org.el b/lisp/org.el
index 331bd9f65..637d2646c 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -5279,6 +5279,14 @@ (defcustom org-allow-promoting-top-level-subtree nil
:version "24.1"
:group 'org-appearance)
+(defcustom org-inline-src-fontify-max-length 200
+ "Maximum content length of an inline src block that will be fontified.
+This is only relevant when `org-src-fontify-natively' is t."
+ :type 'integer
+ :package-version '(Org . "9.5")
+ :group 'org-appearance
+ :group 'org-babel)
+
(defun org-fontify-meta-lines-and-blocks (limit)
(condition-case nil
(org-fontify-meta-lines-and-blocks-1 limit)
@@ -5785,6 +5793,7 @@ (defun org-set-font-lock-defaults ()
'(9 'org-special-keyword t))
;; Blocks and meta lines
'(org-fontify-meta-lines-and-blocks)
+ '(org-fontify-inline-src-blocks)
;; Citations
'(org-cite-activate))))
(setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
--
2.33.1
^ permalink raw reply related [relevance 44%]
* Re: [PATCH] Fontification for inline src blocks
@ 2021-11-22 14:37 93% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-22 14:37 UTC (permalink / raw)
To: Ihor Radchenko; +Cc: org-mode-email
[-- Attachment #1: Type: text/plain, Size: 1336 bytes --]
Hi Ihor,
>>>> org-inline-src-fontify-max-length
>> The description may need updating, as that’s a tad inaccurate. That value
>> actually limits how far forwards a paired paren is searched for. There’s no
>> spurious fontification.
>
> I can see the purpose. However, it still looks like overcomplication.
> org-element-context takes care about this issue simply by narrowing to
> current element (inline src block is an object and hence must end within
> current element).
Well, one simple change we could do is just replace
org-inline-src-fontify-max-length with the addition of `(save-excursion
(search-forward "\n" limit nil) (point))' to the restriction.
>>> It looks like you are repeating the job of org-element-context here. Why
>>> don’t you just get the proper object?
>>
>> IIRC `org-element-context’ doesn’t separate out the `src_’, `lang’, `[options]’, and
>> `{content}’ of an inline source code block (which we want).
>
> Sounds like an omission in org-element-context. At least, the parser
> could return :contents-begin and :contents-end. Then, you would also not
> need to re-implement the parser.
Perhaps. However frankly I don’t think it would do that much to reduce the
complexity, and what’s in this patch seems to work fairly nicely.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Fix regex for determining image width from attribute
@ 2021-11-22 17:54 93% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-22 17:54 UTC (permalink / raw)
To: Matt Huszagh; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 300 bytes --]
Hi Matt,
I’ve just pushed the change I described in 4514a32. This improves the
interpretation of :width attributes and makes a value of “t” work as discussed.
I have not prioritised #+attr_org for now, but that sounds like a change we
could make in the future.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] ob-shell-test, test-ob-shell and introduction
@ 2021-11-22 18:43 89% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-22 18:43 UTC (permalink / raw)
To: Matt; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 1301 bytes --]
Hi Matt,
> I’m interested in getting async into ob-shell.el. Since I’ve never contributed
>before, I figure it’d be good to start with a few easy tasks.
Fantastic! Great to hear from you, and I hope it goes well. Feel free to send
further emails for if you get stuck. If you intend to do more than just one or
two small tweaks (it sounds like you have bigger plans than that), you’ll need
what’s known as [FSF copyright assignment]. Have you done that yet?
> It looks to me like the stdin and cmdline header args aren’t documented anywhere
> (at least I couldn’t find anything). To make sure I’m using them correctly
> before making a patch for the manual, here are some tests.
Great! I’m not overly familiar with these, so I’ll leave other people to take a
look at the tests you’ve written, but your approach sounds good. 👍
> Please let me know if things look okay. It wasn’t clear to me how to send along
> a message with git send-email, so I formatted these patches and included them as
> an attachment. Is that fine?
Patches attached to an email are perfectly fine here.
Thanks for getting in touch, and I hope to see you around 🙂.
All the best,
Timothy
[FSF copyright assignment] <https://orgmode.org/contribute.html#copyright>
^ permalink raw reply [relevance 89%]
* Re: [PATCH] Fix regex for determining image width from attribute
@ 2021-11-23 5:14 93% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-23 5:14 UTC (permalink / raw)
To: Matt Huszagh; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 158 bytes --]
Hi Matt,
This issue and Kyle’s change were resolved in another thread, just FYI this is
fixed now. Thanks for mentioning it.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Fix regex for determining image width from attribute
@ 2021-11-23 5:39 93% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-23 5:39 UTC (permalink / raw)
To: Matt Huszagh; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 100 bytes --]
Hi Matt,
(sigh) well that’s silly. Thanks, I’ve just pushed that.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Fix regex for determining image width from attribute
@ 2021-11-29 5:13 93% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-29 5:13 UTC (permalink / raw)
To: Matt Huszagh; +Cc: Max Nikulin, emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 497 bytes --]
Hi Matt,
> I also still don’t really like the behavior
> here. I don’t think it makes sense to interpret a width as 120% if we
> have something like
>
> #+attr_latex: :width 1.2
What would be a more sensible interpretation in your mind? The “true” value
depends on the number of columns, and fetching that information seems a bit
unreasonable. Since this isn’t just used if nothing else if given, I see a 120%
interpretation as fairly reasonable.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [patch] fix ox-latex async export bug
@ 2021-11-29 5:15 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-11-29 5:15 UTC (permalink / raw)
To: Tim Cross; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 635 bytes --]
Hi Tim,
> I’m wondering if this could be a problem when exporting to latex if the
>underlying latex process encounters errors and is waiting for user input before
>it can continue (which happens if there are problems in the tex source latex is
>trying to process)?
>
> It might be worth checking next time you encounter an error if you can
> run latex on the generated *.tex file and see if it waits for user
> input?
If you look at the default value of `org-latex-pdf-process', it has the flag
`-interaction=nonstopmode' which should mean the process never pauses and waits
for user input.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Accept more :tangle-mode specification forms
2021-11-21 14:33 64% ` Timothy
@ 2021-11-29 18:57 91% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-11-29 18:57 UTC (permalink / raw)
To: Tim Cross; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 868 bytes --]
Hi Tim,
> I’ll let you (/the ML) know when I’ve taken a look at stiky bits, and whether I
> think I’m able to create something that works. My intuition is that if ls -l can
> properly represent sticky bits (and my rudimentary understanding is that it can)
> it should be fine for specifying them too. We’ll see.
I’ve gone away and had a look, then come back and had a think. This has resulted
in 9ce7802. Since we just have to worry about suid/sgid (as :tangle-mode is only
applied to the file produced [with the sticky bit being dir-specific]), not much
is required — we just allow the `s' in place of `x' for the user/group executable
flag.
This is pretty trivial as we’re still relying on `file-modes-symbolic-to-number'
for this, and in a very straight-forward way. So, I think we should be fine now 🙂.
All the best,
Timothy
^ permalink raw reply [relevance 91%]
* Re: [PATCH] Fontification for inline src blocks
@ 2021-11-29 19:21 49% ` Timothy
2021-11-30 11:44 93% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-29 19:21 UTC (permalink / raw)
To: Ihor Radchenko; +Cc: org-mode-email
[-- Attachment #1: Type: text/plain, Size: 923 bytes --]
Hi Ihor,
> That’s an option. Though you should also consider a paragraph ending at
> EOB. Searching for “” will fail with error then.
Don’t worry, that’s just a snippet. The full logic is as follows
┌────
│ (min limit (or (save-excursion (and (search-forward "\n" limit t 2) (point)))
│ (point-max)))
└────
>> [use org-element]
Ah right. We now also have the new thread about using org-element. I think that
sounds like a great change overall, but am still keen for this patch to go
through for the moment — just as a stop gap till org-element exposes all the
necessary information and there are some nice examples of using it for
fontification.
I’ve attached the latest version of the patch. At this point I consider it
fairly well-reviewed, so I’ll push it tomorrow if I don’t hear of any
last-minute issues.
All the best,
Timothy
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-src-Implement-native-inline-src-fontification.patch --]
[-- Type: text/x-patch, Size: 4781 bytes --]
From 690718bd2a31f9293572aa7a583a31a0615d18c8 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Tue, 13 Jul 2021 02:43:29 +0800
Subject: [PATCH] org-src: Implement native inline src fontification
* lisp/org-src.el (org-fontify-inline-src-blocks,
org-fontify-inline-src-blocks-1): Create a function to search the buffer
up to a limit for inline src blocks. Light fontification is applied to
matched inline src blocks. When `org-src-fontify-natively' is
set, `org-src-font-lock-fontify-block' is applied to the content.
* lisp/org.el (org-set-font-lock-defaults): Add
`org-fontify-inline-src-blocks' to `org-font-lock-extra-keywords', which
is locally bound inside `org-set-font-lock-defaults'.
* lisp/org-faces.el: Introduce a new face `org-inline-src-block' which
inherits from `org-block' by default.
---
lisp/org-faces.el | 4 ++++
lisp/org-src.el | 44 ++++++++++++++++++++++++++++++++++++++++++++
lisp/org.el | 1 +
3 files changed, 49 insertions(+)
diff --git a/lisp/org-faces.el b/lisp/org-faces.el
index b151045a9..272762789 100644
--- a/lisp/org-faces.el
+++ b/lisp/org-faces.el
@@ -459,6 +459,10 @@ (defface org-block-end-line '((t (:inherit org-block-begin-line)))
"Face used for the line delimiting the end of source blocks."
:group 'org-faces)
+(defface org-inline-src-block '((t (:inherit org-block)))
+ "Face used for inline source blocks as a whole."
+ :group 'org-faces)
+
(defface org-verbatim '((t (:inherit (fixed-pitch shadow))))
"Face for fixed-with text like code snippets."
:group 'org-faces
diff --git a/lisp/org-src.el b/lisp/org-src.el
index 51dde602d..639a447e8 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -654,6 +654,50 @@ (defun org-src-font-lock-fontify-block (lang start end)
'(font-lock-fontified t fontified t font-lock-multiline t))
(set-buffer-modified-p modified)))))
+(defun org-fontify-inline-src-blocks (limit)
+ "Try to apply `org-fontify-inline-src-blocks-1'."
+ (condition-case nil
+ (org-fontify-inline-src-blocks-1 limit)
+ (error (message "Org mode fontification error in %S at %d"
+ (current-buffer)
+ (line-number-at-pos)))))
+
+(defun org-fontify-inline-src-blocks-1 (limit)
+ "Fontify inline src_LANG blocks, from `point' up to LIMIT."
+ (let ((case-fold-search t)
+ (initial-point (point)))
+ (while (re-search-forward "\\_<src_\\([^ \t\n[{]+\\)[{[]?" limit t) ; copied from `org-element-inline-src-block-parser'
+ (let ((beg (match-beginning 0))
+ (lang-beg (match-beginning 1))
+ (lang-end (match-end 1))
+ pt)
+ (font-lock-append-text-property lang-beg lang-end 'face 'org-meta-line)
+ (font-lock-append-text-property beg lang-beg 'face 'shadow)
+ (font-lock-append-text-property beg lang-end 'face 'org-inline-src-block)
+ (setq pt (goto-char lang-end))
+ ;; `org-element--parse-paired-brackets' doesn't take a limit, so to
+ ;; prevent it searching the entire rest of the buffer we temporarily
+ ;; narrow the active region.
+ (save-restriction
+ (narrow-to-region beg (min limit (or (save-excursion (and (search-forward "\n" limit t 2) (point)))
+ (point-max))))
+ (message "buf: %S" (substring-no-properties (buffer-string)))
+ (when (ignore-errors (org-element--parse-paired-brackets ?\[))
+ (font-lock-append-text-property pt (point) 'face 'org-inline-src-block)
+ (setq pt (point)))
+ (when (ignore-errors (org-element--parse-paired-brackets ?\{))
+ (remove-text-properties pt (point) '(face nil))
+ (font-lock-append-text-property pt (1+ pt) 'face '(org-inline-src-block shadow))
+ (unless (= (1+ pt) (1- (point)))
+ (if org-src-fontify-natively
+ (org-src-font-lock-fontify-block
+ (buffer-substring-no-properties lang-beg lang-end)
+ (1+ pt) (1- (point)))
+ (font-lock-append-text-property (1+ pt) (1- (point)) 'face 'org-inline-src-block)))
+ (font-lock-append-text-property (1- (point)) (point)'face '(org-inline-src-block shadow))
+ (setq pt (point)))))
+ t)))
+
\f
;;; Escape contents
diff --git a/lisp/org.el b/lisp/org.el
index 025513e7a..ec59ddf44 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -5785,6 +5785,7 @@ (defun org-set-font-lock-defaults ()
'(9 'org-special-keyword t))
;; Blocks and meta lines
'(org-fontify-meta-lines-and-blocks)
+ '(org-fontify-inline-src-blocks)
;; Citations
'(org-cite-activate))))
(setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
--
2.33.1
^ permalink raw reply related [relevance 49%]
* Re: Subject: [PATCH] Fix DISPLAY error on exporting org with plantuml to html
@ 2021-11-30 4:18 90% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-11-30 4:18 UTC (permalink / raw)
To: Sun Lin; +Cc: Bastien Guerry, marcowahlsoft@gmail.com, emacs-orgmode@gnu.org
[-- Attachment #1: Type: text/plain, Size: 628 bytes --]
Hi Sun,
> Can any help merge the patch? Or should I request Bastien to merge the patch?
Thanks for not forgetting about this. As I commented before, I this looks
sensible enough to apply, and so I’ve just pushed it as f9dcc3d with a tweaked
commit message.
You’ve also been added to the list of TINYCHANGE contributors on
<https://orgmode.org/worg/contributors.html>. If you submit more than 20
cumulative lines of non-trivial code, you’ll need to get FSF assignment.
Thanks for sharing the issue and making a patch for it 🙂 hopefully we’ll see
you around in the future.
All the best,
Timothy
^ permalink raw reply [relevance 90%]
* Re: [PATCH] Fontification for inline src blocks
2021-11-29 19:21 49% ` Timothy
@ 2021-11-30 11:44 93% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2021-11-30 11:44 UTC (permalink / raw)
To: Ihor Radchenko; +Cc: org-mode-email
[-- Attachment #1: Type: text/plain, Size: 40 bytes --]
Pushed 🙂.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Fontification for inline src blocks
@ 2021-11-30 12:46 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-11-30 12:46 UTC (permalink / raw)
To: sebastien.miquel; +Cc: org-mode-email
[-- Attachment #1: Type: text/plain, Size: 252 bytes --]
Hi Sebastien,
> Sorry for the late reply, but isn’t there a `message' call leftover from
> debugging ?
Ooops! Time for a clean-up patch to fix the things you and Ihor’s just noticed.
Thanks for mentioning this.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Fix regex for determining image width from attribute
@ 2021-12-01 4:54 72% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-12-01 4:54 UTC (permalink / raw)
To: Matt Huszagh; +Cc: Max Nikulin, emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 4048 bytes --]
Hi Matt,
Thanks for your thoughtful deliberation on this.
> I think the essential disagreement is whether org should take an action if not
> explicitly told to do so. I think org should only perform some action if given
> a clear directive. In this context, I feel that org is guessing what the user
> wants and taking an action based on that guess.
I broadly agree with this, but I think this is provided by the four forms that
`org-image-actual-width' can take:
⁃ `t', in which case the actual image width is always used
⁃ an integer, in which case that will always be used as the width
⁃ `nil', which produces the guessing behaviour we’re discussing
⁃ `(val)', which guesses, falling back on `val'
> Ok, back to the fact that there are multiple considerations here. The
> first issue is whether specifying a width for a backend reflects an
> intention to have that same width in the org buffer. As I previously
> stated, I don’t agree that one implies the other. But, as also
> previously discussed, this was a decision that was made almost 10 years
> ago, so changing it would be a breaking change, etc. Because of that,
> I’m not totally sure what org should do, and I expect a lot of people
> won’t want to change this.
I’m not opposed to /expanding/ the behaviour (with due consideration), which could
resolve some of your concerns, but I don’t think it would be good to prevent the
current behaviour, which at this point seems well-established.
> The other consideration is if we take the first point as a given (that
> org should use width directives for other backends), should it also
> attempt to interpret directives that are ambiguous? In this case, do we
> interpret 1.2 as 1.2? If could only be ,
> I’d be inclined to agree that this is logical. I also understand the
> case for , though this is slightly less clear. But, what if
> someone used 1.2? Seems a bit unusual I know, but maybe
> someone would want this. Again, I don’t think we should guess if there’s
> a chance we could be wrong.
I feel this very much depends on how bad “guessing wrong” is, and as previously
discussed, since it’s rather easy to correct or set `org-image-actual-width' to
prevent this, I’m not sure it warrants being terribly concerned about.
> I totally agree with you that we don’t want to implement a pseudo latex
> parser here. But I feel like all this complexity is easily resolved by
> just requiring that people be explicit about their intentions (i.e.,
> specify #+attr_org: :width). That would avoid all the complex behavior
> and surprises that could result from making intelligent guesses about
> what the user wants.
I think prioritising `#+attr_org: :width' makes a lot of sense, but I feel quite
reluctant to /require/ it.
> Anyway, let me know what you want in terms of the patch. I still think
> prioritizing attr_org should be its own patch and changing the regex and
> all the other behavior should be a separate issue. But, if you’d like me
> to perform the change I mentioned in my last email, I can take the time
> to write that up and include it in the same patch.
Thanks for continuing with this. Moving forward, I think it would be best to:
⁃ Make a patch just for prioritising `#+attr_org'
⁃ Make a patch just improving the regex (before or after the `#+attr_org' patch)
⁃ Discuss changing the behaviour of image previews separately later / in another
thread, linking to this thread when doing so.
How does that sound?
Lastly, a comment on your documentation patch from earlier. I like the changes
to `org-image-actual-width', however I think you’ve been over-eager with scrapping
the current docstring for `org-display-inline-image--width'. Since the behaviour
is implemented there, I think it should at a minimum be documented there.
The docstring for a function referring to a variable’s documentation for how it’s
handled by the function seems a bit weird.
All the best,
Timothy
^ permalink raw reply [relevance 72%]
* Re: [PATCH] ob-shell-test, test-ob-shell and introduction
@ 2021-12-02 9:39 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-12-02 9:39 UTC (permalink / raw)
To: Matt; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 713 bytes --]
Hi Matt,
> > [FSF copyright assignment]. Have you done that yet?
>
> I just verified with my employer that my contract grants an exception for this
> project. Just emailed the request to assign@gnu.org. Also, got access from
> Bastien for worg. I figure it’s probably best to reserve any more changes ’til
> the paper work is done?
Great to hear that won’t get in the way of things 🙂. Feel free to develop and
share patches before the assignment is complete, we’ll just wait till it’s gone
through before merging (the process should just be a quick email or two and take
no more than a few days, just let us know if it’s dragging out).
Hope that helps.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Fix ob-plantuml over TRAMP
@ 2021-12-02 9:43 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-12-02 9:43 UTC (permalink / raw)
To: Guillaume Buisson (gbuisson); +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 904 bytes --]
Hi Guillaume,
Thanks for picking this up and submitting a patch.
Have you considered using `tramp-handle-file-exists-p'? I’m not overly familiar
with tramp or this sort of issue, but it looks like it could be exactly what we want.
> My Emacs Workflow involves connecting to a dev server over SSH so I can use my
> machines as thin clients and avoid synchronizing files, for that I do pretty
> much everything over TRAMP as I don’t want to lose the benefits of GUI Emacs.
> While everything mostly works impressively fine, I noticed writing my
> specifications that ob-plantuml fails to generate diagrams.
>
> It seems to me that there is a redundant check which uses `file-exists-p` even
> if using TRAMP, the plantuml jar is located on the remote server, so it fails
> with an error even though the command to actually render the diagrams works
> fine.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Faces for inline src blocks (was: [PATCH] Fontification for inline src blocks)
@ 2021-12-02 13:57 91% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-12-02 13:57 UTC (permalink / raw)
To: Eric S Fraga; +Cc: org-mode-email
[-- Attachment #1: Type: text/plain, Size: 980 bytes --]
Hi Eric,
> However, I am finding the result not very pleasing unfortunately.
> Specifically, I am not happy with the use of other pre-existing faces
> (shadow and org-meta-line) for this purpose. These other faces have
> their specific uses and the resulting combination for inline src blocks,
> for me, is ugly. I do not wish to modify those faces for this use case
> as it will affect their use everywhere else.
Yep, I’ve been quite open to the prospect of adding faces, just not entirely
sure how much / what I should do. Since I didn’t want that block this feature
we’ve currently got the one-face version.
The various elements that could have a face are:
⁃ the src_ prefix
⁃ the lang component
⁃ headers
⁃ the square brackets delimiting the headers / curly brackets delimiting the content
⁃ the content
⁃ the whole block (this is what currently has a dedicated face)
Let me know what your thoughts are.
All the best,
Timothy
^ permalink raw reply [relevance 91%]
* [PATCH] Re: Bug: org-agenda-sort-notime-is-late is not correctly handled by timestamp comparison [9.4.6 (9.4.6-12-gdcc3a8-elpaplus @ /Users/charlestam/.emacs.d/elpa/org-plus-contrib-20210830/)]
@ 2021-12-30 9:20 76% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2021-12-30 9:20 UTC (permalink / raw)
To: Charles Tam; +Cc: emacs-orgmode
[-- Attachment #1.1: Type: text/plain, Size: 393 bytes --]
Hi Charles,
Thanks for reporting this, and sorry it’s taken so long for someone to respond
(it’s been a busy year for the maintainers).
I’ve confirmed both your issue report (thanks for the detailed steps), and your
suggested fix. As such, I’ve produced a patch (attached). It would be good if
someone else could check this looks fine and apply it.
All the best,
Timothy
[-- Attachment #1.2: Type: text/html, Size: 26003 bytes --]
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-agenda-Make-timestamp-ordering-match-docs.patch --]
[-- Type: text/x-patch, Size: 1155 bytes --]
From 9ecd0a9266338d7222281c7ccb16808c319646aa Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Thu, 30 Dec 2021 17:16:29 +0800
Subject: [PATCH] org-agenda: Make timestamp ordering match docs
* lisp/org-agenda.el: When `org-agenda-sort-notime-is-late' is non-nil,
an absent timestamp is now (correctly) treated as maximal.
Bug reported by: Charles Tam <me@charlest.net> in
https://list.orgmode.org/CAKu+9YVNPSHG4HVtPFa2L2tP6yBdjJ38Teyr8OSDmCg0ZEOe9Q@mail.gmail.com
---
lisp/org-agenda.el | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lisp/org-agenda.el b/lisp/org-agenda.el
index 721ef2ced..4321a4f0b 100644
--- a/lisp/org-agenda.el
+++ b/lisp/org-agenda.el
@@ -7729,7 +7729,7 @@ (defsubst org-cmp-ts (a b type)
\"timestamp_ia\", compare within each of these type. When TYPE
is the empty string, compare all timestamps without respect of
their type."
- (let* ((def (and (not org-agenda-sort-notime-is-late) -1))
+ (let* ((def (if org-agenda-sort-notime-is-late 99999999 -1))
(ta (or (and (string-match type (or (get-text-property 1 'type a) ""))
(get-text-property 1 'ts-date a))
def))
--
2.34.1
^ permalink raw reply related [relevance 76%]
* Re: [PATCH] Add support for $…$ latex fragments followed by a dash
@ 2022-01-28 16:37 90% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2022-01-28 16:37 UTC (permalink / raw)
To: Max Nikulin; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 1240 bytes --]
Hi Max,
> There is no point to discuss deprecation of $…$ and ... while usage of
> such constructs is not discouraged by the org manual
Isn’t the point of discussing depreciation now that we’d change the manual etc.
to discourage it? 😛
FWIW, I have an extra 2c to add: I think that there’s a danger in having $…$
/almost/ behave like LaTeX, as the closer it is the more surprising the edge cases
are, and due to the nature of Org there will /always/ be a collection of edge
cases with $…$. By comparison, \(…\) has much less ’surprising’ behaviour.
>> If we do deprecate support for $…$, it might also be a good idea to
>> see if we can add a utility function which would make it easier for
>> people to migrate existing documents to the new/alternative syntax. For
>> the same reason it is hard to reliably parse $…$ syntax, we probably
>> can’t automate that transition, but we should be able to reduce the effort
>> required to update existing documents.
Tim, as mentioned before I’m strongly in favour of a ~half decade transition
including utility functions to shift existing TeX constructs to LaTeX, and
adding warnings, well before dropping support.
All the best,
Timothy
^ permalink raw reply [relevance 90%]
* [PATCH] Remote link localisation, i.e. exporting remote images
@ 2022-01-29 16:00 53% Timothy
2022-01-31 11:40 93% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2022-01-29 16:00 UTC (permalink / raw)
To: orgmode
[-- Attachment #1: Type: text/plain, Size: 583 bytes --]
Hi All,
Following Ihor’s recent org-persist improvements, I’ve prepared a patch making
use of it to allow for remote resource links to be transformed to local file
links during export.
This feature is implemented in ox.el, and made use of in ox-latex.el (for now)
— you can now link to https and tramp files and it should /just work/™.
Here’s a demo of what you can expect:
<file:/tmp/image-link-localisation-demo.png>
The patch is attached. If there aren’t any objections raised, I plan to merge it
shortly (in a day or two).
All the best,
Timothy
[-- Attachment #2: image-link-localisation-demo.png --]
[-- Type: image/png, Size: 133357 bytes --]
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0001-ox-Add-link-localisation-feature-using-persist.patch --]
[-- Type: text/x-patch, Size: 4277 bytes --]
From 721db5de07ca667acde997c621cf62b557f9dc3b Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sat, 29 Jan 2022 23:49:52 +0800
Subject: [PATCH] ox: Add link localisation feature using persist
* lisp/ox.el (org-export-link-remote-p): A new function to determine
whether a link refers to a remote resource.
(org-export-link--remote-local-copy): Download and return the local
location of a remote resource link.
(org-export-link-localise): Transform remote links to refer to a local
copy of the resource.
* lisp/ox-latex.el (org-latex-link, org-latex-inline-image-rules): Make
use of the new functions for remote resources in ox.el to support
embedding https and tramp -linked files.
---
lisp/ox-latex.el | 9 ++++++---
lisp/ox.el | 41 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 47 insertions(+), 3 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 58252c6b8..5dda9b3ab 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -755,8 +755,11 @@ (defcustom org-latex-default-figure-position "htbp"
(defcustom org-latex-inline-image-rules
`(("file" . ,(rx "."
- (or "pdf" "jpeg" "jpg" "png" "ps" "eps" "tikz" "pgf" "svg")
- eos)))
+ (or "pdf" "jpeg" "jpg" "png" "ps" "eps" "tikz" "pgf" "svg")
+ eos))
+ ("https" . ,(rx "."
+ (or "jpeg" "jpg" "png" "ps" "eps" "tikz" "pgf" "svg")
+ eos)))
"Rules characterizing image files that can be inlined into LaTeX.
A rule consists in an association whose key is the type of link
@@ -2588,7 +2591,7 @@ (defun org-latex-link (link desc info)
;; Link type is handled by a special function.
((org-export-custom-protocol-maybe link desc 'latex info))
;; Image file.
- (imagep (org-latex--inline-image link info))
+ (imagep (org-latex--inline-image (org-export-link-localise link) info))
;; Radio link: Transcode target's contents and use them as link's
;; description.
((string= type "radio")
diff --git a/lisp/ox.el b/lisp/ox.el
index 831b3bf12..a258ed57f 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -4446,6 +4446,47 @@ (defun org-export-file-uri (filename)
(concat (if (string-prefix-p "/" fullname) "file://" "file:///")
fullname)))))
+(defun org-export-link-remote-p (link)
+ "Returns non-nil if the link refers to a remote resource."
+ (or (member (org-element-property :type link) '("http" "https" "ftp"))
+ (and (string= (org-element-property :type link) "file")
+ (file-remote-p (org-element-property :path link)))))
+
+(defun org-export-link--remote-local-copy (link)
+ "Download the remote resource specified by LINK, and return its local path."
+ ;; TODO work this into ol.el as a link parameter, say :download.
+ (let* ((location-type
+ (pcase (org-element-property :type link)
+ ((or "http" "https" "ftp") 'url)
+ ((and "file" (guard (file-remote-p
+ (org-element-property :path link))))
+ 'file)
+ (_ (error "Cannot copy %s:%s to a local file"
+ (org-element-property :type link)
+ (org-element-property :path link)))))
+ (path
+ (pcase location-type
+ ('url
+ (concat (org-element-property :type link)
+ ":" (org-element-property :path link)))
+ ('file
+ (org-element-property :path link)))))
+ (or (org-persist-read location-type path)
+ (org-persist-register location-type path
+ :write-immediately t))))
+
+(defun org-export-link-localise (link)
+ "If LINK refers to a remote resource, modify it to point to a local downloaded copy."
+ (when (org-export-link-remote-p link)
+ (let* ((local-path (org-export-link--remote-local-copy link)))
+ (setcdr link
+ (thread-first (cadr link)
+ (plist-put :type "file")
+ (plist-put :path local-path)
+ (plist-put :raw-link (concat "file:" local-path))
+ list))))
+ link)
+
;;;; For References
;;
;; `org-export-get-reference' associate a unique reference for any
--
2.34.1
^ permalink raw reply related [relevance 53%]
* Re: [PATCH] Remote link localisation, i.e. exporting remote images
2022-01-29 16:00 53% [PATCH] Remote link localisation, i.e. exporting remote images Timothy
@ 2022-01-31 11:40 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2022-01-31 11:40 UTC (permalink / raw)
To: orgmode
[-- Attachment #1: Type: text/plain, Size: 39 bytes --]
I’ve just pushed this as 6ee4551 :)
^ permalink raw reply [relevance 93%]
* [PATCH] New LaTeX code export option: engraved
@ 2022-05-04 15:59 12% Timothy
` (2 more replies)
0 siblings, 3 replies; 74+ results
From: Timothy @ 2022-05-04 15:59 UTC (permalink / raw)
To: emacs-orgmode, Daniel Fleischer, Nicolas Goaziou
[-- Attachment #1.1: Type: text/plain, Size: 3432 bytes --]
Hi All,
I’ve been fairly busy as of late (hence my recent silence on this ML), however I
have a patchset that’s been in the works for a while that I’ve finally polished
up.
Short version: It adds a new (superior) option for exporting code blocks with
syntax highlighting to LaTeX.
The long version follows.
Currently there are four options for exporting code to LaTeX (ordered by
complexity/quality).
1. Verbatim, which simply includes the code unstyled
2. Custom, which puts the code in an enviroment of your choice
3. Listings, which uses the LaTeX package by this name. This is quick, but
exceedingly basic.
4. Minted, which uses the LaTeX package by this name. This is slow, but produces
better results than Listings
This patchset accomplishes two things:
1. It refactors the overly large `org-latex-src-block' function, and makes a few
other improvements to pre-existing code
2. It adds a new option for exporting code, named (you guessed it!) “engraved”
What is this new option, and why do we want it?
About a year ago I started work on a package that generalises the functionality
of `htmlize.el', termed `engrave-faces'
(<https://elpa.gnu.org/packages/engrave-faces.html>). It provides the ability to
extract font-lock information and export it to a number of formats: html, ansi,
and (crucially) LaTeX! Since the LaTeX export is built on the `fvextra' (LaTeX)
package (like pygments), the vast majority of the Minted options you’re used to
just carry over.
This allows for a result that is, I think, straight up better than all the
pre-existing options. For starters, you can now apply syntax highlighting to any
language you have a major mode for.
There are a number of optimisations unattempted, but it already significantly
outperforms Minted. Here are some timings from my `config.org':
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
LaTeX code backend Compile time Overhead Overhead ratio
────────────────────────────────────────────────────────────
verbatim 12 s 0 0.0
lstlistings 15 s 3 s 0.2
Engrave 34 s 22 s 1.8
Pygments (Minted) 184 s 172 s 14.3
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Compared to Minted, we also no longer have to install `pygments' or pass the
`--shelll-escape' flag to LaTeX to generate passable code blocks.
Having an all-emacs system also allows for some nifty things, like having entire
documents/presentations based on your Emacs theme (see
<https://github.com/tecosaur/ox-chameleon>). Demo images: <https://0x0.st/oAl1.png>,
<https://0x0.st/oAle.png>.
Here are some more screenshots to see what the result can look like in practice:
• <https://0x0.st/oAl2.png>
• <https://0x0.st/oAl_.png>
• <https://0x0.st/oAlL.png>
So, please take a look at the patches, give this a whirl, and let me know what
you think! 😀
All the best,
Timothy
[-- Attachment #1.2: Type: text/html, Size: 14197 bytes --]
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ox-latex-Refactor-org-latex-src-block.patch --]
[-- Type: text/x-patch, Size: 15943 bytes --]
From 87872cc8f2fb1da1a03dc4aadfbd4af6541d8c13 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 21 Nov 2021 14:35:34 +0800
Subject: [PATCH 1/4] ox-latex: Refactor `org-latex-src-block'
* lisp/ox-latex.el (org-latex-src-block): Extract the per-format logic
from `org-latex-src-block' into new dedicated functions:
+ `org-latex-src-block--verbatim'
+ `org-latex-src-block--custom'
+ `org-latex-src-block--minted'
+ `org-latex-src-block--listings'
This makes `org-latex-src-block' much less monolithic, taking it from
175 lines to 30, and I find also makes it easier to understand.
---
lisp/ox-latex.el | 339 ++++++++++++++++++++++++++---------------------
1 file changed, 185 insertions(+), 154 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 841ad48bc..63855d2f6 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2997,164 +2997,195 @@ (defun org-latex-src-block (src-block _contents info)
(float (plist-get attributes :float))
(listings (plist-get info :latex-listings)))
(cond
- ;; Case 1. No source fontification.
((or (not lang) (not listings))
- (let ((caption-str (org-latex--caption/label-string src-block info))
- (verbatim (format "\\begin{verbatim}\n%s\\end{verbatim}"
- (org-export-format-code-default src-block info))))
- (cond ((string= "multicolumn" float)
- (format "\\begin{figure*}[%s]\n%s%s\n%s\\end{figure*}"
- (plist-get info :latex-default-figure-position)
- (if caption-above-p caption-str "")
- verbatim
- (if caption-above-p "" caption-str)))
- (caption (concat
- (if caption-above-p caption-str "")
- verbatim
- (if caption-above-p "" (concat "\n" caption-str))))
- (t verbatim))))
- ;; Case 2. Custom environment.
+ (org-latex-src-block--verbatim src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
(custom-env
- (let ((caption-str (org-latex--caption/label-string src-block info))
- (formatted-src (org-export-format-code-default src-block info)))
- (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
- (format "\\begin{%s}\n%s\\end{%s}\n"
- custom-env
- (concat (and caption-above-p caption-str)
- formatted-src
- (and (not caption-above-p) caption-str))
- custom-env)
- (format-spec custom-env
- `((?s . ,formatted-src)
- (?c . ,caption)
- (?f . ,float)
- (?l . ,(org-latex--label src-block info))
- (?o . ,(or (plist-get attributes :options) "")))))))
- ;; Case 3. Use minted package.
+ (org-latex-src-block--custom src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float custom-env))
((eq listings 'minted)
- (let* ((caption-str (org-latex--caption/label-string src-block info))
- (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
- (plist-get info :latex-default-figure-position)))
- (float-env
- (cond
- ((string= "multicolumn" float)
- (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- (caption
- (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- ((string= "t" float)
- (concat (format "\\begin{listing}[%s]\n"
- placement)
- "%s\n\\end{listing}"))
- (t "%s")))
- (options (plist-get info :latex-minted-options))
- (body
- (format
- "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
- ;; Options.
- (concat
- (org-latex--make-option-string
- (if (or (not num-start) (assoc "linenos" options))
- options
- (append
- `(("linenos")
- ("firstnumber" ,(number-to-string (1+ num-start))))
- options)))
- (let ((local-options (plist-get attributes :options)))
- (and local-options (concat "," local-options))))
- ;; Language.
- (or (cadr (assq (intern lang)
- (plist-get info :latex-minted-langs)))
- (downcase lang))
- ;; Source code.
- (let* ((code-info (org-export-unravel-code src-block))
- (max-width
- (apply 'max
- (mapcar 'length
- (org-split-string (car code-info)
- "\n")))))
- (org-export-format-code
- (car code-info)
- (lambda (loc _num ref)
- (concat
- loc
- (when ref
- ;; Ensure references are flushed to the right,
- ;; separated with 6 spaces from the widest line
- ;; of code.
- (concat (make-string (+ (- max-width (length loc)) 6)
- ?\s)
- (format "(%s)" ref)))))
- nil (and retain-labels (cdr code-info)))))))
- ;; Return value.
- (format float-env body)))
- ;; Case 4. Use listings package.
+ (org-latex-src-block--minted src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
(t
- (let ((lst-lang
- (or (cadr (assq (intern lang)
- (plist-get info :latex-listings-langs)))
- lang))
- (caption-str
- (when caption
- (let ((main (org-export-get-caption src-block))
- (secondary (org-export-get-caption src-block t)))
- (if (not secondary)
- (format "{%s}" (org-export-data main info))
- (format "{[%s]%s}"
- (org-export-data secondary info)
- (org-export-data main info))))))
- (lst-opt (plist-get info :latex-listings-options)))
- (concat
- ;; Options.
- (format
- "\\lstset{%s}\n"
- (concat
- (org-latex--make-option-string
- (append
- lst-opt
- (cond
- ((and (not float) (plist-member attributes :float)) nil)
- ((string= "multicolumn" float) '(("float" "*")))
- ((and float (not (assoc "float" lst-opt)))
- `(("float" ,(plist-get info :latex-default-figure-position)))))
- `(("language" ,lst-lang))
- (if label
- `(("label" ,(org-latex--label src-block info)))
- '(("label" " ")))
- (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
- `(("captionpos" ,(if caption-above-p "t" "b")))
- (cond ((assoc "numbers" lst-opt) nil)
- ((not num-start) '(("numbers" "none")))
- (t `(("firstnumber" ,(number-to-string (1+ num-start)))
- ("numbers" "left"))))))
- (let ((local-options (plist-get attributes :options)))
- (and local-options (concat "," local-options)))))
- ;; Source code.
- (format
- "\\begin{lstlisting}\n%s\\end{lstlisting}"
- (let* ((code-info (org-export-unravel-code src-block))
- (max-width
- (apply 'max
- (mapcar 'length
- (org-split-string (car code-info) "\n")))))
- (org-export-format-code
- (car code-info)
- (lambda (loc _num ref)
- (concat
- loc
- (when ref
- ;; Ensure references are flushed to the right,
- ;; separated with 6 spaces from the widest line of
- ;; code
- (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
- (format "(%s)" ref)))))
- nil (and retain-labels (cdr code-info))))))))))))
-
+ (org-latex-src-block--listings src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))))))
+
+(defun org-latex-src-block--verbatim
+ (src-block info _lang caption caption-above-p _label
+ _num-start _retain-labels _attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using verbatim.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((caption-str (org-latex--caption/label-string src-block info))
+ (verbatim (format "\\begin{verbatim}\n%s\\end{verbatim}"
+ (org-export-format-code-default src-block info))))
+ (cond ((string= "multicolumn" float)
+ (format "\\begin{figure*}[%s]\n%s%s\n%s\\end{figure*}"
+ (plist-get info :latex-default-figure-position)
+ (if caption-above-p caption-str "")
+ verbatim
+ (if caption-above-p "" caption-str)))
+ (caption (concat
+ (if caption-above-p caption-str "")
+ verbatim
+ (if caption-above-p "" (concat "\n" caption-str))))
+ (t verbatim))))
+
+(defun org-latex-src-block--custom
+ (src-block info _lang caption caption-above-p _label
+ _num-start _retain-labels attributes float custom-env)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using a custom environment.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((caption-str (org-latex--caption/label-string src-block info))
+ (formatted-src (org-export-format-code-default src-block info)))
+ (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
+ (format "\\begin{%s}\n%s\\end{%s}\n"
+ custom-env
+ (concat (and caption-above-p caption-str)
+ formatted-src
+ (and (not caption-above-p) caption-str))
+ custom-env)
+ (format-spec custom-env
+ `((?s . ,formatted-src)
+ (?c . ,caption)
+ (?f . ,float)
+ (?l . ,(org-latex--label src-block info))
+ (?o . ,(or (plist-get attributes :options) "")))))))
+
+(defun org-latex-src-block--minted
+ (src-block info _lang caption caption-above-p _label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using minted.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let* ((caption-str (org-latex--caption/label-string src-block info))
+ (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
+ (plist-get info :latex-default-figure-position)))
+ (float-env
+ (cond
+ ((string= "multicolumn" float)
+ (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ (caption
+ (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ ((string= "t" float)
+ (concat (format "\\begin{listing}[%s]\n"
+ placement)
+ "%s\n\\end{listing}"))
+ (t "%s")))
+ (options (plist-get info :latex-minted-options))
+ (body
+ (format
+ "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
+ ;; Options.
+ (concat
+ (org-latex--make-option-string
+ (if (or (not num-start) (assoc "linenos" options))
+ options
+ (append
+ `(("linenos")
+ ("firstnumber" ,(number-to-string (1+ num-start))))
+ options)))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (concat "," local-options))))
+ ;; Language.
+ (or (cadr (assq (intern lang)
+ (plist-get info :latex-minted-langs)))
+ (downcase lang))
+ ;; Source code.
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info)
+ "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line
+ ;; of code.
+ (concat (make-string (+ (- max-width (length loc)) 6)
+ ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info)))))))
+ ;; Return value.
+ (format float-env body)))
+
+(defun org-latex-src-block--listings
+ (src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using listings.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((lst-lang
+ (or (cadr (assq (intern lang)
+ (plist-get info :latex-listings-langs)))
+ lang))
+ (caption-str
+ (when caption
+ (let ((main (org-export-get-caption src-block))
+ (secondary (org-export-get-caption src-block t)))
+ (if (not secondary)
+ (format "{%s}" (org-export-data main info))
+ (format "{[%s]%s}"
+ (org-export-data secondary info)
+ (org-export-data main info))))))
+ (lst-opt (plist-get info :latex-listings-options)))
+ (concat
+ ;; Options.
+ (format
+ "\\lstset{%s}\n"
+ (concat
+ (org-latex--make-option-string
+ (append
+ lst-opt
+ (cond
+ ((and (not float) (plist-member attributes :float)) nil)
+ ((string= "multicolumn" float) '(("float" "*")))
+ ((and float (not (assoc "float" lst-opt)))
+ `(("float" ,(plist-get info :latex-default-figure-position)))))
+ `(("language" ,lst-lang))
+ (if label
+ `(("label" ,(org-latex--label src-block info)))
+ '(("label" " ")))
+ (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
+ `(("captionpos" ,(if caption-above-p "t" "b")))
+ (cond ((assoc "numbers" lst-opt) nil)
+ ((not num-start) '(("numbers" "none")))
+ (t `(("firstnumber" ,(number-to-string (1+ num-start)))
+ ("numbers" "left"))))))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (concat "," local-options)))))
+ ;; Source code.
+ (format
+ "\\begin{lstlisting}\n%s\\end{lstlisting}"
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info) "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line of
+ ;; code
+ (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info))))))))
;;;; Statistics Cookie
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-ox-latex-Refactor-org-latex-inline-src-block.patch --]
[-- Type: text/x-patch, Size: 3727 bytes --]
From a4d833418c3e2f0a8f365b92c01c091f75b6482d Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Wed, 4 May 2022 18:53:10 +0800
Subject: [PATCH 2/4] ox-latex: Refactor `org-latex-inline-src-block'
* lisp/ox-latex.el (org-latex-inline-src-block,
org-latex-inline-src-block--minted,
org-latex-inline-src-block--listings): Extract the minted and listings
specific logic out of `org-latex-inline-src-block` into the new
functions `org-latex-inline-src-block--minted` and
`org-latex-inline-src-block--listings`.
---
lisp/ox-latex.el | 58 +++++++++++++++++++++++-------------------------
1 file changed, 28 insertions(+), 30 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 63855d2f6..ed66a51c0 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2127,36 +2127,34 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
"Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
CONTENTS holds the contents of the item. INFO is a plist holding
contextual information."
- (let* ((code (org-element-property :value inline-src-block))
- (separator (org-latex--find-verb-separator code)))
- (cl-case (plist-get info :latex-listings)
- ;; Do not use a special package: transcode it verbatim, as code.
- ((nil) (org-latex--text-markup code 'code info))
- ;; Use minted package.
- (minted
- (let* ((org-lang (org-element-property :language inline-src-block))
- (mint-lang (or (cadr (assq (intern org-lang)
- (plist-get info :latex-minted-langs)))
- (downcase org-lang)))
- (options (org-latex--make-option-string
- (plist-get info :latex-minted-options))))
- (format "\\mintinline%s{%s}{%s}"
- (if (string= options "") "" (format "[%s]" options))
- mint-lang
- code)))
- ;; Use listings package.
- (otherwise
- ;; Maybe translate language's name.
- (let* ((org-lang (org-element-property :language inline-src-block))
- (lst-lang (or (cadr (assq (intern org-lang)
- (plist-get info :latex-listings-langs)))
- org-lang))
- (options (org-latex--make-option-string
- (append (plist-get info :latex-listings-options)
- `(("language" ,lst-lang))))))
- (concat (format "\\lstinline[%s]" options)
- separator code separator))))))
-
+ (let ((code (org-element-property :value inline-src-block))
+ (lang (org-element-property :language inline-src-block)))
+ (pcase (plist-get info :latex-listings)
+ ('nil (org-latex--text-markup code 'code info))
+ ('minted (org-latex-inline-src-block--minted info code lang))
+ (_ (org-latex-inline-src-block--listings info code lang)))))
+
+(defun org-latex-inline-src-block--minted (info code lang)
+ (let ((mint-lang (or (cadr (assq (intern lang)
+ (plist-get info :latex-minted-langs)))
+ (downcase org-lang)))
+ (options (org-latex--make-option-string
+ (plist-get info :latex-minted-options))))
+ (format "\\mintinline%s{%s}{%s}"
+ (if (string= options "") "" (format "[%s]" options))
+ mint-lang
+ code)))
+
+(defun org-latex-inline-src-block--listings (info code lang)
+ (let* ((lst-lang (or (cadr (assq (intern lang)
+ (plist-get info :latex-listings-langs)))
+ org-lang))
+ (separator (org-latex--find-verb-separator code))
+ (options (org-latex--make-option-string
+ (append (plist-get info :latex-listings-options)
+ `(("language" ,lst-lang))))))
+ (concat (format "\\lstinline[%s]" options)
+ separator code separator)))
;;;; Inlinetask
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: 0003-ox-latex-More-versitile-option-construction.patch --]
[-- Type: text/x-patch, Size: 4116 bytes --]
From b322e67cc8b88826be26ee8ae155467aa764e743 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Wed, 4 May 2022 23:31:59 +0800
Subject: [PATCH 3/4] ox-latex: More versitile option construction
* lisp/ox-latex.el (org-latex--make-option-string): Support a custom
option seperator string.
(org-latex--make-option-string, org-latex-minted-options,
org-latex-listings-options): The first line of the docstrings for
`org-latex-minted-options` and `org-latex-listings-options` describe the
variables as "association lists", yet `org-latex--make-option-string`
does not handle association lists and an example of two-value lists is
provided. To make the behaviour match the docstring,
`org-latex--make-option-string` is modified to work with either a list
two-value lists or an association list, and the examples in
`org-latex-minted-options` and `org-latex-listings-options` updated
accordingly.
---
lisp/ox-latex.el | 38 ++++++++++++++++++++++++--------------
1 file changed, 24 insertions(+), 14 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index ed66a51c0..6a29efcba 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1006,12 +1006,16 @@ (defcustom org-latex-listings-options nil
These options are supplied as a comma-separated list to the
\\lstset command. Each element of the association list should be
-a list containing two strings: the name of the option, and the
-value. For example,
+a list or cons cell containing two strings: the name of the
+option, and the value. For example,
(setq org-latex-listings-options
\\='((\"basicstyle\" \"\\\\small\")
(\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
+ ; or
+ (setq org-latex-listings-options
+ \\='((\"basicstyle\" . \"\\\\small\")
+ (\"keywordstyle\" . \"\\\\color{black}\\\\bfseries\\\\underbar\")))
will typeset the code in a small size font with underlined, bold
black keywords.
@@ -1059,11 +1063,14 @@ (defcustom org-latex-minted-options nil
These options are supplied within square brackets in
\\begin{minted} environments. Each element of the alist should
-be a list containing two strings: the name of the option, and the
-value. For example,
+be a list or cons cell containing two strings: the name of the
+option, and the value. For example,
(setq org-latex-minted-options
\\='((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
+ ; or
+ (setq org-latex-minted-options
+ \\='((\"bgcolor\" . \"bg\") (\"frame\" . \"lines\")))
will result in source blocks being exported with
@@ -1506,21 +1513,24 @@ (defun org-latex--find-verb-separator (s)
when (not (string-match (regexp-quote (char-to-string c)) s))
return (char-to-string c))))
-(defun org-latex--make-option-string (options)
+(defun org-latex--make-option-string (options &optional seperator)
"Return a comma separated string of keywords and values.
OPTIONS is an alist where the key is the options keyword as
a string, and the value a list containing the keyword value, or
nil."
(mapconcat (lambda (pair)
- (pcase-let ((`(,keyword ,value) pair))
- (concat keyword
- (and (> (length value) 0)
- (concat "="
- (if (string-match-p (rx (any "[]")) value)
- (format "{%s}" value)
- value))))))
- options
- ","))
+ (let ((keyword (car pair))
+ (value (pcase (cdr pair)
+ ((pred stringp) (cdr pair))
+ ((pred consp) (cadr pair)))))
+ (concat keyword
+ (when value
+ (concat "="
+ (if (string-match-p (rx (any "[]")) value)
+ (format "{%s}" value)
+ value))))))
+ options
+ (or seperator ",")))
(defun org-latex--wrap-label (element output info)
"Wrap label associated to ELEMENT around OUTPUT, if appropriate.
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #5: 0004-ox-latex-Introduce-engraved-code-highlighting.patch --]
[-- Type: text/x-patch, Size: 15169 bytes --]
From e0310e21e260a930e06d704ec8893ab1e75518aa Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 21 Nov 2021 20:04:12 +0800
Subject: [PATCH 4/4] ox-latex: Introduce "engraved" code highlighting
* lisp/ox-latex.el (org-latex-src-block, org-latex-src-block--engraved,
org-latex-inline-src-block, org-latex-inline-src-block--engraved,
org-latex-template, org-latex-listings): Make use of the engraved-faces
package (available on ELPA) to provide an alternative LaTeX code
highlighting backend which functions similarly to htmlize.el for HTML
exports.
(org-latex-engraved-preamble, org-latex-engraved-options): Introduce
variables to construct the preamble for engraved code blocks.
* lisp/ox-beamer.el (org-beamer-template): Modify to add engrave-faces
preamble when applicable.
---
lisp/ox-beamer.el | 9 ++
lisp/ox-latex.el | 235 +++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 241 insertions(+), 3 deletions(-)
diff --git a/lisp/ox-beamer.el b/lisp/ox-beamer.el
index 6be73c91e..ca1aeafe4 100644
--- a/lisp/ox-beamer.el
+++ b/lisp/ox-beamer.el
@@ -857,6 +857,15 @@ (defun org-beamer-template (contents info)
(let ((template (plist-get info :latex-hyperref-template)))
(and (stringp template)
(format-spec template (org-latex--format-spec info))))
+ ;; engrave-faces-latex preamble
+ (let ((engraved-p (eq org-latex-listings 'engraved))
+ (src-p (org-element-map (plist-get info :parse-tree)
+ '(src-block inline-src-block) #'identity))
+ (fixedw-p
+ (org-element-map (plist-get info :parse-tree)
+ '(example-block fixed-width) #'identity)))
+ (when (or src-p fixedw-p)
+ (org-latex-generate-engraved-preamble info src-p)))
;; Document start.
"\\begin{document}\n\n"
;; Title command.
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 6a29efcba..4cc82f6e7 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -37,6 +37,8 @@ (defvar org-latex-default-packages-alist)
(defvar org-latex-packages-alist)
(defvar orgtbl-exp-regexp)
+(declare-function engrave-faces-latex-gen-preamble "ext:engrave-faces-latex")
+(declare-function engrave-faces-latex-buffer "ext:engrave-faces-latex")
\f
;;; Define Back-End
@@ -125,6 +127,8 @@ (org-export-define-backend 'latex
(:latex-default-quote-environment nil nil org-latex-default-quote-environment)
(:latex-default-table-mode nil nil org-latex-default-table-mode)
(:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format)
+ (:latex-engraved-options nil nil org-latex-engraved-options)
+ (:latex-engraved-preamble nil nil org-latex-engraved-preamble)
(:latex-footnote-defined-format nil nil org-latex-footnote-defined-format)
(:latex-footnote-separator nil nil org-latex-footnote-separator)
(:latex-format-drawer-function nil nil org-latex-format-drawer-function)
@@ -946,7 +950,24 @@ (defcustom org-latex-listings nil
(add-to-list \\='org-latex-packages-alist \\='(\"\" \"listings\"))
(add-to-list \\='org-latex-packages-alist \\='(\"\" \"color\"))
-Alternatively,
+There are two fancier options for fontification.
+
+The first fancy alternative,
+
+ (setq org-latex-listings \\='engraved)
+
+causes source code to be run through
+`engrave-faces-latex-buffer', which generates colorings using
+Emacs' font-lock information. This requires the engrave-faces
+package (availible from ELPA), and the fvextra LaTeX package be
+installed.
+
+The styling of the engraved result can customised with
+`org-latex-engraved-preamble' and `org-latex-engraved-options'.
+The default preamble also uses the tcolorbox LaTeX package in
+addition to fvextra.
+
+The second fancy alternative,
(setq org-latex-listings \\='minted)
@@ -971,8 +992,9 @@ (defcustom org-latex-listings nil
:type '(choice
(const :tag "Use listings" t)
(const :tag "Use minted" minted)
+ (const :tag "Use engrave-faces-latex" engraved)
(const :tag "Export verbatim" nil))
- :safe (lambda (s) (memq s '(t nil minted))))
+ :safe (lambda (s) (memq s '(t nil minted engraved))))
(defcustom org-latex-listings-langs
'((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
@@ -1142,6 +1164,109 @@ (defcustom org-latex-custom-lang-environments nil
:version "26.1"
:package-version '(Org . "9.0"))
+(defcustom org-latex-engraved-preamble
+ "\\usepackage{fvextra}
+
+[FVEXTRA-SETUP]
+
+\\renewcommand\\theFancyVerbLine{\\footnotesize\\color{black!40!white}\\arabic{FancyVerbLine}}
+
+\\providecolor{codebackground}{HTML}{f7f7f7}
+\\providecolor{codeborder}{HTML}{f0f0f0}
+\\providecolor{EFD}{HTML}{28292e}
+
+% TODO have code boxes keep line vertical alignment
+\\usepackage[breakable,xparse]{tcolorbox}
+\\DeclareTColorBox[]{Code}{o}%
+{colback=codebackground, colframe=codeborder,
+ fontupper=\\footnotesize\\setlength{\\fboxsep}{0pt},
+ colupper=EFD,
+ IfNoValueTF={#1}%
+ {boxsep=2pt, arc=2.5pt, outer arc=2.5pt,
+ boxrule=0.5pt, left=2pt}%
+ {boxsep=2.5pt, arc=0pt, outer arc=0pt,
+ boxrule=0pt, leftrule=1.5pt, left=0.5pt},
+ right=2pt, top=1pt, bottom=0.5pt,
+ breakable}"
+ "Preamble content injected when using engrave-faces-latex for source blocks.
+This is relevant when `org-latex-listings' is set to `engraved'.
+
+There is quite a lot of flexibility in what this preamble can be, as long as it:
+- Loads the fvextra package.
+- Defines a \"Code\" environment (note the capital C), which can be
+ later used to wrap \"Verbatim\" environments (provided by fvextra).
+
+A macro-like placeholder is used to set fvextra's defaults according to
+`org-latex-engraved-options':
+
+ [FVEXTRA-SETUP]"
+ :group 'org-export-latex
+ :type 'string
+ :package-version '(Org . "9.6"))
+
+(defcustom org-latex-engraved-options
+ '(("commandchars" . "\\\\\\{\\}")
+ ("highlightcolor" . "white!95!black!80!blue")
+ ("breaklines" . "true")
+ ("breaksymbol" . "\\color{white!60!black}\\tiny\\ensuremath{\\hookrightarrow}"))
+ "Association list of options for the latex fvextra package when engraving code.
+
+These options are set using \\fvset{...} in the preamble of the
+LaTeX export. Each element of the alist should be a list or cons
+cell containing two strings: the name of the option, and the
+value. For example,
+
+ (setq org-latex-engraved-options
+ \\='((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
+ ; or
+ (setq org-latex-engraved-options
+ \\='((\"bgcolor\" . \"bg\") (\"frame\" . \"lines\")))
+
+will result in the following LaTeX in the preamble
+
+\\fvset{%
+ bgcolor=bg,
+ frame=lines}
+
+This will affect all fvextra environments. Note that the same
+options will be applied to all blocks. If you need
+block-specific options, you may use the following syntax:
+
+ #+ATTR_LATEX: :options key1=value1,key2=value2
+ #+BEGIN_SRC <LANG>
+ ...
+ #+END_SRC")
+
+(defun org-latex-generate-engraved-preamble (info syntax-colours-p)
+ (let* ((engraved-options
+ (plist-get info :latex-engraved-options))
+ (engraved-preamble-template
+ (plist-get info :latex-engraved-preamble))
+ (engraved-preamble
+ (if (string-match "^[ \t]*\\[FVEXTRA-SETUP\\][ \t]*\n?"
+ org-latex-engraved-preamble)
+ (replace-match
+ (concat
+ "\\fvset{%\n "
+ (org-latex--make-option-string engraved-options ",\n ")
+ "}\n")
+ t t
+ engraved-preamble-template)
+ engraved-preamble-template)))
+ (if syntax-colours-p
+ (concat
+ "\n% Setup for code blocks [1/2]\n\n"
+ engraved-preamble
+ "\n\n% Setup for code blocks [2/2]: syntax highlighting colors\n"
+ (if (require 'engrave-faces-latex nil t)
+ (engrave-faces-latex-gen-preamble)
+ (message "Cannot engrave source blocks. Consider installing `engrave-faces'.")
+ "% WARNING syntax highlighting unavailible as engrave-faces-latex was missing.\n")
+ "\n")
+ (concat
+ "\n% Setup for code blocks\n\n"
+ engraved-preamble
+ "\n"))))
;;;; Compilation
@@ -1756,6 +1881,15 @@ (defun org-latex-template (contents info)
(let ((template (plist-get info :latex-hyperref-template)))
(and (stringp template)
(format-spec template spec)))
+ ;; engrave-faces-latex preamble
+ (let ((engraved-p (eq org-latex-listings 'engraved))
+ (src-p (org-element-map (plist-get info :parse-tree)
+ '(src-block inline-src-block) #'identity))
+ (fixedw-p
+ (org-element-map (plist-get info :parse-tree)
+ '(example-block fixed-width) #'identity)))
+ (when (or src-p fixedw-p)
+ (org-latex-generate-engraved-preamble info src-p)))
;; Document start.
"\\begin{document}\n\n"
;; Title command.
@@ -2142,12 +2276,13 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
(pcase (plist-get info :latex-listings)
('nil (org-latex--text-markup code 'code info))
('minted (org-latex-inline-src-block--minted info code lang))
+ ('engraved (org-latex-inline-src-block--engraved info code lang))
(_ (org-latex-inline-src-block--listings info code lang)))))
(defun org-latex-inline-src-block--minted (info code lang)
(let ((mint-lang (or (cadr (assq (intern lang)
(plist-get info :latex-minted-langs)))
- (downcase org-lang)))
+ (downcase lang)))
(options (org-latex--make-option-string
(plist-get info :latex-minted-options))))
(format "\\mintinline%s{%s}{%s}"
@@ -2155,6 +2290,23 @@ (defun org-latex-inline-src-block--minted (info code lang)
mint-lang
code)))
+(defun org-latex-inline-src-block--engraved (info code lang)
+ (if (require 'engrave-faces-latex nil t)
+ (let (engraved-buffer engraved-code)
+ (setq engraved-buffer
+ (with-temp-buffer
+ (insert code)
+ (funcall (org-src-get-lang-mode lang))
+ (engrave-faces-latex-buffer)))
+ (setq engraved-code
+ (with-current-buffer engraved-buffer
+ (buffer-string)))
+ (kill-buffer engraved-buffer)
+ (format "\\Verb{%s}"
+ engraved-code))
+ (message "Cannot engrave inline src block, `engrave-faces-latex' is unavailible.")
+ (insert (org-latex--text-markup code 'code info))))
+
(defun org-latex-inline-src-block--listings (info code lang)
(let* ((lst-lang (or (cadr (assq (intern lang)
(plist-get info :latex-listings-langs)))
@@ -3013,6 +3165,9 @@ (defun org-latex-src-block (src-block _contents info)
num-start retain-labels attributes float custom-env))
((eq listings 'minted)
(org-latex-src-block--minted src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
+ ((eq listings 'engraved)
+ (org-latex-src-block--engraved src-block info lang caption caption-above-p label
num-start retain-labels attributes float))
(t
(org-latex-src-block--listings src-block info lang caption caption-above-p label
@@ -3129,6 +3284,80 @@ (defun org-latex-src-block--minted
;; Return value.
(format float-env body)))
+(defun org-latex-src-block--engraved
+ (src-block info lang caption caption-above-p _label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using engrave-faces-latex.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (if (require 'engrave-faces-latex nil t)
+ (let* ((caption-str (org-latex--caption/label-string src-block info))
+ (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
+ (plist-get info :latex-default-figure-position)))
+ (float-env
+ (cond
+ ((string= "multicolumn" float)
+ (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ (caption
+ (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ ((string= "t" float)
+ (concat (format "\\begin{listing}[%s]\n"
+ placement)
+ "%s\n\\end{listing}"))
+ (t "%s")))
+ (options (plist-get info :latex-engraved-options))
+ (content-buffer
+ (with-temp-buffer
+ (insert
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info)
+ "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line
+ ;; of code.
+ (concat (make-string (+ (- max-width (length loc)) 6)
+ ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info)))))
+ (funcall (org-src-get-lang-mode lang))
+ (engrave-faces-latex-buffer)))
+ (content
+ (with-current-buffer content-buffer
+ (buffer-string)))
+ (body
+ (format
+ "\\begin{Code}\n\\begin{Verbatim}[%s]\n%s\\end{Verbatim}\n\\end{Code}"
+ ;; Options.
+ (concat
+ (org-latex--make-option-string
+ (append
+ (when (and num-start (not (assoc "linenos" options)))
+ `(("linenos")
+ ("firstnumber" ,(number-to-string (1+ num-start)))))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (list local-options))))))
+ content)))
+ (kill-buffer content-buffer)
+ ;; Return value.
+ (format float-env body))
+ (message "Cannot engrave src block, `engrave-faces-latex' is unavailible.")
+ (insert (org-latex--text-markup code 'code info))))
+
(defun org-latex-src-block--listings
(src-block info lang caption caption-above-p label
num-start retain-labels attributes float)
--
2.35.3
^ permalink raw reply related [relevance 12%]
* Re: [PATCH] New LaTeX code export option: engraved
@ 2022-05-05 15:17 12% ` Timothy
2022-05-05 16:13 25% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2022-05-05 15:17 UTC (permalink / raw)
To: Ihor Radchenko; +Cc: emacs-orgmode, Daniel Fleischer, Nicolas Goaziou
[-- Attachment #1: Type: text/plain, Size: 2879 bytes --]
Hi Ihor,
> Thanks!
> Implementing fontification using Emacs capabilities is certainly a step
> in the right direction. LaTeX support for fontification has always been
> tricky.
I’m glad to hear you’re of a similar mind to me with this.
> - I tried to test your patch, and it only works partially. There is some
> stray text caused by LaTeX errors:
>
> [2. application/vnd.lotus-organizer; test.org]…
> [3. application/pdf; test.pdf]…
Ah. I thought that hyperref loaded xcolor, but it seems my assumption was
incorrect. I’ve added `\usepackage{xcolor}' to the default
`org-latex-engraved-preamble', but maybe I should ask people to modify
`org-latex-packages-alist'. I’m not sure.
> - You did not add a NEWS entry and did not update the manual in your patch.
I’m waiting till the functional content of these packages is settled/accepted,
and then I’ll write NEWS and manual entries.
> - There are many compiler warnings emitted when compiling Org with your patch
Oops, I keep on forgetting to check byte compilation. These should all be fixed now.
> The docstrings are missing in the above.
Docstrings have been added.
> I am not sure why, but the word fancy feels slightly annoying here.
Docsting rewritten.
> Since engraved is not entirely relying on LaTeX options, a lot of
> customisation is not mentioned in this docstring. AFAIU, color
> customisation is only possible by changing defcustoms from engrave-faces
> package.
>
> Another related note is what is going to happen in beamer export with
> dark background. The default face mapping in engrave-faces is using some
> kind of light theme, which may lose all the contrast on not-light
> background.
Modifying the style of engraved-faces-latex’s output is indeed done by
customising a engraved-faces variable. I don’t think we should attempt to do
anything further with this within Org.
To elaborate a bit, the generated LaTeX uses the styling information given in
`engrave-faces-preset-styles'. Changing this to use the current Emacs theme is as
simple as `(setq engrave-faces-preset-styles (engrave-faces-generate-preset))'.
> It feels that codebackground, codeborder, and EFD should be customizable
> by org-latex-engraved-options.
Hmm. I don’t think so. They are entirely self-contained within the preamble
variable. Should there be a nice existing `org-latex-user-colors' variable or
such, it would make a lot of sense to shove this there, but since no such
variable exists I’m not sure we can really do much better than just asking users
to modify `org-latex-engraved-preamble'.
> Docstring?
Added.
>> + (message “Cannot engrave inline src block, `engrave-faces-latex’ is unavailible.”)
>
> Why message instead of error?
User errors are now thrown.
Thanks for the feedback!
Timothy
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ox-latex-Refactor-org-latex-src-block.patch --]
[-- Type: text/x-patch, Size: 15942 bytes --]
From d231437e2c9f96bf70520d9ddda810a95667fcdd Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 21 Nov 2021 14:35:34 +0800
Subject: [PATCH 1/4] ox-latex: Refactor `org-latex-src-block'
* lisp/ox-latex.el (org-latex-src-block): Extract the per-format logic
from `org-latex-src-block' into new dedicated functions:
+ `org-latex-src-block--verbatim'
+ `org-latex-src-block--custom'
+ `org-latex-src-block--minted'
+ `org-latex-src-block--listings'
This makes `org-latex-src-block' much less monolithic, taking it from
175 lines to 30, and I find also makes it easier to understand.
---
lisp/ox-latex.el | 339 ++++++++++++++++++++++++++---------------------
1 file changed, 185 insertions(+), 154 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 841ad48bc..c2f728a1c 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2997,164 +2997,195 @@ (defun org-latex-src-block (src-block _contents info)
(float (plist-get attributes :float))
(listings (plist-get info :latex-listings)))
(cond
- ;; Case 1. No source fontification.
((or (not lang) (not listings))
- (let ((caption-str (org-latex--caption/label-string src-block info))
- (verbatim (format "\\begin{verbatim}\n%s\\end{verbatim}"
- (org-export-format-code-default src-block info))))
- (cond ((string= "multicolumn" float)
- (format "\\begin{figure*}[%s]\n%s%s\n%s\\end{figure*}"
- (plist-get info :latex-default-figure-position)
- (if caption-above-p caption-str "")
- verbatim
- (if caption-above-p "" caption-str)))
- (caption (concat
- (if caption-above-p caption-str "")
- verbatim
- (if caption-above-p "" (concat "\n" caption-str))))
- (t verbatim))))
- ;; Case 2. Custom environment.
+ (org-latex-src-block--verbatim src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
(custom-env
- (let ((caption-str (org-latex--caption/label-string src-block info))
- (formatted-src (org-export-format-code-default src-block info)))
- (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
- (format "\\begin{%s}\n%s\\end{%s}\n"
- custom-env
- (concat (and caption-above-p caption-str)
- formatted-src
- (and (not caption-above-p) caption-str))
- custom-env)
- (format-spec custom-env
- `((?s . ,formatted-src)
- (?c . ,caption)
- (?f . ,float)
- (?l . ,(org-latex--label src-block info))
- (?o . ,(or (plist-get attributes :options) "")))))))
- ;; Case 3. Use minted package.
+ (org-latex-src-block--custom src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float custom-env))
((eq listings 'minted)
- (let* ((caption-str (org-latex--caption/label-string src-block info))
- (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
- (plist-get info :latex-default-figure-position)))
- (float-env
- (cond
- ((string= "multicolumn" float)
- (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- (caption
- (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- ((string= "t" float)
- (concat (format "\\begin{listing}[%s]\n"
- placement)
- "%s\n\\end{listing}"))
- (t "%s")))
- (options (plist-get info :latex-minted-options))
- (body
- (format
- "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
- ;; Options.
- (concat
- (org-latex--make-option-string
- (if (or (not num-start) (assoc "linenos" options))
- options
- (append
- `(("linenos")
- ("firstnumber" ,(number-to-string (1+ num-start))))
- options)))
- (let ((local-options (plist-get attributes :options)))
- (and local-options (concat "," local-options))))
- ;; Language.
- (or (cadr (assq (intern lang)
- (plist-get info :latex-minted-langs)))
- (downcase lang))
- ;; Source code.
- (let* ((code-info (org-export-unravel-code src-block))
- (max-width
- (apply 'max
- (mapcar 'length
- (org-split-string (car code-info)
- "\n")))))
- (org-export-format-code
- (car code-info)
- (lambda (loc _num ref)
- (concat
- loc
- (when ref
- ;; Ensure references are flushed to the right,
- ;; separated with 6 spaces from the widest line
- ;; of code.
- (concat (make-string (+ (- max-width (length loc)) 6)
- ?\s)
- (format "(%s)" ref)))))
- nil (and retain-labels (cdr code-info)))))))
- ;; Return value.
- (format float-env body)))
- ;; Case 4. Use listings package.
+ (org-latex-src-block--minted src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
(t
- (let ((lst-lang
- (or (cadr (assq (intern lang)
- (plist-get info :latex-listings-langs)))
- lang))
- (caption-str
- (when caption
- (let ((main (org-export-get-caption src-block))
- (secondary (org-export-get-caption src-block t)))
- (if (not secondary)
- (format "{%s}" (org-export-data main info))
- (format "{[%s]%s}"
- (org-export-data secondary info)
- (org-export-data main info))))))
- (lst-opt (plist-get info :latex-listings-options)))
- (concat
- ;; Options.
- (format
- "\\lstset{%s}\n"
- (concat
- (org-latex--make-option-string
- (append
- lst-opt
- (cond
- ((and (not float) (plist-member attributes :float)) nil)
- ((string= "multicolumn" float) '(("float" "*")))
- ((and float (not (assoc "float" lst-opt)))
- `(("float" ,(plist-get info :latex-default-figure-position)))))
- `(("language" ,lst-lang))
- (if label
- `(("label" ,(org-latex--label src-block info)))
- '(("label" " ")))
- (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
- `(("captionpos" ,(if caption-above-p "t" "b")))
- (cond ((assoc "numbers" lst-opt) nil)
- ((not num-start) '(("numbers" "none")))
- (t `(("firstnumber" ,(number-to-string (1+ num-start)))
- ("numbers" "left"))))))
- (let ((local-options (plist-get attributes :options)))
- (and local-options (concat "," local-options)))))
- ;; Source code.
- (format
- "\\begin{lstlisting}\n%s\\end{lstlisting}"
- (let* ((code-info (org-export-unravel-code src-block))
- (max-width
- (apply 'max
- (mapcar 'length
- (org-split-string (car code-info) "\n")))))
- (org-export-format-code
- (car code-info)
- (lambda (loc _num ref)
- (concat
- loc
- (when ref
- ;; Ensure references are flushed to the right,
- ;; separated with 6 spaces from the widest line of
- ;; code
- (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
- (format "(%s)" ref)))))
- nil (and retain-labels (cdr code-info))))))))))))
-
+ (org-latex-src-block--listings src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))))))
+
+(defun org-latex-src-block--verbatim
+ (src-block info _lang caption caption-above-p _label
+ _num-start _retain-labels _attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using verbatim.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((caption-str (org-latex--caption/label-string src-block info))
+ (verbatim (format "\\begin{verbatim}\n%s\\end{verbatim}"
+ (org-export-format-code-default src-block info))))
+ (cond ((string= "multicolumn" float)
+ (format "\\begin{figure*}[%s]\n%s%s\n%s\\end{figure*}"
+ (plist-get info :latex-default-figure-position)
+ (if caption-above-p caption-str "")
+ verbatim
+ (if caption-above-p "" caption-str)))
+ (caption (concat
+ (if caption-above-p caption-str "")
+ verbatim
+ (if caption-above-p "" (concat "\n" caption-str))))
+ (t verbatim))))
+
+(defun org-latex-src-block--custom
+ (src-block info _lang caption caption-above-p _label
+ _num-start _retain-labels attributes float custom-env)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using a custom environment.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((caption-str (org-latex--caption/label-string src-block info))
+ (formatted-src (org-export-format-code-default src-block info)))
+ (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
+ (format "\\begin{%s}\n%s\\end{%s}\n"
+ custom-env
+ (concat (and caption-above-p caption-str)
+ formatted-src
+ (and (not caption-above-p) caption-str))
+ custom-env)
+ (format-spec custom-env
+ `((?s . ,formatted-src)
+ (?c . ,caption)
+ (?f . ,float)
+ (?l . ,(org-latex--label src-block info))
+ (?o . ,(or (plist-get attributes :options) "")))))))
+
+(defun org-latex-src-block--minted
+ (src-block info lang caption caption-above-p _label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using minted.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let* ((caption-str (org-latex--caption/label-string src-block info))
+ (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
+ (plist-get info :latex-default-figure-position)))
+ (float-env
+ (cond
+ ((string= "multicolumn" float)
+ (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ (caption
+ (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ ((string= "t" float)
+ (concat (format "\\begin{listing}[%s]\n"
+ placement)
+ "%s\n\\end{listing}"))
+ (t "%s")))
+ (options (plist-get info :latex-minted-options))
+ (body
+ (format
+ "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
+ ;; Options.
+ (concat
+ (org-latex--make-option-string
+ (if (or (not num-start) (assoc "linenos" options))
+ options
+ (append
+ `(("linenos")
+ ("firstnumber" ,(number-to-string (1+ num-start))))
+ options)))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (concat "," local-options))))
+ ;; Language.
+ (or (cadr (assq (intern lang)
+ (plist-get info :latex-minted-langs)))
+ (downcase lang))
+ ;; Source code.
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info)
+ "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line
+ ;; of code.
+ (concat (make-string (+ (- max-width (length loc)) 6)
+ ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info)))))))
+ ;; Return value.
+ (format float-env body)))
+
+(defun org-latex-src-block--listings
+ (src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using listings.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((lst-lang
+ (or (cadr (assq (intern lang)
+ (plist-get info :latex-listings-langs)))
+ lang))
+ (caption-str
+ (when caption
+ (let ((main (org-export-get-caption src-block))
+ (secondary (org-export-get-caption src-block t)))
+ (if (not secondary)
+ (format "{%s}" (org-export-data main info))
+ (format "{[%s]%s}"
+ (org-export-data secondary info)
+ (org-export-data main info))))))
+ (lst-opt (plist-get info :latex-listings-options)))
+ (concat
+ ;; Options.
+ (format
+ "\\lstset{%s}\n"
+ (concat
+ (org-latex--make-option-string
+ (append
+ lst-opt
+ (cond
+ ((and (not float) (plist-member attributes :float)) nil)
+ ((string= "multicolumn" float) '(("float" "*")))
+ ((and float (not (assoc "float" lst-opt)))
+ `(("float" ,(plist-get info :latex-default-figure-position)))))
+ `(("language" ,lst-lang))
+ (if label
+ `(("label" ,(org-latex--label src-block info)))
+ '(("label" " ")))
+ (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
+ `(("captionpos" ,(if caption-above-p "t" "b")))
+ (cond ((assoc "numbers" lst-opt) nil)
+ ((not num-start) '(("numbers" "none")))
+ (t `(("firstnumber" ,(number-to-string (1+ num-start)))
+ ("numbers" "left"))))))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (concat "," local-options)))))
+ ;; Source code.
+ (format
+ "\\begin{lstlisting}\n%s\\end{lstlisting}"
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info) "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line of
+ ;; code
+ (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info))))))))
;;;; Statistics Cookie
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-ox-latex-Refactor-org-latex-inline-src-block.patch --]
[-- Type: text/x-patch, Size: 4020 bytes --]
From ea5d116b06326be46e3053e6ded26d5b8b0638d9 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Wed, 4 May 2022 18:53:10 +0800
Subject: [PATCH 2/4] ox-latex: Refactor `org-latex-inline-src-block'
* lisp/ox-latex.el (org-latex-inline-src-block,
org-latex-inline-src-block--minted,
org-latex-inline-src-block--listings): Extract the minted and listings
specific logic out of `org-latex-inline-src-block` into the new
functions `org-latex-inline-src-block--minted` and
`org-latex-inline-src-block--listings`.
---
lisp/ox-latex.el | 62 +++++++++++++++++++++++++-----------------------
1 file changed, 32 insertions(+), 30 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index c2f728a1c..2b732cf16 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2127,36 +2127,38 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
"Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
CONTENTS holds the contents of the item. INFO is a plist holding
contextual information."
- (let* ((code (org-element-property :value inline-src-block))
- (separator (org-latex--find-verb-separator code)))
- (cl-case (plist-get info :latex-listings)
- ;; Do not use a special package: transcode it verbatim, as code.
- ((nil) (org-latex--text-markup code 'code info))
- ;; Use minted package.
- (minted
- (let* ((org-lang (org-element-property :language inline-src-block))
- (mint-lang (or (cadr (assq (intern org-lang)
- (plist-get info :latex-minted-langs)))
- (downcase org-lang)))
- (options (org-latex--make-option-string
- (plist-get info :latex-minted-options))))
- (format "\\mintinline%s{%s}{%s}"
- (if (string= options "") "" (format "[%s]" options))
- mint-lang
- code)))
- ;; Use listings package.
- (otherwise
- ;; Maybe translate language's name.
- (let* ((org-lang (org-element-property :language inline-src-block))
- (lst-lang (or (cadr (assq (intern org-lang)
- (plist-get info :latex-listings-langs)))
- org-lang))
- (options (org-latex--make-option-string
- (append (plist-get info :latex-listings-options)
- `(("language" ,lst-lang))))))
- (concat (format "\\lstinline[%s]" options)
- separator code separator))))))
-
+ (let ((code (org-element-property :value inline-src-block))
+ (lang (org-element-property :language inline-src-block)))
+ (pcase (plist-get info :latex-listings)
+ ('nil (org-latex--text-markup code 'code info))
+ ('minted (org-latex-inline-src-block--minted info code lang))
+ (_ (org-latex-inline-src-block--listings info code lang)))))
+
+(defun org-latex-inline-src-block--minted (info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using minted.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (let ((mint-lang (or (cadr (assq (intern lang)
+ (plist-get info :latex-minted-langs)))
+ (downcase org-lang)))
+ (options (org-latex--make-option-string
+ (plist-get info :latex-minted-options))))
+ (format "\\mintinline%s{%s}{%s}"
+ (if (string= options "") "" (format "[%s]" options))
+ mint-lang
+ code)))
+
+(defun org-latex-inline-src-block--listings (info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using lstlistings.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (let* ((lst-lang (or (cadr (assq (intern lang)
+ (plist-get info :latex-listings-langs)))
+ lang))
+ (separator (org-latex--find-verb-separator code))
+ (options (org-latex--make-option-string
+ (append (plist-get info :latex-listings-options)
+ `(("language" ,lst-lang))))))
+ (concat (format "\\lstinline[%s]" options)
+ separator code separator)))
;;;; Inlinetask
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: 0003-ox-latex-More-versitile-option-construction.patch --]
[-- Type: text/x-patch, Size: 4116 bytes --]
From aa0a9dafab7a7246e2855a2257d4c156eb48752c Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Wed, 4 May 2022 23:31:59 +0800
Subject: [PATCH 3/4] ox-latex: More versitile option construction
* lisp/ox-latex.el (org-latex--make-option-string): Support a custom
option seperator string.
(org-latex--make-option-string, org-latex-minted-options,
org-latex-listings-options): The first line of the docstrings for
`org-latex-minted-options` and `org-latex-listings-options` describe the
variables as "association lists", yet `org-latex--make-option-string`
does not handle association lists and an example of two-value lists is
provided. To make the behaviour match the docstring,
`org-latex--make-option-string` is modified to work with either a list
two-value lists or an association list, and the examples in
`org-latex-minted-options` and `org-latex-listings-options` updated
accordingly.
---
lisp/ox-latex.el | 38 ++++++++++++++++++++++++--------------
1 file changed, 24 insertions(+), 14 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 2b732cf16..4181db175 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1006,12 +1006,16 @@ (defcustom org-latex-listings-options nil
These options are supplied as a comma-separated list to the
\\lstset command. Each element of the association list should be
-a list containing two strings: the name of the option, and the
-value. For example,
+a list or cons cell containing two strings: the name of the
+option, and the value. For example,
(setq org-latex-listings-options
\\='((\"basicstyle\" \"\\\\small\")
(\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
+ ; or
+ (setq org-latex-listings-options
+ \\='((\"basicstyle\" . \"\\\\small\")
+ (\"keywordstyle\" . \"\\\\color{black}\\\\bfseries\\\\underbar\")))
will typeset the code in a small size font with underlined, bold
black keywords.
@@ -1059,11 +1063,14 @@ (defcustom org-latex-minted-options nil
These options are supplied within square brackets in
\\begin{minted} environments. Each element of the alist should
-be a list containing two strings: the name of the option, and the
-value. For example,
+be a list or cons cell containing two strings: the name of the
+option, and the value. For example,
(setq org-latex-minted-options
\\='((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
+ ; or
+ (setq org-latex-minted-options
+ \\='((\"bgcolor\" . \"bg\") (\"frame\" . \"lines\")))
will result in source blocks being exported with
@@ -1506,21 +1513,24 @@ (defun org-latex--find-verb-separator (s)
when (not (string-match (regexp-quote (char-to-string c)) s))
return (char-to-string c))))
-(defun org-latex--make-option-string (options)
+(defun org-latex--make-option-string (options &optional seperator)
"Return a comma separated string of keywords and values.
OPTIONS is an alist where the key is the options keyword as
a string, and the value a list containing the keyword value, or
nil."
(mapconcat (lambda (pair)
- (pcase-let ((`(,keyword ,value) pair))
- (concat keyword
- (and (> (length value) 0)
- (concat "="
- (if (string-match-p (rx (any "[]")) value)
- (format "{%s}" value)
- value))))))
- options
- ","))
+ (let ((keyword (car pair))
+ (value (pcase (cdr pair)
+ ((pred stringp) (cdr pair))
+ ((pred consp) (cadr pair)))))
+ (concat keyword
+ (when value
+ (concat "="
+ (if (string-match-p (rx (any "[]")) value)
+ (format "{%s}" value)
+ value))))))
+ options
+ (or seperator ",")))
(defun org-latex--wrap-label (element output info)
"Wrap label associated to ELEMENT around OUTPUT, if appropriate.
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #5: 0004-ox-latex-Introduce-engraved-code-highlighting.patch --]
[-- Type: text/x-patch, Size: 17464 bytes --]
From 90849f3986cc709c1d01ec9f204a7eb8b940cd27 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 21 Nov 2021 20:04:12 +0800
Subject: [PATCH 4/4] ox-latex: Introduce "engraved" code highlighting
* lisp/ox-latex.el (org-latex-src-block, org-latex-src-block--engraved,
org-latex-inline-src-block, org-latex-inline-src-block--engraved,
org-latex-template, org-latex-listings): Make use of the engraved-faces
package (available on ELPA) to provide an alternative LaTeX code
highlighting backend which functions similarly to htmlize.el for HTML
exports.
(org-latex-engraved-preamble, org-latex-engraved-options): Introduce
variables to construct the preamble for engraved code blocks.
* lisp/ox-beamer.el (org-beamer-template): Modify to add engrave-faces
preamble when applicable.
---
lisp/ox-beamer.el | 9 ++
lisp/ox-latex.el | 273 ++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 272 insertions(+), 10 deletions(-)
diff --git a/lisp/ox-beamer.el b/lisp/ox-beamer.el
index 6be73c91e..73bd95539 100644
--- a/lisp/ox-beamer.el
+++ b/lisp/ox-beamer.el
@@ -857,6 +857,15 @@ (defun org-beamer-template (contents info)
(let ((template (plist-get info :latex-hyperref-template)))
(and (stringp template)
(format-spec template (org-latex--format-spec info))))
+ ;; engrave-faces-latex preamble
+ (when (eq org-latex-listings 'engraved)
+ (let ((src-p (org-element-map (plist-get info :parse-tree)
+ '(src-block inline-src-block) #'identity))
+ (fixedw-p
+ (org-element-map (plist-get info :parse-tree)
+ '(example-block fixed-width) #'identity)))
+ (when (or src-p fixedw-p)
+ (org-latex-generate-engraved-preamble info src-p))))
;; Document start.
"\\begin{document}\n\n"
;; Title command.
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 4181db175..831509a26 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -37,6 +37,8 @@ (defvar org-latex-default-packages-alist)
(defvar org-latex-packages-alist)
(defvar orgtbl-exp-regexp)
+(declare-function engrave-faces-latex-gen-preamble "ext:engrave-faces-latex")
+(declare-function engrave-faces-latex-buffer "ext:engrave-faces-latex")
\f
;;; Define Back-End
@@ -125,6 +127,8 @@ (org-export-define-backend 'latex
(:latex-default-quote-environment nil nil org-latex-default-quote-environment)
(:latex-default-table-mode nil nil org-latex-default-table-mode)
(:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format)
+ (:latex-engraved-options nil nil org-latex-engraved-options)
+ (:latex-engraved-preamble nil nil org-latex-engraved-preamble)
(:latex-footnote-defined-format nil nil org-latex-footnote-defined-format)
(:latex-footnote-separator nil nil org-latex-footnote-separator)
(:latex-format-drawer-function nil nil org-latex-format-drawer-function)
@@ -937,22 +941,48 @@ (defcustom org-latex-listings nil
"Non-nil means export source code using the listings package.
This package will fontify source code, possibly even with color.
-If you want to use this, you also need to make LaTeX use the
-listings package, and if you want to have color, the color
-package. Just add these to `org-latex-packages-alist', for
-example using customize, or with something like:
+There are four implementations of this functionality you may
+choose from (ordered from least to most capable):
+1. Verbatim (nil)
+2. Listings (t)
+3. Minted (minted)
+4. Engraved (engraved)
+
+The first two options provide basic syntax
+highlighting (listings), or none at all (verbatim).
+
+When using listings, you also need to make use of the LaTeX
+\"listings\" package. The \"color\" package is also needed if you
+would like color too. These can simply be added to
+`org-latex-packages-alist', using customise or something like:
(require \\='ox-latex)
(add-to-list \\='org-latex-packages-alist \\='(\"\" \"listings\"))
(add-to-list \\='org-latex-packages-alist \\='(\"\" \"color\"))
-Alternatively,
+There are two options for more comprehensive fontification. The
+first can be set with,
+
+ (setq org-latex-listings \\='engraved)
+
+which causes source code to be run through
+`engrave-faces-latex-buffer', which generates colorings using
+Emacs' font-lock information. This requires the engrave-faces
+package (availible from ELPA), and the fvextra LaTeX package be
+installed.
+
+The styling of the engraved result can customised with
+`org-latex-engraved-preamble' and `org-latex-engraved-options'.
+The default preamble also uses the tcolorbox LaTeX package in
+addition to fvextra.
+
+The second more comprehensive option can be used with,
(setq org-latex-listings \\='minted)
-causes source code to be exported using the minted package as
-opposed to listings. If you want to use minted, you need to add
-the minted package to `org-latex-packages-alist', for example
+which causes source code to be exported using the minted package
+as opposed to listings. If you want to use minted, you need to
+add the minted package to `org-latex-packages-alist', for example
using customize, or with
(require \\='ox-latex)
@@ -971,8 +1001,9 @@ (defcustom org-latex-listings nil
:type '(choice
(const :tag "Use listings" t)
(const :tag "Use minted" minted)
+ (const :tag "Use engrave-faces-latex" engraved)
(const :tag "Export verbatim" nil))
- :safe (lambda (s) (memq s '(t nil minted))))
+ :safe (lambda (s) (memq s '(t nil minted engraved))))
(defcustom org-latex-listings-langs
'((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
@@ -1142,6 +1173,124 @@ (defcustom org-latex-custom-lang-environments nil
:version "26.1"
:package-version '(Org . "9.0"))
+(defcustom org-latex-engraved-preamble
+ "\\usepackage{fvextra}
+
+[FVEXTRA-SETUP]
+
+\\renewcommand\\theFancyVerbLine{\\footnotesize\\color{black!40!white}\\arabic{FancyVerbLine}}
+
+\\usepackage{xcolor}
+
+\\providecolor{codebackground}{HTML}{f7f7f7}
+\\providecolor{codeborder}{HTML}{f0f0f0}
+\\providecolor{EFD}{HTML}{28292e}
+
+% TODO have code boxes keep line vertical alignment
+\\usepackage[breakable,xparse]{tcolorbox}
+\\DeclareTColorBox[]{Code}{o}%
+{colback=codebackground, colframe=codeborder,
+ fontupper=\\footnotesize\\setlength{\\fboxsep}{0pt},
+ colupper=EFD,
+ IfNoValueTF={#1}%
+ {boxsep=2pt, arc=2.5pt, outer arc=2.5pt,
+ boxrule=0.5pt, left=2pt}%
+ {boxsep=2.5pt, arc=0pt, outer arc=0pt,
+ boxrule=0pt, leftrule=1.5pt, left=0.5pt},
+ right=2pt, top=1pt, bottom=0.5pt,
+ breakable}"
+ "Preamble content injected when using engrave-faces-latex for source blocks.
+This is relevant when `org-latex-listings' is set to `engraved'.
+
+There is quite a lot of flexibility in what this preamble can be, as long as it:
+- Loads the fvextra package.
+- Loads the package xcolor (if it is not already loader elsewhere).
+- Defines a \"Code\" environment (note the capital C), which can be
+ later used to wrap \"Verbatim\" environments (provided by fvextra).
+
+A macro-like placeholder is used to set fvextra's defaults according to
+`org-latex-engraved-options':
+
+ [FVEXTRA-SETUP]
+
+In the default value the color \"EFD\" is provided as this is the
+foreground colour provided by engrave-faces-latex. When there
+are example/fixed-width blocks only, the engraved generated
+preamble is not included, and so it is provided so we may use it
+anyway."
+ :group 'org-export-latex
+ :type 'string
+ :package-version '(Org . "9.6"))
+
+(defcustom org-latex-engraved-options
+ '(("commandchars" . "\\\\\\{\\}")
+ ("highlightcolor" . "white!95!black!80!blue")
+ ("breaklines" . "true")
+ ("breaksymbol" . "\\color{white!60!black}\\tiny\\ensuremath{\\hookrightarrow}"))
+ "Association list of options for the latex fvextra package when engraving code.
+
+These options are set using \\fvset{...} in the preamble of the
+LaTeX export. Each element of the alist should be a list or cons
+cell containing two strings: the name of the option, and the
+value. For example,
+
+ (setq org-latex-engraved-options
+ \\='((\"highlightcolor\" \"green\") (\"frame\" \"lines\")))
+ ; or
+ (setq org-latex-engraved-options
+ \\='((\"highlightcolor\" . \"green\") (\"frame\" . \"lines\")))
+
+will result in the following LaTeX in the preamble
+
+\\fvset{%
+ bgcolor=bg,
+ frame=lines}
+
+This will affect all fvextra environments. Note that the same
+options will be applied to all blocks. If you need
+block-specific options, you may use the following syntax:
+
+ #+ATTR_LATEX: :options key1=value1,key2=value2
+ #+BEGIN_SRC <LANG>
+ ...
+ #+END_SRC"
+ :group 'org-export-latex
+ :type '(alist :key-type (string :tag "option")
+ :value-type (string :tag "value")))
+
+(defun org-latex-generate-engraved-preamble (info syntax-colours-p)
+ "Generate the preamble to setup engraved code.
+The result is constructed from `org-latex-engraved-preamble' and
+`org-latex-engraved-options'."
+ (let* ((engraved-options
+ (plist-get info :latex-engraved-options))
+ (engraved-preamble-template
+ (plist-get info :latex-engraved-preamble))
+ (engraved-preamble
+ (if (string-match "^[ \t]*\\[FVEXTRA-SETUP\\][ \t]*\n?"
+ engraved-preamble)
+ (replace-match
+ (concat
+ "\\fvset{%\n "
+ (org-latex--make-option-string engraved-options ",\n ")
+ "}\n")
+ t t
+ engraved-preamble-template)
+ engraved-preamble-template)))
+ (if syntax-colours-p
+ (concat
+ "\n% Setup for code blocks [1/2]\n\n"
+ engraved-preamble
+ "\n\n% Setup for code blocks [2/2]: syntax highlighting colors\n"
+ (if (require 'engrave-faces-latex nil t)
+ (engrave-faces-latex-gen-preamble)
+ (message "Cannot engrave source blocks. Consider installing `engrave-faces'.")
+ "% WARNING syntax highlighting unavailible as engrave-faces-latex was missing.\n")
+ "\n")
+ (concat
+ "\n% Setup for code blocks\n\n"
+ engraved-preamble
+ "\n"))))
;;;; Compilation
@@ -1756,6 +1905,15 @@ (defun org-latex-template (contents info)
(let ((template (plist-get info :latex-hyperref-template)))
(and (stringp template)
(format-spec template spec)))
+ ;; engrave-faces-latex preamble
+ (when (eq org-latex-listings 'engraved)
+ (let ((src-p (org-element-map (plist-get info :parse-tree)
+ '(src-block inline-src-block) #'identity))
+ (fixedw-p
+ (org-element-map (plist-get info :parse-tree)
+ '(example-block fixed-width) #'identity)))
+ (when (or src-p fixedw-p)
+ (org-latex-generate-engraved-preamble info src-p))))
;; Document start.
"\\begin{document}\n\n"
;; Title command.
@@ -2142,6 +2300,7 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
(pcase (plist-get info :latex-listings)
('nil (org-latex--text-markup code 'code info))
('minted (org-latex-inline-src-block--minted info code lang))
+ ('engraved (org-latex-inline-src-block--engraved info code lang))
(_ (org-latex-inline-src-block--listings info code lang)))))
(defun org-latex-inline-src-block--minted (info code lang)
@@ -2149,7 +2308,7 @@ (defun org-latex-inline-src-block--minted (info code lang)
INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
(let ((mint-lang (or (cadr (assq (intern lang)
(plist-get info :latex-minted-langs)))
- (downcase org-lang)))
+ (downcase lang)))
(options (org-latex--make-option-string
(plist-get info :latex-minted-options))))
(format "\\mintinline%s{%s}{%s}"
@@ -2157,6 +2316,24 @@ (defun org-latex-inline-src-block--minted (info code lang)
mint-lang
code)))
+(defun org-latex-inline-src-block--engraved (info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using engrave-faces.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (if (require 'engrave-faces-latex nil t)
+ (let (engraved-buffer engraved-code)
+ (setq engraved-buffer
+ (with-temp-buffer
+ (insert code)
+ (funcall (org-src-get-lang-mode lang))
+ (engrave-faces-latex-buffer)))
+ (setq engraved-code
+ (with-current-buffer engraved-buffer
+ (buffer-string)))
+ (kill-buffer engraved-buffer)
+ (format "\\Verb{%s}"
+ engraved-code))
+ (user-error "Cannot engrave inline src block, `engrave-faces-latex' is unavailible.")))
+
(defun org-latex-inline-src-block--listings (info code lang)
"Transcode an inline src block's content from Org to LaTeX, using lstlistings.
INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
@@ -3017,6 +3194,9 @@ (defun org-latex-src-block (src-block _contents info)
num-start retain-labels attributes float custom-env))
((eq listings 'minted)
(org-latex-src-block--minted src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
+ ((eq listings 'engraved)
+ (org-latex-src-block--engraved src-block info lang caption caption-above-p label
num-start retain-labels attributes float))
(t
(org-latex-src-block--listings src-block info lang caption caption-above-p label
@@ -3133,6 +3313,79 @@ (defun org-latex-src-block--minted
;; Return value.
(format float-env body)))
+(defun org-latex-src-block--engraved
+ (src-block info lang caption caption-above-p _label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using engrave-faces-latex.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (if (require 'engrave-faces-latex nil t)
+ (let* ((caption-str (org-latex--caption/label-string src-block info))
+ (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
+ (plist-get info :latex-default-figure-position)))
+ (float-env
+ (cond
+ ((string= "multicolumn" float)
+ (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ (caption
+ (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ ((string= "t" float)
+ (concat (format "\\begin{listing}[%s]\n"
+ placement)
+ "%s\n\\end{listing}"))
+ (t "%s")))
+ (options (plist-get info :latex-engraved-options))
+ (content-buffer
+ (with-temp-buffer
+ (insert
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info)
+ "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line
+ ;; of code.
+ (concat (make-string (+ (- max-width (length loc)) 6)
+ ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info)))))
+ (funcall (org-src-get-lang-mode lang))
+ (engrave-faces-latex-buffer)))
+ (content
+ (with-current-buffer content-buffer
+ (buffer-string)))
+ (body
+ (format
+ "\\begin{Code}\n\\begin{Verbatim}[%s]\n%s\\end{Verbatim}\n\\end{Code}"
+ ;; Options.
+ (concat
+ (org-latex--make-option-string
+ (append
+ (when (and num-start (not (assoc "linenos" options)))
+ `(("linenos")
+ ("firstnumber" ,(number-to-string (1+ num-start)))))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (list local-options))))))
+ content)))
+ (kill-buffer content-buffer)
+ ;; Return value.
+ (format float-env body))
+ (user-error "Cannot engrave src block, `engrave-faces-latex' is unavailible.")))
+
(defun org-latex-src-block--listings
(src-block info lang caption caption-above-p label
num-start retain-labels attributes float)
--
2.35.3
^ permalink raw reply related [relevance 12%]
* Re: [PATCH] New LaTeX code export option: engraved
@ 2022-05-05 16:09 93% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2022-05-05 16:09 UTC (permalink / raw)
To: Daniel Fleischer; +Cc: emacs-orgmode, Nicolas Goaziou
[-- Attachment #1: Type: text/plain, Size: 336 bytes --]
Hi Daniel,
> Hi Timothy, thank you very much for your work!
Thanks for the kind words 🙂
> Let me learn a bit about the different code highlighting options in
> order to understand what you offer.
Sure. If it’s any help, here’s a comparison example I whipped up:
<https://0x0.st/oAdn.png>
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] New LaTeX code export option: engraved
2022-05-05 15:17 12% ` Timothy
@ 2022-05-05 16:13 25% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2022-05-05 16:13 UTC (permalink / raw)
To: Ihor Radchenko; +Cc: emacs-orgmode, Daniel Fleischer, Nicolas Goaziou
[-- Attachment #1: Type: text/plain, Size: 166 bytes --]
> [5. text/x-patch; 0004-ox-latex-Introduce-engraved-code-highlighting.patch]…
Ooops, I had some ucommited changes. The correct version is attached.
Timothy
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ox-latex-Introduce-engraved-code-highlighting.patch --]
[-- Type: text/x-patch, Size: 17470 bytes --]
From b66c291b1f0d1419742449bcde42bf0c4d620c23 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 21 Nov 2021 20:04:12 +0800
Subject: [PATCH] ox-latex: Introduce "engraved" code highlighting
* lisp/ox-latex.el (org-latex-src-block, org-latex-src-block--engraved,
org-latex-inline-src-block, org-latex-inline-src-block--engraved,
org-latex-template, org-latex-listings): Make use of the engraved-faces
package (available on ELPA) to provide an alternative LaTeX code
highlighting backend which functions similarly to htmlize.el for HTML
exports.
(org-latex-engraved-preamble, org-latex-engraved-options): Introduce
variables to construct the preamble for engraved code blocks.
* lisp/ox-beamer.el (org-beamer-template): Modify to add engrave-faces
preamble when applicable.
---
lisp/ox-beamer.el | 9 ++
lisp/ox-latex.el | 273 ++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 272 insertions(+), 10 deletions(-)
diff --git a/lisp/ox-beamer.el b/lisp/ox-beamer.el
index 6be73c91e..73bd95539 100644
--- a/lisp/ox-beamer.el
+++ b/lisp/ox-beamer.el
@@ -857,6 +857,15 @@ (defun org-beamer-template (contents info)
(let ((template (plist-get info :latex-hyperref-template)))
(and (stringp template)
(format-spec template (org-latex--format-spec info))))
+ ;; engrave-faces-latex preamble
+ (when (eq org-latex-listings 'engraved)
+ (let ((src-p (org-element-map (plist-get info :parse-tree)
+ '(src-block inline-src-block) #'identity))
+ (fixedw-p
+ (org-element-map (plist-get info :parse-tree)
+ '(example-block fixed-width) #'identity)))
+ (when (or src-p fixedw-p)
+ (org-latex-generate-engraved-preamble info src-p))))
;; Document start.
"\\begin{document}\n\n"
;; Title command.
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 4181db175..83bb6f078 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -37,6 +37,8 @@ (defvar org-latex-default-packages-alist)
(defvar org-latex-packages-alist)
(defvar orgtbl-exp-regexp)
+(declare-function engrave-faces-latex-gen-preamble "ext:engrave-faces-latex")
+(declare-function engrave-faces-latex-buffer "ext:engrave-faces-latex")
\f
;;; Define Back-End
@@ -125,6 +127,8 @@ (org-export-define-backend 'latex
(:latex-default-quote-environment nil nil org-latex-default-quote-environment)
(:latex-default-table-mode nil nil org-latex-default-table-mode)
(:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format)
+ (:latex-engraved-options nil nil org-latex-engraved-options)
+ (:latex-engraved-preamble nil nil org-latex-engraved-preamble)
(:latex-footnote-defined-format nil nil org-latex-footnote-defined-format)
(:latex-footnote-separator nil nil org-latex-footnote-separator)
(:latex-format-drawer-function nil nil org-latex-format-drawer-function)
@@ -937,22 +941,48 @@ (defcustom org-latex-listings nil
"Non-nil means export source code using the listings package.
This package will fontify source code, possibly even with color.
-If you want to use this, you also need to make LaTeX use the
-listings package, and if you want to have color, the color
-package. Just add these to `org-latex-packages-alist', for
-example using customize, or with something like:
+There are four implementations of this functionality you may
+choose from (ordered from least to most capable):
+1. Verbatim (nil)
+2. Listings (t)
+3. Minted (minted)
+4. Engraved (engraved)
+
+The first two options provide basic syntax
+highlighting (listings), or none at all (verbatim).
+
+When using listings, you also need to make use of the LaTeX
+\"listings\" package. The \"color\" package is also needed if you
+would like color too. These can simply be added to
+`org-latex-packages-alist', using customise or something like:
(require \\='ox-latex)
(add-to-list \\='org-latex-packages-alist \\='(\"\" \"listings\"))
(add-to-list \\='org-latex-packages-alist \\='(\"\" \"color\"))
-Alternatively,
+There are two options for more comprehensive fontification. The
+first can be set with,
+
+ (setq org-latex-listings \\='engraved)
+
+which causes source code to be run through
+`engrave-faces-latex-buffer', which generates colorings using
+Emacs' font-lock information. This requires the engrave-faces
+package (availible from ELPA), and the fvextra LaTeX package be
+installed.
+
+The styling of the engraved result can customised with
+`org-latex-engraved-preamble' and `org-latex-engraved-options'.
+The default preamble also uses the tcolorbox LaTeX package in
+addition to fvextra.
+
+The second more comprehensive option can be used with,
(setq org-latex-listings \\='minted)
-causes source code to be exported using the minted package as
-opposed to listings. If you want to use minted, you need to add
-the minted package to `org-latex-packages-alist', for example
+which causes source code to be exported using the minted package
+as opposed to listings. If you want to use minted, you need to
+add the minted package to `org-latex-packages-alist', for example
using customize, or with
(require \\='ox-latex)
@@ -971,8 +1001,9 @@ (defcustom org-latex-listings nil
:type '(choice
(const :tag "Use listings" t)
(const :tag "Use minted" minted)
+ (const :tag "Use engrave-faces-latex" engraved)
(const :tag "Export verbatim" nil))
- :safe (lambda (s) (memq s '(t nil minted))))
+ :safe (lambda (s) (memq s '(t nil minted engraved))))
(defcustom org-latex-listings-langs
'((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
@@ -1142,6 +1173,124 @@ (defcustom org-latex-custom-lang-environments nil
:version "26.1"
:package-version '(Org . "9.0"))
+(defcustom org-latex-engraved-preamble
+ "\\usepackage{fvextra}
+
+[FVEXTRA-SETUP]
+
+\\renewcommand\\theFancyVerbLine{\\footnotesize\\color{black!40!white}\\arabic{FancyVerbLine}}
+
+\\usepackage{xcolor}
+
+\\providecolor{codebackground}{HTML}{f7f7f7}
+\\providecolor{codeborder}{HTML}{f0f0f0}
+\\providecolor{EFD}{HTML}{28292e}
+
+% TODO have code boxes keep line vertical alignment
+\\usepackage[breakable,xparse]{tcolorbox}
+\\DeclareTColorBox[]{Code}{o}%
+{colback=codebackground, colframe=codeborder,
+ fontupper=\\footnotesize\\setlength{\\fboxsep}{0pt},
+ colupper=EFD,
+ IfNoValueTF={#1}%
+ {boxsep=2pt, arc=2.5pt, outer arc=2.5pt,
+ boxrule=0.5pt, left=2pt}%
+ {boxsep=2.5pt, arc=0pt, outer arc=0pt,
+ boxrule=0pt, leftrule=1.5pt, left=0.5pt},
+ right=2pt, top=1pt, bottom=0.5pt,
+ breakable}"
+ "Preamble content injected when using engrave-faces-latex for source blocks.
+This is relevant when `org-latex-listings' is set to `engraved'.
+
+There is quite a lot of flexibility in what this preamble can be, as long as it:
+- Loads the fvextra package.
+- Loads the package xcolor (if it is not already loader elsewhere).
+- Defines a \"Code\" environment (note the capital C), which can be
+ later used to wrap \"Verbatim\" environments (provided by fvextra).
+
+A macro-like placeholder is used to set fvextra's defaults according to
+`org-latex-engraved-options':
+
+ [FVEXTRA-SETUP]
+
+In the default value the color \"EFD\" is provided as this is the
+foreground colour provided by engrave-faces-latex. When there
+are example/fixed-width blocks only, the engraved generated
+preamble is not included, and so it is provided so we may use it
+anyway."
+ :group 'org-export-latex
+ :type 'string
+ :package-version '(Org . "9.6"))
+
+(defcustom org-latex-engraved-options
+ '(("commandchars" . "\\\\\\{\\}")
+ ("highlightcolor" . "white!95!black!80!blue")
+ ("breaklines" . "true")
+ ("breaksymbol" . "\\color{white!60!black}\\tiny\\ensuremath{\\hookrightarrow}"))
+ "Association list of options for the latex fvextra package when engraving code.
+
+These options are set using \\fvset{...} in the preamble of the
+LaTeX export. Each element of the alist should be a list or cons
+cell containing two strings: the name of the option, and the
+value. For example,
+
+ (setq org-latex-engraved-options
+ \\='((\"highlightcolor\" \"green\") (\"frame\" \"lines\")))
+ ; or
+ (setq org-latex-engraved-options
+ \\='((\"highlightcolor\" . \"green\") (\"frame\" . \"lines\")))
+
+will result in the following LaTeX in the preamble
+
+\\fvset{%
+ bgcolor=bg,
+ frame=lines}
+
+This will affect all fvextra environments. Note that the same
+options will be applied to all blocks. If you need
+block-specific options, you may use the following syntax:
+
+ #+ATTR_LATEX: :options key1=value1,key2=value2
+ #+BEGIN_SRC <LANG>
+ ...
+ #+END_SRC"
+ :group 'org-export-latex
+ :type '(alist :key-type (string :tag "option")
+ :value-type (string :tag "value")))
+
+(defun org-latex-generate-engraved-preamble (info syntax-colours-p)
+ "Generate the preamble to setup engraved code.
+The result is constructed from `org-latex-engraved-preamble' and
+`org-latex-engraved-options'."
+ (let* ((engraved-options
+ (plist-get info :latex-engraved-options))
+ (engraved-preamble-template
+ (plist-get info :latex-engraved-preamble))
+ (engraved-preamble
+ (if (string-match "^[ \t]*\\[FVEXTRA-SETUP\\][ \t]*\n?"
+ engraved-preamble-template)
+ (replace-match
+ (concat
+ "\\fvset{%\n "
+ (org-latex--make-option-string engraved-options ",\n ")
+ "}\n")
+ t t
+ engraved-preamble-template)
+ engraved-preamble-template)))
+ (if syntax-colours-p
+ (concat
+ "\n% Setup for code blocks [1/2]\n\n"
+ engraved-preamble
+ "\n\n% Setup for code blocks [2/2]: syntax highlighting colors\n"
+ (if (require 'engrave-faces-latex nil t)
+ (engrave-faces-latex-gen-preamble)
+ (message "Cannot engrave source blocks. Consider installing `engrave-faces'.")
+ "% WARNING syntax highlighting unavailible as engrave-faces-latex was missing.\n")
+ "\n")
+ (concat
+ "\n% Setup for code blocks\n\n"
+ engraved-preamble
+ "\n"))))
;;;; Compilation
@@ -1756,6 +1905,15 @@ (defun org-latex-template (contents info)
(let ((template (plist-get info :latex-hyperref-template)))
(and (stringp template)
(format-spec template spec)))
+ ;; engrave-faces-latex preamble
+ (when (eq org-latex-listings 'engraved)
+ (let ((src-p (org-element-map (plist-get info :parse-tree)
+ '(src-block inline-src-block) #'identity))
+ (fixedw-p
+ (org-element-map (plist-get info :parse-tree)
+ '(example-block fixed-width) #'identity)))
+ (when (or src-p fixedw-p)
+ (org-latex-generate-engraved-preamble info src-p))))
;; Document start.
"\\begin{document}\n\n"
;; Title command.
@@ -2142,6 +2300,7 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
(pcase (plist-get info :latex-listings)
('nil (org-latex--text-markup code 'code info))
('minted (org-latex-inline-src-block--minted info code lang))
+ ('engraved (org-latex-inline-src-block--engraved info code lang))
(_ (org-latex-inline-src-block--listings info code lang)))))
(defun org-latex-inline-src-block--minted (info code lang)
@@ -2149,7 +2308,7 @@ (defun org-latex-inline-src-block--minted (info code lang)
INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
(let ((mint-lang (or (cadr (assq (intern lang)
(plist-get info :latex-minted-langs)))
- (downcase org-lang)))
+ (downcase lang)))
(options (org-latex--make-option-string
(plist-get info :latex-minted-options))))
(format "\\mintinline%s{%s}{%s}"
@@ -2157,6 +2316,24 @@ (defun org-latex-inline-src-block--minted (info code lang)
mint-lang
code)))
+(defun org-latex-inline-src-block--engraved (_info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using engrave-faces.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (if (require 'engrave-faces-latex nil t)
+ (let (engraved-buffer engraved-code)
+ (setq engraved-buffer
+ (with-temp-buffer
+ (insert code)
+ (funcall (org-src-get-lang-mode lang))
+ (engrave-faces-latex-buffer)))
+ (setq engraved-code
+ (with-current-buffer engraved-buffer
+ (buffer-string)))
+ (kill-buffer engraved-buffer)
+ (format "\\Verb{%s}"
+ engraved-code))
+ (user-error "Cannot engrave inline src block, `engrave-faces-latex' is unavailible.")))
+
(defun org-latex-inline-src-block--listings (info code lang)
"Transcode an inline src block's content from Org to LaTeX, using lstlistings.
INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
@@ -3017,6 +3194,9 @@ (defun org-latex-src-block (src-block _contents info)
num-start retain-labels attributes float custom-env))
((eq listings 'minted)
(org-latex-src-block--minted src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
+ ((eq listings 'engraved)
+ (org-latex-src-block--engraved src-block info lang caption caption-above-p label
num-start retain-labels attributes float))
(t
(org-latex-src-block--listings src-block info lang caption caption-above-p label
@@ -3133,6 +3313,79 @@ (defun org-latex-src-block--minted
;; Return value.
(format float-env body)))
+(defun org-latex-src-block--engraved
+ (src-block info lang caption caption-above-p _label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using engrave-faces-latex.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (if (require 'engrave-faces-latex nil t)
+ (let* ((caption-str (org-latex--caption/label-string src-block info))
+ (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
+ (plist-get info :latex-default-figure-position)))
+ (float-env
+ (cond
+ ((string= "multicolumn" float)
+ (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ (caption
+ (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ ((string= "t" float)
+ (concat (format "\\begin{listing}[%s]\n"
+ placement)
+ "%s\n\\end{listing}"))
+ (t "%s")))
+ (options (plist-get info :latex-engraved-options))
+ (content-buffer
+ (with-temp-buffer
+ (insert
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info)
+ "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line
+ ;; of code.
+ (concat (make-string (+ (- max-width (length loc)) 6)
+ ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info)))))
+ (funcall (org-src-get-lang-mode lang))
+ (engrave-faces-latex-buffer)))
+ (content
+ (with-current-buffer content-buffer
+ (buffer-string)))
+ (body
+ (format
+ "\\begin{Code}\n\\begin{Verbatim}[%s]\n%s\\end{Verbatim}\n\\end{Code}"
+ ;; Options.
+ (concat
+ (org-latex--make-option-string
+ (append
+ (when (and num-start (not (assoc "linenos" options)))
+ `(("linenos")
+ ("firstnumber" ,(number-to-string (1+ num-start)))))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (list local-options))))))
+ content)))
+ (kill-buffer content-buffer)
+ ;; Return value.
+ (format float-env body))
+ (user-error "Cannot engrave src block, `engrave-faces-latex' is unavailible.")))
+
(defun org-latex-src-block--listings
(src-block info lang caption caption-above-p label
num-start retain-labels attributes float)
--
2.35.3
^ permalink raw reply related [relevance 25%]
* Re: [PATCH] New LaTeX code export option: engraved
@ 2022-05-06 11:23 86% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2022-05-06 11:23 UTC (permalink / raw)
To: Ihor Radchenko; +Cc: Daniel Fleischer, emacs-orgmode, Nicolas Goaziou
[-- Attachment #1: Type: text/plain, Size: 1407 bytes --]
Hi Ihor,
>> Sure. If it’s any help, here’s a comparison example I whipped up:
>> <https://0x0.st/oAdn.png>
>
> Timothy, could you please not use 0x0 to share not-so-large images? 0x0
> deletes uploaded files within ~1year time and this link will no longer
> be valid if someone tries to read this discussion years later.
I forget if org-msg handles image links well (I recall some issue in the past).
Let’s do a little test:
<file:/tmp/ox-latex-src-fontification-comparison.png>
> Also, this illustration makes me wonder if engrave-faces can provide a
> color scheme that is mimicking minted.
One could certainly create `engrave-faces-preset-styles' presets which mimic
certain minted colour schemes.
> Generally, easy customisation of highlight schemes could be useful.
I agree. Is this not already the case though? Manually modifying
`engrave-faces-preset-styles' or calling `engrave-faces-generate-preset' seems
pretty easy to me, the only thing missing is the ability to generate a preset
from a theme not loaded, which I suspect won’t be easy to do.
> On per-src-block basis or even on per-language basis if document contains
> course blocks with different programming languages.
I can’t say I really see the point in this myself. It would certainly be
non-trivial to add, but could be added latter if there’s demand for it.
All the best,
Timothy
[-- Attachment #2: ox-latex-src-fontification-comparison.png --]
[-- Type: image/png, Size: 476505 bytes --]
^ permalink raw reply [relevance 86%]
* Re: [PATCH] New LaTeX code export option: engraved
@ 2022-05-07 6:57 7% ` Timothy
2022-05-07 10:40 8% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2022-05-07 6:57 UTC (permalink / raw)
To: Ihor Radchenko; +Cc: emacs-orgmode, Daniel Fleischer, Nicolas Goaziou
[-- Attachment #1: Type: text/plain, Size: 3409 bytes --]
Hi Ihor,
Find attached an updated patchset, and comments below :)
Ihor Radchenko <yantar92@gmail.com> writes:
> Maybe “The other two options”? Also, using first/second here is a bit
> confusing because: (1) they are actually 3/4 in the above list; (2)
> engraved is listed last.
Docstring changed.
>> +The second more comprehensive option can be used with,
>
> *can be set with
Changed.
> I feel slightly confused about using the word “package” here. Which one
> refers to LaTeX package and which one to Emacs? I would state “Emacs
> package” explicitly to highlight contrast with “LaTeX package”.
Clarifications added to the docstring.
>> +\\{\\\FancyVerbLine}
>
> I’d like to see a comment on what it does.
Added.
> Same request to provide a comment. Also, what it that % TODO doing
> there? It is confusing.
A comment has been added, and the TODO removed.
> Also, it is unclear what the [breakable,xparse] options to tcolorbox are
> doing there and what will happen if the user removes them.
“breakable” allows the box to be broken across pages, and “xparse” provides
`\DeclareTColorBox'.
> Further, I am wondering what is going to happen if the user happens to
> have tcolorbox without options loaded via org-latex-packages-alist.
They could see: ERROR: LaTeX Error: Option clash for package tcolorbox.
They would need to tweak such a tcolorbox entry to include the options used
here.
>> +There is quite a lot of flexibility in what this preamble can be, as long as it:
>> +- Loads the fvextra package.
>> +- Loads the package xcolor (if it is not already loader elsewhere).
>> +- Defines a \“Code\” environment (note the capital C), which can be
>> + later used to wrap \“Verbatim\” environments (provided by fvextra).
>
> The last point is not very clear. What kind of environment?
Anything that the user wants to do to modify the display of the generated
`Verbatim' environment.
>> +(defun org-latex-generate-engraved-preamble (info syntax-colours-p)
>> + “Generate the preamble to setup engraved code.
>> +The result is constructed from `org-latex-engraved-preamble’ and
>> +`org-latex-engraved-options’.”
>
> This is relying on
>
> (:latex-engraved-options nil nil org-latex-engraved-options)
> (:latex-engraved-preamble nil nil org-latex-engraved-preamble)
>
> If it changes any time in future (e.g. to allow per-file settings), the
> docstring may be overlooked.
Docstring tweaked.
> I’d use FIRST-MATCH argument for org-element-map. It will be slightly
> faster on large buffers.
Ah nice, I’ll make use of that.
>> - (downcase org-lang)))
>> + (downcase lang)))
>
> I am not sure if this belongs to this patch. Please double check.
Ooops, moved to the correct patch.
>> + (mapcar ’length
>> + (org-split-string (car code-info)
>> + “”)))))
>
> I am not sure how well it will work with e.g. Chinese characters in comments.
I’ve added a patch replacing `length' with `string-width'
> Maybe the functions could be rewritten using cl-defun with keys and
> &allow-other-keys and then called via apply on a let-bound arg plist?
Done.
All the best,
Timothy
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ox-latex-Refactor-org-latex-src-block.patch --]
[-- Type: text/x-patch, Size: 15942 bytes --]
From d231437e2c9f96bf70520d9ddda810a95667fcdd Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 21 Nov 2021 14:35:34 +0800
Subject: [PATCH 1/7] ox-latex: Refactor `org-latex-src-block'
* lisp/ox-latex.el (org-latex-src-block): Extract the per-format logic
from `org-latex-src-block' into new dedicated functions:
+ `org-latex-src-block--verbatim'
+ `org-latex-src-block--custom'
+ `org-latex-src-block--minted'
+ `org-latex-src-block--listings'
This makes `org-latex-src-block' much less monolithic, taking it from
175 lines to 30, and I find also makes it easier to understand.
---
lisp/ox-latex.el | 339 ++++++++++++++++++++++++++---------------------
1 file changed, 185 insertions(+), 154 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 841ad48bc..c2f728a1c 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2997,164 +2997,195 @@ (defun org-latex-src-block (src-block _contents info)
(float (plist-get attributes :float))
(listings (plist-get info :latex-listings)))
(cond
- ;; Case 1. No source fontification.
((or (not lang) (not listings))
- (let ((caption-str (org-latex--caption/label-string src-block info))
- (verbatim (format "\\begin{verbatim}\n%s\\end{verbatim}"
- (org-export-format-code-default src-block info))))
- (cond ((string= "multicolumn" float)
- (format "\\begin{figure*}[%s]\n%s%s\n%s\\end{figure*}"
- (plist-get info :latex-default-figure-position)
- (if caption-above-p caption-str "")
- verbatim
- (if caption-above-p "" caption-str)))
- (caption (concat
- (if caption-above-p caption-str "")
- verbatim
- (if caption-above-p "" (concat "\n" caption-str))))
- (t verbatim))))
- ;; Case 2. Custom environment.
+ (org-latex-src-block--verbatim src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
(custom-env
- (let ((caption-str (org-latex--caption/label-string src-block info))
- (formatted-src (org-export-format-code-default src-block info)))
- (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
- (format "\\begin{%s}\n%s\\end{%s}\n"
- custom-env
- (concat (and caption-above-p caption-str)
- formatted-src
- (and (not caption-above-p) caption-str))
- custom-env)
- (format-spec custom-env
- `((?s . ,formatted-src)
- (?c . ,caption)
- (?f . ,float)
- (?l . ,(org-latex--label src-block info))
- (?o . ,(or (plist-get attributes :options) "")))))))
- ;; Case 3. Use minted package.
+ (org-latex-src-block--custom src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float custom-env))
((eq listings 'minted)
- (let* ((caption-str (org-latex--caption/label-string src-block info))
- (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
- (plist-get info :latex-default-figure-position)))
- (float-env
- (cond
- ((string= "multicolumn" float)
- (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- (caption
- (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- ((string= "t" float)
- (concat (format "\\begin{listing}[%s]\n"
- placement)
- "%s\n\\end{listing}"))
- (t "%s")))
- (options (plist-get info :latex-minted-options))
- (body
- (format
- "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
- ;; Options.
- (concat
- (org-latex--make-option-string
- (if (or (not num-start) (assoc "linenos" options))
- options
- (append
- `(("linenos")
- ("firstnumber" ,(number-to-string (1+ num-start))))
- options)))
- (let ((local-options (plist-get attributes :options)))
- (and local-options (concat "," local-options))))
- ;; Language.
- (or (cadr (assq (intern lang)
- (plist-get info :latex-minted-langs)))
- (downcase lang))
- ;; Source code.
- (let* ((code-info (org-export-unravel-code src-block))
- (max-width
- (apply 'max
- (mapcar 'length
- (org-split-string (car code-info)
- "\n")))))
- (org-export-format-code
- (car code-info)
- (lambda (loc _num ref)
- (concat
- loc
- (when ref
- ;; Ensure references are flushed to the right,
- ;; separated with 6 spaces from the widest line
- ;; of code.
- (concat (make-string (+ (- max-width (length loc)) 6)
- ?\s)
- (format "(%s)" ref)))))
- nil (and retain-labels (cdr code-info)))))))
- ;; Return value.
- (format float-env body)))
- ;; Case 4. Use listings package.
+ (org-latex-src-block--minted src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
(t
- (let ((lst-lang
- (or (cadr (assq (intern lang)
- (plist-get info :latex-listings-langs)))
- lang))
- (caption-str
- (when caption
- (let ((main (org-export-get-caption src-block))
- (secondary (org-export-get-caption src-block t)))
- (if (not secondary)
- (format "{%s}" (org-export-data main info))
- (format "{[%s]%s}"
- (org-export-data secondary info)
- (org-export-data main info))))))
- (lst-opt (plist-get info :latex-listings-options)))
- (concat
- ;; Options.
- (format
- "\\lstset{%s}\n"
- (concat
- (org-latex--make-option-string
- (append
- lst-opt
- (cond
- ((and (not float) (plist-member attributes :float)) nil)
- ((string= "multicolumn" float) '(("float" "*")))
- ((and float (not (assoc "float" lst-opt)))
- `(("float" ,(plist-get info :latex-default-figure-position)))))
- `(("language" ,lst-lang))
- (if label
- `(("label" ,(org-latex--label src-block info)))
- '(("label" " ")))
- (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
- `(("captionpos" ,(if caption-above-p "t" "b")))
- (cond ((assoc "numbers" lst-opt) nil)
- ((not num-start) '(("numbers" "none")))
- (t `(("firstnumber" ,(number-to-string (1+ num-start)))
- ("numbers" "left"))))))
- (let ((local-options (plist-get attributes :options)))
- (and local-options (concat "," local-options)))))
- ;; Source code.
- (format
- "\\begin{lstlisting}\n%s\\end{lstlisting}"
- (let* ((code-info (org-export-unravel-code src-block))
- (max-width
- (apply 'max
- (mapcar 'length
- (org-split-string (car code-info) "\n")))))
- (org-export-format-code
- (car code-info)
- (lambda (loc _num ref)
- (concat
- loc
- (when ref
- ;; Ensure references are flushed to the right,
- ;; separated with 6 spaces from the widest line of
- ;; code
- (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
- (format "(%s)" ref)))))
- nil (and retain-labels (cdr code-info))))))))))))
-
+ (org-latex-src-block--listings src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))))))
+
+(defun org-latex-src-block--verbatim
+ (src-block info _lang caption caption-above-p _label
+ _num-start _retain-labels _attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using verbatim.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((caption-str (org-latex--caption/label-string src-block info))
+ (verbatim (format "\\begin{verbatim}\n%s\\end{verbatim}"
+ (org-export-format-code-default src-block info))))
+ (cond ((string= "multicolumn" float)
+ (format "\\begin{figure*}[%s]\n%s%s\n%s\\end{figure*}"
+ (plist-get info :latex-default-figure-position)
+ (if caption-above-p caption-str "")
+ verbatim
+ (if caption-above-p "" caption-str)))
+ (caption (concat
+ (if caption-above-p caption-str "")
+ verbatim
+ (if caption-above-p "" (concat "\n" caption-str))))
+ (t verbatim))))
+
+(defun org-latex-src-block--custom
+ (src-block info _lang caption caption-above-p _label
+ _num-start _retain-labels attributes float custom-env)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using a custom environment.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((caption-str (org-latex--caption/label-string src-block info))
+ (formatted-src (org-export-format-code-default src-block info)))
+ (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
+ (format "\\begin{%s}\n%s\\end{%s}\n"
+ custom-env
+ (concat (and caption-above-p caption-str)
+ formatted-src
+ (and (not caption-above-p) caption-str))
+ custom-env)
+ (format-spec custom-env
+ `((?s . ,formatted-src)
+ (?c . ,caption)
+ (?f . ,float)
+ (?l . ,(org-latex--label src-block info))
+ (?o . ,(or (plist-get attributes :options) "")))))))
+
+(defun org-latex-src-block--minted
+ (src-block info lang caption caption-above-p _label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using minted.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let* ((caption-str (org-latex--caption/label-string src-block info))
+ (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
+ (plist-get info :latex-default-figure-position)))
+ (float-env
+ (cond
+ ((string= "multicolumn" float)
+ (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ (caption
+ (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ ((string= "t" float)
+ (concat (format "\\begin{listing}[%s]\n"
+ placement)
+ "%s\n\\end{listing}"))
+ (t "%s")))
+ (options (plist-get info :latex-minted-options))
+ (body
+ (format
+ "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
+ ;; Options.
+ (concat
+ (org-latex--make-option-string
+ (if (or (not num-start) (assoc "linenos" options))
+ options
+ (append
+ `(("linenos")
+ ("firstnumber" ,(number-to-string (1+ num-start))))
+ options)))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (concat "," local-options))))
+ ;; Language.
+ (or (cadr (assq (intern lang)
+ (plist-get info :latex-minted-langs)))
+ (downcase lang))
+ ;; Source code.
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info)
+ "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line
+ ;; of code.
+ (concat (make-string (+ (- max-width (length loc)) 6)
+ ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info)))))))
+ ;; Return value.
+ (format float-env body)))
+
+(defun org-latex-src-block--listings
+ (src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using listings.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((lst-lang
+ (or (cadr (assq (intern lang)
+ (plist-get info :latex-listings-langs)))
+ lang))
+ (caption-str
+ (when caption
+ (let ((main (org-export-get-caption src-block))
+ (secondary (org-export-get-caption src-block t)))
+ (if (not secondary)
+ (format "{%s}" (org-export-data main info))
+ (format "{[%s]%s}"
+ (org-export-data secondary info)
+ (org-export-data main info))))))
+ (lst-opt (plist-get info :latex-listings-options)))
+ (concat
+ ;; Options.
+ (format
+ "\\lstset{%s}\n"
+ (concat
+ (org-latex--make-option-string
+ (append
+ lst-opt
+ (cond
+ ((and (not float) (plist-member attributes :float)) nil)
+ ((string= "multicolumn" float) '(("float" "*")))
+ ((and float (not (assoc "float" lst-opt)))
+ `(("float" ,(plist-get info :latex-default-figure-position)))))
+ `(("language" ,lst-lang))
+ (if label
+ `(("label" ,(org-latex--label src-block info)))
+ '(("label" " ")))
+ (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
+ `(("captionpos" ,(if caption-above-p "t" "b")))
+ (cond ((assoc "numbers" lst-opt) nil)
+ ((not num-start) '(("numbers" "none")))
+ (t `(("firstnumber" ,(number-to-string (1+ num-start)))
+ ("numbers" "left"))))))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (concat "," local-options)))))
+ ;; Source code.
+ (format
+ "\\begin{lstlisting}\n%s\\end{lstlisting}"
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info) "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line of
+ ;; code
+ (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info))))))))
;;;; Statistics Cookie
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-ox-latex-Refactor-org-latex-inline-src-block.patch --]
[-- Type: text/x-patch, Size: 4016 bytes --]
From aa8406ce64507b870322495e8f9b54317930ed24 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Wed, 4 May 2022 18:53:10 +0800
Subject: [PATCH 2/7] ox-latex: Refactor `org-latex-inline-src-block'
* lisp/ox-latex.el (org-latex-inline-src-block,
org-latex-inline-src-block--minted,
org-latex-inline-src-block--listings): Extract the minted and listings
specific logic out of `org-latex-inline-src-block` into the new
functions `org-latex-inline-src-block--minted` and
`org-latex-inline-src-block--listings`.
---
lisp/ox-latex.el | 62 +++++++++++++++++++++++++-----------------------
1 file changed, 32 insertions(+), 30 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index c2f728a1c..38f36a1f3 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2127,36 +2127,38 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
"Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
CONTENTS holds the contents of the item. INFO is a plist holding
contextual information."
- (let* ((code (org-element-property :value inline-src-block))
- (separator (org-latex--find-verb-separator code)))
- (cl-case (plist-get info :latex-listings)
- ;; Do not use a special package: transcode it verbatim, as code.
- ((nil) (org-latex--text-markup code 'code info))
- ;; Use minted package.
- (minted
- (let* ((org-lang (org-element-property :language inline-src-block))
- (mint-lang (or (cadr (assq (intern org-lang)
- (plist-get info :latex-minted-langs)))
- (downcase org-lang)))
- (options (org-latex--make-option-string
- (plist-get info :latex-minted-options))))
- (format "\\mintinline%s{%s}{%s}"
- (if (string= options "") "" (format "[%s]" options))
- mint-lang
- code)))
- ;; Use listings package.
- (otherwise
- ;; Maybe translate language's name.
- (let* ((org-lang (org-element-property :language inline-src-block))
- (lst-lang (or (cadr (assq (intern org-lang)
- (plist-get info :latex-listings-langs)))
- org-lang))
- (options (org-latex--make-option-string
- (append (plist-get info :latex-listings-options)
- `(("language" ,lst-lang))))))
- (concat (format "\\lstinline[%s]" options)
- separator code separator))))))
-
+ (let ((code (org-element-property :value inline-src-block))
+ (lang (org-element-property :language inline-src-block)))
+ (pcase (plist-get info :latex-listings)
+ ('nil (org-latex--text-markup code 'code info))
+ ('minted (org-latex-inline-src-block--minted info code lang))
+ (_ (org-latex-inline-src-block--listings info code lang)))))
+
+(defun org-latex-inline-src-block--minted (info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using minted.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (let ((mint-lang (or (cadr (assq (intern lang)
+ (plist-get info :latex-minted-langs)))
+ (downcase lang)))
+ (options (org-latex--make-option-string
+ (plist-get info :latex-minted-options))))
+ (format "\\mintinline%s{%s}{%s}"
+ (if (string= options "") "" (format "[%s]" options))
+ mint-lang
+ code)))
+
+(defun org-latex-inline-src-block--listings (info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using lstlistings.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (let* ((lst-lang (or (cadr (assq (intern lang)
+ (plist-get info :latex-listings-langs)))
+ lang))
+ (separator (org-latex--find-verb-separator code))
+ (options (org-latex--make-option-string
+ (append (plist-get info :latex-listings-options)
+ `(("language" ,lst-lang))))))
+ (concat (format "\\lstinline[%s]" options)
+ separator code separator)))
;;;; Inlinetask
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: 0003-ox-latex-More-versitile-option-construction.patch --]
[-- Type: text/x-patch, Size: 4116 bytes --]
From 62d6703f0dd622916dff8aab960db6df4cb58cf7 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Wed, 4 May 2022 23:31:59 +0800
Subject: [PATCH 3/7] ox-latex: More versitile option construction
* lisp/ox-latex.el (org-latex--make-option-string): Support a custom
option seperator string.
(org-latex--make-option-string, org-latex-minted-options,
org-latex-listings-options): The first line of the docstrings for
`org-latex-minted-options` and `org-latex-listings-options` describe the
variables as "association lists", yet `org-latex--make-option-string`
does not handle association lists and an example of two-value lists is
provided. To make the behaviour match the docstring,
`org-latex--make-option-string` is modified to work with either a list
two-value lists or an association list, and the examples in
`org-latex-minted-options` and `org-latex-listings-options` updated
accordingly.
---
lisp/ox-latex.el | 38 ++++++++++++++++++++++++--------------
1 file changed, 24 insertions(+), 14 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 38f36a1f3..2d4b3bace 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1006,12 +1006,16 @@ (defcustom org-latex-listings-options nil
These options are supplied as a comma-separated list to the
\\lstset command. Each element of the association list should be
-a list containing two strings: the name of the option, and the
-value. For example,
+a list or cons cell containing two strings: the name of the
+option, and the value. For example,
(setq org-latex-listings-options
\\='((\"basicstyle\" \"\\\\small\")
(\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
+ ; or
+ (setq org-latex-listings-options
+ \\='((\"basicstyle\" . \"\\\\small\")
+ (\"keywordstyle\" . \"\\\\color{black}\\\\bfseries\\\\underbar\")))
will typeset the code in a small size font with underlined, bold
black keywords.
@@ -1059,11 +1063,14 @@ (defcustom org-latex-minted-options nil
These options are supplied within square brackets in
\\begin{minted} environments. Each element of the alist should
-be a list containing two strings: the name of the option, and the
-value. For example,
+be a list or cons cell containing two strings: the name of the
+option, and the value. For example,
(setq org-latex-minted-options
\\='((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
+ ; or
+ (setq org-latex-minted-options
+ \\='((\"bgcolor\" . \"bg\") (\"frame\" . \"lines\")))
will result in source blocks being exported with
@@ -1506,21 +1513,24 @@ (defun org-latex--find-verb-separator (s)
when (not (string-match (regexp-quote (char-to-string c)) s))
return (char-to-string c))))
-(defun org-latex--make-option-string (options)
+(defun org-latex--make-option-string (options &optional seperator)
"Return a comma separated string of keywords and values.
OPTIONS is an alist where the key is the options keyword as
a string, and the value a list containing the keyword value, or
nil."
(mapconcat (lambda (pair)
- (pcase-let ((`(,keyword ,value) pair))
- (concat keyword
- (and (> (length value) 0)
- (concat "="
- (if (string-match-p (rx (any "[]")) value)
- (format "{%s}" value)
- value))))))
- options
- ","))
+ (let ((keyword (car pair))
+ (value (pcase (cdr pair)
+ ((pred stringp) (cdr pair))
+ ((pred consp) (cadr pair)))))
+ (concat keyword
+ (when value
+ (concat "="
+ (if (string-match-p (rx (any "[]")) value)
+ (format "{%s}" value)
+ value))))))
+ options
+ (or seperator ",")))
(defun org-latex--wrap-label (element output info)
"Wrap label associated to ELEMENT around OUTPUT, if appropriate.
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #5: 0004-ox-latex-Introduce-engraved-code-highlighting.patch --]
[-- Type: text/x-patch, Size: 17266 bytes --]
From 410fda41ae1efa67313ebbe2ada381c5d0f0a092 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 21 Nov 2021 20:04:12 +0800
Subject: [PATCH 4/7] ox-latex: Introduce "engraved" code highlighting
* lisp/ox-latex.el (org-latex-src-block, org-latex-src-block--engraved,
org-latex-inline-src-block, org-latex-inline-src-block--engraved,
org-latex-template, org-latex-listings): Make use of the engraved-faces
package (available on ELPA) to provide an alternative LaTeX code
highlighting backend which functions similarly to htmlize.el for HTML
exports.
(org-latex-engraved-preamble, org-latex-engraved-options): Introduce
variables to construct the preamble for engraved code blocks.
* lisp/ox-beamer.el (org-beamer-template): Modify to add engrave-faces
preamble when applicable.
---
lisp/ox-beamer.el | 11 ++
lisp/ox-latex.el | 276 ++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 278 insertions(+), 9 deletions(-)
diff --git a/lisp/ox-beamer.el b/lisp/ox-beamer.el
index 6be73c91e..4d40f6a1d 100644
--- a/lisp/ox-beamer.el
+++ b/lisp/ox-beamer.el
@@ -857,6 +857,17 @@ (defun org-beamer-template (contents info)
(let ((template (plist-get info :latex-hyperref-template)))
(and (stringp template)
(format-spec template (org-latex--format-spec info))))
+ ;; engrave-faces-latex preamble
+ (when (eq org-latex-listings 'engraved)
+ (let ((src-p (org-element-map (plist-get info :parse-tree)
+ '(src-block inline-src-block) #'identity
+ info t))
+ (fixedw-p
+ (org-element-map (plist-get info :parse-tree)
+ '(example-block fixed-width) #'identity
+ info t)))
+ (when (or src-p fixedw-p)
+ (org-latex-generate-engraved-preamble info src-p))))
;; Document start.
"\\begin{document}\n\n"
;; Title command.
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 2d4b3bace..b9c6d4c3e 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -37,6 +37,8 @@ (defvar org-latex-default-packages-alist)
(defvar org-latex-packages-alist)
(defvar orgtbl-exp-regexp)
+(declare-function engrave-faces-latex-gen-preamble "ext:engrave-faces-latex")
+(declare-function engrave-faces-latex-buffer "ext:engrave-faces-latex")
\f
;;; Define Back-End
@@ -125,6 +127,8 @@ (org-export-define-backend 'latex
(:latex-default-quote-environment nil nil org-latex-default-quote-environment)
(:latex-default-table-mode nil nil org-latex-default-table-mode)
(:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format)
+ (:latex-engraved-options nil nil org-latex-engraved-options)
+ (:latex-engraved-preamble nil nil org-latex-engraved-preamble)
(:latex-footnote-defined-format nil nil org-latex-footnote-defined-format)
(:latex-footnote-separator nil nil org-latex-footnote-separator)
(:latex-format-drawer-function nil nil org-latex-format-drawer-function)
@@ -937,22 +941,48 @@ (defcustom org-latex-listings nil
"Non-nil means export source code using the listings package.
This package will fontify source code, possibly even with color.
-If you want to use this, you also need to make LaTeX use the
-listings package, and if you want to have color, the color
-package. Just add these to `org-latex-packages-alist', for
-example using customize, or with something like:
+There are four implementations of this functionality you may
+choose from (ordered from least to most capable):
+1. Verbatim (nil)
+2. Listings (t)
+3. Minted (minted)
+4. Engraved (engraved)
+
+The first two options provide basic syntax
+highlighting (listings), or none at all (verbatim).
+
+When using listings, you also need to make use of the LaTeX
+\"listings\" package. The \"color\" package is also needed if you
+would like color too. These can simply be added to
+`org-latex-packages-alist', using customise or something like:
(require \\='ox-latex)
(add-to-list \\='org-latex-packages-alist \\='(\"\" \"listings\"))
(add-to-list \\='org-latex-packages-alist \\='(\"\" \"color\"))
-Alternatively,
+There are two further options for more comprehensive
+fontification. The first can be set with,
+
+ (setq org-latex-listings \\='engraved)
+
+which causes source code to be run through
+`engrave-faces-latex-buffer', which generates colorings using
+Emacs' font-lock information. This requires the engrave-faces
+package (availible from ELPA), and the fvextra LaTeX package be
+installed.
+
+The styling of the engraved result can customised with
+`org-latex-engraved-preamble' and `org-latex-engraved-options'.
+The default preamble also uses the tcolorbox LaTeX package in
+addition to fvextra.
+
+The second more comprehensive option can be set with,
(setq org-latex-listings \\='minted)
-causes source code to be exported using the minted package as
-opposed to listings. If you want to use minted, you need to add
-the minted package to `org-latex-packages-alist', for example
+which causes source code to be exported using the minted package
+as opposed to listings. If you want to use minted, you need to
+add the minted package to `org-latex-packages-alist', for example
using customize, or with
(require \\='ox-latex)
@@ -971,8 +1001,9 @@ (defcustom org-latex-listings nil
:type '(choice
(const :tag "Use listings" t)
(const :tag "Use minted" minted)
+ (const :tag "Use engrave-faces-latex" engraved)
(const :tag "Export verbatim" nil))
- :safe (lambda (s) (memq s '(t nil minted))))
+ :safe (lambda (s) (memq s '(t nil minted engraved))))
(defcustom org-latex-listings-langs
'((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
@@ -1142,6 +1173,127 @@ (defcustom org-latex-custom-lang-environments nil
:version "26.1"
:package-version '(Org . "9.0"))
+(defcustom org-latex-engraved-preamble
+ "\\usepackage{fvextra}
+
+[FVEXTRA-SETUP]
+
+% Make line numbers smaller and grey.
+\\renewcommand\\theFancyVerbLine{\\footnotesize\\color{black!40!white}\\arabic{FancyVerbLine}}
+
+\\usepackage{xcolor}
+
+\\providecolor{codebackground}{HTML}{f7f7f7}
+\\providecolor{codeborder}{HTML}{f0f0f0}
+\\providecolor{EFD}{HTML}{28292e}
+
+% Define a Code environment to prettily wrap the fontified code.
+\\usepackage[breakable,xparse]{tcolorbox}
+\\DeclareTColorBox[]{Code}{o}%
+{colback=codebackground, colframe=codeborder,
+ fontupper=\\footnotesize\\setlength{\\fboxsep}{0pt},
+ colupper=EFD,
+ IfNoValueTF={#1}%
+ {boxsep=2pt, arc=2.5pt, outer arc=2.5pt,
+ boxrule=0.5pt, left=2pt}%
+ {boxsep=2.5pt, arc=0pt, outer arc=0pt,
+ boxrule=0pt, leftrule=1.5pt, left=0.5pt},
+ right=2pt, top=1pt, bottom=0.5pt,
+ breakable}"
+ "Preamble content injected when using engrave-faces-latex for source blocks.
+This is relevant when `org-latex-listings' is set to `engraved'.
+
+There is quite a lot of flexibility in what this preamble can be, as long as it:
+- Loads the fvextra package.
+- Loads the package xcolor (if it is not already loader elsewhere).
+- Defines a \"Code\" environment (note the capital C), which all
+ \"Verbatim\" environments (provided by fvextra) will be wrapped with.
+
+A macro-like placeholder is used to set fvextra's defaults according to
+`org-latex-engraved-options':
+
+ [FVEXTRA-SETUP]
+
+In the default value the color \"EFD\" is provided as this is the
+foreground colour provided by engrave-faces-latex. When there
+are example/fixed-width blocks only, the engraved generated
+preamble is not included, and so it is provided so we may use it
+anyway."
+ :group 'org-export-latex
+ :type 'string
+ :package-version '(Org . "9.6"))
+
+(defcustom org-latex-engraved-options
+ '(("commandchars" . "\\\\\\{\\}")
+ ("highlightcolor" . "white!95!black!80!blue")
+ ("breaklines" . "true")
+ ("breaksymbol" . "\\color{white!60!black}\\tiny\\ensuremath{\\hookrightarrow}"))
+ "Association list of options for the latex fvextra package when engraving code.
+
+These options are set using \\fvset{...} in the preamble of the
+LaTeX export. Each element of the alist should be a list or cons
+cell containing two strings: the name of the option, and the
+value. For example,
+
+ (setq org-latex-engraved-options
+ \\='((\"highlightcolor\" \"green\") (\"frame\" \"lines\")))
+ ; or
+ (setq org-latex-engraved-options
+ \\='((\"highlightcolor\" . \"green\") (\"frame\" . \"lines\")))
+
+will result in the following LaTeX in the preamble
+
+\\fvset{%
+ bgcolor=bg,
+ frame=lines}
+
+This will affect all fvextra environments. Note that the same
+options will be applied to all blocks. If you need
+block-specific options, you may use the following syntax:
+
+ #+ATTR_LATEX: :options key1=value1,key2=value2
+ #+BEGIN_SRC <LANG>
+ ...
+ #+END_SRC"
+ :group 'org-export-latex
+ :type '(alist :key-type (string :tag "option")
+ :value-type (string :tag "value")))
+
+(defun org-latex-generate-engraved-preamble (info syntax-colours-p)
+ "Generate the preamble to setup engraved code.
+The result is constructed from the :latex-engraved-preamble and
+:latex-engraved-optionsn export options, the default values of
+which are given by `org-latex-engraved-preamble' and
+`org-latex-engraved-options' respectively."
+ (let* ((engraved-options
+ (plist-get info :latex-engraved-options))
+ (engraved-preamble-template
+ (plist-get info :latex-engraved-preamble))
+ (engraved-preamble
+ (if (string-match "^[ \t]*\\[FVEXTRA-SETUP\\][ \t]*\n?"
+ engraved-preamble-template)
+ (replace-match
+ (concat
+ "\\fvset{%\n "
+ (org-latex--make-option-string engraved-options ",\n ")
+ "}\n")
+ t t
+ engraved-preamble-template)
+ engraved-preamble-template)))
+ (if syntax-colours-p
+ (concat
+ "\n% Setup for code blocks [1/2]\n\n"
+ engraved-preamble
+ "\n\n% Setup for code blocks [2/2]: syntax highlighting colors\n"
+ (if (require 'engrave-faces-latex nil t)
+ (engrave-faces-latex-gen-preamble)
+ (message "Cannot engrave source blocks. Consider installing `engrave-faces'.")
+ "% WARNING syntax highlighting unavailible as engrave-faces-latex was missing.\n")
+ "\n")
+ (concat
+ "\n% Setup for code blocks\n\n"
+ engraved-preamble
+ "\n"))))
;;;; Compilation
@@ -1756,6 +1908,17 @@ (defun org-latex-template (contents info)
(let ((template (plist-get info :latex-hyperref-template)))
(and (stringp template)
(format-spec template spec)))
+ ;; engrave-faces-latex preamble
+ (when (eq org-latex-listings 'engraved)
+ (let ((src-p (org-element-map (plist-get info :parse-tree)
+ '(src-block inline-src-block) #'identity
+ info t))
+ (fixedw-p
+ (org-element-map (plist-get info :parse-tree)
+ '(example-block fixed-width) #'identity
+ info t)))
+ (when (or src-p fixedw-p)
+ (org-latex-generate-engraved-preamble info src-p))))
;; Document start.
"\\begin{document}\n\n"
;; Title command.
@@ -2142,6 +2305,7 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
(pcase (plist-get info :latex-listings)
('nil (org-latex--text-markup code 'code info))
('minted (org-latex-inline-src-block--minted info code lang))
+ ('engraved (org-latex-inline-src-block--engraved info code lang))
(_ (org-latex-inline-src-block--listings info code lang)))))
(defun org-latex-inline-src-block--minted (info code lang)
@@ -2157,6 +2321,24 @@ (defun org-latex-inline-src-block--minted (info code lang)
mint-lang
code)))
+(defun org-latex-inline-src-block--engraved (_info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using engrave-faces.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (if (require 'engrave-faces-latex nil t)
+ (let (engraved-buffer engraved-code)
+ (setq engraved-buffer
+ (with-temp-buffer
+ (insert code)
+ (funcall (org-src-get-lang-mode lang))
+ (engrave-faces-latex-buffer)))
+ (setq engraved-code
+ (with-current-buffer engraved-buffer
+ (buffer-string)))
+ (kill-buffer engraved-buffer)
+ (format "\\Verb{%s}"
+ engraved-code))
+ (user-error "Cannot engrave inline src block, `engrave-faces-latex' is unavailible.")))
+
(defun org-latex-inline-src-block--listings (info code lang)
"Transcode an inline src block's content from Org to LaTeX, using lstlistings.
INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
@@ -3017,6 +3199,9 @@ (defun org-latex-src-block (src-block _contents info)
num-start retain-labels attributes float custom-env))
((eq listings 'minted)
(org-latex-src-block--minted src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
+ ((eq listings 'engraved)
+ (org-latex-src-block--engraved src-block info lang caption caption-above-p label
num-start retain-labels attributes float))
(t
(org-latex-src-block--listings src-block info lang caption caption-above-p label
@@ -3133,6 +3318,79 @@ (defun org-latex-src-block--minted
;; Return value.
(format float-env body)))
+(defun org-latex-src-block--engraved
+ (src-block info lang caption caption-above-p _label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using engrave-faces-latex.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (if (require 'engrave-faces-latex nil t)
+ (let* ((caption-str (org-latex--caption/label-string src-block info))
+ (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
+ (plist-get info :latex-default-figure-position)))
+ (float-env
+ (cond
+ ((string= "multicolumn" float)
+ (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ (caption
+ (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ ((string= "t" float)
+ (concat (format "\\begin{listing}[%s]\n"
+ placement)
+ "%s\n\\end{listing}"))
+ (t "%s")))
+ (options (plist-get info :latex-engraved-options))
+ (content-buffer
+ (with-temp-buffer
+ (insert
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info)
+ "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line
+ ;; of code.
+ (concat (make-string (+ (- max-width (length loc)) 6)
+ ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info)))))
+ (funcall (org-src-get-lang-mode lang))
+ (engrave-faces-latex-buffer)))
+ (content
+ (with-current-buffer content-buffer
+ (buffer-string)))
+ (body
+ (format
+ "\\begin{Code}\n\\begin{Verbatim}[%s]\n%s\\end{Verbatim}\n\\end{Code}"
+ ;; Options.
+ (concat
+ (org-latex--make-option-string
+ (append
+ (when (and num-start (not (assoc "linenos" options)))
+ `(("linenos")
+ ("firstnumber" ,(number-to-string (1+ num-start)))))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (list local-options))))))
+ content)))
+ (kill-buffer content-buffer)
+ ;; Return value.
+ (format float-env body))
+ (user-error "Cannot engrave src block, `engrave-faces-latex' is unavailible.")))
+
(defun org-latex-src-block--listings
(src-block info lang caption caption-above-p label
num-start retain-labels attributes float)
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #6: 0005-ox-latex-Don-t-use-length-to-get-string-width.patch --]
[-- Type: text/x-patch, Size: 2005 bytes --]
From 531a8bdd52bc6c9b7ba0da847809c76f14221bb1 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sat, 7 May 2022 13:59:13 +0800
Subject: [PATCH 5/7] ox-latex: Don't use `length' to get string width
* lisp/ox-latex.el (org-latex-src-block--listings,
org-latex-src-block--engraved, org-latex-src-block--minted): Use
`string-width' instead of `length' to gauge the displayed width of the
string in LaTeX. This may not be a perfect match, but it should be an
improvement.
---
lisp/ox-latex.el | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index b9c6d4c3e..6a794c41c 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -3299,7 +3299,7 @@ (defun org-latex-src-block--minted
(let* ((code-info (org-export-unravel-code src-block))
(max-width
(apply 'max
- (mapcar 'length
+ (mapcar 'string-width
(org-split-string (car code-info)
"\n")))))
(org-export-format-code
@@ -3352,7 +3352,7 @@ (defun org-latex-src-block--engraved
(let* ((code-info (org-export-unravel-code src-block))
(max-width
(apply 'max
- (mapcar 'length
+ (mapcar 'string-width
(org-split-string (car code-info)
"\n")))))
(org-export-format-code
@@ -3442,7 +3442,7 @@ (defun org-latex-src-block--listings
(let* ((code-info (org-export-unravel-code src-block))
(max-width
(apply 'max
- (mapcar 'length
+ (mapcar 'string-width
(org-split-string (car code-info) "\n")))))
(org-export-format-code
(car code-info)
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #7: 0006-ox-latex-Refactor-source-block-transcode-fun-sigs.patch --]
[-- Type: text/x-patch, Size: 7091 bytes --]
From 1961039336917e6f4e3698aadccc03444dfc2592 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sat, 7 May 2022 14:02:44 +0800
Subject: [PATCH 6/7] ox-latex: Refactor source block transcode fun sigs
* lisp/ox-latex.el (org-latex-src-block--listings,
org-latex-src-block--engraved, org-latex-src-block--minted,
org-latex-src-block--custom): Refactor these --backend functions to use
cl-defun with keys instead of having an 11-argument signature.
(org-latex-src-block): Adjust for the change in signature of the
--backend functions, and refactor the `cond' statement to use `pcase'.
---
lisp/ox-latex.el | 78 +++++++++++++++++++++++-------------------------
1 file changed, 37 insertions(+), 41 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 6a794c41c..685899c7d 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -3179,37 +3179,37 @@ (defun org-latex-src-block (src-block _contents info)
contextual information."
(when (org-string-nw-p (org-element-property :value src-block))
(let* ((lang (org-element-property :language src-block))
- (caption (org-element-property :caption src-block))
- (caption-above-p (org-latex--caption-above-p src-block info))
- (label (org-element-property :name src-block))
- (custom-env (and lang
- (cadr (assq (intern lang)
- org-latex-custom-lang-environments))))
- (num-start (org-export-get-loc src-block info))
- (retain-labels (org-element-property :retain-labels src-block))
- (attributes (org-export-read-attribute :attr_latex src-block))
- (float (plist-get attributes :float))
- (listings (plist-get info :latex-listings)))
- (cond
- ((or (not lang) (not listings))
- (org-latex-src-block--verbatim src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))
- (custom-env
- (org-latex-src-block--custom src-block info lang caption caption-above-p label
- num-start retain-labels attributes float custom-env))
- ((eq listings 'minted)
- (org-latex-src-block--minted src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))
- ((eq listings 'engraved)
- (org-latex-src-block--engraved src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))
- (t
- (org-latex-src-block--listings src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))))))
-
-(defun org-latex-src-block--verbatim
- (src-block info _lang caption caption-above-p _label
- _num-start _retain-labels _attributes float)
+ (caption (org-element-property :caption src-block))
+ (caption-above-p (org-latex--caption-above-p src-block info))
+ (label (org-element-property :name src-block))
+ (custom-env (and lang
+ (cadr (assq (intern lang)
+ org-latex-custom-lang-environments))))
+ (num-start (org-export-get-loc src-block info))
+ (retain-labels (org-element-property :retain-labels src-block))
+ (attributes (org-export-read-attribute :attr_latex src-block))
+ (float (plist-get attributes :float))
+ (listings (plist-get info :latex-listings)))
+ (funcall
+ (pcase listings
+ ((or (pred not) (guard (not lang))) #'org-latex-src-block--verbatim
+ ((guard custom-env) #'org-latex-src-block--custom)
+ ('minted #'org-latex-src-block--minted)
+ ('engraved #'org-latex-src-block--engraved)
+ (_ #'org-latex-src-block--listings)))
+ :src-block src-block
+ :info info
+ :lang lang
+ :caption caption
+ :caption-above-p caption-above-p
+ :label label
+ :num-start num-start
+ :retain-labels retain-labels
+ :attributes attributes
+ :float float))))
+
+(cl-defun org-latex-src-block--verbatim
+ (&key src-block info caption caption-above-p float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using verbatim.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3228,9 +3228,8 @@ (defun org-latex-src-block--verbatim
(if caption-above-p "" (concat "\n" caption-str))))
(t verbatim))))
-(defun org-latex-src-block--custom
- (src-block info _lang caption caption-above-p _label
- _num-start _retain-labels attributes float custom-env)
+(cl-defun org-latex-src-block--custom
+ (&key src-block info caption caption-above-p attributes float custom-env &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using a custom environment.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3251,8 +3250,7 @@ (defun org-latex-src-block--custom
(?o . ,(or (plist-get attributes :options) "")))))))
(defun org-latex-src-block--minted
- (src-block info lang caption caption-above-p _label
- num-start retain-labels attributes float)
+ (&key src-block info lang caption caption-above-p num-start retain-labels attributes float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using minted.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3319,8 +3317,7 @@ (defun org-latex-src-block--minted
(format float-env body)))
(defun org-latex-src-block--engraved
- (src-block info lang caption caption-above-p _label
- num-start retain-labels attributes float)
+ (&key src-block info lang caption caption-above-p num-start retain-labels attributes float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using engrave-faces-latex.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3391,9 +3388,8 @@ (defun org-latex-src-block--engraved
(format float-env body))
(user-error "Cannot engrave src block, `engrave-faces-latex' is unavailible.")))
-(defun org-latex-src-block--listings
- (src-block info lang caption caption-above-p label
- num-start retain-labels attributes float)
+(cl-defun org-latex-src-block--listings
+ (&key src-block info lang caption caption-above-p label num-start retain-labels attributes float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using listings.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #8: 0007-ox-latex-Replace-org-latex-listings.patch --]
[-- Type: text/x-patch, Size: 16140 bytes --]
From 4d24b74e5490a30c75c7a23721c836a8ce4756b6 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sat, 7 May 2022 14:46:28 +0800
Subject: [PATCH 7/7] ox-latex: Replace `org-latex-listings'
* lisp/ox-latex.el (org-latex-src-block, org-latex-keyword,
org-latex-inline-src-block, org-latex-template,
org-latex--caption/label-string, org-latex-engraved-preamble,
org-latex-listings): Replace `org-latex-listings' with
`org-latex-src-block-backend', which now can be set to listings/verbatim
and no longer advertises t/nil as valid values.
* lisp/ox-beamer.el (org-beamer-template): Update in the same manner as
`org-latex-template'.
* lisp/org-compat.el: Make `org-latex-listings' an obsolete alias for
`org-latex-src-block-backend'.
* testing/lisp/test-ox.el: Replace `org-latex-listings' reference with
`org-latex-src-block-backend'.
* doc/org-manual.org (Footnotes, LaTeX specific properties, Literal
Examples): Replace references to `org-latex-listings' with
`org-latex-src-block-backend'.
* etc/ORG-NEWS: Add a news entry noting this change.
The variable `org-latex-listings' originally indicated whether source
blocks should use the listings LaTeX package, or not. This usage has
evolved over the years, and now it sets one of four different
fontification backends. This renaming should make the variable name a
bit less misleading.
---
doc/org-manual.org | 6 +--
etc/ORG-NEWS | 9 ++++
lisp/org-compat.el | 2 +
lisp/ox-beamer.el | 2 +-
lisp/ox-latex.el | 111 ++++++++++++++++++++++------------------
testing/lisp/test-ox.el | 2 +-
6 files changed, 78 insertions(+), 54 deletions(-)
diff --git a/doc/org-manual.org b/doc/org-manual.org
index c0d38cd8c..60bded419 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -11159,7 +11159,7 @@ ** Literal Examples
#+end_example
#+cindex: formatting source code, markup rules
-#+vindex: org-latex-listings
+#+vindex: org-latex-src-block-backend
If the example is source code from a programming language, or any
other text that can be marked up by Font Lock in Emacs, you can ask
for the example to look like the fontified Emacs buffer[fn:114]. This
@@ -16304,12 +16304,12 @@ **** LaTeX specific properties
| ~:latex-link-with-unknown-path-format~ | ~org-latex-link-with-unknown-path-format~ |
| ~:latex-listings-langs~ | ~org-latex-listings-langs~ |
| ~:latex-listings-options~ | ~org-latex-listings-options~ |
-| ~:latex-listings~ | ~org-latex-listings~ |
| ~:latex-minted-langs~ | ~org-latex-minted-langs~ |
| ~:latex-minted-options~ | ~org-latex-minted-options~ |
| ~:latex-prefer-user-labels~ | ~org-latex-prefer-user-labels~ |
| ~:latex-subtitle-format~ | ~org-latex-subtitle-format~ |
| ~:latex-subtitle-separate~ | ~org-latex-subtitle-separate~ |
+| ~:latex-src-block-backend~ | ~org-latex-src-block-backend~ |
| ~:latex-table-scientific-notation~ | ~org-latex-table-scientific-notation~ |
| ~:latex-tables-booktabs~ | ~org-latex-tables-booktabs~ |
| ~:latex-tables-centered~ | ~org-latex-tables-centered~ |
@@ -22256,7 +22256,7 @@ * Footnotes
version 1.34 of the =htmlize.el= package, which you need to install).
Fontified code chunks in LaTeX can be achieved using either the
[[https://www.ctan.org/pkg/listings][listings]] package or the [[https://www.ctan.org/pkg/minted][minted]] package. Refer to
-~org-latex-listings~ for details.
+~org-latex-src-block-backend~ for details.
[fn:115] Source code in code blocks may also be evaluated either
interactively or on export. See [[*Working with Source Code]] for more
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 1e8558c7b..c5f0bcb33 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -268,6 +268,15 @@ Chmod-style permissions are based on the new variable
~org-babel-tangle-default-file-mode~.
*** A new custom setting =org-agenda-clock-report-header= to add a header to org agenda clock report
+
+*** ~org-latex-listings~ has been replaced with ~org-latex-src-block-backend~
+
+~org-latex-listings~ has been renamed to better reflect the current
+purpose of the variable. The replacement variable
+~org-latex-src-block-backend~ acts in exactly the same way, however it
+accepts =listings= and =verbatim= in place of =t= and =nil= (which
+still work, but are no longer listed as valid options).
+
* Version 9.5
** Important announcements and breaking changes
diff --git a/lisp/org-compat.el b/lisp/org-compat.el
index f599e246e..c1a78e834 100644
--- a/lisp/org-compat.el
+++ b/lisp/org-compat.el
@@ -324,6 +324,8 @@ (define-obsolete-variable-alias 'org-latex-create-formula-image-program
'org-preview-latex-default-process "9.0")
(define-obsolete-variable-alias 'org-latex-preview-ltxpng-directory
'org-preview-latex-image-directory "9.0")
+(define-obsolete-variable-alias 'org-latex-listings
+ 'org-latex-src-block-backend "9.6")
(define-obsolete-function-alias 'org-table-p 'org-at-table-p "9.0")
(define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "9.0")
(define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp "8.3")
diff --git a/lisp/ox-beamer.el b/lisp/ox-beamer.el
index 4d40f6a1d..3baa4e26e 100644
--- a/lisp/ox-beamer.el
+++ b/lisp/ox-beamer.el
@@ -858,7 +858,7 @@ (defun org-beamer-template (contents info)
(and (stringp template)
(format-spec template (org-latex--format-spec info))))
;; engrave-faces-latex preamble
- (when (eq org-latex-listings 'engraved)
+ (when (eq org-latex-src-block-backend 'engraved)
(let ((src-p (org-element-map (plist-get info :parse-tree)
'(src-block inline-src-block) #'identity
info t))
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 685899c7d..8f4206164 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -143,7 +143,7 @@ (org-export-define-backend 'latex
(:latex-inactive-timestamp-format nil nil org-latex-inactive-timestamp-format)
(:latex-inline-image-rules nil nil org-latex-inline-image-rules)
(:latex-link-with-unknown-path-format nil nil org-latex-link-with-unknown-path-format)
- (:latex-listings nil nil org-latex-listings)
+ (:latex-src-block-backend nil nil org-latex-src-block-backend)
(:latex-listings-langs nil nil org-latex-listings-langs)
(:latex-listings-options nil nil org-latex-listings-options)
(:latex-minted-langs nil nil org-latex-minted-langs)
@@ -937,22 +937,22 @@ (defcustom org-latex-format-inlinetask-function
;; Src blocks
-(defcustom org-latex-listings nil
- "Non-nil means export source code using the listings package.
+(defcustom org-latex-src-block-backend 'verbatim
+ "Backend used to generate source code listings.
-This package will fontify source code, possibly even with color.
-There are four implementations of this functionality you may
+This sets the behaviour for fontifying source code, possibly even with
+color. There are four implementations of this functionality you may
choose from (ordered from least to most capable):
-1. Verbatim (nil)
-2. Listings (t)
-3. Minted (minted)
-4. Engraved (engraved)
+1. Verbatim
+2. Listings
+3. Minted
+4. Engraved
The first two options provide basic syntax
highlighting (listings), or none at all (verbatim).
-When using listings, you also need to make use of the LaTeX
-\"listings\" package. The \"color\" package is also needed if you
+When using listings, you also need to make use of LaTeX package
+\"listings\"e. The \"color\" LaTeX package is also needed if you
would like color too. These can simply be added to
`org-latex-packages-alist', using customise or something like:
@@ -963,27 +963,12 @@ (defcustom org-latex-listings nil
There are two further options for more comprehensive
fontification. The first can be set with,
- (setq org-latex-listings \\='engraved)
+ (setq org-latex-src-block-backend \\='minted)
-which causes source code to be run through
-`engrave-faces-latex-buffer', which generates colorings using
-Emacs' font-lock information. This requires the engrave-faces
-package (availible from ELPA), and the fvextra LaTeX package be
-installed.
-
-The styling of the engraved result can customised with
-`org-latex-engraved-preamble' and `org-latex-engraved-options'.
-The default preamble also uses the tcolorbox LaTeX package in
-addition to fvextra.
-
-The second more comprehensive option can be set with,
-
- (setq org-latex-listings \\='minted)
-
-which causes source code to be exported using the minted package
-as opposed to listings. If you want to use minted, you need to
-add the minted package to `org-latex-packages-alist', for example
-using customize, or with
+which causes source code to be exported using the LaTeX package
+minted as opposed to listings. If you want to use minted, you
+need to add the minted package to `org-latex-packages-alist', for
+example using customize, or with
(require \\='ox-latex)
(add-to-list \\='org-latex-packages-alist \\='(\"newfloat\" \"minted\"))
@@ -996,14 +981,29 @@ (defcustom org-latex-listings nil
The minted choice has possible repercussions on the preview of
latex fragments (see `org-preview-latex-fragment'). If you run
into previewing problems, please consult
-URL `https://orgmode.org/worg/org-tutorials/org-latex-preview.html'."
+URL `https://orgmode.org/worg/org-tutorials/org-latex-preview.html'.
+
+The most comprehensive option can be set with,
+
+ (setq org-latex-src-block-backend \\='engraved)
+
+which causes source code to be run through
+`engrave-faces-latex-buffer', which generates colorings using
+Emacs' font-lock information. This requires the Emacs package
+engrave-faces (availible from ELPA), and the LaTeX package
+fvextra be installed.
+
+The styling of the engraved result can customised with
+`org-latex-engraved-preamble' and `org-latex-engraved-options'.
+The default preamble also uses the LaTeX package tcolorbox in
+addition to fvextra."
:group 'org-export-latex
:type '(choice
- (const :tag "Use listings" t)
+ (const :tag "Use listings" listings)
(const :tag "Use minted" minted)
(const :tag "Use engrave-faces-latex" engraved)
- (const :tag "Export verbatim" nil))
- :safe (lambda (s) (memq s '(t nil minted engraved))))
+ (const :tag "Export verbatim" verbatim))
+ :safe (lambda (s) (memq s '(listings minted engraved verbatim))))
(defcustom org-latex-listings-langs
'((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
@@ -1201,7 +1201,7 @@ (defcustom org-latex-engraved-preamble
right=2pt, top=1pt, bottom=0.5pt,
breakable}"
"Preamble content injected when using engrave-faces-latex for source blocks.
-This is relevant when `org-latex-listings' is set to `engraved'.
+This is relevant when `org-latex-src-block-backend' is set to `engraved'.
There is quite a lot of flexibility in what this preamble can be, as long as it:
- Loads the fvextra package.
@@ -1502,7 +1502,8 @@ (defun org-latex--caption/label-string (element info)
main)
(and (eq type 'src-block)
(not (plist-get attr :float))
- (null (plist-get info :latex-listings)))))
+ (memq (plist-get info :latex-src-block-backend)
+ '(verbatim nil)))))
(short (org-export-get-caption element t))
(caption-from-attr-latex (plist-get attr :caption)))
(cond
@@ -1522,7 +1523,8 @@ (defun org-latex--caption/label-string (element info)
(paragraph "figure")
(image "figure")
(special-block "figure")
- (src-block (if (plist-get info :latex-listings)
+ (src-block (if (not (memq (plist-get info :latex-src-block-backend)
+ '(verbatim nil)))
"listing"
"figure"))
(t (symbol-name type*)))
@@ -1909,7 +1911,7 @@ (defun org-latex-template (contents info)
(and (stringp template)
(format-spec template spec)))
;; engrave-faces-latex preamble
- (when (eq org-latex-listings 'engraved)
+ (when (eq org-latex-src-block-backend 'engraved)
(let ((src-p (org-element-map (plist-get info :parse-tree)
'(src-block inline-src-block) #'identity
info t))
@@ -2302,11 +2304,17 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
contextual information."
(let ((code (org-element-property :value inline-src-block))
(lang (org-element-property :language inline-src-block)))
- (pcase (plist-get info :latex-listings)
- ('nil (org-latex--text-markup code 'code info))
+ (pcase (plist-get info :latex-src-block-backend)
+ ('verbatim (org-latex--text-markup code 'code info))
('minted (org-latex-inline-src-block--minted info code lang))
('engraved (org-latex-inline-src-block--engraved info code lang))
- (_ (org-latex-inline-src-block--listings info code lang)))))
+ ('listings (org-latex-inline-src-block--listings info code lang))
+ (oldval
+ (message "Please update the LaTeX src-block-backend to %s"
+ (if oldval "listings" "verbatim"))
+ (if oldval
+ (org-latex-inline-src-block--listings info code lang)
+ (org-latex--text-markup code 'code info))))))
(defun org-latex-inline-src-block--minted (info code lang)
"Transcode an inline src block's content from Org to LaTeX, using minted.
@@ -2502,7 +2510,7 @@ (defun org-latex-keyword (keyword _contents info)
(concat depth (and depth "\n") "\\tableofcontents"))))
((string-match-p "\\<tables\\>" value) "\\listoftables")
((string-match-p "\\<listings\\>" value)
- (cl-case (plist-get info :latex-listings)
+ (cl-case (plist-get info :latex-src-block-backend)
((nil) "\\listoffigures")
(minted "\\listoflistings")
(otherwise "\\lstlistoflistings")))))))))
@@ -3188,15 +3196,20 @@ (defun org-latex-src-block (src-block _contents info)
(num-start (org-export-get-loc src-block info))
(retain-labels (org-element-property :retain-labels src-block))
(attributes (org-export-read-attribute :attr_latex src-block))
- (float (plist-get attributes :float))
- (listings (plist-get info :latex-listings)))
+ (float (plist-get attributes :float)))
(funcall
- (pcase listings
- ((or (pred not) (guard (not lang))) #'org-latex-src-block--verbatim
- ((guard custom-env) #'org-latex-src-block--custom)
+ (pcase (plist-get info :latex-src-block-backend)
+ ((or 'verbatim (guard (not lang))) #'org-latex-src-block--verbatim
('minted #'org-latex-src-block--minted)
('engraved #'org-latex-src-block--engraved)
- (_ #'org-latex-src-block--listings)))
+ ('listings #'org-latex-src-block--listings)
+ ((guard custom-env) #'org-latex-src-block--custom)
+ (oldval
+ (message "Please update the LaTeX src-block-backend to %s"
+ (if oldval "listings" "verbatim"))
+ (if oldval
+ #'org-latex-src-block--listings
+ #'org-latex-src-block--verbatim))))
:src-block src-block
:info info
:lang lang
diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el
index 25e02b258..28f950813 100644
--- a/testing/lisp/test-ox.el
+++ b/testing/lisp/test-ox.el
@@ -3978,7 +3978,7 @@ (ert-deftest test-org-export/latex-src-block-verbatim-caption ()
\\end{verbatim}
\\caption{Caption is below, 60\\%s}
\\end{figure*}"
- (let ((org-latex-listings 'minted) ; inactive due to missing lang
+ (let ((org-latex-src-block-backend 'minted) ; inactive due to missing lang
(org-latex-default-figure-position "tp"))
;; Namely "multicolumn" value to get just figure environment
;; looks like a bug.
--
2.35.3
^ permalink raw reply related [relevance 7%]
* Re: [PATCH] New LaTeX code export option: engraved
2022-05-07 6:57 7% ` Timothy
@ 2022-05-07 10:40 8% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2022-05-07 10:40 UTC (permalink / raw)
To: Ihor Radchenko; +Cc: emacs-orgmode, Daniel Fleischer, Nicolas Goaziou
[-- Attachment #1: Type: text/plain, Size: 131 bytes --]
Ooops, a paren error managed to slip in there.
Here’s a new set, with a little extra refactoring slipped in.
–
Timothy
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ox-latex-Refactor-org-latex-src-block.patch --]
[-- Type: text/x-patch, Size: 15942 bytes --]
From d231437e2c9f96bf70520d9ddda810a95667fcdd Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 21 Nov 2021 14:35:34 +0800
Subject: [PATCH 1/7] ox-latex: Refactor `org-latex-src-block'
* lisp/ox-latex.el (org-latex-src-block): Extract the per-format logic
from `org-latex-src-block' into new dedicated functions:
+ `org-latex-src-block--verbatim'
+ `org-latex-src-block--custom'
+ `org-latex-src-block--minted'
+ `org-latex-src-block--listings'
This makes `org-latex-src-block' much less monolithic, taking it from
175 lines to 30, and I find also makes it easier to understand.
---
lisp/ox-latex.el | 339 ++++++++++++++++++++++++++---------------------
1 file changed, 185 insertions(+), 154 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 841ad48bc..c2f728a1c 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2997,164 +2997,195 @@ (defun org-latex-src-block (src-block _contents info)
(float (plist-get attributes :float))
(listings (plist-get info :latex-listings)))
(cond
- ;; Case 1. No source fontification.
((or (not lang) (not listings))
- (let ((caption-str (org-latex--caption/label-string src-block info))
- (verbatim (format "\\begin{verbatim}\n%s\\end{verbatim}"
- (org-export-format-code-default src-block info))))
- (cond ((string= "multicolumn" float)
- (format "\\begin{figure*}[%s]\n%s%s\n%s\\end{figure*}"
- (plist-get info :latex-default-figure-position)
- (if caption-above-p caption-str "")
- verbatim
- (if caption-above-p "" caption-str)))
- (caption (concat
- (if caption-above-p caption-str "")
- verbatim
- (if caption-above-p "" (concat "\n" caption-str))))
- (t verbatim))))
- ;; Case 2. Custom environment.
+ (org-latex-src-block--verbatim src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
(custom-env
- (let ((caption-str (org-latex--caption/label-string src-block info))
- (formatted-src (org-export-format-code-default src-block info)))
- (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
- (format "\\begin{%s}\n%s\\end{%s}\n"
- custom-env
- (concat (and caption-above-p caption-str)
- formatted-src
- (and (not caption-above-p) caption-str))
- custom-env)
- (format-spec custom-env
- `((?s . ,formatted-src)
- (?c . ,caption)
- (?f . ,float)
- (?l . ,(org-latex--label src-block info))
- (?o . ,(or (plist-get attributes :options) "")))))))
- ;; Case 3. Use minted package.
+ (org-latex-src-block--custom src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float custom-env))
((eq listings 'minted)
- (let* ((caption-str (org-latex--caption/label-string src-block info))
- (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
- (plist-get info :latex-default-figure-position)))
- (float-env
- (cond
- ((string= "multicolumn" float)
- (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- (caption
- (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- ((string= "t" float)
- (concat (format "\\begin{listing}[%s]\n"
- placement)
- "%s\n\\end{listing}"))
- (t "%s")))
- (options (plist-get info :latex-minted-options))
- (body
- (format
- "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
- ;; Options.
- (concat
- (org-latex--make-option-string
- (if (or (not num-start) (assoc "linenos" options))
- options
- (append
- `(("linenos")
- ("firstnumber" ,(number-to-string (1+ num-start))))
- options)))
- (let ((local-options (plist-get attributes :options)))
- (and local-options (concat "," local-options))))
- ;; Language.
- (or (cadr (assq (intern lang)
- (plist-get info :latex-minted-langs)))
- (downcase lang))
- ;; Source code.
- (let* ((code-info (org-export-unravel-code src-block))
- (max-width
- (apply 'max
- (mapcar 'length
- (org-split-string (car code-info)
- "\n")))))
- (org-export-format-code
- (car code-info)
- (lambda (loc _num ref)
- (concat
- loc
- (when ref
- ;; Ensure references are flushed to the right,
- ;; separated with 6 spaces from the widest line
- ;; of code.
- (concat (make-string (+ (- max-width (length loc)) 6)
- ?\s)
- (format "(%s)" ref)))))
- nil (and retain-labels (cdr code-info)))))))
- ;; Return value.
- (format float-env body)))
- ;; Case 4. Use listings package.
+ (org-latex-src-block--minted src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
(t
- (let ((lst-lang
- (or (cadr (assq (intern lang)
- (plist-get info :latex-listings-langs)))
- lang))
- (caption-str
- (when caption
- (let ((main (org-export-get-caption src-block))
- (secondary (org-export-get-caption src-block t)))
- (if (not secondary)
- (format "{%s}" (org-export-data main info))
- (format "{[%s]%s}"
- (org-export-data secondary info)
- (org-export-data main info))))))
- (lst-opt (plist-get info :latex-listings-options)))
- (concat
- ;; Options.
- (format
- "\\lstset{%s}\n"
- (concat
- (org-latex--make-option-string
- (append
- lst-opt
- (cond
- ((and (not float) (plist-member attributes :float)) nil)
- ((string= "multicolumn" float) '(("float" "*")))
- ((and float (not (assoc "float" lst-opt)))
- `(("float" ,(plist-get info :latex-default-figure-position)))))
- `(("language" ,lst-lang))
- (if label
- `(("label" ,(org-latex--label src-block info)))
- '(("label" " ")))
- (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
- `(("captionpos" ,(if caption-above-p "t" "b")))
- (cond ((assoc "numbers" lst-opt) nil)
- ((not num-start) '(("numbers" "none")))
- (t `(("firstnumber" ,(number-to-string (1+ num-start)))
- ("numbers" "left"))))))
- (let ((local-options (plist-get attributes :options)))
- (and local-options (concat "," local-options)))))
- ;; Source code.
- (format
- "\\begin{lstlisting}\n%s\\end{lstlisting}"
- (let* ((code-info (org-export-unravel-code src-block))
- (max-width
- (apply 'max
- (mapcar 'length
- (org-split-string (car code-info) "\n")))))
- (org-export-format-code
- (car code-info)
- (lambda (loc _num ref)
- (concat
- loc
- (when ref
- ;; Ensure references are flushed to the right,
- ;; separated with 6 spaces from the widest line of
- ;; code
- (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
- (format "(%s)" ref)))))
- nil (and retain-labels (cdr code-info))))))))))))
-
+ (org-latex-src-block--listings src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))))))
+
+(defun org-latex-src-block--verbatim
+ (src-block info _lang caption caption-above-p _label
+ _num-start _retain-labels _attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using verbatim.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((caption-str (org-latex--caption/label-string src-block info))
+ (verbatim (format "\\begin{verbatim}\n%s\\end{verbatim}"
+ (org-export-format-code-default src-block info))))
+ (cond ((string= "multicolumn" float)
+ (format "\\begin{figure*}[%s]\n%s%s\n%s\\end{figure*}"
+ (plist-get info :latex-default-figure-position)
+ (if caption-above-p caption-str "")
+ verbatim
+ (if caption-above-p "" caption-str)))
+ (caption (concat
+ (if caption-above-p caption-str "")
+ verbatim
+ (if caption-above-p "" (concat "\n" caption-str))))
+ (t verbatim))))
+
+(defun org-latex-src-block--custom
+ (src-block info _lang caption caption-above-p _label
+ _num-start _retain-labels attributes float custom-env)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using a custom environment.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((caption-str (org-latex--caption/label-string src-block info))
+ (formatted-src (org-export-format-code-default src-block info)))
+ (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
+ (format "\\begin{%s}\n%s\\end{%s}\n"
+ custom-env
+ (concat (and caption-above-p caption-str)
+ formatted-src
+ (and (not caption-above-p) caption-str))
+ custom-env)
+ (format-spec custom-env
+ `((?s . ,formatted-src)
+ (?c . ,caption)
+ (?f . ,float)
+ (?l . ,(org-latex--label src-block info))
+ (?o . ,(or (plist-get attributes :options) "")))))))
+
+(defun org-latex-src-block--minted
+ (src-block info lang caption caption-above-p _label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using minted.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let* ((caption-str (org-latex--caption/label-string src-block info))
+ (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
+ (plist-get info :latex-default-figure-position)))
+ (float-env
+ (cond
+ ((string= "multicolumn" float)
+ (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ (caption
+ (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ ((string= "t" float)
+ (concat (format "\\begin{listing}[%s]\n"
+ placement)
+ "%s\n\\end{listing}"))
+ (t "%s")))
+ (options (plist-get info :latex-minted-options))
+ (body
+ (format
+ "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
+ ;; Options.
+ (concat
+ (org-latex--make-option-string
+ (if (or (not num-start) (assoc "linenos" options))
+ options
+ (append
+ `(("linenos")
+ ("firstnumber" ,(number-to-string (1+ num-start))))
+ options)))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (concat "," local-options))))
+ ;; Language.
+ (or (cadr (assq (intern lang)
+ (plist-get info :latex-minted-langs)))
+ (downcase lang))
+ ;; Source code.
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info)
+ "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line
+ ;; of code.
+ (concat (make-string (+ (- max-width (length loc)) 6)
+ ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info)))))))
+ ;; Return value.
+ (format float-env body)))
+
+(defun org-latex-src-block--listings
+ (src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using listings.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((lst-lang
+ (or (cadr (assq (intern lang)
+ (plist-get info :latex-listings-langs)))
+ lang))
+ (caption-str
+ (when caption
+ (let ((main (org-export-get-caption src-block))
+ (secondary (org-export-get-caption src-block t)))
+ (if (not secondary)
+ (format "{%s}" (org-export-data main info))
+ (format "{[%s]%s}"
+ (org-export-data secondary info)
+ (org-export-data main info))))))
+ (lst-opt (plist-get info :latex-listings-options)))
+ (concat
+ ;; Options.
+ (format
+ "\\lstset{%s}\n"
+ (concat
+ (org-latex--make-option-string
+ (append
+ lst-opt
+ (cond
+ ((and (not float) (plist-member attributes :float)) nil)
+ ((string= "multicolumn" float) '(("float" "*")))
+ ((and float (not (assoc "float" lst-opt)))
+ `(("float" ,(plist-get info :latex-default-figure-position)))))
+ `(("language" ,lst-lang))
+ (if label
+ `(("label" ,(org-latex--label src-block info)))
+ '(("label" " ")))
+ (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
+ `(("captionpos" ,(if caption-above-p "t" "b")))
+ (cond ((assoc "numbers" lst-opt) nil)
+ ((not num-start) '(("numbers" "none")))
+ (t `(("firstnumber" ,(number-to-string (1+ num-start)))
+ ("numbers" "left"))))))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (concat "," local-options)))))
+ ;; Source code.
+ (format
+ "\\begin{lstlisting}\n%s\\end{lstlisting}"
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info) "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line of
+ ;; code
+ (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info))))))))
;;;; Statistics Cookie
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-ox-latex-Refactor-org-latex-inline-src-block.patch --]
[-- Type: text/x-patch, Size: 4016 bytes --]
From aa8406ce64507b870322495e8f9b54317930ed24 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Wed, 4 May 2022 18:53:10 +0800
Subject: [PATCH 2/7] ox-latex: Refactor `org-latex-inline-src-block'
* lisp/ox-latex.el (org-latex-inline-src-block,
org-latex-inline-src-block--minted,
org-latex-inline-src-block--listings): Extract the minted and listings
specific logic out of `org-latex-inline-src-block` into the new
functions `org-latex-inline-src-block--minted` and
`org-latex-inline-src-block--listings`.
---
lisp/ox-latex.el | 62 +++++++++++++++++++++++++-----------------------
1 file changed, 32 insertions(+), 30 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index c2f728a1c..38f36a1f3 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2127,36 +2127,38 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
"Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
CONTENTS holds the contents of the item. INFO is a plist holding
contextual information."
- (let* ((code (org-element-property :value inline-src-block))
- (separator (org-latex--find-verb-separator code)))
- (cl-case (plist-get info :latex-listings)
- ;; Do not use a special package: transcode it verbatim, as code.
- ((nil) (org-latex--text-markup code 'code info))
- ;; Use minted package.
- (minted
- (let* ((org-lang (org-element-property :language inline-src-block))
- (mint-lang (or (cadr (assq (intern org-lang)
- (plist-get info :latex-minted-langs)))
- (downcase org-lang)))
- (options (org-latex--make-option-string
- (plist-get info :latex-minted-options))))
- (format "\\mintinline%s{%s}{%s}"
- (if (string= options "") "" (format "[%s]" options))
- mint-lang
- code)))
- ;; Use listings package.
- (otherwise
- ;; Maybe translate language's name.
- (let* ((org-lang (org-element-property :language inline-src-block))
- (lst-lang (or (cadr (assq (intern org-lang)
- (plist-get info :latex-listings-langs)))
- org-lang))
- (options (org-latex--make-option-string
- (append (plist-get info :latex-listings-options)
- `(("language" ,lst-lang))))))
- (concat (format "\\lstinline[%s]" options)
- separator code separator))))))
-
+ (let ((code (org-element-property :value inline-src-block))
+ (lang (org-element-property :language inline-src-block)))
+ (pcase (plist-get info :latex-listings)
+ ('nil (org-latex--text-markup code 'code info))
+ ('minted (org-latex-inline-src-block--minted info code lang))
+ (_ (org-latex-inline-src-block--listings info code lang)))))
+
+(defun org-latex-inline-src-block--minted (info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using minted.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (let ((mint-lang (or (cadr (assq (intern lang)
+ (plist-get info :latex-minted-langs)))
+ (downcase lang)))
+ (options (org-latex--make-option-string
+ (plist-get info :latex-minted-options))))
+ (format "\\mintinline%s{%s}{%s}"
+ (if (string= options "") "" (format "[%s]" options))
+ mint-lang
+ code)))
+
+(defun org-latex-inline-src-block--listings (info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using lstlistings.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (let* ((lst-lang (or (cadr (assq (intern lang)
+ (plist-get info :latex-listings-langs)))
+ lang))
+ (separator (org-latex--find-verb-separator code))
+ (options (org-latex--make-option-string
+ (append (plist-get info :latex-listings-options)
+ `(("language" ,lst-lang))))))
+ (concat (format "\\lstinline[%s]" options)
+ separator code separator)))
;;;; Inlinetask
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: 0003-ox-latex-More-versitile-option-construction.patch --]
[-- Type: text/x-patch, Size: 4116 bytes --]
From 62d6703f0dd622916dff8aab960db6df4cb58cf7 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Wed, 4 May 2022 23:31:59 +0800
Subject: [PATCH 3/7] ox-latex: More versitile option construction
* lisp/ox-latex.el (org-latex--make-option-string): Support a custom
option seperator string.
(org-latex--make-option-string, org-latex-minted-options,
org-latex-listings-options): The first line of the docstrings for
`org-latex-minted-options` and `org-latex-listings-options` describe the
variables as "association lists", yet `org-latex--make-option-string`
does not handle association lists and an example of two-value lists is
provided. To make the behaviour match the docstring,
`org-latex--make-option-string` is modified to work with either a list
two-value lists or an association list, and the examples in
`org-latex-minted-options` and `org-latex-listings-options` updated
accordingly.
---
lisp/ox-latex.el | 38 ++++++++++++++++++++++++--------------
1 file changed, 24 insertions(+), 14 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 38f36a1f3..2d4b3bace 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1006,12 +1006,16 @@ (defcustom org-latex-listings-options nil
These options are supplied as a comma-separated list to the
\\lstset command. Each element of the association list should be
-a list containing two strings: the name of the option, and the
-value. For example,
+a list or cons cell containing two strings: the name of the
+option, and the value. For example,
(setq org-latex-listings-options
\\='((\"basicstyle\" \"\\\\small\")
(\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
+ ; or
+ (setq org-latex-listings-options
+ \\='((\"basicstyle\" . \"\\\\small\")
+ (\"keywordstyle\" . \"\\\\color{black}\\\\bfseries\\\\underbar\")))
will typeset the code in a small size font with underlined, bold
black keywords.
@@ -1059,11 +1063,14 @@ (defcustom org-latex-minted-options nil
These options are supplied within square brackets in
\\begin{minted} environments. Each element of the alist should
-be a list containing two strings: the name of the option, and the
-value. For example,
+be a list or cons cell containing two strings: the name of the
+option, and the value. For example,
(setq org-latex-minted-options
\\='((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
+ ; or
+ (setq org-latex-minted-options
+ \\='((\"bgcolor\" . \"bg\") (\"frame\" . \"lines\")))
will result in source blocks being exported with
@@ -1506,21 +1513,24 @@ (defun org-latex--find-verb-separator (s)
when (not (string-match (regexp-quote (char-to-string c)) s))
return (char-to-string c))))
-(defun org-latex--make-option-string (options)
+(defun org-latex--make-option-string (options &optional seperator)
"Return a comma separated string of keywords and values.
OPTIONS is an alist where the key is the options keyword as
a string, and the value a list containing the keyword value, or
nil."
(mapconcat (lambda (pair)
- (pcase-let ((`(,keyword ,value) pair))
- (concat keyword
- (and (> (length value) 0)
- (concat "="
- (if (string-match-p (rx (any "[]")) value)
- (format "{%s}" value)
- value))))))
- options
- ","))
+ (let ((keyword (car pair))
+ (value (pcase (cdr pair)
+ ((pred stringp) (cdr pair))
+ ((pred consp) (cadr pair)))))
+ (concat keyword
+ (when value
+ (concat "="
+ (if (string-match-p (rx (any "[]")) value)
+ (format "{%s}" value)
+ value))))))
+ options
+ (or seperator ",")))
(defun org-latex--wrap-label (element output info)
"Wrap label associated to ELEMENT around OUTPUT, if appropriate.
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #5: 0004-ox-latex-Introduce-engraved-code-highlighting.patch --]
[-- Type: text/x-patch, Size: 17212 bytes --]
From 81155a80fb261812718dcb991fd22f87bb1d5ef6 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 21 Nov 2021 20:04:12 +0800
Subject: [PATCH 4/7] ox-latex: Introduce "engraved" code highlighting
* lisp/ox-latex.el (org-latex-src-block, org-latex-src-block--engraved,
org-latex-inline-src-block, org-latex-inline-src-block--engraved,
org-latex-src--engrave-code, org-latex-template, org-latex-listings):
Make use of the engraved-faces package (available on ELPA) to provide an
alternative LaTeX code highlighting backend which functions similarly to
htmlize.el for HTML exports.
(org-latex-engraved-preamble, org-latex-engraved-options): Introduce
variables to construct the preamble for engraved code blocks.
* lisp/ox-beamer.el (org-beamer-template): Modify to add engrave-faces
preamble when applicable.
tweak
---
lisp/ox-beamer.el | 11 ++
lisp/ox-latex.el | 272 ++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 274 insertions(+), 9 deletions(-)
diff --git a/lisp/ox-beamer.el b/lisp/ox-beamer.el
index 6be73c91e..4d40f6a1d 100644
--- a/lisp/ox-beamer.el
+++ b/lisp/ox-beamer.el
@@ -857,6 +857,17 @@ (defun org-beamer-template (contents info)
(let ((template (plist-get info :latex-hyperref-template)))
(and (stringp template)
(format-spec template (org-latex--format-spec info))))
+ ;; engrave-faces-latex preamble
+ (when (eq org-latex-listings 'engraved)
+ (let ((src-p (org-element-map (plist-get info :parse-tree)
+ '(src-block inline-src-block) #'identity
+ info t))
+ (fixedw-p
+ (org-element-map (plist-get info :parse-tree)
+ '(example-block fixed-width) #'identity
+ info t)))
+ (when (or src-p fixedw-p)
+ (org-latex-generate-engraved-preamble info src-p))))
;; Document start.
"\\begin{document}\n\n"
;; Title command.
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 2d4b3bace..10cd6d76d 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -37,6 +37,8 @@ (defvar org-latex-default-packages-alist)
(defvar org-latex-packages-alist)
(defvar orgtbl-exp-regexp)
+(declare-function engrave-faces-latex-gen-preamble "ext:engrave-faces-latex")
+(declare-function engrave-faces-latex-buffer "ext:engrave-faces-latex")
\f
;;; Define Back-End
@@ -125,6 +127,8 @@ (org-export-define-backend 'latex
(:latex-default-quote-environment nil nil org-latex-default-quote-environment)
(:latex-default-table-mode nil nil org-latex-default-table-mode)
(:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format)
+ (:latex-engraved-options nil nil org-latex-engraved-options)
+ (:latex-engraved-preamble nil nil org-latex-engraved-preamble)
(:latex-footnote-defined-format nil nil org-latex-footnote-defined-format)
(:latex-footnote-separator nil nil org-latex-footnote-separator)
(:latex-format-drawer-function nil nil org-latex-format-drawer-function)
@@ -937,22 +941,48 @@ (defcustom org-latex-listings nil
"Non-nil means export source code using the listings package.
This package will fontify source code, possibly even with color.
-If you want to use this, you also need to make LaTeX use the
-listings package, and if you want to have color, the color
-package. Just add these to `org-latex-packages-alist', for
-example using customize, or with something like:
+There are four implementations of this functionality you may
+choose from (ordered from least to most capable):
+1. Verbatim (nil)
+2. Listings (t)
+3. Minted (minted)
+4. Engraved (engraved)
+
+The first two options provide basic syntax
+highlighting (listings), or none at all (verbatim).
+
+When using listings, you also need to make use of the LaTeX
+\"listings\" package. The \"color\" package is also needed if you
+would like color too. These can simply be added to
+`org-latex-packages-alist', using customise or something like:
(require \\='ox-latex)
(add-to-list \\='org-latex-packages-alist \\='(\"\" \"listings\"))
(add-to-list \\='org-latex-packages-alist \\='(\"\" \"color\"))
-Alternatively,
+There are two further options for more comprehensive
+fontification. The first can be set with,
+
+ (setq org-latex-listings \\='engraved)
+
+which causes source code to be run through
+`engrave-faces-latex-buffer', which generates colorings using
+Emacs' font-lock information. This requires the engrave-faces
+package (availible from ELPA), and the fvextra LaTeX package be
+installed.
+
+The styling of the engraved result can customised with
+`org-latex-engraved-preamble' and `org-latex-engraved-options'.
+The default preamble also uses the tcolorbox LaTeX package in
+addition to fvextra.
+
+The second more comprehensive option can be set with,
(setq org-latex-listings \\='minted)
-causes source code to be exported using the minted package as
-opposed to listings. If you want to use minted, you need to add
-the minted package to `org-latex-packages-alist', for example
+which causes source code to be exported using the minted package
+as opposed to listings. If you want to use minted, you need to
+add the minted package to `org-latex-packages-alist', for example
using customize, or with
(require \\='ox-latex)
@@ -971,8 +1001,9 @@ (defcustom org-latex-listings nil
:type '(choice
(const :tag "Use listings" t)
(const :tag "Use minted" minted)
+ (const :tag "Use engrave-faces-latex" engraved)
(const :tag "Export verbatim" nil))
- :safe (lambda (s) (memq s '(t nil minted))))
+ :safe (lambda (s) (memq s '(t nil minted engraved))))
(defcustom org-latex-listings-langs
'((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
@@ -1142,6 +1173,127 @@ (defcustom org-latex-custom-lang-environments nil
:version "26.1"
:package-version '(Org . "9.0"))
+(defcustom org-latex-engraved-preamble
+ "\\usepackage{fvextra}
+
+[FVEXTRA-SETUP]
+
+% Make line numbers smaller and grey.
+\\renewcommand\\theFancyVerbLine{\\footnotesize\\color{black!40!white}\\arabic{FancyVerbLine}}
+
+\\usepackage{xcolor}
+
+\\providecolor{codebackground}{HTML}{f7f7f7}
+\\providecolor{codeborder}{HTML}{f0f0f0}
+\\providecolor{EFD}{HTML}{28292e}
+
+% Define a Code environment to prettily wrap the fontified code.
+\\usepackage[breakable,xparse]{tcolorbox}
+\\DeclareTColorBox[]{Code}{o}%
+{colback=codebackground, colframe=codeborder,
+ fontupper=\\footnotesize\\setlength{\\fboxsep}{0pt},
+ colupper=EFD,
+ IfNoValueTF={#1}%
+ {boxsep=2pt, arc=2.5pt, outer arc=2.5pt,
+ boxrule=0.5pt, left=2pt}%
+ {boxsep=2.5pt, arc=0pt, outer arc=0pt,
+ boxrule=0pt, leftrule=1.5pt, left=0.5pt},
+ right=2pt, top=1pt, bottom=0.5pt,
+ breakable}"
+ "Preamble content injected when using engrave-faces-latex for source blocks.
+This is relevant when `org-latex-listings' is set to `engraved'.
+
+There is quite a lot of flexibility in what this preamble can be, as long as it:
+- Loads the fvextra package.
+- Loads the package xcolor (if it is not already loader elsewhere).
+- Defines a \"Code\" environment (note the capital C), which all
+ \"Verbatim\" environments (provided by fvextra) will be wrapped with.
+
+A macro-like placeholder is used to set fvextra's defaults according to
+`org-latex-engraved-options':
+
+ [FVEXTRA-SETUP]
+
+In the default value the color \"EFD\" is provided as this is the
+foreground colour provided by engrave-faces-latex. When there
+are example/fixed-width blocks only, the engraved generated
+preamble is not included, and so it is provided so we may use it
+anyway."
+ :group 'org-export-latex
+ :type 'string
+ :package-version '(Org . "9.6"))
+
+(defcustom org-latex-engraved-options
+ '(("commandchars" . "\\\\\\{\\}")
+ ("highlightcolor" . "white!95!black!80!blue")
+ ("breaklines" . "true")
+ ("breaksymbol" . "\\color{white!60!black}\\tiny\\ensuremath{\\hookrightarrow}"))
+ "Association list of options for the latex fvextra package when engraving code.
+
+These options are set using \\fvset{...} in the preamble of the
+LaTeX export. Each element of the alist should be a list or cons
+cell containing two strings: the name of the option, and the
+value. For example,
+
+ (setq org-latex-engraved-options
+ \\='((\"highlightcolor\" \"green\") (\"frame\" \"lines\")))
+ ; or
+ (setq org-latex-engraved-options
+ \\='((\"highlightcolor\" . \"green\") (\"frame\" . \"lines\")))
+
+will result in the following LaTeX in the preamble
+
+\\fvset{%
+ bgcolor=bg,
+ frame=lines}
+
+This will affect all fvextra environments. Note that the same
+options will be applied to all blocks. If you need
+block-specific options, you may use the following syntax:
+
+ #+ATTR_LATEX: :options key1=value1,key2=value2
+ #+BEGIN_SRC <LANG>
+ ...
+ #+END_SRC"
+ :group 'org-export-latex
+ :type '(alist :key-type (string :tag "option")
+ :value-type (string :tag "value")))
+
+(defun org-latex-generate-engraved-preamble (info syntax-colours-p)
+ "Generate the preamble to setup engraved code.
+The result is constructed from the :latex-engraved-preamble and
+:latex-engraved-optionsn export options, the default values of
+which are given by `org-latex-engraved-preamble' and
+`org-latex-engraved-options' respectively."
+ (let* ((engraved-options
+ (plist-get info :latex-engraved-options))
+ (engraved-preamble-template
+ (plist-get info :latex-engraved-preamble))
+ (engraved-preamble
+ (if (string-match "^[ \t]*\\[FVEXTRA-SETUP\\][ \t]*\n?"
+ engraved-preamble-template)
+ (replace-match
+ (concat
+ "\\fvset{%\n "
+ (org-latex--make-option-string engraved-options ",\n ")
+ "}\n")
+ t t
+ engraved-preamble-template)
+ engraved-preamble-template)))
+ (if syntax-colours-p
+ (concat
+ "\n% Setup for code blocks [1/2]\n\n"
+ engraved-preamble
+ "\n\n% Setup for code blocks [2/2]: syntax highlighting colors\n"
+ (if (require 'engrave-faces-latex nil t)
+ (engrave-faces-latex-gen-preamble)
+ (message "Cannot engrave source blocks. Consider installing `engrave-faces'.")
+ "% WARNING syntax highlighting unavailible as engrave-faces-latex was missing.\n")
+ "\n")
+ (concat
+ "\n% Setup for code blocks\n\n"
+ engraved-preamble
+ "\n"))))
;;;; Compilation
@@ -1756,6 +1908,17 @@ (defun org-latex-template (contents info)
(let ((template (plist-get info :latex-hyperref-template)))
(and (stringp template)
(format-spec template spec)))
+ ;; engrave-faces-latex preamble
+ (when (eq org-latex-listings 'engraved)
+ (let ((src-p (org-element-map (plist-get info :parse-tree)
+ '(src-block inline-src-block) #'identity
+ info t))
+ (fixedw-p
+ (org-element-map (plist-get info :parse-tree)
+ '(example-block fixed-width) #'identity
+ info t)))
+ (when (or src-p fixedw-p)
+ (org-latex-generate-engraved-preamble info src-p))))
;; Document start.
"\\begin{document}\n\n"
;; Title command.
@@ -2142,6 +2305,7 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
(pcase (plist-get info :latex-listings)
('nil (org-latex--text-markup code 'code info))
('minted (org-latex-inline-src-block--minted info code lang))
+ ('engraved (org-latex-inline-src-block--engraved info code lang))
(_ (org-latex-inline-src-block--listings info code lang)))))
(defun org-latex-inline-src-block--minted (info code lang)
@@ -2157,6 +2321,11 @@ (defun org-latex-inline-src-block--minted (info code lang)
mint-lang
code)))
+(defun org-latex-inline-src-block--engraved (_info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using engrave-faces.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (format "\\Verb{%s}" (org-latex-src--engrave-code code lang)))
+
(defun org-latex-inline-src-block--listings (info code lang)
"Transcode an inline src block's content from Org to LaTeX, using lstlistings.
INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
@@ -3017,6 +3186,9 @@ (defun org-latex-src-block (src-block _contents info)
num-start retain-labels attributes float custom-env))
((eq listings 'minted)
(org-latex-src-block--minted src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
+ ((eq listings 'engraved)
+ (org-latex-src-block--engraved src-block info lang caption caption-above-p label
num-start retain-labels attributes float))
(t
(org-latex-src-block--listings src-block info lang caption caption-above-p label
@@ -3133,6 +3305,88 @@ (defun org-latex-src-block--minted
;; Return value.
(format float-env body)))
+(defun org-latex-src--engrave-code (content lang)
+ "Engrave CONTENT to LaTeX in a LANG-mode buffer, and give the result."
+ (if (require 'engrave-faces-latex nil t)
+ (let* ((lang-mode (and lang (org-src-get-lang-mode lang)))
+ (engraved-buffer
+ (with-temp-buffer
+ (insert content)
+ (when lang-mode
+ (if (functionp lang-mode)
+ (funcall lang-mode)
+ (message "Cannot engrave code as %s. %s is undefined."
+ lang lang-mode)))
+ (engrave-faces-latex-buffer)))
+ (engraved-code
+ (with-current-buffer engraved-buffer
+ (buffer-string))))
+ (kill-buffer engraved-buffer)
+ engraved-code)
+ (user-error "Cannot engrave code as `engrave-faces-latex' is unavailible.")))
+
+(defun org-latex-src-block--engraved
+ (src-block info lang caption caption-above-p _label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using engrave-faces-latex.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let* ((caption-str (org-latex--caption/label-string src-block info))
+ (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
+ (plist-get info :latex-default-figure-position)))
+ (float-env
+ (cond
+ ((string= "multicolumn" float)
+ (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ (caption
+ (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ ((string= "t" float)
+ (concat (format "\\begin{listing}[%s]\n"
+ placement)
+ "%s\n\\end{listing}"))
+ (t "%s")))
+ (options (plist-get info :latex-engraved-options))
+ (content
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'string-width
+ (org-split-string (car code-info)
+ "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line
+ ;; of code.
+ (concat (make-string (+ (- max-width (length loc)) 6)
+ ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info)))))
+ (body
+ (format
+ "\\begin{Code}\n\\begin{Verbatim}[%s]\n%s\\end{Verbatim}\n\\end{Code}"
+ ;; Options.
+ (concat
+ (org-latex--make-option-string
+ (append
+ (when (and num-start (not (assoc "linenos" options)))
+ `(("linenos")
+ ("firstnumber" ,(number-to-string (1+ num-start)))))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (list local-options))))))
+ (org-latex-src--engrave-code content lang))))
+ (format float-env body)))
+
(defun org-latex-src-block--listings
(src-block info lang caption caption-above-p label
num-start retain-labels attributes float)
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #6: 0005-ox-latex-Don-t-use-length-to-get-string-width.patch --]
[-- Type: text/x-patch, Size: 1537 bytes --]
From 96cf9853329dacd69745a225a889855dd13e5c2c Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sat, 7 May 2022 13:59:13 +0800
Subject: [PATCH 5/7] ox-latex: Don't use `length' to get string width
* lisp/ox-latex.el (org-latex-src-block--listings,
org-latex-src-block--engraved, org-latex-src-block--minted): Use
`string-width' instead of `length' to gauge the displayed width of the
string in LaTeX. This may not be a perfect match, but it should be an
improvement.
---
lisp/ox-latex.el | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 10cd6d76d..afd7ee48e 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -3286,7 +3286,7 @@ (defun org-latex-src-block--minted
(let* ((code-info (org-export-unravel-code src-block))
(max-width
(apply 'max
- (mapcar 'length
+ (mapcar 'string-width
(org-split-string (car code-info)
"\n")))))
(org-export-format-code
@@ -3438,7 +3438,7 @@ (defun org-latex-src-block--listings
(let* ((code-info (org-export-unravel-code src-block))
(max-width
(apply 'max
- (mapcar 'length
+ (mapcar 'string-width
(org-split-string (car code-info) "\n")))))
(org-export-format-code
(car code-info)
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #7: 0006-ox-latex-Refactor-source-block-transcode-fun-sigs.patch --]
[-- Type: text/x-patch, Size: 7297 bytes --]
From f191a03ef97f5f8e29b18b73238ab659709ae230 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sat, 7 May 2022 14:02:44 +0800
Subject: [PATCH 6/7] ox-latex: Refactor source block transcode fun sigs
* lisp/ox-latex.el (org-latex-src-block--listings,
org-latex-src-block--engraved, org-latex-src-block--minted,
org-latex-src-block--custom): Refactor these --backend functions to use
cl-defun with keys instead of having an 11-argument signature.
(org-latex-src-block): Adjust for the change in signature of the
--backend functions, and refactor the `cond' statement to use `pcase'.
---
lisp/ox-latex.el | 82 +++++++++++++++++++++++-------------------------
1 file changed, 39 insertions(+), 43 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index afd7ee48e..9150c1011 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -3166,37 +3166,37 @@ (defun org-latex-src-block (src-block _contents info)
contextual information."
(when (org-string-nw-p (org-element-property :value src-block))
(let* ((lang (org-element-property :language src-block))
- (caption (org-element-property :caption src-block))
- (caption-above-p (org-latex--caption-above-p src-block info))
- (label (org-element-property :name src-block))
- (custom-env (and lang
- (cadr (assq (intern lang)
- org-latex-custom-lang-environments))))
- (num-start (org-export-get-loc src-block info))
- (retain-labels (org-element-property :retain-labels src-block))
- (attributes (org-export-read-attribute :attr_latex src-block))
- (float (plist-get attributes :float))
- (listings (plist-get info :latex-listings)))
- (cond
- ((or (not lang) (not listings))
- (org-latex-src-block--verbatim src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))
- (custom-env
- (org-latex-src-block--custom src-block info lang caption caption-above-p label
- num-start retain-labels attributes float custom-env))
- ((eq listings 'minted)
- (org-latex-src-block--minted src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))
- ((eq listings 'engraved)
- (org-latex-src-block--engraved src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))
- (t
- (org-latex-src-block--listings src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))))))
-
-(defun org-latex-src-block--verbatim
- (src-block info _lang caption caption-above-p _label
- _num-start _retain-labels _attributes float)
+ (caption (org-element-property :caption src-block))
+ (caption-above-p (org-latex--caption-above-p src-block info))
+ (label (org-element-property :name src-block))
+ (custom-env (and lang
+ (cadr (assq (intern lang)
+ org-latex-custom-lang-environments))))
+ (num-start (org-export-get-loc src-block info))
+ (retain-labels (org-element-property :retain-labels src-block))
+ (attributes (org-export-read-attribute :attr_latex src-block))
+ (float (plist-get attributes :float))
+ (listings (plist-get info :latex-listings)))
+ (funcall
+ (pcase listings
+ ((or (pred not) (guard (not lang))) #'org-latex-src-block--verbatim)
+ ((guard custom-env) #'org-latex-src-block--custom)
+ ('minted #'org-latex-src-block--minted)
+ ('engraved #'org-latex-src-block--engraved)
+ (_ #'org-latex-src-block--listings))
+ :src-block src-block
+ :info info
+ :lang lang
+ :caption caption
+ :caption-above-p caption-above-p
+ :label label
+ :num-start num-start
+ :retain-labels retain-labels
+ :attributes attributes
+ :float float))))
+
+(cl-defun org-latex-src-block--verbatim
+ (&key src-block info caption caption-above-p float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using verbatim.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3215,9 +3215,8 @@ (defun org-latex-src-block--verbatim
(if caption-above-p "" (concat "\n" caption-str))))
(t verbatim))))
-(defun org-latex-src-block--custom
- (src-block info _lang caption caption-above-p _label
- _num-start _retain-labels attributes float custom-env)
+(cl-defun org-latex-src-block--custom
+ (&key src-block info caption caption-above-p attributes float custom-env &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using a custom environment.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3237,9 +3236,8 @@ (defun org-latex-src-block--custom
(?l . ,(org-latex--label src-block info))
(?o . ,(or (plist-get attributes :options) "")))))))
-(defun org-latex-src-block--minted
- (src-block info lang caption caption-above-p _label
- num-start retain-labels attributes float)
+(cl-defun org-latex-src-block--minted
+ (&key src-block info lang caption caption-above-p num-start retain-labels attributes float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using minted.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3325,9 +3323,8 @@ (defun org-latex-src--engrave-code (content lang)
engraved-code)
(user-error "Cannot engrave code as `engrave-faces-latex' is unavailible.")))
-(defun org-latex-src-block--engraved
- (src-block info lang caption caption-above-p _label
- num-start retain-labels attributes float)
+(cl-defun org-latex-src-block--engraved
+ (&key src-block info lang caption caption-above-p num-start retain-labels attributes float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using engrave-faces-latex.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3387,9 +3384,8 @@ (defun org-latex-src-block--engraved
(org-latex-src--engrave-code content lang))))
(format float-env body)))
-(defun org-latex-src-block--listings
- (src-block info lang caption caption-above-p label
- num-start retain-labels attributes float)
+(cl-defun org-latex-src-block--listings
+ (&key src-block info lang caption caption-above-p label num-start retain-labels attributes float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using listings.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #8: 0007-ox-latex-Replace-org-latex-listings.patch --]
[-- Type: text/x-patch, Size: 16128 bytes --]
From adb4b6df3b4549fd6afd6cfb6ae8945e89882cfc Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sat, 7 May 2022 14:46:28 +0800
Subject: [PATCH 7/7] ox-latex: Replace `org-latex-listings'
* lisp/ox-latex.el (org-latex-src-block, org-latex-keyword,
org-latex-inline-src-block, org-latex-template,
org-latex--caption/label-string, org-latex-engraved-preamble,
org-latex-listings): Replace `org-latex-listings' with
`org-latex-src-block-backend', which now can be set to listings/verbatim
and no longer advertises t/nil as valid values.
* lisp/ox-beamer.el (org-beamer-template): Update in the same manner as
`org-latex-template'.
* lisp/org-compat.el: Make `org-latex-listings' an obsolete alias for
`org-latex-src-block-backend'.
* testing/lisp/test-ox.el: Replace `org-latex-listings' reference with
`org-latex-src-block-backend'.
* doc/org-manual.org (Footnotes, LaTeX specific properties, Literal
Examples): Replace references to `org-latex-listings' with
`org-latex-src-block-backend'.
* etc/ORG-NEWS: Add a news entry noting this change.
The variable `org-latex-listings' originally indicated whether source
blocks should use the listings LaTeX package, or not. This usage has
evolved over the years, and now it sets one of four different
fontification backends. This renaming should make the variable name a
bit less misleading.
---
doc/org-manual.org | 6 +--
etc/ORG-NEWS | 9 ++++
lisp/org-compat.el | 2 +
lisp/ox-beamer.el | 2 +-
lisp/ox-latex.el | 111 ++++++++++++++++++++++------------------
testing/lisp/test-ox.el | 2 +-
6 files changed, 78 insertions(+), 54 deletions(-)
diff --git a/doc/org-manual.org b/doc/org-manual.org
index c0d38cd8c..60bded419 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -11159,7 +11159,7 @@ ** Literal Examples
#+end_example
#+cindex: formatting source code, markup rules
-#+vindex: org-latex-listings
+#+vindex: org-latex-src-block-backend
If the example is source code from a programming language, or any
other text that can be marked up by Font Lock in Emacs, you can ask
for the example to look like the fontified Emacs buffer[fn:114]. This
@@ -16304,12 +16304,12 @@ **** LaTeX specific properties
| ~:latex-link-with-unknown-path-format~ | ~org-latex-link-with-unknown-path-format~ |
| ~:latex-listings-langs~ | ~org-latex-listings-langs~ |
| ~:latex-listings-options~ | ~org-latex-listings-options~ |
-| ~:latex-listings~ | ~org-latex-listings~ |
| ~:latex-minted-langs~ | ~org-latex-minted-langs~ |
| ~:latex-minted-options~ | ~org-latex-minted-options~ |
| ~:latex-prefer-user-labels~ | ~org-latex-prefer-user-labels~ |
| ~:latex-subtitle-format~ | ~org-latex-subtitle-format~ |
| ~:latex-subtitle-separate~ | ~org-latex-subtitle-separate~ |
+| ~:latex-src-block-backend~ | ~org-latex-src-block-backend~ |
| ~:latex-table-scientific-notation~ | ~org-latex-table-scientific-notation~ |
| ~:latex-tables-booktabs~ | ~org-latex-tables-booktabs~ |
| ~:latex-tables-centered~ | ~org-latex-tables-centered~ |
@@ -22256,7 +22256,7 @@ * Footnotes
version 1.34 of the =htmlize.el= package, which you need to install).
Fontified code chunks in LaTeX can be achieved using either the
[[https://www.ctan.org/pkg/listings][listings]] package or the [[https://www.ctan.org/pkg/minted][minted]] package. Refer to
-~org-latex-listings~ for details.
+~org-latex-src-block-backend~ for details.
[fn:115] Source code in code blocks may also be evaluated either
interactively or on export. See [[*Working with Source Code]] for more
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 1e8558c7b..c5f0bcb33 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -268,6 +268,15 @@ Chmod-style permissions are based on the new variable
~org-babel-tangle-default-file-mode~.
*** A new custom setting =org-agenda-clock-report-header= to add a header to org agenda clock report
+
+*** ~org-latex-listings~ has been replaced with ~org-latex-src-block-backend~
+
+~org-latex-listings~ has been renamed to better reflect the current
+purpose of the variable. The replacement variable
+~org-latex-src-block-backend~ acts in exactly the same way, however it
+accepts =listings= and =verbatim= in place of =t= and =nil= (which
+still work, but are no longer listed as valid options).
+
* Version 9.5
** Important announcements and breaking changes
diff --git a/lisp/org-compat.el b/lisp/org-compat.el
index f599e246e..c1a78e834 100644
--- a/lisp/org-compat.el
+++ b/lisp/org-compat.el
@@ -324,6 +324,8 @@ (define-obsolete-variable-alias 'org-latex-create-formula-image-program
'org-preview-latex-default-process "9.0")
(define-obsolete-variable-alias 'org-latex-preview-ltxpng-directory
'org-preview-latex-image-directory "9.0")
+(define-obsolete-variable-alias 'org-latex-listings
+ 'org-latex-src-block-backend "9.6")
(define-obsolete-function-alias 'org-table-p 'org-at-table-p "9.0")
(define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "9.0")
(define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp "8.3")
diff --git a/lisp/ox-beamer.el b/lisp/ox-beamer.el
index 4d40f6a1d..3baa4e26e 100644
--- a/lisp/ox-beamer.el
+++ b/lisp/ox-beamer.el
@@ -858,7 +858,7 @@ (defun org-beamer-template (contents info)
(and (stringp template)
(format-spec template (org-latex--format-spec info))))
;; engrave-faces-latex preamble
- (when (eq org-latex-listings 'engraved)
+ (when (eq org-latex-src-block-backend 'engraved)
(let ((src-p (org-element-map (plist-get info :parse-tree)
'(src-block inline-src-block) #'identity
info t))
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 9150c1011..ffa89188b 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -143,7 +143,7 @@ (org-export-define-backend 'latex
(:latex-inactive-timestamp-format nil nil org-latex-inactive-timestamp-format)
(:latex-inline-image-rules nil nil org-latex-inline-image-rules)
(:latex-link-with-unknown-path-format nil nil org-latex-link-with-unknown-path-format)
- (:latex-listings nil nil org-latex-listings)
+ (:latex-src-block-backend nil nil org-latex-src-block-backend)
(:latex-listings-langs nil nil org-latex-listings-langs)
(:latex-listings-options nil nil org-latex-listings-options)
(:latex-minted-langs nil nil org-latex-minted-langs)
@@ -937,22 +937,22 @@ (defcustom org-latex-format-inlinetask-function
;; Src blocks
-(defcustom org-latex-listings nil
- "Non-nil means export source code using the listings package.
+(defcustom org-latex-src-block-backend 'verbatim
+ "Backend used to generate source code listings.
-This package will fontify source code, possibly even with color.
-There are four implementations of this functionality you may
+This sets the behaviour for fontifying source code, possibly even with
+color. There are four implementations of this functionality you may
choose from (ordered from least to most capable):
-1. Verbatim (nil)
-2. Listings (t)
-3. Minted (minted)
-4. Engraved (engraved)
+1. Verbatim
+2. Listings
+3. Minted
+4. Engraved
The first two options provide basic syntax
highlighting (listings), or none at all (verbatim).
-When using listings, you also need to make use of the LaTeX
-\"listings\" package. The \"color\" package is also needed if you
+When using listings, you also need to make use of LaTeX package
+\"listings\"e. The \"color\" LaTeX package is also needed if you
would like color too. These can simply be added to
`org-latex-packages-alist', using customise or something like:
@@ -963,27 +963,12 @@ (defcustom org-latex-listings nil
There are two further options for more comprehensive
fontification. The first can be set with,
- (setq org-latex-listings \\='engraved)
+ (setq org-latex-src-block-backend \\='minted)
-which causes source code to be run through
-`engrave-faces-latex-buffer', which generates colorings using
-Emacs' font-lock information. This requires the engrave-faces
-package (availible from ELPA), and the fvextra LaTeX package be
-installed.
-
-The styling of the engraved result can customised with
-`org-latex-engraved-preamble' and `org-latex-engraved-options'.
-The default preamble also uses the tcolorbox LaTeX package in
-addition to fvextra.
-
-The second more comprehensive option can be set with,
-
- (setq org-latex-listings \\='minted)
-
-which causes source code to be exported using the minted package
-as opposed to listings. If you want to use minted, you need to
-add the minted package to `org-latex-packages-alist', for example
-using customize, or with
+which causes source code to be exported using the LaTeX package
+minted as opposed to listings. If you want to use minted, you
+need to add the minted package to `org-latex-packages-alist', for
+example using customize, or with
(require \\='ox-latex)
(add-to-list \\='org-latex-packages-alist \\='(\"newfloat\" \"minted\"))
@@ -996,14 +981,29 @@ (defcustom org-latex-listings nil
The minted choice has possible repercussions on the preview of
latex fragments (see `org-preview-latex-fragment'). If you run
into previewing problems, please consult
-URL `https://orgmode.org/worg/org-tutorials/org-latex-preview.html'."
+URL `https://orgmode.org/worg/org-tutorials/org-latex-preview.html'.
+
+The most comprehensive option can be set with,
+
+ (setq org-latex-src-block-backend \\='engraved)
+
+which causes source code to be run through
+`engrave-faces-latex-buffer', which generates colorings using
+Emacs' font-lock information. This requires the Emacs package
+engrave-faces (availible from ELPA), and the LaTeX package
+fvextra be installed.
+
+The styling of the engraved result can customised with
+`org-latex-engraved-preamble' and `org-latex-engraved-options'.
+The default preamble also uses the LaTeX package tcolorbox in
+addition to fvextra."
:group 'org-export-latex
:type '(choice
- (const :tag "Use listings" t)
+ (const :tag "Use listings" listings)
(const :tag "Use minted" minted)
(const :tag "Use engrave-faces-latex" engraved)
- (const :tag "Export verbatim" nil))
- :safe (lambda (s) (memq s '(t nil minted engraved))))
+ (const :tag "Export verbatim" verbatim))
+ :safe (lambda (s) (memq s '(listings minted engraved verbatim))))
(defcustom org-latex-listings-langs
'((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
@@ -1201,7 +1201,7 @@ (defcustom org-latex-engraved-preamble
right=2pt, top=1pt, bottom=0.5pt,
breakable}"
"Preamble content injected when using engrave-faces-latex for source blocks.
-This is relevant when `org-latex-listings' is set to `engraved'.
+This is relevant when `org-latex-src-block-backend' is set to `engraved'.
There is quite a lot of flexibility in what this preamble can be, as long as it:
- Loads the fvextra package.
@@ -1502,7 +1502,8 @@ (defun org-latex--caption/label-string (element info)
main)
(and (eq type 'src-block)
(not (plist-get attr :float))
- (null (plist-get info :latex-listings)))))
+ (memq (plist-get info :latex-src-block-backend)
+ '(verbatim nil)))))
(short (org-export-get-caption element t))
(caption-from-attr-latex (plist-get attr :caption)))
(cond
@@ -1522,7 +1523,8 @@ (defun org-latex--caption/label-string (element info)
(paragraph "figure")
(image "figure")
(special-block "figure")
- (src-block (if (plist-get info :latex-listings)
+ (src-block (if (not (memq (plist-get info :latex-src-block-backend)
+ '(verbatim nil)))
"listing"
"figure"))
(t (symbol-name type*)))
@@ -1909,7 +1911,7 @@ (defun org-latex-template (contents info)
(and (stringp template)
(format-spec template spec)))
;; engrave-faces-latex preamble
- (when (eq org-latex-listings 'engraved)
+ (when (eq org-latex-src-block-backend 'engraved)
(let ((src-p (org-element-map (plist-get info :parse-tree)
'(src-block inline-src-block) #'identity
info t))
@@ -2302,11 +2304,17 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
contextual information."
(let ((code (org-element-property :value inline-src-block))
(lang (org-element-property :language inline-src-block)))
- (pcase (plist-get info :latex-listings)
- ('nil (org-latex--text-markup code 'code info))
+ (pcase (plist-get info :latex-src-block-backend)
+ ('verbatim (org-latex--text-markup code 'code info))
('minted (org-latex-inline-src-block--minted info code lang))
('engraved (org-latex-inline-src-block--engraved info code lang))
- (_ (org-latex-inline-src-block--listings info code lang)))))
+ ('listings (org-latex-inline-src-block--listings info code lang))
+ (oldval
+ (message "Please update the LaTeX src-block-backend to %s"
+ (if oldval "listings" "verbatim"))
+ (if oldval
+ (org-latex-inline-src-block--listings info code lang)
+ (org-latex--text-markup code 'code info))))))
(defun org-latex-inline-src-block--minted (info code lang)
"Transcode an inline src block's content from Org to LaTeX, using minted.
@@ -2489,7 +2497,7 @@ (defun org-latex-keyword (keyword _contents info)
(concat depth (and depth "\n") "\\tableofcontents"))))
((string-match-p "\\<tables\\>" value) "\\listoftables")
((string-match-p "\\<listings\\>" value)
- (cl-case (plist-get info :latex-listings)
+ (cl-case (plist-get info :latex-src-block-backend)
((nil) "\\listoffigures")
(minted "\\listoflistings")
(otherwise "\\lstlistoflistings")))))))))
@@ -3175,15 +3183,20 @@ (defun org-latex-src-block (src-block _contents info)
(num-start (org-export-get-loc src-block info))
(retain-labels (org-element-property :retain-labels src-block))
(attributes (org-export-read-attribute :attr_latex src-block))
- (float (plist-get attributes :float))
- (listings (plist-get info :latex-listings)))
+ (float (plist-get attributes :float)))
(funcall
- (pcase listings
- ((or (pred not) (guard (not lang))) #'org-latex-src-block--verbatim)
- ((guard custom-env) #'org-latex-src-block--custom)
+ (pcase (plist-get info :latex-src-block-backend)
+ ((or 'verbatim (guard (not lang))) #'org-latex-src-block--verbatim)
('minted #'org-latex-src-block--minted)
('engraved #'org-latex-src-block--engraved)
- (_ #'org-latex-src-block--listings))
+ ('listings #'org-latex-src-block--listings)
+ ((guard custom-env) #'org-latex-src-block--custom)
+ (oldval
+ (message "Please update the LaTeX src-block-backend to %s"
+ (if oldval "listings" "verbatim"))
+ (if oldval
+ #'org-latex-src-block--listings
+ #'org-latex-src-block--verbatim)))
:src-block src-block
:info info
:lang lang
diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el
index 25e02b258..28f950813 100644
--- a/testing/lisp/test-ox.el
+++ b/testing/lisp/test-ox.el
@@ -3978,7 +3978,7 @@ (ert-deftest test-org-export/latex-src-block-verbatim-caption ()
\\end{verbatim}
\\caption{Caption is below, 60\\%s}
\\end{figure*}"
- (let ((org-latex-listings 'minted) ; inactive due to missing lang
+ (let ((org-latex-src-block-backend 'minted) ; inactive due to missing lang
(org-latex-default-figure-position "tp"))
;; Namely "multicolumn" value to get just figure environment
;; looks like a bug.
--
2.35.3
^ permalink raw reply related [relevance 8%]
* Re: [PATCH] (v2) New LaTeX code export option: engraved
@ 2022-05-08 14:30 6% ` Timothy
2022-05-11 16:05 5% ` [PATCH] (v3) " Timothy
0 siblings, 2 replies; 74+ results
From: Timothy @ 2022-05-08 14:30 UTC (permalink / raw)
To: Daniel Fleischer; +Cc: Ihor Radchenko, emacs-orgmode, Nicolas Goaziou
[-- Attachment #1: Type: text/plain, Size: 2337 bytes --]
Hi Daniel & co.,
I have a new set of patches (attached), which make use of a major new feature in
engrave-faces v0.3: engraved themes.
Now, the theme used for source blocks can be set by the document keyword
`#+latex_engraved_theme: THEME-NAME'. Furthermore, the theme can be customised on
a per-code-block basis with `#+attr_latex: :engraved-theme THEME-NAME'.
If you try this out, make sure you have the most recent commit of engrave-faces
(531ea687e, soon to be released in v0.3.1).
I’ve also fixed the “Bug: Percentage in caption (even escaped) does not work in
LaTeX export” that Max raised
(<https://list.orgmode.org/YT2PR01MB45101E27DC6251D8F8B7B366F6EC9@YT2PR01MB4510.CANPRD01.PROD.OUTLOOK.COM>).
Daniel Fleischer <danflscr@gmail.com> writes:
> Very nice set of patches, it looks good.
Glad to hear you’ve been able to have a look over the patches. With the feedback
I’ve received here so far, if no issues come up in the next few days I’m
inclined to merge this, add documentation, and see what feedback pops up.
> I must admit it’s not clear how to customize faces since engrave is a bridge
> between major modes choices and latex but maybe show one or two examples (say
> elisp and python) of how to change/override some face.
Sure. The result should be visually identical to what you’d see in elisp/python
buffer, but possibly with a different theme.
Say you wanted to use a tweaked version of the default theme, this could be
accomplished like so:
┌────
│ (setq default2 (alist-get 'default engrave-faces-themes))
│ ;; Make comments hot pink
│ (plist-put (alist-get 'font-lock-comment-face default2) :foreground "#ff69b4")
│ ;; Any other customisations...
│ (add-to-list 'engrave-faces-themes (cons 'default2 default2))
└────
Then in the Org file you can use the “default2” engraved theme with
`#+latex_engraved_theme: default2'.
Let me know if there are any other examples you’d like to see.
> I think next we should apply these patches and be open to feedback
> about source code latex export using these new changes. I also wish
> there was a bit more documentation, but we can add more with time.
> Maybe some feedback from Nicolas G.?
👍 see my earlier comment on merging.
All the best,
Timothy
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ox-latex-Refactor-org-latex-src-block.patch --]
[-- Type: text/x-patch, Size: 15944 bytes --]
From 18adbd0e1226cf3307090861e09e92bfa9cdfbf1 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 21 Nov 2021 14:35:34 +0800
Subject: [PATCH 01/10] ox-latex: Refactor `org-latex-src-block'
* lisp/ox-latex.el (org-latex-src-block): Extract the per-format logic
from `org-latex-src-block' into new dedicated functions:
+ `org-latex-src-block--verbatim'
+ `org-latex-src-block--custom'
+ `org-latex-src-block--minted'
+ `org-latex-src-block--listings'
This makes `org-latex-src-block' much less monolithic, taking it from
175 lines to 30, and I find also makes it easier to understand.
---
lisp/ox-latex.el | 339 ++++++++++++++++++++++++++---------------------
1 file changed, 185 insertions(+), 154 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 841ad48bc..c2f728a1c 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2997,164 +2997,195 @@ (defun org-latex-src-block (src-block _contents info)
(float (plist-get attributes :float))
(listings (plist-get info :latex-listings)))
(cond
- ;; Case 1. No source fontification.
((or (not lang) (not listings))
- (let ((caption-str (org-latex--caption/label-string src-block info))
- (verbatim (format "\\begin{verbatim}\n%s\\end{verbatim}"
- (org-export-format-code-default src-block info))))
- (cond ((string= "multicolumn" float)
- (format "\\begin{figure*}[%s]\n%s%s\n%s\\end{figure*}"
- (plist-get info :latex-default-figure-position)
- (if caption-above-p caption-str "")
- verbatim
- (if caption-above-p "" caption-str)))
- (caption (concat
- (if caption-above-p caption-str "")
- verbatim
- (if caption-above-p "" (concat "\n" caption-str))))
- (t verbatim))))
- ;; Case 2. Custom environment.
+ (org-latex-src-block--verbatim src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
(custom-env
- (let ((caption-str (org-latex--caption/label-string src-block info))
- (formatted-src (org-export-format-code-default src-block info)))
- (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
- (format "\\begin{%s}\n%s\\end{%s}\n"
- custom-env
- (concat (and caption-above-p caption-str)
- formatted-src
- (and (not caption-above-p) caption-str))
- custom-env)
- (format-spec custom-env
- `((?s . ,formatted-src)
- (?c . ,caption)
- (?f . ,float)
- (?l . ,(org-latex--label src-block info))
- (?o . ,(or (plist-get attributes :options) "")))))))
- ;; Case 3. Use minted package.
+ (org-latex-src-block--custom src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float custom-env))
((eq listings 'minted)
- (let* ((caption-str (org-latex--caption/label-string src-block info))
- (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
- (plist-get info :latex-default-figure-position)))
- (float-env
- (cond
- ((string= "multicolumn" float)
- (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- (caption
- (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- ((string= "t" float)
- (concat (format "\\begin{listing}[%s]\n"
- placement)
- "%s\n\\end{listing}"))
- (t "%s")))
- (options (plist-get info :latex-minted-options))
- (body
- (format
- "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
- ;; Options.
- (concat
- (org-latex--make-option-string
- (if (or (not num-start) (assoc "linenos" options))
- options
- (append
- `(("linenos")
- ("firstnumber" ,(number-to-string (1+ num-start))))
- options)))
- (let ((local-options (plist-get attributes :options)))
- (and local-options (concat "," local-options))))
- ;; Language.
- (or (cadr (assq (intern lang)
- (plist-get info :latex-minted-langs)))
- (downcase lang))
- ;; Source code.
- (let* ((code-info (org-export-unravel-code src-block))
- (max-width
- (apply 'max
- (mapcar 'length
- (org-split-string (car code-info)
- "\n")))))
- (org-export-format-code
- (car code-info)
- (lambda (loc _num ref)
- (concat
- loc
- (when ref
- ;; Ensure references are flushed to the right,
- ;; separated with 6 spaces from the widest line
- ;; of code.
- (concat (make-string (+ (- max-width (length loc)) 6)
- ?\s)
- (format "(%s)" ref)))))
- nil (and retain-labels (cdr code-info)))))))
- ;; Return value.
- (format float-env body)))
- ;; Case 4. Use listings package.
+ (org-latex-src-block--minted src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
(t
- (let ((lst-lang
- (or (cadr (assq (intern lang)
- (plist-get info :latex-listings-langs)))
- lang))
- (caption-str
- (when caption
- (let ((main (org-export-get-caption src-block))
- (secondary (org-export-get-caption src-block t)))
- (if (not secondary)
- (format "{%s}" (org-export-data main info))
- (format "{[%s]%s}"
- (org-export-data secondary info)
- (org-export-data main info))))))
- (lst-opt (plist-get info :latex-listings-options)))
- (concat
- ;; Options.
- (format
- "\\lstset{%s}\n"
- (concat
- (org-latex--make-option-string
- (append
- lst-opt
- (cond
- ((and (not float) (plist-member attributes :float)) nil)
- ((string= "multicolumn" float) '(("float" "*")))
- ((and float (not (assoc "float" lst-opt)))
- `(("float" ,(plist-get info :latex-default-figure-position)))))
- `(("language" ,lst-lang))
- (if label
- `(("label" ,(org-latex--label src-block info)))
- '(("label" " ")))
- (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
- `(("captionpos" ,(if caption-above-p "t" "b")))
- (cond ((assoc "numbers" lst-opt) nil)
- ((not num-start) '(("numbers" "none")))
- (t `(("firstnumber" ,(number-to-string (1+ num-start)))
- ("numbers" "left"))))))
- (let ((local-options (plist-get attributes :options)))
- (and local-options (concat "," local-options)))))
- ;; Source code.
- (format
- "\\begin{lstlisting}\n%s\\end{lstlisting}"
- (let* ((code-info (org-export-unravel-code src-block))
- (max-width
- (apply 'max
- (mapcar 'length
- (org-split-string (car code-info) "\n")))))
- (org-export-format-code
- (car code-info)
- (lambda (loc _num ref)
- (concat
- loc
- (when ref
- ;; Ensure references are flushed to the right,
- ;; separated with 6 spaces from the widest line of
- ;; code
- (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
- (format "(%s)" ref)))))
- nil (and retain-labels (cdr code-info))))))))))))
-
+ (org-latex-src-block--listings src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))))))
+
+(defun org-latex-src-block--verbatim
+ (src-block info _lang caption caption-above-p _label
+ _num-start _retain-labels _attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using verbatim.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((caption-str (org-latex--caption/label-string src-block info))
+ (verbatim (format "\\begin{verbatim}\n%s\\end{verbatim}"
+ (org-export-format-code-default src-block info))))
+ (cond ((string= "multicolumn" float)
+ (format "\\begin{figure*}[%s]\n%s%s\n%s\\end{figure*}"
+ (plist-get info :latex-default-figure-position)
+ (if caption-above-p caption-str "")
+ verbatim
+ (if caption-above-p "" caption-str)))
+ (caption (concat
+ (if caption-above-p caption-str "")
+ verbatim
+ (if caption-above-p "" (concat "\n" caption-str))))
+ (t verbatim))))
+
+(defun org-latex-src-block--custom
+ (src-block info _lang caption caption-above-p _label
+ _num-start _retain-labels attributes float custom-env)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using a custom environment.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((caption-str (org-latex--caption/label-string src-block info))
+ (formatted-src (org-export-format-code-default src-block info)))
+ (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
+ (format "\\begin{%s}\n%s\\end{%s}\n"
+ custom-env
+ (concat (and caption-above-p caption-str)
+ formatted-src
+ (and (not caption-above-p) caption-str))
+ custom-env)
+ (format-spec custom-env
+ `((?s . ,formatted-src)
+ (?c . ,caption)
+ (?f . ,float)
+ (?l . ,(org-latex--label src-block info))
+ (?o . ,(or (plist-get attributes :options) "")))))))
+
+(defun org-latex-src-block--minted
+ (src-block info lang caption caption-above-p _label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using minted.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let* ((caption-str (org-latex--caption/label-string src-block info))
+ (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
+ (plist-get info :latex-default-figure-position)))
+ (float-env
+ (cond
+ ((string= "multicolumn" float)
+ (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ (caption
+ (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ ((string= "t" float)
+ (concat (format "\\begin{listing}[%s]\n"
+ placement)
+ "%s\n\\end{listing}"))
+ (t "%s")))
+ (options (plist-get info :latex-minted-options))
+ (body
+ (format
+ "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
+ ;; Options.
+ (concat
+ (org-latex--make-option-string
+ (if (or (not num-start) (assoc "linenos" options))
+ options
+ (append
+ `(("linenos")
+ ("firstnumber" ,(number-to-string (1+ num-start))))
+ options)))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (concat "," local-options))))
+ ;; Language.
+ (or (cadr (assq (intern lang)
+ (plist-get info :latex-minted-langs)))
+ (downcase lang))
+ ;; Source code.
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info)
+ "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line
+ ;; of code.
+ (concat (make-string (+ (- max-width (length loc)) 6)
+ ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info)))))))
+ ;; Return value.
+ (format float-env body)))
+
+(defun org-latex-src-block--listings
+ (src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using listings.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((lst-lang
+ (or (cadr (assq (intern lang)
+ (plist-get info :latex-listings-langs)))
+ lang))
+ (caption-str
+ (when caption
+ (let ((main (org-export-get-caption src-block))
+ (secondary (org-export-get-caption src-block t)))
+ (if (not secondary)
+ (format "{%s}" (org-export-data main info))
+ (format "{[%s]%s}"
+ (org-export-data secondary info)
+ (org-export-data main info))))))
+ (lst-opt (plist-get info :latex-listings-options)))
+ (concat
+ ;; Options.
+ (format
+ "\\lstset{%s}\n"
+ (concat
+ (org-latex--make-option-string
+ (append
+ lst-opt
+ (cond
+ ((and (not float) (plist-member attributes :float)) nil)
+ ((string= "multicolumn" float) '(("float" "*")))
+ ((and float (not (assoc "float" lst-opt)))
+ `(("float" ,(plist-get info :latex-default-figure-position)))))
+ `(("language" ,lst-lang))
+ (if label
+ `(("label" ,(org-latex--label src-block info)))
+ '(("label" " ")))
+ (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
+ `(("captionpos" ,(if caption-above-p "t" "b")))
+ (cond ((assoc "numbers" lst-opt) nil)
+ ((not num-start) '(("numbers" "none")))
+ (t `(("firstnumber" ,(number-to-string (1+ num-start)))
+ ("numbers" "left"))))))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (concat "," local-options)))))
+ ;; Source code.
+ (format
+ "\\begin{lstlisting}\n%s\\end{lstlisting}"
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info) "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line of
+ ;; code
+ (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info))))))))
;;;; Statistics Cookie
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-ox-latex-Refactor-org-latex-inline-src-block.patch --]
[-- Type: text/x-patch, Size: 4018 bytes --]
From 6635b72356192c3b0be500984b327d0b4ebd8e2b Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Wed, 4 May 2022 18:53:10 +0800
Subject: [PATCH 02/10] ox-latex: Refactor `org-latex-inline-src-block'
* lisp/ox-latex.el (org-latex-inline-src-block,
org-latex-inline-src-block--minted,
org-latex-inline-src-block--listings): Extract the minted and listings
specific logic out of `org-latex-inline-src-block` into the new
functions `org-latex-inline-src-block--minted` and
`org-latex-inline-src-block--listings`.
---
lisp/ox-latex.el | 62 +++++++++++++++++++++++++-----------------------
1 file changed, 32 insertions(+), 30 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index c2f728a1c..38f36a1f3 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2127,36 +2127,38 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
"Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
CONTENTS holds the contents of the item. INFO is a plist holding
contextual information."
- (let* ((code (org-element-property :value inline-src-block))
- (separator (org-latex--find-verb-separator code)))
- (cl-case (plist-get info :latex-listings)
- ;; Do not use a special package: transcode it verbatim, as code.
- ((nil) (org-latex--text-markup code 'code info))
- ;; Use minted package.
- (minted
- (let* ((org-lang (org-element-property :language inline-src-block))
- (mint-lang (or (cadr (assq (intern org-lang)
- (plist-get info :latex-minted-langs)))
- (downcase org-lang)))
- (options (org-latex--make-option-string
- (plist-get info :latex-minted-options))))
- (format "\\mintinline%s{%s}{%s}"
- (if (string= options "") "" (format "[%s]" options))
- mint-lang
- code)))
- ;; Use listings package.
- (otherwise
- ;; Maybe translate language's name.
- (let* ((org-lang (org-element-property :language inline-src-block))
- (lst-lang (or (cadr (assq (intern org-lang)
- (plist-get info :latex-listings-langs)))
- org-lang))
- (options (org-latex--make-option-string
- (append (plist-get info :latex-listings-options)
- `(("language" ,lst-lang))))))
- (concat (format "\\lstinline[%s]" options)
- separator code separator))))))
-
+ (let ((code (org-element-property :value inline-src-block))
+ (lang (org-element-property :language inline-src-block)))
+ (pcase (plist-get info :latex-listings)
+ ('nil (org-latex--text-markup code 'code info))
+ ('minted (org-latex-inline-src-block--minted info code lang))
+ (_ (org-latex-inline-src-block--listings info code lang)))))
+
+(defun org-latex-inline-src-block--minted (info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using minted.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (let ((mint-lang (or (cadr (assq (intern lang)
+ (plist-get info :latex-minted-langs)))
+ (downcase lang)))
+ (options (org-latex--make-option-string
+ (plist-get info :latex-minted-options))))
+ (format "\\mintinline%s{%s}{%s}"
+ (if (string= options "") "" (format "[%s]" options))
+ mint-lang
+ code)))
+
+(defun org-latex-inline-src-block--listings (info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using lstlistings.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (let* ((lst-lang (or (cadr (assq (intern lang)
+ (plist-get info :latex-listings-langs)))
+ lang))
+ (separator (org-latex--find-verb-separator code))
+ (options (org-latex--make-option-string
+ (append (plist-get info :latex-listings-options)
+ `(("language" ,lst-lang))))))
+ (concat (format "\\lstinline[%s]" options)
+ separator code separator)))
;;;; Inlinetask
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: 0003-ox-latex-More-versitile-option-construction.patch --]
[-- Type: text/x-patch, Size: 4118 bytes --]
From 108f321a80de89c72974fc25fcde36c85023daca Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Wed, 4 May 2022 23:31:59 +0800
Subject: [PATCH 03/10] ox-latex: More versitile option construction
* lisp/ox-latex.el (org-latex--make-option-string): Support a custom
option seperator string.
(org-latex--make-option-string, org-latex-minted-options,
org-latex-listings-options): The first line of the docstrings for
`org-latex-minted-options` and `org-latex-listings-options` describe the
variables as "association lists", yet `org-latex--make-option-string`
does not handle association lists and an example of two-value lists is
provided. To make the behaviour match the docstring,
`org-latex--make-option-string` is modified to work with either a list
two-value lists or an association list, and the examples in
`org-latex-minted-options` and `org-latex-listings-options` updated
accordingly.
---
lisp/ox-latex.el | 38 ++++++++++++++++++++++++--------------
1 file changed, 24 insertions(+), 14 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 38f36a1f3..2d4b3bace 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1006,12 +1006,16 @@ (defcustom org-latex-listings-options nil
These options are supplied as a comma-separated list to the
\\lstset command. Each element of the association list should be
-a list containing two strings: the name of the option, and the
-value. For example,
+a list or cons cell containing two strings: the name of the
+option, and the value. For example,
(setq org-latex-listings-options
\\='((\"basicstyle\" \"\\\\small\")
(\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
+ ; or
+ (setq org-latex-listings-options
+ \\='((\"basicstyle\" . \"\\\\small\")
+ (\"keywordstyle\" . \"\\\\color{black}\\\\bfseries\\\\underbar\")))
will typeset the code in a small size font with underlined, bold
black keywords.
@@ -1059,11 +1063,14 @@ (defcustom org-latex-minted-options nil
These options are supplied within square brackets in
\\begin{minted} environments. Each element of the alist should
-be a list containing two strings: the name of the option, and the
-value. For example,
+be a list or cons cell containing two strings: the name of the
+option, and the value. For example,
(setq org-latex-minted-options
\\='((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
+ ; or
+ (setq org-latex-minted-options
+ \\='((\"bgcolor\" . \"bg\") (\"frame\" . \"lines\")))
will result in source blocks being exported with
@@ -1506,21 +1513,24 @@ (defun org-latex--find-verb-separator (s)
when (not (string-match (regexp-quote (char-to-string c)) s))
return (char-to-string c))))
-(defun org-latex--make-option-string (options)
+(defun org-latex--make-option-string (options &optional seperator)
"Return a comma separated string of keywords and values.
OPTIONS is an alist where the key is the options keyword as
a string, and the value a list containing the keyword value, or
nil."
(mapconcat (lambda (pair)
- (pcase-let ((`(,keyword ,value) pair))
- (concat keyword
- (and (> (length value) 0)
- (concat "="
- (if (string-match-p (rx (any "[]")) value)
- (format "{%s}" value)
- value))))))
- options
- ","))
+ (let ((keyword (car pair))
+ (value (pcase (cdr pair)
+ ((pred stringp) (cdr pair))
+ ((pred consp) (cadr pair)))))
+ (concat keyword
+ (when value
+ (concat "="
+ (if (string-match-p (rx (any "[]")) value)
+ (format "{%s}" value)
+ value))))))
+ options
+ (or seperator ",")))
(defun org-latex--wrap-label (element output info)
"Wrap label associated to ELEMENT around OUTPUT, if appropriate.
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #5: 0004-ox-latex-Introduce-engraved-code-highlighting.patch --]
[-- Type: text/x-patch, Size: 18190 bytes --]
From 6f2edb89ff4dd5ce7d6618fc13fa83cbeb028e7b Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 21 Nov 2021 20:04:12 +0800
Subject: [PATCH 04/10] ox-latex: Introduce "engraved" code highlighting
* lisp/ox-latex.el (org-latex-src-block, org-latex-src-block--engraved,
org-latex-inline-src-block, org-latex-inline-src-block--engraved,
org-latex-src--engrave-code, org-latex-template, org-latex-listings):
Make use of the engraved-faces package (available on ELPA) to provide an
alternative LaTeX code highlighting backend which functions similarly to
htmlize.el for HTML exports.
(org-latex-engraved-preamble, org-latex-engraved-options): Introduce
variables to construct the preamble for engraved code blocks.
* lisp/ox-beamer.el (org-beamer-template): Modify to add engrave-faces
preamble when applicable.
---
lisp/ox-beamer.el | 11 ++
lisp/ox-latex.el | 294 ++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 296 insertions(+), 9 deletions(-)
diff --git a/lisp/ox-beamer.el b/lisp/ox-beamer.el
index 6be73c91e..4d40f6a1d 100644
--- a/lisp/ox-beamer.el
+++ b/lisp/ox-beamer.el
@@ -857,6 +857,17 @@ (defun org-beamer-template (contents info)
(let ((template (plist-get info :latex-hyperref-template)))
(and (stringp template)
(format-spec template (org-latex--format-spec info))))
+ ;; engrave-faces-latex preamble
+ (when (eq org-latex-listings 'engraved)
+ (let ((src-p (org-element-map (plist-get info :parse-tree)
+ '(src-block inline-src-block) #'identity
+ info t))
+ (fixedw-p
+ (org-element-map (plist-get info :parse-tree)
+ '(example-block fixed-width) #'identity
+ info t)))
+ (when (or src-p fixedw-p)
+ (org-latex-generate-engraved-preamble info src-p))))
;; Document start.
"\\begin{document}\n\n"
;; Title command.
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 2d4b3bace..1f4d4007d 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -37,6 +37,8 @@ (defvar org-latex-default-packages-alist)
(defvar org-latex-packages-alist)
(defvar orgtbl-exp-regexp)
+(declare-function engrave-faces-latex-gen-preamble "ext:engrave-faces-latex")
+(declare-function engrave-faces-latex-buffer "ext:engrave-faces-latex")
\f
;;; Define Back-End
@@ -125,6 +127,8 @@ (org-export-define-backend 'latex
(:latex-default-quote-environment nil nil org-latex-default-quote-environment)
(:latex-default-table-mode nil nil org-latex-default-table-mode)
(:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format)
+ (:latex-engraved-options nil nil org-latex-engraved-options)
+ (:latex-engraved-preamble nil nil org-latex-engraved-preamble)
(:latex-footnote-defined-format nil nil org-latex-footnote-defined-format)
(:latex-footnote-separator nil nil org-latex-footnote-separator)
(:latex-format-drawer-function nil nil org-latex-format-drawer-function)
@@ -937,22 +941,48 @@ (defcustom org-latex-listings nil
"Non-nil means export source code using the listings package.
This package will fontify source code, possibly even with color.
-If you want to use this, you also need to make LaTeX use the
-listings package, and if you want to have color, the color
-package. Just add these to `org-latex-packages-alist', for
-example using customize, or with something like:
+There are four implementations of this functionality you may
+choose from (ordered from least to most capable):
+1. Verbatim (nil)
+2. Listings (t)
+3. Minted (minted)
+4. Engraved (engraved)
+
+The first two options provide basic syntax
+highlighting (listings), or none at all (verbatim).
+
+When using listings, you also need to make use of the LaTeX
+\"listings\" package. The \"color\" package is also needed if you
+would like color too. These can simply be added to
+`org-latex-packages-alist', using customise or something like:
(require \\='ox-latex)
(add-to-list \\='org-latex-packages-alist \\='(\"\" \"listings\"))
(add-to-list \\='org-latex-packages-alist \\='(\"\" \"color\"))
-Alternatively,
+There are two further options for more comprehensive
+fontification. The first can be set with,
+
+ (setq org-latex-listings \\='engraved)
+
+which causes source code to be run through
+`engrave-faces-latex-buffer', which generates colorings using
+Emacs' font-lock information. This requires the engrave-faces
+package (availible from ELPA), and the fvextra LaTeX package be
+installed.
+
+The styling of the engraved result can customised with
+`org-latex-engraved-preamble' and `org-latex-engraved-options'.
+The default preamble also uses the tcolorbox LaTeX package in
+addition to fvextra.
+
+The second more comprehensive option can be set with,
(setq org-latex-listings \\='minted)
-causes source code to be exported using the minted package as
-opposed to listings. If you want to use minted, you need to add
-the minted package to `org-latex-packages-alist', for example
+which causes source code to be exported using the minted package
+as opposed to listings. If you want to use minted, you need to
+add the minted package to `org-latex-packages-alist', for example
using customize, or with
(require \\='ox-latex)
@@ -971,8 +1001,9 @@ (defcustom org-latex-listings nil
:type '(choice
(const :tag "Use listings" t)
(const :tag "Use minted" minted)
+ (const :tag "Use engrave-faces-latex" engraved)
(const :tag "Export verbatim" nil))
- :safe (lambda (s) (memq s '(t nil minted))))
+ :safe (lambda (s) (memq s '(t nil minted engraved))))
(defcustom org-latex-listings-langs
'((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
@@ -1142,6 +1173,148 @@ (defcustom org-latex-custom-lang-environments nil
:version "26.1"
:package-version '(Org . "9.0"))
+(defcustom org-latex-engraved-preamble
+ "\\usepackage{fvextra}
+
+[FVEXTRA-SETUP]
+
+% Make line numbers smaller and grey.
+\\renewcommand\\theFancyVerbLine{\\footnotesize\\color{black!40!white}\\arabic{FancyVerbLine}}
+
+\\usepackage{xcolor}
+
+% In case engrave-faces-latex-gen-preamble has not been run.
+\\providecolor{EfD}{HTML}{f7f7f7}
+\\providecolor{EFD}{HTML}{28292e}
+
+% Define a Code environment to prettily wrap the fontified code.
+\\usepackage[breakable,xparse]{tcolorbox}
+\\DeclareTColorBox[]{Code}{o}%
+{colback=EfD!98!EFD, colframe=EfD!95!EFD,
+ fontupper=\\footnotesize\\setlength{\\fboxsep}{0pt},
+ colupper=EFD,
+ IfNoValueTF={#1}%
+ {boxsep=2pt, arc=2.5pt, outer arc=2.5pt,
+ boxrule=0.5pt, left=2pt}%
+ {boxsep=2.5pt, arc=0pt, outer arc=0pt,
+ boxrule=0pt, leftrule=1.5pt, left=0.5pt},
+ right=2pt, top=1pt, bottom=0.5pt,
+ breakable}
+
+[LISTINGS-SETUP]"
+ "Preamble content injected when using engrave-faces-latex for source blocks.
+This is relevant when `org-latex-listings' is set to `engraved'.
+
+There is quite a lot of flexibility in what this preamble can be,
+as long as it:
+- Loads the fvextra package.
+- Loads the package xcolor (if it is not already loaded elsewhere).
+- Defines a \"Code\" environment (note the capital C), which all
+ \"Verbatim\" environments (provided by fvextra) will be wrapped with.
+
+In the default value the color \"EFD\" is provided as this is the
+foreground colour provided by engrave-faces-latex. When there
+are example/fixed-width blocks only, the engraved generated
+preamble is not included, and so it is provided so we may use it
+anyway.
+
+Within this preamble there are two recognised macro-like placeholders:
+
+ [FVEXTRA-SETUP]
+
+ [LISTINGS-SETUP]
+
+FVEXTRA-SETUP sets fvextra's defaults according to
+`org-latex-engraved-options', and LISTINGS-SETUP creates the
+listings environment and defines \\listoflistings."
+ :group 'org-export-latex
+ :type 'string
+ :package-version '(Org . "9.6"))
+
+(defcustom org-latex-engraved-options
+ '(("commandchars" . "\\\\\\{\\}")
+ ("highlightcolor" . "white!95!black!80!blue")
+ ("breaklines" . "true")
+ ("breaksymbol" . "\\color{white!60!black}\\tiny\\ensuremath{\\hookrightarrow}"))
+ "Association list of options for the latex fvextra package when engraving code.
+
+These options are set using \\fvset{...} in the preamble of the
+LaTeX export. Each element of the alist should be a list or cons
+cell containing two strings: the name of the option, and the
+value. For example,
+
+ (setq org-latex-engraved-options
+ \\='((\"highlightcolor\" \"green\") (\"frame\" \"lines\")))
+ ; or
+ (setq org-latex-engraved-options
+ \\='((\"highlightcolor\" . \"green\") (\"frame\" . \"lines\")))
+
+will result in the following LaTeX in the preamble
+
+\\fvset{%
+ bgcolor=bg,
+ frame=lines}
+
+This will affect all fvextra environments. Note that the same
+options will be applied to all blocks. If you need
+block-specific options, you may use the following syntax:
+
+ #+ATTR_LATEX: :options key1=value1,key2=value2
+ #+BEGIN_SRC <LANG>
+ ...
+ #+END_SRC"
+ :group 'org-export-latex
+ :type '(alist :key-type (string :tag "option")
+ :value-type (string :tag "value")))
+
+(defun org-latex-generate-engraved-preamble (info syntax-colours-p)
+ "Generate the preamble to setup engraved code.
+The result is constructed from the :latex-engraved-preamble and
+:latex-engraved-optionsn export options, the default values of
+which are given by `org-latex-engraved-preamble' and
+`org-latex-engraved-options' respectively."
+ (let* ((engraved-options
+ (plist-get info :latex-engraved-options))
+ (engraved-preamble (plist-get info :latex-engraved-preamble)))
+ (when (string-match "^[ \t]*\\[FVEXTRA-SETUP\\][ \t]*\n?" engraved-preamble)
+ (setq engraved-preamble
+ (replace-match
+ (concat
+ "\\fvset{%\n "
+ (org-latex--make-option-string engraved-options ",\n ")
+ "}\n")
+ t t
+ engraved-preamble)))
+ (when (string-match "^[ \t]*\\[LISTINGS-SETUP\\][ \t]*\n?" engraved-preamble)
+ (setq engraved-preamble
+ (replace-match
+ (format
+ "%% Support listings with captions
+\\usepackage{float}
+\\floatstyle{%s}
+\\newfloat{listing}{htbp}{lst}
+\\newcommand{\\listingsname}{Listing}
+\\floatname{listing}{\\listingsname}
+\\newcommand{\\listoflistingsname}{List of Listings}
+\\providecommand{\\listoflistings}{\\listof{listing}{\\listoflistingsname}}\n"
+ (if (memq 'src-block org-latex-caption-above)
+ "plaintop" "plain"))
+ t t
+ engraved-preamble)))
+ (if syntax-colours-p
+ (concat
+ "\n% Setup for code blocks [1/2]\n\n"
+ engraved-preamble
+ "\n\n% Setup for code blocks [2/2]: syntax highlighting colors\n"
+ (if (require 'engrave-faces-latex nil t)
+ (engrave-faces-latex-gen-preamble)
+ (message "Cannot engrave source blocks. Consider installing `engrave-faces'.")
+ "% WARNING syntax highlighting unavailible as engrave-faces-latex was missing.\n")
+ "\n")
+ (concat
+ "\n% Setup for code blocks\n\n"
+ engraved-preamble
+ "\n"))))
;;;; Compilation
@@ -1756,6 +1929,17 @@ (defun org-latex-template (contents info)
(let ((template (plist-get info :latex-hyperref-template)))
(and (stringp template)
(format-spec template spec)))
+ ;; engrave-faces-latex preamble
+ (when (eq org-latex-listings 'engraved)
+ (let ((src-p (org-element-map (plist-get info :parse-tree)
+ '(src-block inline-src-block) #'identity
+ info t))
+ (fixedw-p
+ (org-element-map (plist-get info :parse-tree)
+ '(example-block fixed-width) #'identity
+ info t)))
+ (when (or src-p fixedw-p)
+ (org-latex-generate-engraved-preamble info src-p))))
;; Document start.
"\\begin{document}\n\n"
;; Title command.
@@ -2142,6 +2326,7 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
(pcase (plist-get info :latex-listings)
('nil (org-latex--text-markup code 'code info))
('minted (org-latex-inline-src-block--minted info code lang))
+ ('engraved (org-latex-inline-src-block--engraved info code lang))
(_ (org-latex-inline-src-block--listings info code lang)))))
(defun org-latex-inline-src-block--minted (info code lang)
@@ -2157,6 +2342,11 @@ (defun org-latex-inline-src-block--minted (info code lang)
mint-lang
code)))
+(defun org-latex-inline-src-block--engraved (_info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using engrave-faces.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (format "\\Verb{%s}" (org-latex-src--engrave-code code lang)))
+
(defun org-latex-inline-src-block--listings (info code lang)
"Transcode an inline src block's content from Org to LaTeX, using lstlistings.
INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
@@ -2323,6 +2513,7 @@ (defun org-latex-keyword (keyword _contents info)
(cl-case (plist-get info :latex-listings)
((nil) "\\listoffigures")
(minted "\\listoflistings")
+ (engraved "\\listoflistings")
(otherwise "\\lstlistoflistings")))))))))
@@ -3017,6 +3208,9 @@ (defun org-latex-src-block (src-block _contents info)
num-start retain-labels attributes float custom-env))
((eq listings 'minted)
(org-latex-src-block--minted src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
+ ((eq listings 'engraved)
+ (org-latex-src-block--engraved src-block info lang caption caption-above-p label
num-start retain-labels attributes float))
(t
(org-latex-src-block--listings src-block info lang caption caption-above-p label
@@ -3133,6 +3327,88 @@ (defun org-latex-src-block--minted
;; Return value.
(format float-env body)))
+(defun org-latex-src--engrave-code (content lang)
+ "Engrave CONTENT to LaTeX in a LANG-mode buffer, and give the result."
+ (if (require 'engrave-faces-latex nil t)
+ (let* ((lang-mode (and lang (org-src-get-lang-mode lang)))
+ (engraved-buffer
+ (with-temp-buffer
+ (insert content)
+ (when lang-mode
+ (if (functionp lang-mode)
+ (funcall lang-mode)
+ (message "Cannot engrave code as %s. %s is undefined."
+ lang lang-mode)))
+ (engrave-faces-latex-buffer)))
+ (engraved-code
+ (with-current-buffer engraved-buffer
+ (buffer-string))))
+ (kill-buffer engraved-buffer)
+ engraved-code)
+ (user-error "Cannot engrave code as `engrave-faces-latex' is unavailible.")))
+
+(defun org-latex-src-block--engraved
+ (src-block info lang caption caption-above-p _label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using engrave-faces-latex.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let* ((caption-str (org-latex--caption/label-string src-block info))
+ (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
+ (plist-get info :latex-default-figure-position)))
+ (float-env
+ (cond
+ ((string= "multicolumn" float)
+ (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ (caption
+ (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ ((string= "t" float)
+ (concat (format "\\begin{listing}[%s]\n"
+ placement)
+ "%s\n\\end{listing}"))
+ (t "%s")))
+ (options (plist-get info :latex-engraved-options))
+ (content
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'string-width
+ (org-split-string (car code-info)
+ "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line
+ ;; of code.
+ (concat (make-string (+ (- max-width (length loc)) 6)
+ ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info)))))
+ (body
+ (format
+ "\\begin{Code}\n\\begin{Verbatim}[%s]\n%s\\end{Verbatim}\n\\end{Code}"
+ ;; Options.
+ (concat
+ (org-latex--make-option-string
+ (append
+ (when (and num-start (not (assoc "linenos" options)))
+ `(("linenos")
+ ("firstnumber" ,(number-to-string (1+ num-start)))))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (list local-options))))))
+ (org-latex-src--engrave-code content lang))))
+ (format float-env body)))
+
(defun org-latex-src-block--listings
(src-block info lang caption caption-above-p label
num-start retain-labels attributes float)
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #6: 0005-ox-latex-Don-t-use-length-to-get-string-width.patch --]
[-- Type: text/x-patch, Size: 1539 bytes --]
From 27a83b36b77e267feb3267d6a4dffd16fdf1d5a7 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sat, 7 May 2022 13:59:13 +0800
Subject: [PATCH 05/10] ox-latex: Don't use `length' to get string width
* lisp/ox-latex.el (org-latex-src-block--listings,
org-latex-src-block--engraved, org-latex-src-block--minted): Use
`string-width' instead of `length' to gauge the displayed width of the
string in LaTeX. This may not be a perfect match, but it should be an
improvement.
---
lisp/ox-latex.el | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 1f4d4007d..13ba6cd8c 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -3308,7 +3308,7 @@ (defun org-latex-src-block--minted
(let* ((code-info (org-export-unravel-code src-block))
(max-width
(apply 'max
- (mapcar 'length
+ (mapcar 'string-width
(org-split-string (car code-info)
"\n")))))
(org-export-format-code
@@ -3460,7 +3460,7 @@ (defun org-latex-src-block--listings
(let* ((code-info (org-export-unravel-code src-block))
(max-width
(apply 'max
- (mapcar 'length
+ (mapcar 'string-width
(org-split-string (car code-info) "\n")))))
(org-export-format-code
(car code-info)
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #7: 0006-ox-latex-Refactor-source-block-transcode-fun-sigs.patch --]
[-- Type: text/x-patch, Size: 7299 bytes --]
From ce7b8d8d1eafa6f2e6d0e247661000fc5272a24c Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sat, 7 May 2022 14:02:44 +0800
Subject: [PATCH 06/10] ox-latex: Refactor source block transcode fun sigs
* lisp/ox-latex.el (org-latex-src-block--listings,
org-latex-src-block--engraved, org-latex-src-block--minted,
org-latex-src-block--custom): Refactor these --backend functions to use
cl-defun with keys instead of having an 11-argument signature.
(org-latex-src-block): Adjust for the change in signature of the
--backend functions, and refactor the `cond' statement to use `pcase'.
---
lisp/ox-latex.el | 82 +++++++++++++++++++++++-------------------------
1 file changed, 39 insertions(+), 43 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 13ba6cd8c..4421bb8f5 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -3188,37 +3188,37 @@ (defun org-latex-src-block (src-block _contents info)
contextual information."
(when (org-string-nw-p (org-element-property :value src-block))
(let* ((lang (org-element-property :language src-block))
- (caption (org-element-property :caption src-block))
- (caption-above-p (org-latex--caption-above-p src-block info))
- (label (org-element-property :name src-block))
- (custom-env (and lang
- (cadr (assq (intern lang)
- org-latex-custom-lang-environments))))
- (num-start (org-export-get-loc src-block info))
- (retain-labels (org-element-property :retain-labels src-block))
- (attributes (org-export-read-attribute :attr_latex src-block))
- (float (plist-get attributes :float))
- (listings (plist-get info :latex-listings)))
- (cond
- ((or (not lang) (not listings))
- (org-latex-src-block--verbatim src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))
- (custom-env
- (org-latex-src-block--custom src-block info lang caption caption-above-p label
- num-start retain-labels attributes float custom-env))
- ((eq listings 'minted)
- (org-latex-src-block--minted src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))
- ((eq listings 'engraved)
- (org-latex-src-block--engraved src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))
- (t
- (org-latex-src-block--listings src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))))))
-
-(defun org-latex-src-block--verbatim
- (src-block info _lang caption caption-above-p _label
- _num-start _retain-labels _attributes float)
+ (caption (org-element-property :caption src-block))
+ (caption-above-p (org-latex--caption-above-p src-block info))
+ (label (org-element-property :name src-block))
+ (custom-env (and lang
+ (cadr (assq (intern lang)
+ org-latex-custom-lang-environments))))
+ (num-start (org-export-get-loc src-block info))
+ (retain-labels (org-element-property :retain-labels src-block))
+ (attributes (org-export-read-attribute :attr_latex src-block))
+ (float (plist-get attributes :float))
+ (listings (plist-get info :latex-listings)))
+ (funcall
+ (pcase listings
+ ((or (pred not) (guard (not lang))) #'org-latex-src-block--verbatim)
+ ((guard custom-env) #'org-latex-src-block--custom)
+ ('minted #'org-latex-src-block--minted)
+ ('engraved #'org-latex-src-block--engraved)
+ (_ #'org-latex-src-block--listings))
+ :src-block src-block
+ :info info
+ :lang lang
+ :caption caption
+ :caption-above-p caption-above-p
+ :label label
+ :num-start num-start
+ :retain-labels retain-labels
+ :attributes attributes
+ :float float))))
+
+(cl-defun org-latex-src-block--verbatim
+ (&key src-block info caption caption-above-p float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using verbatim.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3237,9 +3237,8 @@ (defun org-latex-src-block--verbatim
(if caption-above-p "" (concat "\n" caption-str))))
(t verbatim))))
-(defun org-latex-src-block--custom
- (src-block info _lang caption caption-above-p _label
- _num-start _retain-labels attributes float custom-env)
+(cl-defun org-latex-src-block--custom
+ (&key src-block info caption caption-above-p attributes float custom-env &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using a custom environment.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3259,9 +3258,8 @@ (defun org-latex-src-block--custom
(?l . ,(org-latex--label src-block info))
(?o . ,(or (plist-get attributes :options) "")))))))
-(defun org-latex-src-block--minted
- (src-block info lang caption caption-above-p _label
- num-start retain-labels attributes float)
+(cl-defun org-latex-src-block--minted
+ (&key src-block info lang caption caption-above-p num-start retain-labels attributes float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using minted.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3347,9 +3345,8 @@ (defun org-latex-src--engrave-code (content lang)
engraved-code)
(user-error "Cannot engrave code as `engrave-faces-latex' is unavailible.")))
-(defun org-latex-src-block--engraved
- (src-block info lang caption caption-above-p _label
- num-start retain-labels attributes float)
+(cl-defun org-latex-src-block--engraved
+ (&key src-block info lang caption caption-above-p num-start retain-labels attributes float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using engrave-faces-latex.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3409,9 +3406,8 @@ (defun org-latex-src-block--engraved
(org-latex-src--engrave-code content lang))))
(format float-env body)))
-(defun org-latex-src-block--listings
- (src-block info lang caption caption-above-p label
- num-start retain-labels attributes float)
+(cl-defun org-latex-src-block--listings
+ (&key src-block info lang caption caption-above-p label num-start retain-labels attributes float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using listings.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #8: 0007-ox-latex-Replace-org-latex-listings.patch --]
[-- Type: text/x-patch, Size: 16058 bytes --]
From 17e91e6816eeeed398a7f2779108e499729005c8 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sat, 7 May 2022 14:46:28 +0800
Subject: [PATCH 07/10] ox-latex: Replace `org-latex-listings'
* lisp/ox-latex.el (org-latex-src-block, org-latex-keyword,
org-latex-inline-src-block, org-latex-template,
org-latex--caption/label-string, org-latex-engraved-preamble,
org-latex-listings): Replace `org-latex-listings' with
`org-latex-src-block-backend', which now can be set to listings/verbatim
and no longer advertises t/nil as valid values.
* lisp/ox-beamer.el (org-beamer-template): Update in the same manner as
`org-latex-template'.
* lisp/org-compat.el: Make `org-latex-listings' an obsolete alias for
`org-latex-src-block-backend'.
* testing/lisp/test-ox.el: Replace `org-latex-listings' reference with
`org-latex-src-block-backend'.
* doc/org-manual.org (Footnotes, LaTeX specific properties, Literal
Examples): Replace references to `org-latex-listings' with
`org-latex-src-block-backend'.
* etc/ORG-NEWS: Add a news entry noting this change.
The variable `org-latex-listings' originally indicated whether source
blocks should use the listings LaTeX package, or not. This usage has
evolved over the years, and now it sets one of four different
fontification backends. This renaming should make the variable name a
bit less misleading.
---
doc/org-manual.org | 6 +--
etc/ORG-NEWS | 9 ++++
lisp/org-compat.el | 2 +
lisp/ox-beamer.el | 2 +-
lisp/ox-latex.el | 111 ++++++++++++++++++++++------------------
testing/lisp/test-ox.el | 2 +-
6 files changed, 78 insertions(+), 54 deletions(-)
diff --git a/doc/org-manual.org b/doc/org-manual.org
index c0d38cd8c..60bded419 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -11159,7 +11159,7 @@ ** Literal Examples
#+end_example
#+cindex: formatting source code, markup rules
-#+vindex: org-latex-listings
+#+vindex: org-latex-src-block-backend
If the example is source code from a programming language, or any
other text that can be marked up by Font Lock in Emacs, you can ask
for the example to look like the fontified Emacs buffer[fn:114]. This
@@ -16304,12 +16304,12 @@ **** LaTeX specific properties
| ~:latex-link-with-unknown-path-format~ | ~org-latex-link-with-unknown-path-format~ |
| ~:latex-listings-langs~ | ~org-latex-listings-langs~ |
| ~:latex-listings-options~ | ~org-latex-listings-options~ |
-| ~:latex-listings~ | ~org-latex-listings~ |
| ~:latex-minted-langs~ | ~org-latex-minted-langs~ |
| ~:latex-minted-options~ | ~org-latex-minted-options~ |
| ~:latex-prefer-user-labels~ | ~org-latex-prefer-user-labels~ |
| ~:latex-subtitle-format~ | ~org-latex-subtitle-format~ |
| ~:latex-subtitle-separate~ | ~org-latex-subtitle-separate~ |
+| ~:latex-src-block-backend~ | ~org-latex-src-block-backend~ |
| ~:latex-table-scientific-notation~ | ~org-latex-table-scientific-notation~ |
| ~:latex-tables-booktabs~ | ~org-latex-tables-booktabs~ |
| ~:latex-tables-centered~ | ~org-latex-tables-centered~ |
@@ -22256,7 +22256,7 @@ * Footnotes
version 1.34 of the =htmlize.el= package, which you need to install).
Fontified code chunks in LaTeX can be achieved using either the
[[https://www.ctan.org/pkg/listings][listings]] package or the [[https://www.ctan.org/pkg/minted][minted]] package. Refer to
-~org-latex-listings~ for details.
+~org-latex-src-block-backend~ for details.
[fn:115] Source code in code blocks may also be evaluated either
interactively or on export. See [[*Working with Source Code]] for more
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 27de6da62..ebb3cd649 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -273,6 +273,15 @@ Chmod-style permissions are based on the new variable
~org-babel-tangle-default-file-mode~.
*** A new custom setting =org-agenda-clock-report-header= to add a header to org agenda clock report
+
+*** ~org-latex-listings~ has been replaced with ~org-latex-src-block-backend~
+
+~org-latex-listings~ has been renamed to better reflect the current
+purpose of the variable. The replacement variable
+~org-latex-src-block-backend~ acts in exactly the same way, however it
+accepts =listings= and =verbatim= in place of =t= and =nil= (which
+still work, but are no longer listed as valid options).
+
* Version 9.5
** Important announcements and breaking changes
diff --git a/lisp/org-compat.el b/lisp/org-compat.el
index a29f206a4..704197645 100644
--- a/lisp/org-compat.el
+++ b/lisp/org-compat.el
@@ -324,6 +324,8 @@ (define-obsolete-variable-alias 'org-latex-create-formula-image-program
'org-preview-latex-default-process "9.0")
(define-obsolete-variable-alias 'org-latex-preview-ltxpng-directory
'org-preview-latex-image-directory "9.0")
+(define-obsolete-variable-alias 'org-latex-listings
+ 'org-latex-src-block-backend "9.6")
(define-obsolete-function-alias 'org-table-p 'org-at-table-p "9.0")
(define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "9.0")
(define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp "8.3")
diff --git a/lisp/ox-beamer.el b/lisp/ox-beamer.el
index 4d40f6a1d..3baa4e26e 100644
--- a/lisp/ox-beamer.el
+++ b/lisp/ox-beamer.el
@@ -858,7 +858,7 @@ (defun org-beamer-template (contents info)
(and (stringp template)
(format-spec template (org-latex--format-spec info))))
;; engrave-faces-latex preamble
- (when (eq org-latex-listings 'engraved)
+ (when (eq org-latex-src-block-backend 'engraved)
(let ((src-p (org-element-map (plist-get info :parse-tree)
'(src-block inline-src-block) #'identity
info t))
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 4421bb8f5..cc6ac291a 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -143,7 +143,7 @@ (org-export-define-backend 'latex
(:latex-inactive-timestamp-format nil nil org-latex-inactive-timestamp-format)
(:latex-inline-image-rules nil nil org-latex-inline-image-rules)
(:latex-link-with-unknown-path-format nil nil org-latex-link-with-unknown-path-format)
- (:latex-listings nil nil org-latex-listings)
+ (:latex-src-block-backend nil nil org-latex-src-block-backend)
(:latex-listings-langs nil nil org-latex-listings-langs)
(:latex-listings-options nil nil org-latex-listings-options)
(:latex-minted-langs nil nil org-latex-minted-langs)
@@ -937,22 +937,22 @@ (defcustom org-latex-format-inlinetask-function
;; Src blocks
-(defcustom org-latex-listings nil
- "Non-nil means export source code using the listings package.
+(defcustom org-latex-src-block-backend 'verbatim
+ "Backend used to generate source code listings.
-This package will fontify source code, possibly even with color.
-There are four implementations of this functionality you may
+This sets the behaviour for fontifying source code, possibly even with
+color. There are four implementations of this functionality you may
choose from (ordered from least to most capable):
-1. Verbatim (nil)
-2. Listings (t)
-3. Minted (minted)
-4. Engraved (engraved)
+1. Verbatim
+2. Listings
+3. Minted
+4. Engraved
The first two options provide basic syntax
highlighting (listings), or none at all (verbatim).
-When using listings, you also need to make use of the LaTeX
-\"listings\" package. The \"color\" package is also needed if you
+When using listings, you also need to make use of LaTeX package
+\"listings\"e. The \"color\" LaTeX package is also needed if you
would like color too. These can simply be added to
`org-latex-packages-alist', using customise or something like:
@@ -963,27 +963,12 @@ (defcustom org-latex-listings nil
There are two further options for more comprehensive
fontification. The first can be set with,
- (setq org-latex-listings \\='engraved)
+ (setq org-latex-src-block-backend \\='minted)
-which causes source code to be run through
-`engrave-faces-latex-buffer', which generates colorings using
-Emacs' font-lock information. This requires the engrave-faces
-package (availible from ELPA), and the fvextra LaTeX package be
-installed.
-
-The styling of the engraved result can customised with
-`org-latex-engraved-preamble' and `org-latex-engraved-options'.
-The default preamble also uses the tcolorbox LaTeX package in
-addition to fvextra.
-
-The second more comprehensive option can be set with,
-
- (setq org-latex-listings \\='minted)
-
-which causes source code to be exported using the minted package
-as opposed to listings. If you want to use minted, you need to
-add the minted package to `org-latex-packages-alist', for example
-using customize, or with
+which causes source code to be exported using the LaTeX package
+minted as opposed to listings. If you want to use minted, you
+need to add the minted package to `org-latex-packages-alist', for
+example using customize, or with
(require \\='ox-latex)
(add-to-list \\='org-latex-packages-alist \\='(\"newfloat\" \"minted\"))
@@ -996,14 +981,29 @@ (defcustom org-latex-listings nil
The minted choice has possible repercussions on the preview of
latex fragments (see `org-preview-latex-fragment'). If you run
into previewing problems, please consult
-URL `https://orgmode.org/worg/org-tutorials/org-latex-preview.html'."
+URL `https://orgmode.org/worg/org-tutorials/org-latex-preview.html'.
+
+The most comprehensive option can be set with,
+
+ (setq org-latex-src-block-backend \\='engraved)
+
+which causes source code to be run through
+`engrave-faces-latex-buffer', which generates colorings using
+Emacs' font-lock information. This requires the Emacs package
+engrave-faces (availible from ELPA), and the LaTeX package
+fvextra be installed.
+
+The styling of the engraved result can customised with
+`org-latex-engraved-preamble' and `org-latex-engraved-options'.
+The default preamble also uses the LaTeX package tcolorbox in
+addition to fvextra."
:group 'org-export-latex
:type '(choice
- (const :tag "Use listings" t)
+ (const :tag "Use listings" listings)
(const :tag "Use minted" minted)
(const :tag "Use engrave-faces-latex" engraved)
- (const :tag "Export verbatim" nil))
- :safe (lambda (s) (memq s '(t nil minted engraved))))
+ (const :tag "Export verbatim" verbatim))
+ :safe (lambda (s) (memq s '(listings minted engraved verbatim))))
(defcustom org-latex-listings-langs
'((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
@@ -1203,7 +1203,7 @@ (defcustom org-latex-engraved-preamble
[LISTINGS-SETUP]"
"Preamble content injected when using engrave-faces-latex for source blocks.
-This is relevant when `org-latex-listings' is set to `engraved'.
+This is relevant when `org-latex-src-block-backend' is set to `engraved'.
There is quite a lot of flexibility in what this preamble can be,
as long as it:
@@ -1523,7 +1523,8 @@ (defun org-latex--caption/label-string (element info)
main)
(and (eq type 'src-block)
(not (plist-get attr :float))
- (null (plist-get info :latex-listings)))))
+ (memq (plist-get info :latex-src-block-backend)
+ '(verbatim nil)))))
(short (org-export-get-caption element t))
(caption-from-attr-latex (plist-get attr :caption)))
(cond
@@ -1543,7 +1544,8 @@ (defun org-latex--caption/label-string (element info)
(paragraph "figure")
(image "figure")
(special-block "figure")
- (src-block (if (plist-get info :latex-listings)
+ (src-block (if (not (memq (plist-get info :latex-src-block-backend)
+ '(verbatim nil)))
"listing"
"figure"))
(t (symbol-name type*)))
@@ -1930,7 +1932,7 @@ (defun org-latex-template (contents info)
(and (stringp template)
(format-spec template spec)))
;; engrave-faces-latex preamble
- (when (eq org-latex-listings 'engraved)
+ (when (eq org-latex-src-block-backend 'engraved)
(let ((src-p (org-element-map (plist-get info :parse-tree)
'(src-block inline-src-block) #'identity
info t))
@@ -2323,11 +2325,17 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
contextual information."
(let ((code (org-element-property :value inline-src-block))
(lang (org-element-property :language inline-src-block)))
- (pcase (plist-get info :latex-listings)
- ('nil (org-latex--text-markup code 'code info))
+ (pcase (plist-get info :latex-src-block-backend)
+ ('verbatim (org-latex--text-markup code 'code info))
('minted (org-latex-inline-src-block--minted info code lang))
('engraved (org-latex-inline-src-block--engraved info code lang))
- (_ (org-latex-inline-src-block--listings info code lang)))))
+ ('listings (org-latex-inline-src-block--listings info code lang))
+ (oldval
+ (message "Please update the LaTeX src-block-backend to %s"
+ (if oldval "listings" "verbatim"))
+ (if oldval
+ (org-latex-inline-src-block--listings info code lang)
+ (org-latex--text-markup code 'code info))))))
(defun org-latex-inline-src-block--minted (info code lang)
"Transcode an inline src block's content from Org to LaTeX, using minted.
@@ -2510,7 +2518,7 @@ (defun org-latex-keyword (keyword _contents info)
(concat depth (and depth "\n") "\\tableofcontents"))))
((string-match-p "\\<tables\\>" value) "\\listoftables")
((string-match-p "\\<listings\\>" value)
- (cl-case (plist-get info :latex-listings)
+ (cl-case (plist-get info :latex-src-block-backend)
((nil) "\\listoffigures")
(minted "\\listoflistings")
(engraved "\\listoflistings")
@@ -3197,15 +3205,20 @@ (defun org-latex-src-block (src-block _contents info)
(num-start (org-export-get-loc src-block info))
(retain-labels (org-element-property :retain-labels src-block))
(attributes (org-export-read-attribute :attr_latex src-block))
- (float (plist-get attributes :float))
- (listings (plist-get info :latex-listings)))
+ (float (plist-get attributes :float)))
(funcall
- (pcase listings
- ((or (pred not) (guard (not lang))) #'org-latex-src-block--verbatim)
- ((guard custom-env) #'org-latex-src-block--custom)
+ (pcase (plist-get info :latex-src-block-backend)
+ ((or 'verbatim (guard (not lang))) #'org-latex-src-block--verbatim)
('minted #'org-latex-src-block--minted)
('engraved #'org-latex-src-block--engraved)
- (_ #'org-latex-src-block--listings))
+ ('listings #'org-latex-src-block--listings)
+ ((guard custom-env) #'org-latex-src-block--custom)
+ (oldval
+ (message "Please update the LaTeX src-block-backend to %s"
+ (if oldval "listings" "verbatim"))
+ (if oldval
+ #'org-latex-src-block--listings
+ #'org-latex-src-block--verbatim)))
:src-block src-block
:info info
:lang lang
diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el
index 25e02b258..28f950813 100644
--- a/testing/lisp/test-ox.el
+++ b/testing/lisp/test-ox.el
@@ -3978,7 +3978,7 @@ (ert-deftest test-org-export/latex-src-block-verbatim-caption ()
\\end{verbatim}
\\caption{Caption is below, 60\\%s}
\\end{figure*}"
- (let ((org-latex-listings 'minted) ; inactive due to missing lang
+ (let ((org-latex-src-block-backend 'minted) ; inactive due to missing lang
(org-latex-default-figure-position "tp"))
;; Namely "multicolumn" value to get just figure environment
;; looks like a bug.
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #9: 0008-ox-latex-Support-setting-the-engraved-theme.patch --]
[-- Type: text/x-patch, Size: 3181 bytes --]
From 146146df02434f8b224b21293753f260d0d624b2 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 8 May 2022 02:01:34 +0800
Subject: [PATCH 08/10] ox-latex: Support setting the engraved theme
* lisp/ox-latex.el (org-latex-generate-engraved-preamble,
org-latex-engraved-theme): Introduce the new export keyword
LATEX_ENGRAVED_THEME with default value given by
`org-latex-engraved-theme'. This is used to set the engraved theme used
in org-latex-engraved-theme.
This bumps the minimum required version of engrave-faces from v0.2 to
v0.3.
---
lisp/ox-latex.el | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index cc6ac291a..21541e9aa 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -129,6 +129,7 @@ (org-export-define-backend 'latex
(:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format)
(:latex-engraved-options nil nil org-latex-engraved-options)
(:latex-engraved-preamble nil nil org-latex-engraved-preamble)
+ (:latex-engraved-theme "LATEX_ENGRAVED_THEME" nil org-latex-engraved-theme)
(:latex-footnote-defined-format nil nil org-latex-footnote-defined-format)
(:latex-footnote-separator nil nil org-latex-footnote-separator)
(:latex-format-drawer-function nil nil org-latex-format-drawer-function)
@@ -1267,6 +1268,14 @@ (defcustom org-latex-engraved-options
:type '(alist :key-type (string :tag "option")
:value-type (string :tag "value")))
+(defcustom org-latex-engraved-theme nil
+ "The theme that should be used for engraved code, when non-nil.
+This can be set to any theme defined in `engrave-faces-themes' or
+loadable by Emacs. When set to t, the current Emacs theme is
+used."
+ :group 'org-export-latex
+ :type 'symbol)
+
(defun org-latex-generate-engraved-preamble (info syntax-colours-p)
"Generate the preamble to setup engraved code.
The result is constructed from the :latex-engraved-preamble and
@@ -1275,7 +1284,8 @@ (defun org-latex-generate-engraved-preamble (info syntax-colours-p)
`org-latex-engraved-options' respectively."
(let* ((engraved-options
(plist-get info :latex-engraved-options))
- (engraved-preamble (plist-get info :latex-engraved-preamble)))
+ (engraved-preamble (plist-get info :latex-engraved-preamble))
+ (engraved-theme (plist-get info :latex-engraved-theme)))
(when (string-match "^[ \t]*\\[FVEXTRA-SETUP\\][ \t]*\n?" engraved-preamble)
(setq engraved-preamble
(replace-match
@@ -1307,7 +1317,8 @@ (defun org-latex-generate-engraved-preamble (info syntax-colours-p)
engraved-preamble
"\n\n% Setup for code blocks [2/2]: syntax highlighting colors\n"
(if (require 'engrave-faces-latex nil t)
- (engrave-faces-latex-gen-preamble)
+ (engrave-faces-latex-gen-preamble
+ (when engraved-theme (intern engraved-theme)))
(message "Cannot engrave source blocks. Consider installing `engrave-faces'.")
"% WARNING syntax highlighting unavailible as engrave-faces-latex was missing.\n")
"\n")
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #10: 0009-ox-latex-Support-setting-engraved-theme-per-block.patch --]
[-- Type: text/x-patch, Size: 8907 bytes --]
From dc4989ccf41f09e7fce08036f89e9274ead4e257 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 8 May 2022 15:28:29 +0800
Subject: [PATCH 09/10] ox-latex: Support setting engraved theme per-block
* lisp/ox-latex.el (org-latex-src-block--engraved,
org-latex-src--engrave-code, org-latex-inline-src-block--engraved,
org-latex-generate-engraved-preamble): Allow for the engraved theme used
to be set on a per-src-block basis with #+attr_latex: :engraved-theme
THEME. Extra setup code is now generated in
`org-latex-generate-engraved-preamble'. To facilitate the application
of themes to src blocks, `org-latex-src--engrave-code' now takes on a
larger portion of the transcoding work.
---
lisp/ox-latex.el | 113 +++++++++++++++++++++++++++++++++++++----------
1 file changed, 89 insertions(+), 24 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 21541e9aa..528243a59 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1285,7 +1285,27 @@ (defun org-latex-generate-engraved-preamble (info syntax-colours-p)
(let* ((engraved-options
(plist-get info :latex-engraved-options))
(engraved-preamble (plist-get info :latex-engraved-preamble))
- (engraved-theme (plist-get info :latex-engraved-theme)))
+ (engraved-theme (plist-get info :latex-engraved-theme))
+ (engraved-themes
+ (cl-delete-duplicates
+ (org-element-map
+ (plist-get info :parse-tree)
+ '(src-block inline-src-block)
+ (lambda (src)
+ (plist-get
+ (org-export-read-attribute :attr_latex src)
+ :engraved-theme))
+ info)))
+ (gen-theme-spec
+ (lambda (theme)
+ (if (eq engrave-faces-latex-output-style 'preset)
+ (engrave-faces-latex-gen-preamble (when theme (intern theme)))
+ (engrave-faces-latex-gen-preamble-line
+ 'default
+ (alist-get 'default
+ (if theme
+ (engrave-faces-get-theme (intern theme))
+ engrave-faces-current-preset-style)))))))
(when (string-match "^[ \t]*\\[FVEXTRA-SETUP\\][ \t]*\n?" engraved-preamble)
(setq engraved-preamble
(replace-match
@@ -1315,10 +1335,31 @@ (defun org-latex-generate-engraved-preamble (info syntax-colours-p)
(concat
"\n% Setup for code blocks [1/2]\n\n"
engraved-preamble
- "\n\n% Setup for code blocks [2/2]: syntax highlighting colors\n"
+ "\n\n% Setup for code blocks [2/2]: syntax highlighting colors\n\n"
(if (require 'engrave-faces-latex nil t)
- (engrave-faces-latex-gen-preamble
- (when engraved-theme (intern engraved-theme)))
+ (if engraved-themes
+ (concat
+ (mapconcat
+ (lambda (theme)
+ (format
+ "\n\\newcommand{\\engravedtheme%s}{%%\n%s\n}"
+ (replace-regexp-in-string "[^A-Za-z]" "" theme)
+ (replace-regexp-in-string
+ "newcommand" "renewcommand"
+ (replace-regexp-in-string
+ "#" "##"
+ (funcall gen-theme-spec theme)))))
+ engraved-themes
+ "\n")
+ "\n\n"
+ (cond
+ ((memq engraved-theme engraved-themes)
+ (concat "\\engravedtheme"
+ (replace-regexp-in-string
+ "[^A-Za-z]" "" engraved-theme)
+ "\n"))
+ (t (funcall gen-theme-spec engraved-theme))))
+ (funcall gen-theme-spec engraved-theme))
(message "Cannot engrave source blocks. Consider installing `engrave-faces'.")
"% WARNING syntax highlighting unavailible as engrave-faces-latex was missing.\n")
"\n")
@@ -2361,10 +2402,11 @@ (defun org-latex-inline-src-block--minted (info code lang)
mint-lang
code)))
-(defun org-latex-inline-src-block--engraved (_info code lang)
+(defun org-latex-inline-src-block--engraved (info code lang)
"Transcode an inline src block's content from Org to LaTeX, using engrave-faces.
INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
- (format "\\Verb{%s}" (org-latex-src--engrave-code code lang)))
+ (org-latex-src--engrave-code
+ code lang nil (plist-get info :latex-engraved-options) t))
(defun org-latex-inline-src-block--listings (info code lang)
"Transcode an inline src block's content from Org to LaTeX, using lstlistings.
@@ -3349,13 +3391,18 @@ (cl-defun org-latex-src-block--minted
;; Return value.
(format float-env body)))
-(defun org-latex-src--engrave-code (content lang)
- "Engrave CONTENT to LaTeX in a LANG-mode buffer, and give the result."
+(defun org-latex-src--engrave-code (content lang &optional theme options inline)
+ "Engrave CONTENT to LaTeX in a LANG-mode buffer, and give the result.
+When THEME is non-nil, it will be used."
(if (require 'engrave-faces-latex nil t)
(let* ((lang-mode (and lang (org-src-get-lang-mode lang)))
+ (engrave-faces-current-preset-style
+ (if theme
+ (engrave-faces-get-theme theme)
+ engrave-faces-current-preset-style))
(engraved-buffer
(with-temp-buffer
- (insert content)
+ (insert (string-trim-right content "\n"))
(when lang-mode
(if (functionp lang-mode)
(funcall lang-mode)
@@ -3364,9 +3411,27 @@ (defun org-latex-src--engrave-code (content lang)
(engrave-faces-latex-buffer)))
(engraved-code
(with-current-buffer engraved-buffer
- (buffer-string))))
+ (buffer-string)))
+ (engraved-options
+ (when options
+ (concat "["
+ (if (listp options)
+ (org-latex--make-option-string options)
+ options)
+ "]")))
+ (engraved-wrapped
+ (if inline
+ (concat "\\Verb" engraved-options "{" engraved-code "}")
+ (concat "\\begin{Code}\\begin{Verbatim}" engraved-options "\n"
+ engraved-code "\n\\end{Verbatim}\n\\end{Code}"))))
(kill-buffer engraved-buffer)
- engraved-code)
+ (if theme
+ (concat "{\\engravedtheme"
+ (replace-regexp-in-string "[^A-Za-z]" ""
+ (symbol-name theme))
+ engraved-wrapped
+ "}")
+ engraved-wrapped))
(user-error "Cannot engrave code as `engrave-faces-latex' is unavailible.")))
(cl-defun org-latex-src-block--engraved
@@ -3394,7 +3459,15 @@ (cl-defun org-latex-src-block--engraved
placement)
"%s\n\\end{listing}"))
(t "%s")))
- (options (plist-get info :latex-engraved-options))
+ (options
+ (let ((engraved-options (plist-get info :latex-engraved-options))
+ (local-options (plist-get attributes :options)))
+ (append
+ (when (and num-start (not (assoc "linenos" engraved-options)))
+ `(("linenos")
+ ("firstnumber" ,(number-to-string (1+ num-start)))))
+ (and local-options (list local-options)))))
+ (engraved-theme (plist-get attributes :engraved-theme))
(content
(let* ((code-info (org-export-unravel-code src-block))
(max-width
@@ -3416,18 +3489,10 @@ (cl-defun org-latex-src-block--engraved
(format "(%s)" ref)))))
nil (and retain-labels (cdr code-info)))))
(body
- (format
- "\\begin{Code}\n\\begin{Verbatim}[%s]\n%s\\end{Verbatim}\n\\end{Code}"
- ;; Options.
- (concat
- (org-latex--make-option-string
- (append
- (when (and num-start (not (assoc "linenos" options)))
- `(("linenos")
- ("firstnumber" ,(number-to-string (1+ num-start)))))
- (let ((local-options (plist-get attributes :options)))
- (and local-options (list local-options))))))
- (org-latex-src--engrave-code content lang))))
+ (org-latex-src--engrave-code
+ content lang
+ (when engraved-theme (intern engraved-theme))
+ options)))
(format float-env body)))
(cl-defun org-latex-src-block--listings
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #11: 0010-ox-latex-Fix-captions-in-minted-engraved-code.patch --]
[-- Type: text/x-patch, Size: 4986 bytes --]
From ec560f6f037b902d603e9f34bbe593cb4c6bf4a9 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Mon, 9 May 2022 00:04:10 +0800
Subject: [PATCH 10/10] ox-latex: Fix %-captions in minted/engraved code
* lisp/ox-latex.el (org-latex-src-block--engraved,
org-latex-src-block--minted): Refactor float-env to be clearer, and
switch from `format' to `concat' to fix the bug where %-chars in
captions are interpreted as a format specifier.
---
lisp/ox-latex.el | 55 +++++++++++++++++++++---------------------------
1 file changed, 24 insertions(+), 31 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 528243a59..bb8072230 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -3332,23 +3332,20 @@ (cl-defun org-latex-src-block--minted
(let* ((caption-str (org-latex--caption/label-string src-block info))
(placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
(plist-get info :latex-default-figure-position)))
+ (multicolumn-p (string= "multicolumn" float))
(float-env
(cond
- ((string= "multicolumn" float)
- (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- (caption
- (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
+ ((or caption multicolumn-p)
+ (cons
+ (concat "\\begin{listing" (when multicolumn-p "*")
+ "}[" placement "]\n"
+ (if caption-above-p caption-str ""))
+ (concat "\n" (if caption-above-p "" caption-str)
+ "\\end{listing" (when multicolumn-p "*") "}")))
((string= "t" float)
- (concat (format "\\begin{listing}[%s]\n"
- placement)
- "%s\n\\end{listing}"))
- (t "%s")))
+ (cons
+ (concat "\\begin{listing}[" placement "]\n")
+ "\n\\end{listing}"))))
(options (plist-get info :latex-minted-options))
(body
(format
@@ -3388,8 +3385,7 @@ (cl-defun org-latex-src-block--minted
?\s)
(format "(%s)" ref)))))
nil (and retain-labels (cdr code-info)))))))
- ;; Return value.
- (format float-env body)))
+ (concat (car float-env) body (cdr float-env))))
(defun org-latex-src--engrave-code (content lang &optional theme options inline)
"Engrave CONTENT to LaTeX in a LANG-mode buffer, and give the result.
@@ -3442,23 +3438,20 @@ (cl-defun org-latex-src-block--engraved
(let* ((caption-str (org-latex--caption/label-string src-block info))
(placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
(plist-get info :latex-default-figure-position)))
+ (multicolumn-p (string= "multicolumn" float))
(float-env
(cond
- ((string= "multicolumn" float)
- (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- (caption
- (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
+ ((or caption multicolumn-p)
+ (cons
+ (concat "\\begin{listing" (when multicolumn-p "*")
+ "}[" placement "]\n"
+ (if caption-above-p caption-str ""))
+ (concat "\n" (if caption-above-p "" caption-str)
+ "\\end{listing" (when multicolumn-p "*") "}")))
((string= "t" float)
- (concat (format "\\begin{listing}[%s]\n"
- placement)
- "%s\n\\end{listing}"))
- (t "%s")))
+ (cons
+ (concat "\\begin{listing}[" placement "]\n")
+ "\n\\end{listing}"))))
(options
(let ((engraved-options (plist-get info :latex-engraved-options))
(local-options (plist-get attributes :options)))
@@ -3493,7 +3486,7 @@ (cl-defun org-latex-src-block--engraved
content lang
(when engraved-theme (intern engraved-theme))
options)))
- (format float-env body)))
+ (concat (car float-env) body (cdr float-env))))
(cl-defun org-latex-src-block--listings
(&key src-block info lang caption caption-above-p label num-start retain-labels attributes float &allow-other-keys)
--
2.35.3
^ permalink raw reply related [relevance 6%]
* Re: [PATCH] (v2) New LaTeX code export option: engraved
@ 2022-05-09 12:57 81% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2022-05-09 12:57 UTC (permalink / raw)
To: Ihor Radchenko; +Cc: Daniel Fleischer, emacs-orgmode, Nicolas Goaziou
[-- Attachment #1: Type: text/plain, Size: 5007 bytes --]
Hi Ihor,
> Before merging, could you also try to implement tests at least for
> engraved feature? If I recall correctly, we do not currently have
> backend-specific tests. But it would certainly help if we did. You might
> as well write a small “nucleus” test for ox-latex.
Probably not a bad idea, I’m just not sure what/how I’d go about this.
> Also, unless I miss something, there is no support for coderefs in the
> patchset. Is it intentional?
I think you’ve missed something. It has the same coderefs support as minted.
> It appears that the code for caption-str is duplicated. It could be
> also factored out.
It could, but I’m not sure this is actually a good idea as the duplication is
more the result of chance than a common dependency.
>> + (format-spec custom-env
>> + `((?s . ,formatted-src)
>> + (?c . ,caption)
>> + (?f . ,float)
>> + (?l . ,(org-latex–label src-block info))
>> + (?o . ,(or (plist-get attributes :options) “”)))))))
>
> I do not see a definition of `format-spec’ function. There is only
> `spec’ above in the code. Can you double check? Either I am missing
> something or something fishy is going on.
This code isn’t introduced by my patches, just relocated. FWIW `format-spec' is
provided by Emacs, and I don’t think is new.
>> + (let ((code (org-element-property :value inline-src-block))
>> + (lang (org-element-property :language inline-src-block)))
>> + (pcase (plist-get info :latex-listings)
>> + (’nil (org-latex–text-markup code ’code info))
>> + (’minted (org-latex-inline-src-block–minted info code lang))
>> + (_ (org-latex-inline-src-block–listings info code lang)))))
>
> Please use `nil and `minted. Using ’ may create issues in older Emacs.
Done.
>> +% In case engrave-faces-latex-gen-preamble has not been run.
>> +\
>> +\
>
> What is the difference between EfD and EFD? EfD is also not documented.
Documentation added. EfD is the background colour.
>> +FVEXTRA-SETUP sets fvextra’s defaults according to
>> +`org-latex-engraved-options’, and LISTINGS-SETUP creates the
>> +listings environment and defines \.“
>
> It is unclear what listings environment does given that we use engraved
> package. Can you provide a bit more details in the docstring on what the
> user will get if [LISTINGS-SETUP] is present.
> It may catch users by surprise that deleting this will make captions
> disappear.
It does the same thing as minted’s `listings' environment, hence the choice of
name. Documentation added.
> Why not just
> (org-element-map (plist-get info :parse-tree)
> ’(src-block inline-src-block example-block fixed-width) #’identity
> info t)
Ah, in my config I’m also using some engraved info for example/fixed-width
blocks. I’ll leave this out for now.
>> (pcase (plist-get info :latex-listings)
>> (’nil (org-latex–text-markup code ’code info))
>> …
> Same comment about ` in pcase.
Done.
> What will happen if there is no [LISTINGS-SETUP]?
Captioned/Floating code blocks won’t work.
>> +(defcustom org-latex-engraved-theme nil
> Docstring does not explain what happens when set to nil.
> Also, does :type ’symbol allow t and nil?
I think so, `symbolp' says they’re symbols if nothing else.
> Will it work when engraved-theme is t?
Yes.
> What about example-block and fixed-width? I may be missing something,
> but earlier in the patchset you had conditionals of the type
> (or src-p fixedw-p). So, does engrave-faces do anything with fixedw-type
> elements?
Resolved earlier.
>> + (gen-theme-spec
>> + (lambda (theme) …
>
> This appears to be not guarded by (require ’engrave-faces-latex nil t).
It is before it’s actually called.
>> -(defun org-latex-src–engrave-code (content lang)
>> - “Engrave CONTENT to LaTeX in a LANG-mode buffer, and give the result.”
>> +(defun org-latex-src–engrave-code (content lang &optional theme options inline)
>> + “Engrave CONTENT to LaTeX in a LANG-mode buffer, and give the result.
>> +When THEME is non-nil, it will be used.”
>
> You did not document the remaining two arguments. (this makes me ask a
> question whether you checked compile warnings). Also, consider running
> M-x checkdoc on ox-latex.el.
Now documented.
I have checked compile warnings (there are none), but checkdoc is currently a
bit dodgy on the Emacs commit I’m on.
>> - (insert content)
>> + (insert (string-trim-right content “”))
>
> As a theoretical possibility, what will happen if content has something
> like “%”?
Discussed in DMs. It just gets rid of trailing newlines, and it’s fine with
escaping.
All the best,
Timothy
^ permalink raw reply [relevance 81%]
* Re: [PATCH] New LaTeX code export option: engraved
@ 2022-05-10 1:13 93% ` Timothy
2022-05-10 16:10 93% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2022-05-10 1:13 UTC (permalink / raw)
To: sebastien.miquel; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 1087 bytes --]
Hi Sebastien,
> I’m quite exited and impressed to see this alternative to minted,
> thank you.
That’s great to hear!
> I haven’t been able to test your patches to org, but I have tried
> engrave-faces.el. Here’s a couple issues I’ve run into.
Still helpful, thanks.
> 1. engrave-faces-generate-preset generates emacs colors such as
> `:foreground “grey70”` which are not supported by
> engrave-faces-latex-gen-preamble-line and co.
Ah. I’ve just found this snippet from `list-colors-print'
┌────
│ (apply 'format "#%02x%02x%02x"
│ (mapcar (lambda (c) (ash c -8))
│ (color-values (car color))))
└────
I’ll see if I can incorporate this into engrave-faces v0.3.1.
> 2. `minted` supports a `mathescape` option to render math content
> inside code. `fvextra` supports the same option, but maths
> characters are escaped by engrave-faces-latex-face-mapper.
I’l also take a look at this :)
Thanks for the comments Sebastien.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] New LaTeX code export option: engraved
2022-05-10 1:13 93% ` Timothy
@ 2022-05-10 16:10 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2022-05-10 16:10 UTC (permalink / raw)
To: sebastien.miquel; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 757 bytes --]
Hi Sebastien,
>> 1. engrave-faces-generate-preset generates emacs colors such as
>> `:foreground “grey70”` which are not supported by
>> engrave-faces-latex-gen-preamble-line and co.
>
> I’ll see if I can incorporate this into engrave-faces v0.3.1.
Done.
>> 2. `minted` supports a `mathescape` option to render math content
>> inside code. `fvextra` supports the same option, but maths
>> characters are escaped by engrave-faces-latex-face-mapper.
>
> I’l also take a look at this :)
Implemented in engrave-faces-latex, but not worked into ox-html yet. If you
could check it out on the engrave-faces-latex side and check it’s behaving
sanely, that would be helpful.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] (v3) New LaTeX code export option: engraved
2022-05-08 14:30 6% ` [PATCH] (v2) " Timothy
@ 2022-05-11 16:05 5% ` Timothy
1 sibling, 1 reply; 74+ results
From: Timothy @ 2022-05-11 16:05 UTC (permalink / raw)
To: Daniel Fleischer; +Cc: Ihor Radchenko, emacs-orgmode, Nicolas Goaziou
[-- Attachment #1: Type: text/plain, Size: 799 bytes --]
Hi All,
I now have what I hope is the final set of patches[1].
Compared to v2 we have:
• a fix for % in captions (long standing bug)
• support for mathescaped code
• a news/manual entry for engraved
• a collection of tweaks based on Ihor’s lovely feedback 🤗
I’ve also slipped in a patch to `org-latex--label' which adds the `lst:' prefix
(short for listings) to `\label's generated for source blocks. This seemed like a
decent idea while I was working on captions, if anyone thinks otherwise I’ll
happily leave it out.
Daniel, if you’re happy with these commits let me know and I’ll push them :)
All the best,
Timothy
Footnotes
─────────
[1] For getting this feature
into Org. I’m sure there will be tweaks in the future.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ox-latex-Refactor-org-latex-src-block.patch --]
[-- Type: text/x-patch, Size: 15944 bytes --]
From 18adbd0e1226cf3307090861e09e92bfa9cdfbf1 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 21 Nov 2021 14:35:34 +0800
Subject: [PATCH 01/13] ox-latex: Refactor `org-latex-src-block'
* lisp/ox-latex.el (org-latex-src-block): Extract the per-format logic
from `org-latex-src-block' into new dedicated functions:
+ `org-latex-src-block--verbatim'
+ `org-latex-src-block--custom'
+ `org-latex-src-block--minted'
+ `org-latex-src-block--listings'
This makes `org-latex-src-block' much less monolithic, taking it from
175 lines to 30, and I find also makes it easier to understand.
---
lisp/ox-latex.el | 339 ++++++++++++++++++++++++++---------------------
1 file changed, 185 insertions(+), 154 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 841ad48bc..c2f728a1c 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2997,164 +2997,195 @@ (defun org-latex-src-block (src-block _contents info)
(float (plist-get attributes :float))
(listings (plist-get info :latex-listings)))
(cond
- ;; Case 1. No source fontification.
((or (not lang) (not listings))
- (let ((caption-str (org-latex--caption/label-string src-block info))
- (verbatim (format "\\begin{verbatim}\n%s\\end{verbatim}"
- (org-export-format-code-default src-block info))))
- (cond ((string= "multicolumn" float)
- (format "\\begin{figure*}[%s]\n%s%s\n%s\\end{figure*}"
- (plist-get info :latex-default-figure-position)
- (if caption-above-p caption-str "")
- verbatim
- (if caption-above-p "" caption-str)))
- (caption (concat
- (if caption-above-p caption-str "")
- verbatim
- (if caption-above-p "" (concat "\n" caption-str))))
- (t verbatim))))
- ;; Case 2. Custom environment.
+ (org-latex-src-block--verbatim src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
(custom-env
- (let ((caption-str (org-latex--caption/label-string src-block info))
- (formatted-src (org-export-format-code-default src-block info)))
- (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
- (format "\\begin{%s}\n%s\\end{%s}\n"
- custom-env
- (concat (and caption-above-p caption-str)
- formatted-src
- (and (not caption-above-p) caption-str))
- custom-env)
- (format-spec custom-env
- `((?s . ,formatted-src)
- (?c . ,caption)
- (?f . ,float)
- (?l . ,(org-latex--label src-block info))
- (?o . ,(or (plist-get attributes :options) "")))))))
- ;; Case 3. Use minted package.
+ (org-latex-src-block--custom src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float custom-env))
((eq listings 'minted)
- (let* ((caption-str (org-latex--caption/label-string src-block info))
- (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
- (plist-get info :latex-default-figure-position)))
- (float-env
- (cond
- ((string= "multicolumn" float)
- (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- (caption
- (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- ((string= "t" float)
- (concat (format "\\begin{listing}[%s]\n"
- placement)
- "%s\n\\end{listing}"))
- (t "%s")))
- (options (plist-get info :latex-minted-options))
- (body
- (format
- "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
- ;; Options.
- (concat
- (org-latex--make-option-string
- (if (or (not num-start) (assoc "linenos" options))
- options
- (append
- `(("linenos")
- ("firstnumber" ,(number-to-string (1+ num-start))))
- options)))
- (let ((local-options (plist-get attributes :options)))
- (and local-options (concat "," local-options))))
- ;; Language.
- (or (cadr (assq (intern lang)
- (plist-get info :latex-minted-langs)))
- (downcase lang))
- ;; Source code.
- (let* ((code-info (org-export-unravel-code src-block))
- (max-width
- (apply 'max
- (mapcar 'length
- (org-split-string (car code-info)
- "\n")))))
- (org-export-format-code
- (car code-info)
- (lambda (loc _num ref)
- (concat
- loc
- (when ref
- ;; Ensure references are flushed to the right,
- ;; separated with 6 spaces from the widest line
- ;; of code.
- (concat (make-string (+ (- max-width (length loc)) 6)
- ?\s)
- (format "(%s)" ref)))))
- nil (and retain-labels (cdr code-info)))))))
- ;; Return value.
- (format float-env body)))
- ;; Case 4. Use listings package.
+ (org-latex-src-block--minted src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
(t
- (let ((lst-lang
- (or (cadr (assq (intern lang)
- (plist-get info :latex-listings-langs)))
- lang))
- (caption-str
- (when caption
- (let ((main (org-export-get-caption src-block))
- (secondary (org-export-get-caption src-block t)))
- (if (not secondary)
- (format "{%s}" (org-export-data main info))
- (format "{[%s]%s}"
- (org-export-data secondary info)
- (org-export-data main info))))))
- (lst-opt (plist-get info :latex-listings-options)))
- (concat
- ;; Options.
- (format
- "\\lstset{%s}\n"
- (concat
- (org-latex--make-option-string
- (append
- lst-opt
- (cond
- ((and (not float) (plist-member attributes :float)) nil)
- ((string= "multicolumn" float) '(("float" "*")))
- ((and float (not (assoc "float" lst-opt)))
- `(("float" ,(plist-get info :latex-default-figure-position)))))
- `(("language" ,lst-lang))
- (if label
- `(("label" ,(org-latex--label src-block info)))
- '(("label" " ")))
- (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
- `(("captionpos" ,(if caption-above-p "t" "b")))
- (cond ((assoc "numbers" lst-opt) nil)
- ((not num-start) '(("numbers" "none")))
- (t `(("firstnumber" ,(number-to-string (1+ num-start)))
- ("numbers" "left"))))))
- (let ((local-options (plist-get attributes :options)))
- (and local-options (concat "," local-options)))))
- ;; Source code.
- (format
- "\\begin{lstlisting}\n%s\\end{lstlisting}"
- (let* ((code-info (org-export-unravel-code src-block))
- (max-width
- (apply 'max
- (mapcar 'length
- (org-split-string (car code-info) "\n")))))
- (org-export-format-code
- (car code-info)
- (lambda (loc _num ref)
- (concat
- loc
- (when ref
- ;; Ensure references are flushed to the right,
- ;; separated with 6 spaces from the widest line of
- ;; code
- (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
- (format "(%s)" ref)))))
- nil (and retain-labels (cdr code-info))))))))))))
-
+ (org-latex-src-block--listings src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))))))
+
+(defun org-latex-src-block--verbatim
+ (src-block info _lang caption caption-above-p _label
+ _num-start _retain-labels _attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using verbatim.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((caption-str (org-latex--caption/label-string src-block info))
+ (verbatim (format "\\begin{verbatim}\n%s\\end{verbatim}"
+ (org-export-format-code-default src-block info))))
+ (cond ((string= "multicolumn" float)
+ (format "\\begin{figure*}[%s]\n%s%s\n%s\\end{figure*}"
+ (plist-get info :latex-default-figure-position)
+ (if caption-above-p caption-str "")
+ verbatim
+ (if caption-above-p "" caption-str)))
+ (caption (concat
+ (if caption-above-p caption-str "")
+ verbatim
+ (if caption-above-p "" (concat "\n" caption-str))))
+ (t verbatim))))
+
+(defun org-latex-src-block--custom
+ (src-block info _lang caption caption-above-p _label
+ _num-start _retain-labels attributes float custom-env)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using a custom environment.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((caption-str (org-latex--caption/label-string src-block info))
+ (formatted-src (org-export-format-code-default src-block info)))
+ (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env)
+ (format "\\begin{%s}\n%s\\end{%s}\n"
+ custom-env
+ (concat (and caption-above-p caption-str)
+ formatted-src
+ (and (not caption-above-p) caption-str))
+ custom-env)
+ (format-spec custom-env
+ `((?s . ,formatted-src)
+ (?c . ,caption)
+ (?f . ,float)
+ (?l . ,(org-latex--label src-block info))
+ (?o . ,(or (plist-get attributes :options) "")))))))
+
+(defun org-latex-src-block--minted
+ (src-block info lang caption caption-above-p _label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using minted.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let* ((caption-str (org-latex--caption/label-string src-block info))
+ (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
+ (plist-get info :latex-default-figure-position)))
+ (float-env
+ (cond
+ ((string= "multicolumn" float)
+ (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ (caption
+ (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ ((string= "t" float)
+ (concat (format "\\begin{listing}[%s]\n"
+ placement)
+ "%s\n\\end{listing}"))
+ (t "%s")))
+ (options (plist-get info :latex-minted-options))
+ (body
+ (format
+ "\\begin{minted}[%s]{%s}\n%s\\end{minted}"
+ ;; Options.
+ (concat
+ (org-latex--make-option-string
+ (if (or (not num-start) (assoc "linenos" options))
+ options
+ (append
+ `(("linenos")
+ ("firstnumber" ,(number-to-string (1+ num-start))))
+ options)))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (concat "," local-options))))
+ ;; Language.
+ (or (cadr (assq (intern lang)
+ (plist-get info :latex-minted-langs)))
+ (downcase lang))
+ ;; Source code.
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info)
+ "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line
+ ;; of code.
+ (concat (make-string (+ (- max-width (length loc)) 6)
+ ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info)))))))
+ ;; Return value.
+ (format float-env body)))
+
+(defun org-latex-src-block--listings
+ (src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using listings.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let ((lst-lang
+ (or (cadr (assq (intern lang)
+ (plist-get info :latex-listings-langs)))
+ lang))
+ (caption-str
+ (when caption
+ (let ((main (org-export-get-caption src-block))
+ (secondary (org-export-get-caption src-block t)))
+ (if (not secondary)
+ (format "{%s}" (org-export-data main info))
+ (format "{[%s]%s}"
+ (org-export-data secondary info)
+ (org-export-data main info))))))
+ (lst-opt (plist-get info :latex-listings-options)))
+ (concat
+ ;; Options.
+ (format
+ "\\lstset{%s}\n"
+ (concat
+ (org-latex--make-option-string
+ (append
+ lst-opt
+ (cond
+ ((and (not float) (plist-member attributes :float)) nil)
+ ((string= "multicolumn" float) '(("float" "*")))
+ ((and float (not (assoc "float" lst-opt)))
+ `(("float" ,(plist-get info :latex-default-figure-position)))))
+ `(("language" ,lst-lang))
+ (if label
+ `(("label" ,(org-latex--label src-block info)))
+ '(("label" " ")))
+ (if caption-str `(("caption" ,caption-str)) '(("caption" " ")))
+ `(("captionpos" ,(if caption-above-p "t" "b")))
+ (cond ((assoc "numbers" lst-opt) nil)
+ ((not num-start) '(("numbers" "none")))
+ (t `(("firstnumber" ,(number-to-string (1+ num-start)))
+ ("numbers" "left"))))))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (concat "," local-options)))))
+ ;; Source code.
+ (format
+ "\\begin{lstlisting}\n%s\\end{lstlisting}"
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'length
+ (org-split-string (car code-info) "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line of
+ ;; code
+ (concat (make-string (+ (- max-width (length loc)) 6) ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info))))))))
;;;; Statistics Cookie
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-ox-latex-Refactor-org-latex-inline-src-block.patch --]
[-- Type: text/x-patch, Size: 4018 bytes --]
From 6635b72356192c3b0be500984b327d0b4ebd8e2b Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Wed, 4 May 2022 18:53:10 +0800
Subject: [PATCH 02/13] ox-latex: Refactor `org-latex-inline-src-block'
* lisp/ox-latex.el (org-latex-inline-src-block,
org-latex-inline-src-block--minted,
org-latex-inline-src-block--listings): Extract the minted and listings
specific logic out of `org-latex-inline-src-block` into the new
functions `org-latex-inline-src-block--minted` and
`org-latex-inline-src-block--listings`.
---
lisp/ox-latex.el | 62 +++++++++++++++++++++++++-----------------------
1 file changed, 32 insertions(+), 30 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index c2f728a1c..38f36a1f3 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2127,36 +2127,38 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
"Transcode an INLINE-SRC-BLOCK element from Org to LaTeX.
CONTENTS holds the contents of the item. INFO is a plist holding
contextual information."
- (let* ((code (org-element-property :value inline-src-block))
- (separator (org-latex--find-verb-separator code)))
- (cl-case (plist-get info :latex-listings)
- ;; Do not use a special package: transcode it verbatim, as code.
- ((nil) (org-latex--text-markup code 'code info))
- ;; Use minted package.
- (minted
- (let* ((org-lang (org-element-property :language inline-src-block))
- (mint-lang (or (cadr (assq (intern org-lang)
- (plist-get info :latex-minted-langs)))
- (downcase org-lang)))
- (options (org-latex--make-option-string
- (plist-get info :latex-minted-options))))
- (format "\\mintinline%s{%s}{%s}"
- (if (string= options "") "" (format "[%s]" options))
- mint-lang
- code)))
- ;; Use listings package.
- (otherwise
- ;; Maybe translate language's name.
- (let* ((org-lang (org-element-property :language inline-src-block))
- (lst-lang (or (cadr (assq (intern org-lang)
- (plist-get info :latex-listings-langs)))
- org-lang))
- (options (org-latex--make-option-string
- (append (plist-get info :latex-listings-options)
- `(("language" ,lst-lang))))))
- (concat (format "\\lstinline[%s]" options)
- separator code separator))))))
-
+ (let ((code (org-element-property :value inline-src-block))
+ (lang (org-element-property :language inline-src-block)))
+ (pcase (plist-get info :latex-listings)
+ ('nil (org-latex--text-markup code 'code info))
+ ('minted (org-latex-inline-src-block--minted info code lang))
+ (_ (org-latex-inline-src-block--listings info code lang)))))
+
+(defun org-latex-inline-src-block--minted (info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using minted.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (let ((mint-lang (or (cadr (assq (intern lang)
+ (plist-get info :latex-minted-langs)))
+ (downcase lang)))
+ (options (org-latex--make-option-string
+ (plist-get info :latex-minted-options))))
+ (format "\\mintinline%s{%s}{%s}"
+ (if (string= options "") "" (format "[%s]" options))
+ mint-lang
+ code)))
+
+(defun org-latex-inline-src-block--listings (info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using lstlistings.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (let* ((lst-lang (or (cadr (assq (intern lang)
+ (plist-get info :latex-listings-langs)))
+ lang))
+ (separator (org-latex--find-verb-separator code))
+ (options (org-latex--make-option-string
+ (append (plist-get info :latex-listings-options)
+ `(("language" ,lst-lang))))))
+ (concat (format "\\lstinline[%s]" options)
+ separator code separator)))
;;;; Inlinetask
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: 0003-ox-latex-More-versitile-option-construction.patch --]
[-- Type: text/x-patch, Size: 4118 bytes --]
From 108f321a80de89c72974fc25fcde36c85023daca Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Wed, 4 May 2022 23:31:59 +0800
Subject: [PATCH 03/13] ox-latex: More versitile option construction
* lisp/ox-latex.el (org-latex--make-option-string): Support a custom
option seperator string.
(org-latex--make-option-string, org-latex-minted-options,
org-latex-listings-options): The first line of the docstrings for
`org-latex-minted-options` and `org-latex-listings-options` describe the
variables as "association lists", yet `org-latex--make-option-string`
does not handle association lists and an example of two-value lists is
provided. To make the behaviour match the docstring,
`org-latex--make-option-string` is modified to work with either a list
two-value lists or an association list, and the examples in
`org-latex-minted-options` and `org-latex-listings-options` updated
accordingly.
---
lisp/ox-latex.el | 38 ++++++++++++++++++++++++--------------
1 file changed, 24 insertions(+), 14 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 38f36a1f3..2d4b3bace 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1006,12 +1006,16 @@ (defcustom org-latex-listings-options nil
These options are supplied as a comma-separated list to the
\\lstset command. Each element of the association list should be
-a list containing two strings: the name of the option, and the
-value. For example,
+a list or cons cell containing two strings: the name of the
+option, and the value. For example,
(setq org-latex-listings-options
\\='((\"basicstyle\" \"\\\\small\")
(\"keywordstyle\" \"\\\\color{black}\\\\bfseries\\\\underbar\")))
+ ; or
+ (setq org-latex-listings-options
+ \\='((\"basicstyle\" . \"\\\\small\")
+ (\"keywordstyle\" . \"\\\\color{black}\\\\bfseries\\\\underbar\")))
will typeset the code in a small size font with underlined, bold
black keywords.
@@ -1059,11 +1063,14 @@ (defcustom org-latex-minted-options nil
These options are supplied within square brackets in
\\begin{minted} environments. Each element of the alist should
-be a list containing two strings: the name of the option, and the
-value. For example,
+be a list or cons cell containing two strings: the name of the
+option, and the value. For example,
(setq org-latex-minted-options
\\='((\"bgcolor\" \"bg\") (\"frame\" \"lines\")))
+ ; or
+ (setq org-latex-minted-options
+ \\='((\"bgcolor\" . \"bg\") (\"frame\" . \"lines\")))
will result in source blocks being exported with
@@ -1506,21 +1513,24 @@ (defun org-latex--find-verb-separator (s)
when (not (string-match (regexp-quote (char-to-string c)) s))
return (char-to-string c))))
-(defun org-latex--make-option-string (options)
+(defun org-latex--make-option-string (options &optional seperator)
"Return a comma separated string of keywords and values.
OPTIONS is an alist where the key is the options keyword as
a string, and the value a list containing the keyword value, or
nil."
(mapconcat (lambda (pair)
- (pcase-let ((`(,keyword ,value) pair))
- (concat keyword
- (and (> (length value) 0)
- (concat "="
- (if (string-match-p (rx (any "[]")) value)
- (format "{%s}" value)
- value))))))
- options
- ","))
+ (let ((keyword (car pair))
+ (value (pcase (cdr pair)
+ ((pred stringp) (cdr pair))
+ ((pred consp) (cadr pair)))))
+ (concat keyword
+ (when value
+ (concat "="
+ (if (string-match-p (rx (any "[]")) value)
+ (format "{%s}" value)
+ value))))))
+ options
+ (or seperator ",")))
(defun org-latex--wrap-label (element output info)
"Wrap label associated to ELEMENT around OUTPUT, if appropriate.
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #5: 0004-ox-latex-Introduce-engraved-code-highlighting.patch --]
[-- Type: text/x-patch, Size: 17857 bytes --]
From ac29f0f3991faf457f161698b4ad311edfe5d55d Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 21 Nov 2021 20:04:12 +0800
Subject: [PATCH 04/13] ox-latex: Introduce "engraved" code highlighting
* lisp/ox-latex.el (org-latex-src-block, org-latex-src-block--engraved,
org-latex-inline-src-block, org-latex-inline-src-block--engraved,
org-latex-src--engrave-code, org-latex-template, org-latex-listings):
Make use of the engraved-faces package (available on ELPA) to provide an
alternative LaTeX code highlighting backend which functions similarly to
htmlize.el for HTML exports.
(org-latex-engraved-preamble, org-latex-engraved-options): Introduce
variables to construct the preamble for engraved code blocks.
* lisp/ox-beamer.el (org-beamer-template): Modify to add engrave-faces
preamble when applicable.
---
lisp/ox-beamer.el | 6 +
lisp/ox-latex.el | 292 ++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 289 insertions(+), 9 deletions(-)
diff --git a/lisp/ox-beamer.el b/lisp/ox-beamer.el
index 6be73c91e..fe73bf686 100644
--- a/lisp/ox-beamer.el
+++ b/lisp/ox-beamer.el
@@ -857,6 +857,12 @@ (defun org-beamer-template (contents info)
(let ((template (plist-get info :latex-hyperref-template)))
(and (stringp template)
(format-spec template (org-latex--format-spec info))))
+ ;; engrave-faces-latex preamble
+ (when (and (eq org-latex-listings 'engraved)
+ (org-element-map (plist-get info :parse-tree)
+ '(src-block inline-src-block) #'identity
+ info t))
+ (org-latex-generate-engraved-preamble info t))
;; Document start.
"\\begin{document}\n\n"
;; Title command.
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 2d4b3bace..e0f47cf80 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -37,6 +37,8 @@ (defvar org-latex-default-packages-alist)
(defvar org-latex-packages-alist)
(defvar orgtbl-exp-regexp)
+(declare-function engrave-faces-latex-gen-preamble "ext:engrave-faces-latex")
+(declare-function engrave-faces-latex-buffer "ext:engrave-faces-latex")
\f
;;; Define Back-End
@@ -125,6 +127,8 @@ (org-export-define-backend 'latex
(:latex-default-quote-environment nil nil org-latex-default-quote-environment)
(:latex-default-table-mode nil nil org-latex-default-table-mode)
(:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format)
+ (:latex-engraved-options nil nil org-latex-engraved-options)
+ (:latex-engraved-preamble nil nil org-latex-engraved-preamble)
(:latex-footnote-defined-format nil nil org-latex-footnote-defined-format)
(:latex-footnote-separator nil nil org-latex-footnote-separator)
(:latex-format-drawer-function nil nil org-latex-format-drawer-function)
@@ -937,22 +941,48 @@ (defcustom org-latex-listings nil
"Non-nil means export source code using the listings package.
This package will fontify source code, possibly even with color.
-If you want to use this, you also need to make LaTeX use the
-listings package, and if you want to have color, the color
-package. Just add these to `org-latex-packages-alist', for
-example using customize, or with something like:
+There are four implementations of this functionality you may
+choose from (ordered from least to most capable):
+1. Verbatim (nil)
+2. Listings (t)
+3. Minted (minted)
+4. Engraved (engraved)
+
+The first two options provide basic syntax
+highlighting (listings), or none at all (verbatim).
+
+When using listings, you also need to make use of the LaTeX
+\"listings\" package. The \"color\" package is also needed if you
+would like color too. These can simply be added to
+`org-latex-packages-alist', using customise or something like:
(require \\='ox-latex)
(add-to-list \\='org-latex-packages-alist \\='(\"\" \"listings\"))
(add-to-list \\='org-latex-packages-alist \\='(\"\" \"color\"))
-Alternatively,
+There are two further options for more comprehensive
+fontification. The first can be set with,
+
+ (setq org-latex-listings \\='engraved)
+
+which causes source code to be run through
+`engrave-faces-latex-buffer', which generates colorings using
+Emacs' font-lock information. This requires the engrave-faces
+package (availible from ELPA), and the fvextra LaTeX package be
+installed.
+
+The styling of the engraved result can customised with
+`org-latex-engraved-preamble' and `org-latex-engraved-options'.
+The default preamble also uses the tcolorbox LaTeX package in
+addition to fvextra.
+
+The second more comprehensive option can be set with,
(setq org-latex-listings \\='minted)
-causes source code to be exported using the minted package as
-opposed to listings. If you want to use minted, you need to add
-the minted package to `org-latex-packages-alist', for example
+which causes source code to be exported using the minted package
+as opposed to listings. If you want to use minted, you need to
+add the minted package to `org-latex-packages-alist', for example
using customize, or with
(require \\='ox-latex)
@@ -971,8 +1001,9 @@ (defcustom org-latex-listings nil
:type '(choice
(const :tag "Use listings" t)
(const :tag "Use minted" minted)
+ (const :tag "Use engrave-faces-latex" engraved)
(const :tag "Export verbatim" nil))
- :safe (lambda (s) (memq s '(t nil minted))))
+ :safe (lambda (s) (memq s '(t nil minted engraved))))
(defcustom org-latex-listings-langs
'((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
@@ -1142,6 +1173,151 @@ (defcustom org-latex-custom-lang-environments nil
:version "26.1"
:package-version '(Org . "9.0"))
+(defcustom org-latex-engraved-preamble
+ "\\usepackage{fvextra}
+
+[FVEXTRA-SETUP]
+
+% Make line numbers smaller and grey.
+\\renewcommand\\theFancyVerbLine{\\footnotesize\\color{black!40!white}\\arabic{FancyVerbLine}}
+
+\\usepackage{xcolor}
+
+% In case engrave-faces-latex-gen-preamble has not been run.
+\\providecolor{EfD}{HTML}{f7f7f7}
+\\providecolor{EFD}{HTML}{28292e}
+
+% Define a Code environment to prettily wrap the fontified code.
+\\usepackage[breakable,xparse]{tcolorbox}
+\\DeclareTColorBox[]{Code}{o}%
+{colback=EfD!98!EFD, colframe=EfD!95!EFD,
+ fontupper=\\footnotesize\\setlength{\\fboxsep}{0pt},
+ colupper=EFD,
+ IfNoValueTF={#1}%
+ {boxsep=2pt, arc=2.5pt, outer arc=2.5pt,
+ boxrule=0.5pt, left=2pt}%
+ {boxsep=2.5pt, arc=0pt, outer arc=0pt,
+ boxrule=0pt, leftrule=1.5pt, left=0.5pt},
+ right=2pt, top=1pt, bottom=0.5pt,
+ breakable}
+
+[LISTINGS-SETUP]"
+ "Preamble content injected when using engrave-faces-latex for source blocks.
+This is relevant when `org-latex-listings' is set to `engraved'.
+
+There is quite a lot of flexibility in what this preamble can be,
+as long as it:
+- Loads the fvextra package.
+- Loads the package xcolor (if it is not already loaded elsewhere).
+- Defines a \"Code\" environment (note the capital C), which all
+ \"Verbatim\" environments (provided by fvextra) will be wrapped with.
+
+In the default value the colors \"EFD\" and \"EfD\" are provided
+as they are respectively the foreground and background colours,
+just in case they aren't provided by the generated preamble, so
+we can asume they are always set.
+
+Within this preamble there are two recognised macro-like placeholders:
+
+ [FVEXTRA-SETUP]
+
+ [LISTINGS-SETUP]
+
+Unless you have a very good reason, both of these placeholders
+should be included in the preamble.
+
+FVEXTRA-SETUP sets fvextra's defaults according to
+`org-latex-engraved-options', and LISTINGS-SETUP creates the
+listings environment used for captioned or floating code blocks,
+as well as defining \\listoflistings."
+ :group 'org-export-latex
+ :type 'string
+ :package-version '(Org . "9.6"))
+
+(defcustom org-latex-engraved-options
+ '(("commandchars" . "\\\\\\{\\}")
+ ("highlightcolor" . "white!95!black!80!blue")
+ ("breaklines" . "true")
+ ("breaksymbol" . "\\color{white!60!black}\\tiny\\ensuremath{\\hookrightarrow}"))
+ "Association list of options for the latex fvextra package when engraving code.
+
+These options are set using \\fvset{...} in the preamble of the
+LaTeX export. Each element of the alist should be a list or cons
+cell containing two strings: the name of the option, and the
+value. For example,
+
+ (setq org-latex-engraved-options
+ \\='((\"highlightcolor\" \"green\") (\"frame\" \"lines\")))
+ ; or
+ (setq org-latex-engraved-options
+ \\='((\"highlightcolor\" . \"green\") (\"frame\" . \"lines\")))
+
+will result in the following LaTeX in the preamble
+
+\\fvset{%
+ bgcolor=bg,
+ frame=lines}
+
+This will affect all fvextra environments. Note that the same
+options will be applied to all blocks. If you need
+block-specific options, you may use the following syntax:
+
+ #+ATTR_LATEX: :options key1=value1,key2=value2
+ #+BEGIN_SRC <LANG>
+ ...
+ #+END_SRC"
+ :group 'org-export-latex
+ :type '(alist :key-type (string :tag "option")
+ :value-type (string :tag "value")))
+
+(defun org-latex-generate-engraved-preamble (info syntax-colours-p)
+ "Generate the preamble to setup engraved code.
+The result is constructed from the :latex-engraved-preamble and
+:latex-engraved-optionsn export options, the default values of
+which are given by `org-latex-engraved-preamble' and
+`org-latex-engraved-options' respectively."
+ (let* ((engraved-options
+ (plist-get info :latex-engraved-options))
+ (engraved-preamble (plist-get info :latex-engraved-preamble)))
+ (when (string-match "^[ \t]*\\[FVEXTRA-SETUP\\][ \t]*\n?" engraved-preamble)
+ (setq engraved-preamble
+ (replace-match
+ (concat
+ "\\fvset{%\n "
+ (org-latex--make-option-string engraved-options ",\n ")
+ "}\n")
+ t t
+ engraved-preamble)))
+ (when (string-match "^[ \t]*\\[LISTINGS-SETUP\\][ \t]*\n?" engraved-preamble)
+ (setq engraved-preamble
+ (replace-match
+ (format
+ "%% Support listings with captions
+\\usepackage{float}
+\\floatstyle{%s}
+\\newfloat{listing}{htbp}{lst}
+\\newcommand{\\listingsname}{Listing}
+\\floatname{listing}{\\listingsname}
+\\newcommand{\\listoflistingsname}{List of Listings}
+\\providecommand{\\listoflistings}{\\listof{listing}{\\listoflistingsname}}\n"
+ (if (memq 'src-block org-latex-caption-above)
+ "plaintop" "plain"))
+ t t
+ engraved-preamble)))
+ (if syntax-colours-p
+ (concat
+ "\n% Setup for code blocks [1/2]\n\n"
+ engraved-preamble
+ "\n\n% Setup for code blocks [2/2]: syntax highlighting colors\n"
+ (if (require 'engrave-faces-latex nil t)
+ (engrave-faces-latex-gen-preamble)
+ (message "Cannot engrave source blocks. Consider installing `engrave-faces'.")
+ "% WARNING syntax highlighting unavailible as engrave-faces-latex was missing.\n")
+ "\n")
+ (concat
+ "\n% Setup for code blocks\n\n"
+ engraved-preamble
+ "\n"))))
;;;; Compilation
@@ -1756,6 +1932,12 @@ (defun org-latex-template (contents info)
(let ((template (plist-get info :latex-hyperref-template)))
(and (stringp template)
(format-spec template spec)))
+ ;; engrave-faces-latex preamble
+ (when (and (eq org-latex-listings 'engraved)
+ (org-element-map (plist-get info :parse-tree)
+ '(src-block inline-src-block) #'identity
+ info t))
+ (org-latex-generate-engraved-preamble info t))
;; Document start.
"\\begin{document}\n\n"
;; Title command.
@@ -2142,6 +2324,7 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
(pcase (plist-get info :latex-listings)
('nil (org-latex--text-markup code 'code info))
('minted (org-latex-inline-src-block--minted info code lang))
+ ('engraved (org-latex-inline-src-block--engraved info code lang))
(_ (org-latex-inline-src-block--listings info code lang)))))
(defun org-latex-inline-src-block--minted (info code lang)
@@ -2157,6 +2340,11 @@ (defun org-latex-inline-src-block--minted (info code lang)
mint-lang
code)))
+(defun org-latex-inline-src-block--engraved (_info code lang)
+ "Transcode an inline src block's content from Org to LaTeX, using engrave-faces.
+INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
+ (format "\\Verb{%s}" (org-latex-src--engrave-code code lang)))
+
(defun org-latex-inline-src-block--listings (info code lang)
"Transcode an inline src block's content from Org to LaTeX, using lstlistings.
INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
@@ -2323,6 +2511,7 @@ (defun org-latex-keyword (keyword _contents info)
(cl-case (plist-get info :latex-listings)
((nil) "\\listoffigures")
(minted "\\listoflistings")
+ (engraved "\\listoflistings")
(otherwise "\\lstlistoflistings")))))))))
@@ -3017,6 +3206,9 @@ (defun org-latex-src-block (src-block _contents info)
num-start retain-labels attributes float custom-env))
((eq listings 'minted)
(org-latex-src-block--minted src-block info lang caption caption-above-p label
+ num-start retain-labels attributes float))
+ ((eq listings 'engraved)
+ (org-latex-src-block--engraved src-block info lang caption caption-above-p label
num-start retain-labels attributes float))
(t
(org-latex-src-block--listings src-block info lang caption caption-above-p label
@@ -3133,6 +3325,88 @@ (defun org-latex-src-block--minted
;; Return value.
(format float-env body)))
+(defun org-latex-src--engrave-code (content lang)
+ "Engrave CONTENT to LaTeX in a LANG-mode buffer, and give the result."
+ (if (require 'engrave-faces-latex nil t)
+ (let* ((lang-mode (and lang (org-src-get-lang-mode lang)))
+ (engraved-buffer
+ (with-temp-buffer
+ (insert content)
+ (when lang-mode
+ (if (functionp lang-mode)
+ (funcall lang-mode)
+ (message "Cannot engrave code as %s. %s is undefined."
+ lang lang-mode)))
+ (engrave-faces-latex-buffer)))
+ (engraved-code
+ (with-current-buffer engraved-buffer
+ (buffer-string))))
+ (kill-buffer engraved-buffer)
+ engraved-code)
+ (user-error "Cannot engrave code as `engrave-faces-latex' is unavailible.")))
+
+(defun org-latex-src-block--engraved
+ (src-block info lang caption caption-above-p _label
+ num-start retain-labels attributes float)
+ "Transcode a SRC-BLOCK element from Org to LaTeX, using engrave-faces-latex.
+LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
+and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
+ (let* ((caption-str (org-latex--caption/label-string src-block info))
+ (placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
+ (plist-get info :latex-default-figure-position)))
+ (float-env
+ (cond
+ ((string= "multicolumn" float)
+ (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ (caption
+ (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
+ placement
+ (if caption-above-p caption-str "")
+ (if caption-above-p "" caption-str)))
+ ((string= "t" float)
+ (concat (format "\\begin{listing}[%s]\n"
+ placement)
+ "%s\n\\end{listing}"))
+ (t "%s")))
+ (options (plist-get info :latex-engraved-options))
+ (content
+ (let* ((code-info (org-export-unravel-code src-block))
+ (max-width
+ (apply 'max
+ (mapcar 'string-width
+ (org-split-string (car code-info)
+ "\n")))))
+ (org-export-format-code
+ (car code-info)
+ (lambda (loc _num ref)
+ (concat
+ loc
+ (when ref
+ ;; Ensure references are flushed to the right,
+ ;; separated with 6 spaces from the widest line
+ ;; of code.
+ (concat (make-string (+ (- max-width (length loc)) 6)
+ ?\s)
+ (format "(%s)" ref)))))
+ nil (and retain-labels (cdr code-info)))))
+ (body
+ (format
+ "\\begin{Code}\n\\begin{Verbatim}[%s]\n%s\\end{Verbatim}\n\\end{Code}"
+ ;; Options.
+ (concat
+ (org-latex--make-option-string
+ (append
+ (when (and num-start (not (assoc "linenos" options)))
+ `(("linenos")
+ ("firstnumber" ,(number-to-string (1+ num-start)))))
+ (let ((local-options (plist-get attributes :options)))
+ (and local-options (list local-options))))))
+ (org-latex-src--engrave-code content lang))))
+ (format float-env body)))
+
(defun org-latex-src-block--listings
(src-block info lang caption caption-above-p label
num-start retain-labels attributes float)
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #6: 0005-ox-latex-Don-t-use-length-to-get-string-width.patch --]
[-- Type: text/x-patch, Size: 1539 bytes --]
From 815de46384a566b3dcd86061b9aa93563a7acb29 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sat, 7 May 2022 13:59:13 +0800
Subject: [PATCH 05/13] ox-latex: Don't use `length' to get string width
* lisp/ox-latex.el (org-latex-src-block--listings,
org-latex-src-block--engraved, org-latex-src-block--minted): Use
`string-width' instead of `length' to gauge the displayed width of the
string in LaTeX. This may not be a perfect match, but it should be an
improvement.
---
lisp/ox-latex.el | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index e0f47cf80..0788b40c0 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -3306,7 +3306,7 @@ (defun org-latex-src-block--minted
(let* ((code-info (org-export-unravel-code src-block))
(max-width
(apply 'max
- (mapcar 'length
+ (mapcar 'string-width
(org-split-string (car code-info)
"\n")))))
(org-export-format-code
@@ -3458,7 +3458,7 @@ (defun org-latex-src-block--listings
(let* ((code-info (org-export-unravel-code src-block))
(max-width
(apply 'max
- (mapcar 'length
+ (mapcar 'string-width
(org-split-string (car code-info) "\n")))))
(org-export-format-code
(car code-info)
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #7: 0006-ox-latex-Refactor-source-block-transcode-fun-sigs.patch --]
[-- Type: text/x-patch, Size: 7299 bytes --]
From fadf2011884d0e46b5442ec43dc942050e2fd048 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sat, 7 May 2022 14:02:44 +0800
Subject: [PATCH 06/13] ox-latex: Refactor source block transcode fun sigs
* lisp/ox-latex.el (org-latex-src-block--listings,
org-latex-src-block--engraved, org-latex-src-block--minted,
org-latex-src-block--custom): Refactor these --backend functions to use
cl-defun with keys instead of having an 11-argument signature.
(org-latex-src-block): Adjust for the change in signature of the
--backend functions, and refactor the `cond' statement to use `pcase'.
---
lisp/ox-latex.el | 82 +++++++++++++++++++++++-------------------------
1 file changed, 39 insertions(+), 43 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 0788b40c0..4a077bb27 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -3186,37 +3186,37 @@ (defun org-latex-src-block (src-block _contents info)
contextual information."
(when (org-string-nw-p (org-element-property :value src-block))
(let* ((lang (org-element-property :language src-block))
- (caption (org-element-property :caption src-block))
- (caption-above-p (org-latex--caption-above-p src-block info))
- (label (org-element-property :name src-block))
- (custom-env (and lang
- (cadr (assq (intern lang)
- org-latex-custom-lang-environments))))
- (num-start (org-export-get-loc src-block info))
- (retain-labels (org-element-property :retain-labels src-block))
- (attributes (org-export-read-attribute :attr_latex src-block))
- (float (plist-get attributes :float))
- (listings (plist-get info :latex-listings)))
- (cond
- ((or (not lang) (not listings))
- (org-latex-src-block--verbatim src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))
- (custom-env
- (org-latex-src-block--custom src-block info lang caption caption-above-p label
- num-start retain-labels attributes float custom-env))
- ((eq listings 'minted)
- (org-latex-src-block--minted src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))
- ((eq listings 'engraved)
- (org-latex-src-block--engraved src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))
- (t
- (org-latex-src-block--listings src-block info lang caption caption-above-p label
- num-start retain-labels attributes float))))))
-
-(defun org-latex-src-block--verbatim
- (src-block info _lang caption caption-above-p _label
- _num-start _retain-labels _attributes float)
+ (caption (org-element-property :caption src-block))
+ (caption-above-p (org-latex--caption-above-p src-block info))
+ (label (org-element-property :name src-block))
+ (custom-env (and lang
+ (cadr (assq (intern lang)
+ org-latex-custom-lang-environments))))
+ (num-start (org-export-get-loc src-block info))
+ (retain-labels (org-element-property :retain-labels src-block))
+ (attributes (org-export-read-attribute :attr_latex src-block))
+ (float (plist-get attributes :float))
+ (listings (plist-get info :latex-listings)))
+ (funcall
+ (pcase listings
+ ((or (pred not) (guard (not lang))) #'org-latex-src-block--verbatim)
+ ((guard custom-env) #'org-latex-src-block--custom)
+ ('minted #'org-latex-src-block--minted)
+ ('engraved #'org-latex-src-block--engraved)
+ (_ #'org-latex-src-block--listings))
+ :src-block src-block
+ :info info
+ :lang lang
+ :caption caption
+ :caption-above-p caption-above-p
+ :label label
+ :num-start num-start
+ :retain-labels retain-labels
+ :attributes attributes
+ :float float))))
+
+(cl-defun org-latex-src-block--verbatim
+ (&key src-block info caption caption-above-p float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using verbatim.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3235,9 +3235,8 @@ (defun org-latex-src-block--verbatim
(if caption-above-p "" (concat "\n" caption-str))))
(t verbatim))))
-(defun org-latex-src-block--custom
- (src-block info _lang caption caption-above-p _label
- _num-start _retain-labels attributes float custom-env)
+(cl-defun org-latex-src-block--custom
+ (&key src-block info caption caption-above-p attributes float custom-env &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using a custom environment.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3257,9 +3256,8 @@ (defun org-latex-src-block--custom
(?l . ,(org-latex--label src-block info))
(?o . ,(or (plist-get attributes :options) "")))))))
-(defun org-latex-src-block--minted
- (src-block info lang caption caption-above-p _label
- num-start retain-labels attributes float)
+(cl-defun org-latex-src-block--minted
+ (&key src-block info lang caption caption-above-p num-start retain-labels attributes float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using minted.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3345,9 +3343,8 @@ (defun org-latex-src--engrave-code (content lang)
engraved-code)
(user-error "Cannot engrave code as `engrave-faces-latex' is unavailible.")))
-(defun org-latex-src-block--engraved
- (src-block info lang caption caption-above-p _label
- num-start retain-labels attributes float)
+(cl-defun org-latex-src-block--engraved
+ (&key src-block info lang caption caption-above-p num-start retain-labels attributes float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using engrave-faces-latex.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
@@ -3407,9 +3404,8 @@ (defun org-latex-src-block--engraved
(org-latex-src--engrave-code content lang))))
(format float-env body)))
-(defun org-latex-src-block--listings
- (src-block info lang caption caption-above-p label
- num-start retain-labels attributes float)
+(cl-defun org-latex-src-block--listings
+ (&key src-block info lang caption caption-above-p label num-start retain-labels attributes float &allow-other-keys)
"Transcode a SRC-BLOCK element from Org to LaTeX, using listings.
LANG, CAPTION, CAPTION-ABOVE-P, LABEL, NUM-START, RETAIN-LABELS, ATTRIBUTES
and FLOAT are extracted from SRC-BLOCK and INFO in `org-latex-src-block'."
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #8: 0007-ox-latex-Replace-org-latex-listings.patch --]
[-- Type: text/x-patch, Size: 16239 bytes --]
From e6dfd941daf06beb859ae6effd0be9d734121897 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sat, 7 May 2022 14:46:28 +0800
Subject: [PATCH 07/13] ox-latex: Replace `org-latex-listings'
* lisp/ox-latex.el (org-latex-src-block, org-latex-keyword,
org-latex-inline-src-block, org-latex-template,
org-latex--caption/label-string, org-latex-engraved-preamble,
org-latex-listings): Replace `org-latex-listings' with
`org-latex-src-block-backend', which now can be set to listings/verbatim
and no longer advertises t/nil as valid values.
* lisp/ox-beamer.el (org-beamer-template): Update in the same manner as
`org-latex-template'.
* lisp/org-compat.el: Make `org-latex-listings' an obsolete alias for
`org-latex-src-block-backend'.
* testing/lisp/test-ox.el: Replace `org-latex-listings' reference with
`org-latex-src-block-backend'.
* doc/org-manual.org (Footnotes, LaTeX specific properties, Literal
Examples): Replace references to `org-latex-listings' with
`org-latex-src-block-backend'.
* etc/ORG-NEWS: Add a news entry noting this change.
The variable `org-latex-listings' originally indicated whether source
blocks should use the listings LaTeX package, or not. This usage has
evolved over the years, and now it sets one of four different
fontification backends. This renaming should make the variable name a
bit less misleading.
---
doc/org-manual.org | 6 +--
etc/ORG-NEWS | 9 ++++
lisp/org-compat.el | 2 +
lisp/ox-beamer.el | 2 +-
lisp/ox-latex.el | 117 ++++++++++++++++++++++------------------
testing/lisp/test-ox.el | 2 +-
6 files changed, 81 insertions(+), 57 deletions(-)
diff --git a/doc/org-manual.org b/doc/org-manual.org
index c0d38cd8c..60bded419 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -11159,7 +11159,7 @@ ** Literal Examples
#+end_example
#+cindex: formatting source code, markup rules
-#+vindex: org-latex-listings
+#+vindex: org-latex-src-block-backend
If the example is source code from a programming language, or any
other text that can be marked up by Font Lock in Emacs, you can ask
for the example to look like the fontified Emacs buffer[fn:114]. This
@@ -16304,12 +16304,12 @@ **** LaTeX specific properties
| ~:latex-link-with-unknown-path-format~ | ~org-latex-link-with-unknown-path-format~ |
| ~:latex-listings-langs~ | ~org-latex-listings-langs~ |
| ~:latex-listings-options~ | ~org-latex-listings-options~ |
-| ~:latex-listings~ | ~org-latex-listings~ |
| ~:latex-minted-langs~ | ~org-latex-minted-langs~ |
| ~:latex-minted-options~ | ~org-latex-minted-options~ |
| ~:latex-prefer-user-labels~ | ~org-latex-prefer-user-labels~ |
| ~:latex-subtitle-format~ | ~org-latex-subtitle-format~ |
| ~:latex-subtitle-separate~ | ~org-latex-subtitle-separate~ |
+| ~:latex-src-block-backend~ | ~org-latex-src-block-backend~ |
| ~:latex-table-scientific-notation~ | ~org-latex-table-scientific-notation~ |
| ~:latex-tables-booktabs~ | ~org-latex-tables-booktabs~ |
| ~:latex-tables-centered~ | ~org-latex-tables-centered~ |
@@ -22256,7 +22256,7 @@ * Footnotes
version 1.34 of the =htmlize.el= package, which you need to install).
Fontified code chunks in LaTeX can be achieved using either the
[[https://www.ctan.org/pkg/listings][listings]] package or the [[https://www.ctan.org/pkg/minted][minted]] package. Refer to
-~org-latex-listings~ for details.
+~org-latex-src-block-backend~ for details.
[fn:115] Source code in code blocks may also be evaluated either
interactively or on export. See [[*Working with Source Code]] for more
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 27de6da62..ebb3cd649 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -273,6 +273,15 @@ Chmod-style permissions are based on the new variable
~org-babel-tangle-default-file-mode~.
*** A new custom setting =org-agenda-clock-report-header= to add a header to org agenda clock report
+
+*** ~org-latex-listings~ has been replaced with ~org-latex-src-block-backend~
+
+~org-latex-listings~ has been renamed to better reflect the current
+purpose of the variable. The replacement variable
+~org-latex-src-block-backend~ acts in exactly the same way, however it
+accepts =listings= and =verbatim= in place of =t= and =nil= (which
+still work, but are no longer listed as valid options).
+
* Version 9.5
** Important announcements and breaking changes
diff --git a/lisp/org-compat.el b/lisp/org-compat.el
index a29f206a4..704197645 100644
--- a/lisp/org-compat.el
+++ b/lisp/org-compat.el
@@ -324,6 +324,8 @@ (define-obsolete-variable-alias 'org-latex-create-formula-image-program
'org-preview-latex-default-process "9.0")
(define-obsolete-variable-alias 'org-latex-preview-ltxpng-directory
'org-preview-latex-image-directory "9.0")
+(define-obsolete-variable-alias 'org-latex-listings
+ 'org-latex-src-block-backend "9.6")
(define-obsolete-function-alias 'org-table-p 'org-at-table-p "9.0")
(define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "9.0")
(define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp "8.3")
diff --git a/lisp/ox-beamer.el b/lisp/ox-beamer.el
index fe73bf686..e6232d8d2 100644
--- a/lisp/ox-beamer.el
+++ b/lisp/ox-beamer.el
@@ -858,7 +858,7 @@ (defun org-beamer-template (contents info)
(and (stringp template)
(format-spec template (org-latex--format-spec info))))
;; engrave-faces-latex preamble
- (when (and (eq org-latex-listings 'engraved)
+ (when (and (eq org-latex-src-block-backend 'engraved)
(org-element-map (plist-get info :parse-tree)
'(src-block inline-src-block) #'identity
info t))
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 4a077bb27..f81625b45 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -143,7 +143,7 @@ (org-export-define-backend 'latex
(:latex-inactive-timestamp-format nil nil org-latex-inactive-timestamp-format)
(:latex-inline-image-rules nil nil org-latex-inline-image-rules)
(:latex-link-with-unknown-path-format nil nil org-latex-link-with-unknown-path-format)
- (:latex-listings nil nil org-latex-listings)
+ (:latex-src-block-backend nil nil org-latex-src-block-backend)
(:latex-listings-langs nil nil org-latex-listings-langs)
(:latex-listings-options nil nil org-latex-listings-options)
(:latex-minted-langs nil nil org-latex-minted-langs)
@@ -937,22 +937,22 @@ (defcustom org-latex-format-inlinetask-function
;; Src blocks
-(defcustom org-latex-listings nil
- "Non-nil means export source code using the listings package.
+(defcustom org-latex-src-block-backend 'verbatim
+ "Backend used to generate source code listings.
-This package will fontify source code, possibly even with color.
-There are four implementations of this functionality you may
+This sets the behaviour for fontifying source code, possibly even with
+color. There are four implementations of this functionality you may
choose from (ordered from least to most capable):
-1. Verbatim (nil)
-2. Listings (t)
-3. Minted (minted)
-4. Engraved (engraved)
+1. Verbatim
+2. Listings
+3. Minted
+4. Engraved
The first two options provide basic syntax
highlighting (listings), or none at all (verbatim).
-When using listings, you also need to make use of the LaTeX
-\"listings\" package. The \"color\" package is also needed if you
+When using listings, you also need to make use of LaTeX package
+\"listings\"e. The \"color\" LaTeX package is also needed if you
would like color too. These can simply be added to
`org-latex-packages-alist', using customise or something like:
@@ -963,27 +963,12 @@ (defcustom org-latex-listings nil
There are two further options for more comprehensive
fontification. The first can be set with,
- (setq org-latex-listings \\='engraved)
+ (setq org-latex-src-block-backend \\='minted)
-which causes source code to be run through
-`engrave-faces-latex-buffer', which generates colorings using
-Emacs' font-lock information. This requires the engrave-faces
-package (availible from ELPA), and the fvextra LaTeX package be
-installed.
-
-The styling of the engraved result can customised with
-`org-latex-engraved-preamble' and `org-latex-engraved-options'.
-The default preamble also uses the tcolorbox LaTeX package in
-addition to fvextra.
-
-The second more comprehensive option can be set with,
-
- (setq org-latex-listings \\='minted)
-
-which causes source code to be exported using the minted package
-as opposed to listings. If you want to use minted, you need to
-add the minted package to `org-latex-packages-alist', for example
-using customize, or with
+which causes source code to be exported using the LaTeX package
+minted as opposed to listings. If you want to use minted, you
+need to add the minted package to `org-latex-packages-alist', for
+example using customize, or with
(require \\='ox-latex)
(add-to-list \\='org-latex-packages-alist \\='(\"newfloat\" \"minted\"))
@@ -996,14 +981,29 @@ (defcustom org-latex-listings nil
The minted choice has possible repercussions on the preview of
latex fragments (see `org-preview-latex-fragment'). If you run
into previewing problems, please consult
-URL `https://orgmode.org/worg/org-tutorials/org-latex-preview.html'."
+URL `https://orgmode.org/worg/org-tutorials/org-latex-preview.html'.
+
+The most comprehensive option can be set with,
+
+ (setq org-latex-src-block-backend \\='engraved)
+
+which causes source code to be run through
+`engrave-faces-latex-buffer', which generates colorings using
+Emacs' font-lock information. This requires the Emacs package
+engrave-faces (availible from ELPA), and the LaTeX package
+fvextra be installed.
+
+The styling of the engraved result can customised with
+`org-latex-engraved-preamble' and `org-latex-engraved-options'.
+The default preamble also uses the LaTeX package tcolorbox in
+addition to fvextra."
:group 'org-export-latex
:type '(choice
- (const :tag "Use listings" t)
+ (const :tag "Use listings" listings)
(const :tag "Use minted" minted)
(const :tag "Use engrave-faces-latex" engraved)
- (const :tag "Export verbatim" nil))
- :safe (lambda (s) (memq s '(t nil minted engraved))))
+ (const :tag "Export verbatim" verbatim))
+ :safe (lambda (s) (memq s '(listings minted engraved verbatim))))
(defcustom org-latex-listings-langs
'((emacs-lisp "Lisp") (lisp "Lisp") (clojure "Lisp")
@@ -1203,7 +1203,7 @@ (defcustom org-latex-engraved-preamble
[LISTINGS-SETUP]"
"Preamble content injected when using engrave-faces-latex for source blocks.
-This is relevant when `org-latex-listings' is set to `engraved'.
+This is relevant when `org-latex-src-block-backend' is set to `engraved'.
There is quite a lot of flexibility in what this preamble can be,
as long as it:
@@ -1526,7 +1526,8 @@ (defun org-latex--caption/label-string (element info)
main)
(and (eq type 'src-block)
(not (plist-get attr :float))
- (null (plist-get info :latex-listings)))))
+ (memq (plist-get info :latex-src-block-backend)
+ '(verbatim nil)))))
(short (org-export-get-caption element t))
(caption-from-attr-latex (plist-get attr :caption)))
(cond
@@ -1546,7 +1547,8 @@ (defun org-latex--caption/label-string (element info)
(paragraph "figure")
(image "figure")
(special-block "figure")
- (src-block (if (plist-get info :latex-listings)
+ (src-block (if (not (memq (plist-get info :latex-src-block-backend)
+ '(verbatim nil)))
"listing"
"figure"))
(t (symbol-name type*)))
@@ -1933,7 +1935,7 @@ (defun org-latex-template (contents info)
(and (stringp template)
(format-spec template spec)))
;; engrave-faces-latex preamble
- (when (and (eq org-latex-listings 'engraved)
+ (when (and (eq org-latex-src-block-backend 'engraved)
(org-element-map (plist-get info :parse-tree)
'(src-block inline-src-block) #'identity
info t))
@@ -2321,11 +2323,17 @@ (defun org-latex-inline-src-block (inline-src-block _contents info)
contextual information."
(let ((code (org-element-property :value inline-src-block))
(lang (org-element-property :language inline-src-block)))
- (pcase (plist-get info :latex-listings)
- ('nil (org-latex--text-markup code 'code info))
- ('minted (org-latex-inline-src-block--minted info code lang))
- ('engraved (org-latex-inline-src-block--engraved info code lang))
- (_ (org-latex-inline-src-block--listings info code lang)))))
+ (pcase (plist-get info :latex-src-block-backend)
+ (`verbatim (org-latex--text-markup code 'code info))
+ (`minted (org-latex-inline-src-block--minted info code lang))
+ (`engraved (org-latex-inline-src-block--engraved info code lang))
+ (`listings (org-latex-inline-src-block--listings info code lang))
+ (oldval
+ (message "Please update the LaTeX src-block-backend to %s"
+ (if oldval "listings" "verbatim"))
+ (if oldval
+ (org-latex-inline-src-block--listings info code lang)
+ (org-latex--text-markup code 'code info))))))
(defun org-latex-inline-src-block--minted (info code lang)
"Transcode an inline src block's content from Org to LaTeX, using minted.
@@ -2508,7 +2516,7 @@ (defun org-latex-keyword (keyword _contents info)
(concat depth (and depth "\n") "\\tableofcontents"))))
((string-match-p "\\<tables\\>" value) "\\listoftables")
((string-match-p "\\<listings\\>" value)
- (cl-case (plist-get info :latex-listings)
+ (cl-case (plist-get info :latex-src-block-backend)
((nil) "\\listoffigures")
(minted "\\listoflistings")
(engraved "\\listoflistings")
@@ -3195,15 +3203,20 @@ (defun org-latex-src-block (src-block _contents info)
(num-start (org-export-get-loc src-block info))
(retain-labels (org-element-property :retain-labels src-block))
(attributes (org-export-read-attribute :attr_latex src-block))
- (float (plist-get attributes :float))
- (listings (plist-get info :latex-listings)))
+ (float (plist-get attributes :float)))
(funcall
- (pcase listings
- ((or (pred not) (guard (not lang))) #'org-latex-src-block--verbatim)
+ (pcase (plist-get info :latex-src-block-backend)
+ ((or `verbatim (guard (not lang))) #'org-latex-src-block--verbatim)
+ (`minted #'org-latex-src-block--minted)
+ (`engraved #'org-latex-src-block--engraved)
+ (`listings #'org-latex-src-block--listings)
((guard custom-env) #'org-latex-src-block--custom)
- ('minted #'org-latex-src-block--minted)
- ('engraved #'org-latex-src-block--engraved)
- (_ #'org-latex-src-block--listings))
+ (oldval
+ (message "Please update the LaTeX src-block-backend to %s"
+ (if oldval "listings" "verbatim"))
+ (if oldval
+ #'org-latex-src-block--listings
+ #'org-latex-src-block--verbatim)))
:src-block src-block
:info info
:lang lang
diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el
index 25e02b258..28f950813 100644
--- a/testing/lisp/test-ox.el
+++ b/testing/lisp/test-ox.el
@@ -3978,7 +3978,7 @@ (ert-deftest test-org-export/latex-src-block-verbatim-caption ()
\\end{verbatim}
\\caption{Caption is below, 60\\%s}
\\end{figure*}"
- (let ((org-latex-listings 'minted) ; inactive due to missing lang
+ (let ((org-latex-src-block-backend 'minted) ; inactive due to missing lang
(org-latex-default-figure-position "tp"))
;; Namely "multicolumn" value to get just figure environment
;; looks like a bug.
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #9: 0008-ox-latex-Support-setting-the-engraved-theme.patch --]
[-- Type: text/x-patch, Size: 3213 bytes --]
From 7bc1f26f126449d8e457d88d6344aa1b5d439dfe Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 8 May 2022 02:01:34 +0800
Subject: [PATCH 08/13] ox-latex: Support setting the engraved theme
* lisp/ox-latex.el (org-latex-generate-engraved-preamble,
org-latex-engraved-theme): Introduce the new export keyword
LATEX_ENGRAVED_THEME with default value given by
`org-latex-engraved-theme'. This is used to set the engraved theme used
in org-latex-engraved-theme.
This bumps the minimum required version of engrave-faces from v0.2 to
v0.3.
---
lisp/ox-latex.el | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index f81625b45..e367e9040 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -129,6 +129,7 @@ (org-export-define-backend 'latex
(:latex-diary-timestamp-format nil nil org-latex-diary-timestamp-format)
(:latex-engraved-options nil nil org-latex-engraved-options)
(:latex-engraved-preamble nil nil org-latex-engraved-preamble)
+ (:latex-engraved-theme "LATEX_ENGRAVED_THEME" nil org-latex-engraved-theme)
(:latex-footnote-defined-format nil nil org-latex-footnote-defined-format)
(:latex-footnote-separator nil nil org-latex-footnote-separator)
(:latex-format-drawer-function nil nil org-latex-format-drawer-function)
@@ -1270,6 +1271,14 @@ (defcustom org-latex-engraved-options
:type '(alist :key-type (string :tag "option")
:value-type (string :tag "value")))
+(defcustom org-latex-engraved-theme nil
+ "The theme that should be used for engraved code, when non-nil.
+This can be set to any theme defined in `engrave-faces-themes' or
+loadable by Emacs. When set to t, the current Emacs theme is
+used. When nil, no theme is applied."
+ :group 'org-export-latex
+ :type 'symbol)
+
(defun org-latex-generate-engraved-preamble (info syntax-colours-p)
"Generate the preamble to setup engraved code.
The result is constructed from the :latex-engraved-preamble and
@@ -1278,7 +1287,8 @@ (defun org-latex-generate-engraved-preamble (info syntax-colours-p)
`org-latex-engraved-options' respectively."
(let* ((engraved-options
(plist-get info :latex-engraved-options))
- (engraved-preamble (plist-get info :latex-engraved-preamble)))
+ (engraved-preamble (plist-get info :latex-engraved-preamble))
+ (engraved-theme (plist-get info :latex-engraved-theme)))
(when (string-match "^[ \t]*\\[FVEXTRA-SETUP\\][ \t]*\n?" engraved-preamble)
(setq engraved-preamble
(replace-match
@@ -1310,7 +1320,8 @@ (defun org-latex-generate-engraved-preamble (info syntax-colours-p)
engraved-preamble
"\n\n% Setup for code blocks [2/2]: syntax highlighting colors\n"
(if (require 'engrave-faces-latex nil t)
- (engrave-faces-latex-gen-preamble)
+ (engrave-faces-latex-gen-preamble
+ (when engraved-theme (intern engraved-theme)))
(message "Cannot engrave source blocks. Consider installing `engrave-faces'.")
"% WARNING syntax highlighting unavailible as engrave-faces-latex was missing.\n")
"\n")
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #10: 0009-ox-latex-Support-setting-engraved-theme-per-block.patch --]
[-- Type: text/x-patch, Size: 9236 bytes --]
From 24db3cc69c2e28e4207532ac5181e27a28456eae Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 8 May 2022 15:28:29 +0800
Subject: [PATCH 09/13] ox-latex: Support setting engraved theme per-block
* lisp/ox-latex.el (org-latex-src-block--engraved,
org-latex-src--engrave-code, org-latex-inline-src-block--engraved,
org-latex-generate-engraved-preamble): Allow for the engraved theme used
to be set on a per-src-block basis with #+attr_latex: :engraved-theme
THEME. Extra setup code is now generated in
`org-latex-generate-engraved-preamble'. To facilitate the application
of themes to src blocks, `org-latex-src--engrave-code' now takes on a
larger portion of the transcoding work.
---
lisp/ox-latex.el | 120 +++++++++++++++++++++++++++++++++++++----------
1 file changed, 96 insertions(+), 24 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index e367e9040..9634461bd 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1288,7 +1288,27 @@ (defun org-latex-generate-engraved-preamble (info syntax-colours-p)
(let* ((engraved-options
(plist-get info :latex-engraved-options))
(engraved-preamble (plist-get info :latex-engraved-preamble))
- (engraved-theme (plist-get info :latex-engraved-theme)))
+ (engraved-theme (plist-get info :latex-engraved-theme))
+ (engraved-themes
+ (cl-delete-duplicates
+ (org-element-map
+ (plist-get info :parse-tree)
+ '(src-block inline-src-block)
+ (lambda (src)
+ (plist-get
+ (org-export-read-attribute :attr_latex src)
+ :engraved-theme))
+ info)))
+ (gen-theme-spec
+ (lambda (theme)
+ (if (eq engrave-faces-latex-output-style 'preset)
+ (engrave-faces-latex-gen-preamble (when theme (intern theme)))
+ (engrave-faces-latex-gen-preamble-line
+ 'default
+ (alist-get 'default
+ (if theme
+ (engrave-faces-get-theme (intern theme))
+ engrave-faces-current-preset-style)))))))
(when (string-match "^[ \t]*\\[FVEXTRA-SETUP\\][ \t]*\n?" engraved-preamble)
(setq engraved-preamble
(replace-match
@@ -1318,10 +1338,31 @@ (defun org-latex-generate-engraved-preamble (info syntax-colours-p)
(concat
"\n% Setup for code blocks [1/2]\n\n"
engraved-preamble
- "\n\n% Setup for code blocks [2/2]: syntax highlighting colors\n"
+ "\n\n% Setup for code blocks [2/2]: syntax highlighting colors\n\n"
(if (require 'engrave-faces-latex nil t)
- (engrave-faces-latex-gen-preamble
- (when engraved-theme (intern engraved-theme)))
+ (if engraved-themes
+ (concat
+ (mapconcat
+ (lambda (theme)
+ (format
+ "\n\\newcommand{\\engravedtheme%s}{%%\n%s\n}"
+ (replace-regexp-in-string "[^A-Za-z]" "" theme)
+ (replace-regexp-in-string
+ "newcommand" "renewcommand"
+ (replace-regexp-in-string
+ "#" "##"
+ (funcall gen-theme-spec theme)))))
+ engraved-themes
+ "\n")
+ "\n\n"
+ (cond
+ ((memq engraved-theme engraved-themes)
+ (concat "\\engravedtheme"
+ (replace-regexp-in-string
+ "[^A-Za-z]" "" engraved-theme)
+ "\n"))
+ (t (funcall gen-theme-spec engraved-theme))))
+ (funcall gen-theme-spec engraved-theme))
(message "Cannot engrave source blocks. Consider installing `engrave-faces'.")
"% WARNING syntax highlighting unavailible as engrave-faces-latex was missing.\n")
"\n")
@@ -2359,10 +2400,11 @@ (defun org-latex-inline-src-block--minted (info code lang)
mint-lang
code)))
-(defun org-latex-inline-src-block--engraved (_info code lang)
+(defun org-latex-inline-src-block--engraved (info code lang)
"Transcode an inline src block's content from Org to LaTeX, using engrave-faces.
INFO, CODE, and LANG are provided by `org-latex-inline-src-block'."
- (format "\\Verb{%s}" (org-latex-src--engrave-code code lang)))
+ (org-latex-src--engrave-code
+ code lang nil (plist-get info :latex-engraved-options) t))
(defun org-latex-inline-src-block--listings (info code lang)
"Transcode an inline src block's content from Org to LaTeX, using lstlistings.
@@ -3347,13 +3389,25 @@ (cl-defun org-latex-src-block--minted
;; Return value.
(format float-env body)))
-(defun org-latex-src--engrave-code (content lang)
- "Engrave CONTENT to LaTeX in a LANG-mode buffer, and give the result."
+(defun org-latex-src--engrave-code (content lang &optional theme options inline)
+ "Engrave CONTENT to LaTeX in a LANG-mode buffer, and give the result.
+When the THEME symbol is non-nil, that theme will be used.
+
+When INLINE is nil, a Verbatim environment wrapped in a Code
+environment will be used. When t, a Verb command will be used.
+
+When OPTIONS is provided, as either a string or list of key-value
+pairs accepted by `org-latex--make-option-string', it is passed
+to the Verbatim environment or Verb command."
(if (require 'engrave-faces-latex nil t)
(let* ((lang-mode (and lang (org-src-get-lang-mode lang)))
+ (engrave-faces-current-preset-style
+ (if theme
+ (engrave-faces-get-theme theme)
+ engrave-faces-current-preset-style))
(engraved-buffer
(with-temp-buffer
- (insert content)
+ (insert (string-trim-right content "\n"))
(when lang-mode
(if (functionp lang-mode)
(funcall lang-mode)
@@ -3362,9 +3416,27 @@ (defun org-latex-src--engrave-code (content lang)
(engrave-faces-latex-buffer)))
(engraved-code
(with-current-buffer engraved-buffer
- (buffer-string))))
+ (buffer-string)))
+ (engraved-options
+ (when options
+ (concat "["
+ (if (listp options)
+ (org-latex--make-option-string options)
+ options)
+ "]")))
+ (engraved-wrapped
+ (if inline
+ (concat "\\Verb" engraved-options "{" engraved-code "}")
+ (concat "\\begin{Code}\n\\begin{Verbatim}" engraved-options "\n"
+ engraved-code "\n\\end{Verbatim}\n\\end{Code}"))))
(kill-buffer engraved-buffer)
- engraved-code)
+ (if theme
+ (concat "{\\engravedtheme"
+ (replace-regexp-in-string "[^A-Za-z]" ""
+ (symbol-name theme))
+ engraved-wrapped
+ "}")
+ engraved-wrapped))
(user-error "Cannot engrave code as `engrave-faces-latex' is unavailible.")))
(cl-defun org-latex-src-block--engraved
@@ -3392,7 +3464,15 @@ (cl-defun org-latex-src-block--engraved
placement)
"%s\n\\end{listing}"))
(t "%s")))
- (options (plist-get info :latex-engraved-options))
+ (options
+ (let ((engraved-options (plist-get info :latex-engraved-options))
+ (local-options (plist-get attributes :options)))
+ (append
+ (when (and num-start (not (assoc "linenos" engraved-options)))
+ `(("linenos")
+ ("firstnumber" ,(number-to-string (1+ num-start)))))
+ (and local-options (list local-options)))))
+ (engraved-theme (plist-get attributes :engraved-theme))
(content
(let* ((code-info (org-export-unravel-code src-block))
(max-width
@@ -3414,18 +3494,10 @@ (cl-defun org-latex-src-block--engraved
(format "(%s)" ref)))))
nil (and retain-labels (cdr code-info)))))
(body
- (format
- "\\begin{Code}\n\\begin{Verbatim}[%s]\n%s\\end{Verbatim}\n\\end{Code}"
- ;; Options.
- (concat
- (org-latex--make-option-string
- (append
- (when (and num-start (not (assoc "linenos" options)))
- `(("linenos")
- ("firstnumber" ,(number-to-string (1+ num-start)))))
- (let ((local-options (plist-get attributes :options)))
- (and local-options (list local-options))))))
- (org-latex-src--engrave-code content lang))))
+ (org-latex-src--engrave-code
+ content lang
+ (when engraved-theme (intern engraved-theme))
+ options)))
(format float-env body)))
(cl-defun org-latex-src-block--listings
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #11: 0010-ox-latex-Fix-captions-in-minted-engraved-code.patch --]
[-- Type: text/x-patch, Size: 4986 bytes --]
From 7d9306fe8ada094ff6964422cc527645eb9ffc55 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Mon, 9 May 2022 00:04:10 +0800
Subject: [PATCH 10/13] ox-latex: Fix %-captions in minted/engraved code
* lisp/ox-latex.el (org-latex-src-block--engraved,
org-latex-src-block--minted): Refactor float-env to be clearer, and
switch from `format' to `concat' to fix the bug where %-chars in
captions are interpreted as a format specifier.
---
lisp/ox-latex.el | 55 +++++++++++++++++++++---------------------------
1 file changed, 24 insertions(+), 31 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 9634461bd..ceb1bd483 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -3330,23 +3330,20 @@ (cl-defun org-latex-src-block--minted
(let* ((caption-str (org-latex--caption/label-string src-block info))
(placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
(plist-get info :latex-default-figure-position)))
+ (multicolumn-p (string= "multicolumn" float))
(float-env
(cond
- ((string= "multicolumn" float)
- (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- (caption
- (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
+ ((or caption multicolumn-p)
+ (cons
+ (concat "\\begin{listing" (when multicolumn-p "*")
+ "}[" placement "]\n"
+ (if caption-above-p caption-str ""))
+ (concat "\n" (if caption-above-p "" caption-str)
+ "\\end{listing" (when multicolumn-p "*") "}")))
((string= "t" float)
- (concat (format "\\begin{listing}[%s]\n"
- placement)
- "%s\n\\end{listing}"))
- (t "%s")))
+ (cons
+ (concat "\\begin{listing}[" placement "]\n")
+ "\n\\end{listing}"))))
(options (plist-get info :latex-minted-options))
(body
(format
@@ -3386,8 +3383,7 @@ (cl-defun org-latex-src-block--minted
?\s)
(format "(%s)" ref)))))
nil (and retain-labels (cdr code-info)))))))
- ;; Return value.
- (format float-env body)))
+ (concat (car float-env) body (cdr float-env))))
(defun org-latex-src--engrave-code (content lang &optional theme options inline)
"Engrave CONTENT to LaTeX in a LANG-mode buffer, and give the result.
@@ -3447,23 +3443,20 @@ (cl-defun org-latex-src-block--engraved
(let* ((caption-str (org-latex--caption/label-string src-block info))
(placement (or (org-unbracket-string "[" "]" (plist-get attributes :placement))
(plist-get info :latex-default-figure-position)))
+ (multicolumn-p (string= "multicolumn" float))
(float-env
(cond
- ((string= "multicolumn" float)
- (format "\\begin{listing*}[%s]\n%s%%s\n%s\\end{listing*}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
- (caption
- (format "\\begin{listing}[%s]\n%s%%s\n%s\\end{listing}"
- placement
- (if caption-above-p caption-str "")
- (if caption-above-p "" caption-str)))
+ ((or caption multicolumn-p)
+ (cons
+ (concat "\\begin{listing" (when multicolumn-p "*")
+ "}[" placement "]\n"
+ (if caption-above-p caption-str ""))
+ (concat "\n" (if caption-above-p "" caption-str)
+ "\\end{listing" (when multicolumn-p "*") "}")))
((string= "t" float)
- (concat (format "\\begin{listing}[%s]\n"
- placement)
- "%s\n\\end{listing}"))
- (t "%s")))
+ (cons
+ (concat "\\begin{listing}[" placement "]\n")
+ "\n\\end{listing}"))))
(options
(let ((engraved-options (plist-get info :latex-engraved-options))
(local-options (plist-get attributes :options)))
@@ -3498,7 +3491,7 @@ (cl-defun org-latex-src-block--engraved
content lang
(when engraved-theme (intern engraved-theme))
options)))
- (format float-env body)))
+ (concat (car float-env) body (cdr float-env))))
(cl-defun org-latex-src-block--listings
(&key src-block info lang caption caption-above-p label num-start retain-labels attributes float &allow-other-keys)
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #12: 0011-ox-latex-Support-mathescape-d-code-with-engraved.patch --]
[-- Type: text/x-patch, Size: 3891 bytes --]
From 5fdbbbe811a4d15fa1f7fa4bfad9d3e11e6f4eb8 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Wed, 11 May 2022 23:18:19 +0800
Subject: [PATCH 11/13] ox-latex: Support mathescape'd code with engraved
* lisp/ox-latex.el (org-latex-src-block--engraved,
org-latex-src--engrave-mathescape-p): Using the mathescape functionality
in engrave-faces-latex v0.3.1, we can support the mathescape option of
fvextra well. Along the way, we fix a minor issue with the localoptions
list in `org-latex-src-block--engraved`.
---
lisp/ox-latex.el | 46 +++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 41 insertions(+), 5 deletions(-)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index ceb1bd483..29543f8e9 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -3385,6 +3385,40 @@ (cl-defun org-latex-src-block--minted
nil (and retain-labels (cdr code-info)))))))
(concat (car float-env) body (cdr float-env))))
+(defun org-latex-src--engrave-mathescape-p (info options)
+ "From the export INFO plist, and the per-block OPTIONS, determine mathescape."
+ (let ((default-options (plist-get info :latex-engraved-options))
+ (mathescape-status
+ (lambda (opts)
+ (cl-some
+ (lambda (opt)
+ (or (and
+ (null (cdr opt))
+ (cond
+ ((string-match-p
+ "\\(?:^\\|,\\)mathescape=false\\(?:,\\|$\\)"
+ (car opt))
+ 'no)
+ ((or (string-match-p
+ "\\(?:^\\|,\\)mathescape\\(?:=true\\)?\\(?:,\\|$\\)"
+ (car opt))
+ (string= "mathescape" (car opt)))
+ 'yes)))
+ (and
+ (string= (car opt) "mathescape")
+ (cond
+ ((or (and (stringp (cdr opt)) (string= (cdr opt) "true"))
+ (equal '("true") (cdr opt)))
+ 'yes)
+ ((or (and (stringp (cdr opt)) (string= "false" (cdr opt)))
+ (equal '("false") (cdr opt)))
+ 'no)))))
+ opts))))
+ (if-let ((mathescape (or (funcall mathescape-status default-options)
+ (funcall mathescape-status options))))
+ (when (eq mathescape 'yes)
+ (or engrave-faces-latex-mathescape t)))))
+
(defun org-latex-src--engrave-code (content lang &optional theme options inline)
"Engrave CONTENT to LaTeX in a LANG-mode buffer, and give the result.
When the THEME symbol is non-nil, that theme will be used.
@@ -3464,7 +3498,7 @@ (cl-defun org-latex-src-block--engraved
(when (and num-start (not (assoc "linenos" engraved-options)))
`(("linenos")
("firstnumber" ,(number-to-string (1+ num-start)))))
- (and local-options (list local-options)))))
+ (and local-options `((,local-options))))))
(engraved-theme (plist-get attributes :engraved-theme))
(content
(let* ((code-info (org-export-unravel-code src-block))
@@ -3487,10 +3521,12 @@ (cl-defun org-latex-src-block--engraved
(format "(%s)" ref)))))
nil (and retain-labels (cdr code-info)))))
(body
- (org-latex-src--engrave-code
- content lang
- (when engraved-theme (intern engraved-theme))
- options)))
+ (let ((engrave-faces-latex-mathescape
+ (org-latex-src--engrave-mathescape-p info options)))
+ (org-latex-src--engrave-code
+ content lang
+ (when engraved-theme (intern engraved-theme))
+ options))))
(concat (car float-env) body (cdr float-env))))
(cl-defun org-latex-src-block--listings
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #13: 0012-news-and-manual-Mention-ox-latex-s-engraved-code.patch --]
[-- Type: text/x-patch, Size: 4727 bytes --]
From a57baf2c0f863aba49264875fd93d62a602ec0d9 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Thu, 12 May 2022 00:03:11 +0800
Subject: [PATCH 12/13] news and manual: Mention ox-latex's engraved code
* etc/ORG-NEWS: Mention the addition of the "engraved" source block
transcoding backend.
* doc/org-manual.org (Footnotes, LaTeX specific properties, Source
blocks in LaTeX export): Basic documentation on the "engraved" source
block transcoding backend.
---
doc/org-manual.org | 25 ++++++++++++++++---------
etc/ORG-NEWS | 9 +++++++++
2 files changed, 25 insertions(+), 9 deletions(-)
diff --git a/doc/org-manual.org b/doc/org-manual.org
index 60bded419..7cba5f18d 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -13811,22 +13811,26 @@ *** Source blocks in LaTeX export
#+vindex: org-latex-listings-options
#+vindex: org-latex-minted-options
+#+vindex: org-latex-engraved-options
The LaTeX export back-end passes string values in =:options= to LaTeX
packages for customization of that specific source block. In the
-example below, the =:options= are set for Minted. Minted is a source
-code highlighting LaTeX package with many configurable options[fn:134].
+example below, the =:options= are set for Engraved or Minted. Minted
+is a source code highlighting LaTeX package with many configurable
+options[fn:134]. Both Minted and Engraved are built on [[https://www.ctan.org/pkg/fvextra][fvextra]], and
+so support many of the same options.
#+begin_example
-,#+ATTR_LATEX: :options commentstyle=\bfseries
+,#+ATTR_LATEX: :options mathescape
,#+BEGIN_SRC emacs-lisp
- (defun Fib (n)
+ (defun Fib (n) ; $n_i = n_{i-2} + n_{i-1}$
(if (< n 2) n (+ (Fib (- n 1)) (Fib (- n 2)))))
,#+END_SRC
#+end_example
-To apply similar configuration options for all source blocks in
-a file, use the ~org-latex-listings-options~ and
-~org-latex-minted-options~ variables.
+To apply similar configuration options for all source blocks in a
+file, use the ~org-latex-listings-options~,
+~org-latex-engraved-options~, and ~org-latex-minted-options~
+variables.
*** Example blocks in LaTeX export
:PROPERTIES:
@@ -16289,6 +16293,9 @@ **** LaTeX specific properties
| ~:latex-default-table-environment~ | ~org-latex-default-table-environment~ |
| ~:latex-default-table-mode~ | ~org-latex-default-table-mode~ |
| ~:latex-diary-timestamp-format~ | ~org-latex-diary-timestamp-format~ |
+| ~:latex-engraved-options~ | ~org-latex-engraved-options~ |
+| ~:latex-engraved-preamble~ | ~org-latex-engraved-preamble~ |
+| ~:latex-engraved-theme~ | ~org-latex-engraved-theme~ |
| ~:latex-footnote-defined-format~ | ~org-latex-footnote-defined-format~ |
| ~:latex-footnote-separator~ | ~org-latex-footnote-separator~ |
| ~:latex-format-drawer-function~ | ~org-latex-format-drawer-function~ |
@@ -22255,8 +22262,8 @@ * Footnotes
[fn:114] This works automatically for the HTML backend (it requires
version 1.34 of the =htmlize.el= package, which you need to install).
Fontified code chunks in LaTeX can be achieved using either the
-[[https://www.ctan.org/pkg/listings][listings]] package or the [[https://www.ctan.org/pkg/minted][minted]] package. Refer to
-~org-latex-src-block-backend~ for details.
+[[https://www.ctan.org/pkg/listings][listings]] LaTeX package, [[https://www.ctan.org/pkg/minted][minted]] LaTeX package, or by using
+[[https://elpa.gnu.org/packages/engrave-faces.html][engrave-faces]] . Refer to ~org-latex-src-block-backend~ for details.
[fn:115] Source code in code blocks may also be evaluated either
interactively or on export. See [[*Working with Source Code]] for more
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index ebb3cd649..582816534 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -180,6 +180,15 @@ commands.
=:noweb-prefix= can be set to =no= to prevent the prefix characters
from being repeated when expanding a multiline noweb reference.
+*** New LaTeX source block backend using =engraved-faces-latex=
+
+When ~org-latex-src-block-backend~ is set to ~engraved~,
+=engrave-faces-latex= from [[http://elpa.gnu.org/packages/engrave-faces.html][engrave-faces]] is used to transcode source
+blocks to LaTeX. This requires the =fvextra=, =float=, and (by
+default, but not necessarily) =tcolorbox= LaTeX packages be
+installed. It uses Emacs' font-lock information, and so tends to
+produce results superior to Minted or Listings.
+
** New functions and changes in function arguments
*** New function ~org-element-cache-map~ for quick mapping across Org elements
--
2.35.3
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #14: 0013-ox-latex-Prefix-lst-to-source-block-labels.patch --]
[-- Type: text/x-patch, Size: 848 bytes --]
From c3851e657611424af10859a2236c0b4e183cc1d7 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Wed, 11 May 2022 21:50:36 +0800
Subject: [PATCH 13/13] ox-latex: Prefix lst: to source block labels
* lisp/ox-latex.el (org-latex--label): Use the "lst:" prefix when
generating source block labels ("lst" contracted from "listing").
---
lisp/ox-latex.el | 1 +
1 file changed, 1 insertion(+)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 29543f8e9..998f2238d 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1554,6 +1554,7 @@ (defun org-latex--label (datum info &optional force full)
(`paragraph
(and (org-element-property :caption datum)
"fig:"))
+ (`src-block "lst:")
(_ nil))
(org-export-get-reference datum info))))))
(cond ((not full) label)
--
2.35.3
^ permalink raw reply related [relevance 5%]
* Re: [PATCH] (v3) New LaTeX code export option: engraved
@ 2022-05-12 16:44 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2022-05-12 16:44 UTC (permalink / raw)
To: Daniel Fleischer; +Cc: Ihor Radchenko, emacs-orgmode, Nicolas Goaziou
[-- Attachment #1: Type: text/plain, Size: 635 bytes --]
Hi Daniel,
> Looks good. I had to update `engrave-faces’ to 0.3 because of new
> variables/functions and it works now. It does what it says on the box so
> let’s move forward introducing these changes and be open to feedback.
>
> Thank you very much for introducing this feature!
That’s great to hear! I’ve just tagged and pushed 0.3.1 of engrave-faces, so
HEAD is no longer required, along with my patch of ox-latex commits.
Thanks for helping with the process of getting this in, this has been over a
year in the works and so it feels fantastic to have this feature in-tree at last!
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* [PATCH] Support #+include-ing URLs
@ 2022-06-05 14:32 43% Timothy
2022-06-12 9:52 93% ` Timothy
0 siblings, 2 replies; 74+ results
From: Timothy @ 2022-06-05 14:32 UTC (permalink / raw)
To: Org Mode
[-- Attachment #1.1: Type: text/plain, Size: 218 bytes --]
Hi All,
This is just a little patchset to treat `#+include: URL' the same way as
`#+setupfile: URL'. All the usual `#+include:' bells and whistles (`::*Heading',
`:lines', etc.) work as normal.
All the best,
Timothy
[-- Attachment #1.2: Type: text/html, Size: 3326 bytes --]
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0002-org-lint-don-t-complain-about-include-URL.patch --]
[-- Type: text/x-patch, Size: 4172 bytes --]
From df0b382e43cf44860247fafd14bd2932fe3ed026 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 5 Jun 2022 22:28:39 +0800
Subject: [PATCH 2/2] org-lint: don't complain about #+include URL
* lisp/org-lint.el (org-lint-wrong-include-link-parameter): When the
included file is a URL, skip the usual file checks.
---
lisp/org-lint.el | 67 ++++++++++++++++++++++++------------------------
1 file changed, 34 insertions(+), 33 deletions(-)
diff --git a/lisp/org-lint.el b/lisp/org-lint.el
index cce6fddbd..1e0dba4a0 100644
--- a/lisp/org-lint.el
+++ b/lisp/org-lint.el
@@ -649,39 +649,40 @@ (defun org-lint-wrong-include-link-parameter (ast)
(org-element-map ast 'keyword
(lambda (k)
(when (equal (org-element-property :key k) "INCLUDE")
- (let* ((value (org-element-property :value k))
- (path
- (and (string-match "^\\(\".+\"\\|\\S-+\\)[ \t]*" value)
- (save-match-data
- (org-strip-quotes (match-string 1 value))))))
- (if (not path)
- (list (org-element-property :post-affiliated k)
- "Missing location argument in INCLUDE keyword")
- (let* ((file (org-string-nw-p
- (if (string-match "::\\(.*\\)\\'" path)
- (substring path 0 (match-beginning 0))
- path)))
- (search (and (not (equal file path))
- (org-string-nw-p (match-string 1 path)))))
- (if (and file
- (not (file-remote-p file))
- (not (file-exists-p file)))
- (list (org-element-property :post-affiliated k)
- "Non-existent file argument in INCLUDE keyword")
- (let* ((visiting (if file (find-buffer-visiting file)
- (current-buffer)))
- (buffer (or visiting (find-file-noselect file)))
- (org-link-search-must-match-exact-headline t))
- (unwind-protect
- (with-current-buffer buffer
- (when (and search
- (not (ignore-errors
- (org-link-search search nil t))))
- (list (org-element-property :post-affiliated k)
- (format
- "Invalid search part \"%s\" in INCLUDE keyword"
- search))))
- (unless visiting (kill-buffer buffer))))))))))))
+ (let* ((value (org-element-property :value k))
+ (path
+ (and (string-match "^\\(\".+\"\\|\\S-+\\)[ \t]*" value)
+ (save-match-data
+ (org-strip-quotes (match-string 1 value))))))
+ (if (not path)
+ (list (org-element-property :post-affiliated k)
+ "Missing location argument in INCLUDE keyword")
+ (let* ((file (org-string-nw-p
+ (if (string-match "::\\(.*\\)\\'" path)
+ (substring path 0 (match-beginning 0))
+ path)))
+ (search (and (not (equal file path))
+ (org-string-nw-p (match-string 1 path)))))
+ (unless (org-url-p file)
+ (if (and file
+ (not (file-remote-p file))
+ (not (file-exists-p file)))
+ (list (org-element-property :post-affiliated k)
+ "Non-existent file argument in INCLUDE keyword")
+ (let* ((visiting (if file (find-buffer-visiting file)
+ (current-buffer)))
+ (buffer (or visiting (find-file-noselect file)))
+ (org-link-search-must-match-exact-headline t))
+ (unwind-protect
+ (with-current-buffer buffer
+ (when (and search
+ (not (ignore-errors
+ (org-link-search search nil t))))
+ (list (org-element-property :post-affiliated k)
+ (format
+ "Invalid search part \"%s\" in INCLUDE keyword"
+ search))))
+ (unless visiting (kill-buffer buffer)))))))))))))
(defun org-lint-obsolete-include-markup (ast)
(let ((regexp (format "\\`\\(?:\".+\"\\|\\S-+\\)[ \t]+%s"
--
2.36.1
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0001-ox-Support-include-ing-URLs.patch --]
[-- Type: text/x-patch, Size: 4174 bytes --]
From 2e2189c451c8e8bbd8461a626fe63e5e97dd4a07 Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 5 Jun 2022 22:25:22 +0800
Subject: [PATCH 1/2] ox: Support #+include-ing URLs
* lisp/ox.el (org-export--prepare-file-contents,
org-export--inclusion-absolute-lines): Replace instances of
`(insert-file-contents FILE)' with `(insert (org-file-contents FILE))',
as in `org--collect-keywords-1'.
(org-export-expand-include-keyword): Tweak to accept a URL as FILE, and
not perform the standard "file exists and is readable" check.
* etc/ORG-NEWS: Mention this change in behaviour.
---
etc/ORG-NEWS | 3 +++
lisp/ox.el | 22 +++++++++++++---------
2 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 35af94f92..397cb668c 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -219,6 +219,9 @@ blocks to LaTeX. This requires the =fvextra=, =float=, and (by
default, but not necessarily) =tcolorbox= LaTeX packages be
installed. It uses Emacs' font-lock information, and so tends to
produce results superior to Minted or Listings.
+*** Support for =#+include=-ing URLs
+
+=#+include: FILE= will now accept URLs as the file.
** New functions and changes in function arguments
diff --git a/lisp/ox.el b/lisp/ox.el
index 9a8e63046..fc0ee671f 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -3229,15 +3229,18 @@ (defun org-export-expand-include-keyword (&optional included dir footnotes)
value)
(prog1
(save-match-data
- (let ((matched (match-string 1 value)))
+ (let ((matched (match-string 1 value))
+ stripped)
(when (string-match "\\(::\\(.*?\\)\\)\"?\\'"
matched)
(setq location (match-string 2 matched))
(setq matched
(replace-match "" nil nil matched 1)))
- (expand-file-name (org-strip-quotes matched)
- dir)))
- (setq value (replace-match "" nil nil value)))))
+ (setq stripped (org-strip-quotes matched))
+ (if (org-url-p stripped)
+ stripped
+ (expand-file-name stripped dir))))
+ (setq value (replace-match "" nil nil value)))))
(only-contents
(and (string-match ":only-contents *\\([^: \r\t\n]\\S-*\\)?"
value)
@@ -3273,7 +3276,7 @@ (defun org-export-expand-include-keyword (&optional included dir footnotes)
(delete-region (point) (line-beginning-position 2))
(cond
((not file) nil)
- ((not (file-readable-p file))
+ ((and (not (org-url-p file)) (not (file-readable-p file)))
(error "Cannot include file %s" file))
;; Check if files has already been parsed. Look after
;; inclusion lines too, as different parts of the same
@@ -3319,8 +3322,9 @@ (defun org-export-expand-include-keyword (&optional included dir footnotes)
includer-file)))
(org-export-expand-include-keyword
(cons (list file lines) included)
- (file-name-directory file)
- footnotes)
+ (unless (org-url-p file)
+ (file-name-directory file))
+ footnotes)
(buffer-string)))))
;; Expand footnotes after all files have been
;; included. Footnotes are stored at end of buffer.
@@ -3343,7 +3347,7 @@ (defun org-export--inclusion-absolute-lines (file location only-contents lines)
Return a string of lines to be included in the format expected by
`org-export--prepare-file-contents'."
(with-temp-buffer
- (insert-file-contents file)
+ (insert (org-file-contents file))
(unless (eq major-mode 'org-mode)
(let ((org-inhibit-startup t)) (org-mode)))
(condition-case err
@@ -3448,7 +3452,7 @@ (defun org-export--prepare-file-contents
Optional argument INCLUDER is the file name where the inclusion
is to happen."
(with-temp-buffer
- (insert-file-contents file)
+ (insert (org-file-contents file))
(when lines
(let* ((lines (split-string lines "-"))
(lbeg (string-to-number (car lines)))
--
2.36.1
^ permalink raw reply related [relevance 43%]
* Re: [PATCH] Support #+include-ing URLs
@ 2022-06-07 11:27 91% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2022-06-07 11:27 UTC (permalink / raw)
To: Fraga, Eric; +Cc: Max Nikulin, emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 682 bytes --]
Hi Eric,
>> Is it possible to disable fetching remote files by some setting?
>
> I would not want automatic retrieval of URLs without confirmation.
This already occurs with #+setupfile.
I think this is a good point to raise, but a slightly separate one as it
concerns the pre-existing behaviour of org-file-contents.
A new patch set introducing a defcustom with three values (t, prompt, nil) and
accompanying modification to org-file-contents (it’s the only use of
`url-retrieve-synchronously' other than org-feed I see) would probably be a decent
idea. If nobody makes such a patch in the next week or so I’ll take a punt at
this.
All the best,
Timothy
^ permalink raw reply [relevance 91%]
* Re: [PATCH] Support #+include-ing URLs
2022-06-05 14:32 43% [PATCH] Support #+include-ing URLs Timothy
@ 2022-06-12 9:52 93% ` Timothy
1 sibling, 0 replies; 74+ results
From: Timothy @ 2022-06-12 9:52 UTC (permalink / raw)
To: Org Mode
Hi All,
> This is just a little patchset to treat #+include: URL the same way as
> #+setupfile: URL. All the usual #+include: bells and whistles
> (::*Heading, :lines, etc.) work as normal.
Since it's been a week and nobody has raised any objections to this
functionality (though it has provoked a proposed change to
org-file-contents), I'm going to tentatively merge this and prepare a
patch for org-file-contents to add download settings.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* [PATCH] New remote resource download policy
@ 2022-06-12 14:43 37% Timothy
0 siblings, 2 replies; 74+ results
From: Timothy @ 2022-06-12 14:43 UTC (permalink / raw)
To: emacs-orgmode
[-- Attachment #1.1: Type: text/plain, Size: 492 bytes --]
Hi All,
As was raised in the `#+include: URL' thread
(<https://list.orgmode.org/877d5sd7yu.fsf@gmail.com>), currently Org will
automatically download files without confirmation in various circumstances.
This patch introduces two variables to control Org’s attitude towards
downloading files, and hooks them into the relevant parts of the codebase.
When prompting for downloading, this uses an approach borrowed from file local
variable confirmation.
All the best,
Timothy
[-- Attachment #1.2: Type: text/html, Size: 3186 bytes --]
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-Add-setting-for-remote-file-download-policy.patch --]
[-- Type: text/x-patch, Size: 8990 bytes --]
From 4f3437a2386e2ffdf37c99d476fa5ea3481b8d3c Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Sun, 12 Jun 2022 22:37:42 +0800
Subject: [PATCH] org: Add setting for remote file download policy
* lisp/org.el (org-download-remote-resources,
org-safe-remote-resources): Two new customisations to configure the
policy for downloading remote resources.
(org--should-fetch-remote-resource-p, org--safe-remote-resource-p,
org--confirm-resource-safe, org-download-remote-resources): Introduce
the new function `org--should-fetch-remote-resource-p' for internal use
determining whether a remote resource should be downloaded according to
the download policy. This function makes use of two helper functions,
`org--safe-remote-resource-p' and `org--confirm-resource-safe'.
(org-file-contents): Apply `org--safe-remote-resource-p' to file
downloading.
* lisp/org-persist.el (org-persist-write): Apply
`org--safe-remote-resource-p' to url downloading.
* lisp/org-attach.el (org-attach-attach): Apply
`org--safe-remote-resource-p' to url downloading.
---
lisp/org-attach.el | 6 ++-
lisp/org-persist.el | 5 +-
lisp/org.el | 115 ++++++++++++++++++++++++++++++++++++++------
3 files changed, 109 insertions(+), 17 deletions(-)
diff --git a/lisp/org-attach.el b/lisp/org-attach.el
index 5ee2b84b2..6f21ad716 100644
--- a/lisp/org-attach.el
+++ b/lisp/org-attach.el
@@ -525,7 +525,11 @@ (defun org-attach-attach (file &optional visit-dir method)
((eq method 'cp) (copy-file file attach-file))
((eq method 'ln) (add-name-to-file file attach-file))
((eq method 'lns) (make-symbolic-link file attach-file))
- ((eq method 'url) (url-copy-file file attach-file)))
+ ((eq method 'url)
+ (if (or (not noninteractive) (org--should-fetch-remote-resource-p file))
+ (url-copy-file file attach-file)
+ (error "The remote resource %S is considered unsafe, and will not be downloaded."
+ file))))
(run-hook-with-args 'org-attach-after-change-hook attach-dir)
(org-attach-tag)
(cond ((eq org-attach-store-link-p 'attached)
diff --git a/lisp/org-persist.el b/lisp/org-persist.el
index 068f58cec..f49abe8cd 100644
--- a/lisp/org-persist.el
+++ b/lisp/org-persist.el
@@ -655,7 +655,10 @@ (defun org-persist-write:url (c collection)
(format "%s-%s.%s" persist-file (md5 path) ext))))
(unless (file-exists-p (file-name-directory file-copy))
(make-directory (file-name-directory file-copy) t))
- (url-copy-file path file-copy 'overwrite)
+ (if (org--should-fetch-remote-resource-p path)
+ (url-copy-file path file-copy 'overwrite)
+ (error "The remote resource %S is considered unsafe, and will not be downloaded."
+ path))
(format "%s-%s.%s" persist-file (md5 path) ext)))))
(defun org-persist-write:index (container _)
diff --git a/lisp/org.el b/lisp/org.el
index 8e7aadde5..3a8acaa8f 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -1352,6 +1352,32 @@ (defcustom org-file-apps
(string :tag "Command")
(function :tag "Function")))))
+(defcustom org-download-remote-resources 'prompt
+ "The policy applied to requests to obtain remote resources.
+
+This affects keywords like #+setupfile and #+incude on export,
+`org-persist-write:url',and `org-attach-attach' in
+non-interactive Emacs sessions.
+
+This recognises four possible values:
+- t, remote resources should always be downloaded.
+- prompt, you will be prompted to download resources nt considered safe.
+- safe, only resources considered safe will be downloaded.
+- nil, never download remote resources.
+
+A resource is considered safe if it matches one of the patterns
+in `org-safe-remote-resources'."
+ :group 'org
+ :type '(choice (const :tag "Always download remote resources" t)
+ (const :tag "Prompt before downloading an unsafe resource" prompt)
+ (const :tag "Only download resources considered safe" safe)
+ (const :tag "Never download any resources" nil)))
+
+(defcustom org-safe-remote-resources nil
+ "A list of regexp patterns matching safe URIs."
+ :group 'org
+ :type '(list regexp))
+
(defcustom org-open-non-existing-files nil
"Non-nil means `org-open-file' opens non-existing files.
@@ -4466,21 +4492,25 @@ (defun org-file-contents (file &optional noerror nocache)
(cond
(cache)
(is-url
- (with-current-buffer (url-retrieve-synchronously file)
- (goto-char (point-min))
- ;; Move point to after the url-retrieve header.
- (search-forward "\n\n" nil :move)
- ;; Search for the success code only in the url-retrieve header.
- (if (save-excursion
- (re-search-backward "HTTP.*\\s-+200\\s-OK" nil :noerror))
- ;; Update the cache `org--file-cache' and return contents.
- (puthash file
- (buffer-substring-no-properties (point) (point-max))
- org--file-cache)
- (funcall (if noerror #'message #'user-error)
- "Unable to fetch file from %S"
- file)
- nil)))
+ (if (org--should-fetch-remote-resource-p file)
+ (with-current-buffer (url-retrieve-synchronously file)
+ (goto-char (point-min))
+ ;; Move point to after the url-retrieve header.
+ (search-forward "\n\n" nil :move)
+ ;; Search for the success code only in the url-retrieve header.
+ (if (save-excursion
+ (re-search-backward "HTTP.*\\s-+200\\s-OK" nil :noerror))
+ ;; Update the cache `org--file-cache' and return contents.
+ (puthash file
+ (buffer-substring-no-properties (point) (point-max))
+ org--file-cache)
+ (funcall (if noerror #'message #'user-error)
+ "Unable to fetch file from %S"
+ file)
+ nil))
+ (funcall (if noerror #'message #'user-error)
+ "The remote resource %S is considered unsafe, and will not be downloaded."
+ file)))
(t
(with-temp-buffer
(condition-case nil
@@ -4493,6 +4523,61 @@ (defun org-file-contents (file &optional noerror nocache)
file)
nil)))))))
+(defun org--should-fetch-remote-resource-p (uri)
+ "Return non-nil if the URI should be fetched."
+ (or (eq org-download-remote-resources t)
+ (org--safe-remote-resource-p uri)
+ (and (eq org-download-remote-resources 'prompt)
+ (org--confirm-resource-safe uri))))
+
+(defun org--safe-remote-resource-p (uri)
+ "Return non-nil if URI is considered safe.
+This checks every pattern in `org-safe-remote-resources', and
+returns non-nil if any of them match."
+ (let (match-p (uri-patterns org-safe-remote-resources))
+ (while (and (not match-p) uri-patterns)
+ (setq match-p (string-match-p (car uri-patterns) uri)
+ uri-patterns (cdr uri-patterns)))
+ match-p))
+
+(defun org--confirm-resource-safe (uri)
+ "Ask the user if URI should be considered safe, returning non-nil if so."
+ (unless noninteractive
+ (let ((buf (get-buffer-create "*Org Remote Resource*")))
+ ;; Set up the contents of the *Local Variables* buffer.
+ (with-current-buffer buf
+ (erase-buffer)
+ (insert "An org-mode document would like to download "
+ (propertize uri 'face '(:inherit org-link :weight normal))
+ ", which is not considered safe.\n\n"
+ "Do you want to download this? You can type\n "
+ (propertize "!" 'face 'success)
+ " to download this resource, and permanantly mark it as safe.\n "
+ (propertize "y" 'face 'warning)
+ " to download this resource, just this once.\n "
+ (propertize "n" 'face 'error)
+ " to skip this resource.\n")
+ (setq-local cursor-type nil)
+ (set-buffer-modified-p nil)
+ (goto-char (point-min)))
+ ;; Display the buffer and read a choice.
+ (save-window-excursion
+ (pop-to-buffer buf)
+ (let* ((exit-chars '(?y ?n ?! ?\s))
+ (prompt (format "Please type y, n, or !%s: "
+ (if (< (line-number-at-pos (point-max))
+ (window-body-height))
+ ""
+ ", or C-v/M-v to scroll")))
+ char)
+ (setq char (read-char-choice prompt exit-chars))
+ (when (= char ?!)
+ (customize-push-and-save
+ 'org-safe-remote-resources
+ (regexp-quote uri)))
+ (prog1 (memq char '(?! ?\s ?y))
+ (quit-window t)))))))
+
(defun org-extract-log-state-settings (x)
"Extract the log state setting from a TODO keyword string.
This will extract info from a string like \"WAIT(w@/!)\"."
--
2.36.1
^ permalink raw reply related [relevance 37%]
* Re: [PATCH] New remote resource download policy
@ 2022-06-22 9:58 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2022-06-22 9:58 UTC (permalink / raw)
To: Robert Pluim; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 719 bytes --]
Hi Robert,
> Timothy> +(defcustom org-download-remote-resources ’prompt
>
> ’prompted when downloading unsafe resources’
>
> maybe?
>
> Does this need a ’prompt-always option? I guess thatʼs what you get
> with the default value of `org-safe-remote-resources’, but maybe it
> makes sense to be explicit?
That isn’t the behaviour. `prompt' doesn’t /always/ prompt, just when the resource
in question is not considered safe according to `org--safe-remote-resource-p'.
So, `prompt-always' would be something else, but one can easily make prompt behave
that way by setting `org-safe-remote-resources' to nil, so I don’t think it
warrants an extra option.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] New remote resource download policy
@ 2022-06-22 10:01 85% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2022-06-22 10:01 UTC (permalink / raw)
To: Max Nikulin; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 3028 bytes --]
Hi Max,
>> — a/lisp/org-attach.el
>> +++ b/lisp/org-attach.el
>> @@ -525,7 +525,11 @@ (defun org-attach-attach (file &optional visit-dir method)
>> [snip]
>> + (if (or (not noninteractive) (org–should-fetch-remote-resource-p file))
>
> I am confused by (not noninteractive). Does it mean that interactive call is
> enough to bypass protection? It may have sense it at this step there is no
> ambiguity what resources is fetched. On the other hand I am unsure concerning a
> case when `org-attach-attach’ is a part of a larger command.
The idea here is that when this is done interactively the user will be aware of
the URL this is being applied to, and so it isn’t a risk. Let me know if this
assumption doesn’t hold.
>> +(defcustom org-download-remote-resources ’prompt
>
> The name sounds like some function.
Mmm. I could add `-policy' to that variable name perhaps.
>> +(defun org–confirm-resource-safe (uri)
>> + “Ask the user if URI should be considered safe, returning non-nil if so.”
>> + (unless noninteractive
>> + (let ((buf (get-buffer-create “*Org Remote Resource*”)))
>
> I see your intention to add something fancy to the dialog. May `org-mks’ be
> reused instead to avoid proliferation variants of rather similar UI code?
Well, the thing here is that I’m explicitly trying to mimic the
file-local-variable dialog, and since a general form isn’t exposed by Emacs, a
little bit of proliferation seems like the best option to me.
>> + ;; Set up the contents of the *Local Variables* buffer.
>
> I am in doubts concerning “once”. I tried “y” in a file having to “#+include:”
> of the same file. I did not get question for second include. I did not get
> prompt for this file anymore at all, even during next export. I modified the
> remote file, but stale content appeared during export. So the file was really
> downloaded once, but it is hardly in agreement with my expectations. Behavior is
> unrelated to this patch, concerning wording I am not sure, but I have no a
> better variant.
Ok, that is not the intended behaviour. I’ll see if I can work out what’s going
on here. Oh, and I should change that comment to “Set up the contents of the *Org
Remote Resource* buffer.”.
>> + (propertize “n” ’face ’error)
>> + “ to skip this resource.”)
>
> From “skip” I do not expect aborting of export.
Hmm, the “skip” action isn’t determined by the “is it safe” functions, but some
wording that doesn’t sound completely safe at least would be good.
> I have an idea but unsure if it should be implemented. Consider
> `org-remote-resources-policy’ custom variable that is a list of pairs
> (url-regexp . policy) for fine grain tuning instead of 2 variables. The price is
> more complicated structure, so higher chance of user error.
My initial reaction is that this feels like overkill.
All the best,
Timothy
^ permalink raw reply [relevance 85%]
* Re: [STYLE] :version tags in defcustom definitions (was: [PATCH] Improve look of agenda on graphical displays)
@ 2022-06-29 14:07 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2022-06-29 14:07 UTC (permalink / raw)
To: Stefan Kangas; +Cc: Ihor Radchenko, Daniel Mendler, emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 451 bytes --]
Hi Ihor and Stefan,
>> Sure. Just trying to clarify my confusion. The inconsistency with some
>> defcustoms using :version and some not is bugging me.
>
> Agreed. It would be better to be consistent with this.
Given that org-mode is distributed separately to Emacs, and I get the impression
having a newer org-mode version that Emacs version is not uncommon, I think it
would make sense to have /just/ org-mode version tags.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Fix caption format for custom latex src block
@ 2022-06-29 15:20 93% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2022-06-29 15:20 UTC (permalink / raw)
To: Ihor Radchenko; +Cc: Matt Huszagh, Emacs-Orgmode
[-- Attachment #1: Type: text/plain, Size: 310 bytes --]
Hi Ihor,
> Timothy, looking at the new implementation of org-latex-src-block, I do
> not see custom-env being passed as an argument of
> org-latex-src-block–custom. This is likely a bug.
Ah yes, this was an oversight. Corrected in 30953bd7b701c870152cd60f52f2d484970caeb9.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [PATCH] Fix caption format for custom latex src block
@ 2022-06-29 15:26 89% ` Timothy
1 sibling, 0 replies; 74+ results
From: Timothy @ 2022-06-29 15:26 UTC (permalink / raw)
To: Matt Huszagh; +Cc: Ihor Radchenko, emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 1123 bytes --]
Hi Matt,
As mentioned by Ihor, you’ll want to take a look at the curent version of
ox-latex, specifically `org-latex-src-block--custom' which now contains `(?c .
,caption)'. You may want to consider modifying `org-latex--caption/label-string' to
accept the form `(element info &optional content-only)' or reimplementing just the
bits needed for this purpose — which ever works out nicer. Looking at
`org-latex--caption/label-string' a substring approach looks quite fragile, though
you might be able to get away with an application of `replace-regexp-in-string',
though I’d personally consider this a bit of a last resort.
Matt Huszagh <huszaghmatt@gmail.com> writes:
> Ihor Radchenko <yantar92@gmail.com> writes:
>
>> I think that it will be better if you use
>> org-latex–caption/label-string instead. It will take care about short
>> captions as well.
>
> Changing this to (?c . ,caption-str) yields for the original example
>
> {Perform inter-sample interpolation.
> }
>
> I could use substring to remove the leading { and trailing
> }. What do you think?
All the best,
Timothy
^ permalink raw reply [relevance 89%]
* Re: [PATCH] New remote resource download policy
@ 2022-06-29 15:27 93% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2022-06-29 15:27 UTC (permalink / raw)
To: Max Nikulin; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 1563 bytes --]
Hi Max,
Max Nikulin <manikulin@gmail.com> writes:
>>> I see your intention to add something fancy to the dialog. May `org-mks’ be
>>> reused instead to avoid proliferation variants of rather similar UI code?
>> Well, the thing here is that I’m explicitly trying to mimic the
>> file-local-variable dialog, and since a general form isn’t exposed by Emacs, a
>> little bit of proliferation seems like the best option to me.
>
> From my point of view the result is rather close (prompt should be adjusted):
>
> (let ((uri “<https://orgmode.org>”))
> (org-mks
> nil
> (format “An org-mode document would like to download %s, which is not
> considered safe.
>
> Do you want to download this?”
> (propertize uri ’face ’(:inherit org-link :weight normal)))
> nil ; prompt
> `((,(propertize “!” ’face ’success)
> “download this resource, and permanantly mark it as safe.”)
> (,(propertize “y” ’face ’warning)
> “to download this resource, just this once.”)
> (,(propertize “n” ’face ’error)
> “skip this resource.”))))
I just tried this snippet and it looked quite different to me, that said I have
tweaked `org-mks' a bit in my config… (I initially found org-capture quite
ugly).
I also really don’t like how `org-mks' so forcefully grabs all keyboard input. I
can see it being nice to jump back to the buffer and see where the resource is
being used, which isn’t really possible using `org-mks'.
All the best,
Timothy
^ permalink raw reply [relevance 93%]
* Re: [possible patch] Basic fontspec code for LuaLaTeX and XelaTeX (was "LaTeX export: when is it more useful...")
@ 2022-07-11 14:19 76% ` Timothy
0 siblings, 1 reply; 74+ results
From: Timothy @ 2022-07-11 14:19 UTC (permalink / raw)
To: Ihor Radchenko; +Cc: Juan Manuel Macías, orgmode
[-- Attachment #1: Type: text/plain, Size: 1786 bytes --]
Hi Ihor & co.,
> As I recall, Timothy has been working on simplifying preamble
> generation. If we do not put unnecessary packages into preamble,
> compilation will be significantly faster.
>
> If Timothy can come up with a patch some time soon, I’d prefer to have a
> more targeted preamble. Otherwise, the proposed approach is the way to
> go.
Yep, I’ve got something in my config currently that intercepts LaTeX preamble
creation and generates it only the fly from a list of detected features based on
the exported document and capability providers. I use this in my config to
automatically switch to LuaLaTeX when necessary and use pdfLaTeX the rest of the
time.
This should also be able to be able clean up some of the currently kludgy
preamble modifications like in oc-csl.el.
This has been on the back-burner for a while (I want to implement this is a way
that can be generalised across all output backends), but I’ll see if I can make
some progress and hopefully have a preliminary patch set in the next few weeks.
Lastly, there’s something extra I want to note. If we talk about including a
font customisation, I’d advocate for supporting font sets, not fonts. Once
again, this is something I’m a fan of from my config, and this could potentially
be supported across multiple export formats.
As an illustrative example, if I include this in one of my documents and create
a PDF:
┌────
│ #+options: fontset:biolinum
└────
Then I’ll get text with:
⁃ libertine roman as the serif font
⁃ biolinum as the serif, and default, font
⁃ source code pro as the mono font
⁃ newtxmath as the maths font
Similarly I can do `fontset:noto' and you can guess what that does.
All the best,
Timothy
^ permalink raw reply [relevance 76%]
* Re: fontsets (was: [possible patch] Basic fontspec code for LuaLaTeX and XelaTeX (was "LaTeX export: when is it more useful..."))
@ 2022-07-11 17:45 85% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2022-07-11 17:45 UTC (permalink / raw)
To: Juan Manuel Macías; +Cc: Maxim Nikulin, Ihor Radchenko, orgmode
[-- Attachment #1: Type: text/plain, Size: 1432 bytes --]
Hi Juan,
> When you talk about fontset, I understand that you mean lists of
> families with their options that you have previously defined, is that
> right?
Yep, so in my config’s implementation I have an alist of fontset names and
individual fonts. For something part of org-mode itself, we’d probably want to
add a format level to this, something like:
┌────
│ ((fontset-name .
│ ((serif .
│ ((pdflatex . "\\usepackage{myserif}")
│ (lualatex . "etc.")
│ (html . "and so on")))
│ (sans ...) ... ))
│ (another-fontset ...) ...)
└────
Actually, now that I think of it maybe it would be better to seperate out the
fontsets and fots, e.g.
┌────
│ ;; Fonts
│ ((myfonta . ((pdflatex . "etc.") (lualatex ...) (html ...) ...))
│ (myfontb ...)
│ ...)
│ ;; Fontsets
│ ((myfontset .
│ ((sans . myfonta)
│ (serif . myfontb)
│ (mono . myfontc)
│ ...))
│ ...)
└────
> In any case, I think it would also be nice if the user could add only
> one family for roman, sans, mono or math, if he/she prefers it that way.
> Something like:
>
> #+options: rmfont:Minion Pro
Sure. There’s another bit of functionality in my config which I think is worth
noting, you can add a -sans/-serif/-mono suffix to the fontset name to override
the default body text font.
All the best,
Timothy
^ permalink raw reply [relevance 85%]
* Re: [PATCH] New remote resource download policy
@ 2022-07-16 9:47 92% ` Timothy
0 siblings, 0 replies; 74+ results
From: Timothy @ 2022-07-16 9:47 UTC (permalink / raw)
To: Max Nikulin; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 1480 bytes --]
Hi Max,
I’ve just made a few more tweaks, tested it, and pushed this as 0583a0c. We may
want to tweak this further before the next Org release, but I think this is a
good enough starting point.
> An idea: a menu entry that displays location in the org file that caused the
> prompt. However it may require enough work to pass context data to the function
> rendering menu.
This could be added in future.
> I see you point concerning blocking modal prompt and I do not like it as well.
> Have you seen the following thread:
>
> Arthur Miller. Proposal: ’executable’ org-capture-templaes. Wed, 22 Jun 2022
> 14:13:51 +0200.
> <https://list.orgmode.org/AM9PR09MB49771CF015DAECBF3E5F955E96B29@AM9PR09MB4977.eurprd09.prod.outlook.com>
>
> If it were merged already, would it be enough for you to implement the dialog or
> the proposal lacks some features necessary for smooth user experience?
I have not read much of this thread, but from a brief glance — possibly.
> The reason why I asked about `org-mks’ is expectation of UI consistency withing
> Org. It might help users having customized `display-buffer-alist’
Here I was more thinking of UI consistency within Emacs.
> I still do not like code adding a menu close enough to existing one, but I will
> not object any more.
Since this is separate to the functionality, I’m going to push this for now, and
we can update the UI side of things later.
All the best,
Timothy
^ permalink raw reply [relevance 92%]
Results 201-274 of 274 prev (older) | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2021-03-31 15:00 [PATCH] Fontification for inline src blocks Timothy
2021-11-21 14:09 49% ` Timothy
2021-11-22 11:52 45% ` Timothy
2021-11-22 12:23 ` Ihor Radchenko
2021-11-22 13:43 44% ` Timothy
2021-11-22 14:35 ` Ihor Radchenko
2021-11-22 14:37 93% ` Timothy
2021-11-23 13:30 ` Ihor Radchenko
2021-11-29 19:21 49% ` Timothy
2021-11-30 11:44 93% ` Timothy
2021-11-30 12:45 ` Sébastien Miquel
2021-11-30 12:46 93% ` Timothy
2021-12-02 12:53 ` Eric S Fraga
2021-12-02 13:57 91% ` Faces for inline src blocks (was: [PATCH] Fontification for inline src blocks) Timothy
2021-08-05 12:12 [PATCH] Rename headline to heading André A. Gomes
2021-09-26 9:27 ` Bastien
2021-09-30 12:21 ` André A. Gomes
2021-10-01 8:38 ` Bastien
2021-10-15 8:52 ` André A. Gomes
2021-10-15 9:56 93% ` Timothy
2021-09-02 4:47 Bug: org-agenda-sort-notime-is-late is not correctly handled by timestamp comparison [9.4.6 (9.4.6-12-gdcc3a8-elpaplus @ /Users/charlestam/.emacs.d/elpa/org-plus-contrib-20210830/)] Charles Tam
2021-12-30 9:20 76% ` [PATCH] " Timothy
2021-09-29 13:33 [PATCH] Prevent displayed images from being re-scaled Timothy
2021-10-02 20:30 ` Timothy
2021-10-25 13:49 93% ` Timothy
2021-09-29 14:26 [PATCH] Treat :tangle-mode as an octal value not integer tomas
2021-09-29 17:18 ` Greg Minshall
2021-11-18 12:04 93% ` Timothy
2021-09-30 18:14 [PATCH] Accept more :tangle-mode specification forms Timothy
2021-10-05 14:45 93% ` Timothy
2021-10-05 15:54 ` unknown@email.com
2021-10-05 16:13 93% ` Timothy
2021-11-18 10:20 93% ` Timothy
2021-11-18 17:22 87% ` Timothy
2021-11-19 16:31 ` Tim Cross
2021-11-20 8:08 60% ` Timothy
2021-11-20 12:25 ` tomas
2021-11-20 14:50 93% ` Timothy
2021-11-21 4:08 ` Greg Minshall
2021-11-21 4:27 93% ` Timothy
2021-11-20 19:49 ` Tim Cross
2021-11-21 4:02 67% ` Timothy
2021-11-21 13:51 ` Tim Cross
2021-11-21 14:33 64% ` Timothy
2021-11-29 18:57 91% ` Timothy
2021-10-05 22:37 [PATCH] ob-R async evaluation Jeremie Juste
2021-11-18 10:36 93% ` Timothy
2021-10-13 22:14 [PATCH] org-manual.org: org-cite additions Bruce D'Arcus
2021-11-18 8:08 93% ` Timothy
2021-10-23 12:18 [PATCH] org.el (org-display-inline-image--width): Small fix Sébastien Miquel
2021-11-03 16:25 ` Nicolas Goaziou
2021-11-18 12:06 93% ` Timothy
2021-10-24 17:20 [patch] Fix link to Library of Babel Thomas S. Dye
2021-11-21 7:04 93% ` Dodgy Worg publishing? (was: [patch] Fix link to Library of Babel) Timothy
2021-10-28 15:24 [PATCH] org-src: Reset buffer-modified-p after fontifying Clément Pit-Claudel
2021-11-21 6:58 93% ` Timothy
[not found] <388850760.3644614.1634354740816.ref@mail.yahoo.com>
2021-10-16 3:25 ` Subject: [PATCH] Fix DISPLAY error on exporting org with plantuml to html Sun Lin
2021-11-18 7:38 92% ` Timothy
2021-11-18 7:47 ` Sun Lin
2021-11-30 1:41 ` Sun Lin
2021-11-30 4:18 90% ` Timothy
2021-11-21 18:41 [PATCH] Fix window width when line numbers present Matt Huszagh
2021-11-21 19:14 92% ` Timothy
2021-11-21 21:08 ` Nicolas Goaziou
2021-11-21 21:14 ` Matt Huszagh
2021-11-21 21:16 ` Bastien
2021-11-21 21:22 ` Matt Huszagh
2021-11-22 5:14 ` Bastien
2021-11-22 5:31 93% ` Timothy
2021-11-22 5:44 ` Bastien
2021-11-22 5:51 90% ` Supported Emacs version (was: [PATCH] Fix window width when line numbers present) Timothy
2021-11-22 3:16 93% ` [PATCH] Fix window width when line numbers present Timothy
2021-11-21 19:19 93% ` Timothy
2021-11-21 19:08 [PATCH] Fix regex for determining image width from attribute Matt Huszagh
2021-11-21 19:20 81% ` Timothy
2021-11-21 19:51 ` Matt Huszagh
2021-11-22 8:29 91% ` Timothy
2021-11-22 16:11 ` Matt Huszagh
2021-11-22 17:54 93% ` Timothy
2021-11-22 20:53 ` Matt Huszagh
2021-11-23 5:14 93% ` Timothy
2021-11-23 5:38 ` Matt Huszagh
2021-11-23 5:39 93% ` Timothy
2021-11-23 7:46 ` Matt Huszagh
2021-11-23 16:44 ` Max Nikulin
2021-11-24 1:57 ` Matt Huszagh
2021-11-24 14:48 ` Max Nikulin
2021-11-24 15:59 ` Matt Huszagh
2021-11-24 17:00 ` Max Nikulin
2021-11-25 16:43 ` Max Nikulin
2021-11-29 0:23 ` Matt Huszagh
2021-11-29 5:13 93% ` Timothy
2021-12-01 3:24 ` Matt Huszagh
2021-12-01 4:54 72% ` Timothy
2021-11-22 18:37 [PATCH] ob-shell-test, test-ob-shell and introduction Matt
2021-11-22 18:43 89% ` Timothy
2021-11-24 18:48 ` Matt
2021-12-02 9:39 93% ` Timothy
2021-11-24 16:11 [PATCH] Fix ob-plantuml over TRAMP Guillaume Buisson (gbuisson)
2021-12-02 9:43 93% ` Timothy
2021-11-28 15:59 [patch] fix ox-latex async export bug Rasmus
2021-11-28 19:52 ` Nicolas Goaziou
2021-11-28 22:54 ` Tim Cross
2021-11-29 5:15 93% ` Timothy
2021-12-30 4:05 [PATCH] Fix caption format for custom latex src block Matt Huszagh
2022-06-14 3:51 ` Ihor Radchenko
2022-06-28 4:07 ` Matt Huszagh
2022-06-29 2:24 ` Ihor Radchenko
2022-06-29 15:20 93% ` Timothy
2022-06-29 15:26 89% ` Timothy
2022-01-24 16:42 [PATCH] Add support for $…$ latex fragments followed by a dash Sébastien Miquel
2022-01-25 7:56 ` Eric S Fraga
2022-01-26 17:15 ` Rudolf Adamkovič
2022-01-27 8:28 ` Ihor Radchenko
2022-01-27 19:15 ` Tim Cross
2022-01-28 14:37 ` Max Nikulin
2022-01-28 16:37 90% ` Timothy
2022-01-29 16:00 53% [PATCH] Remote link localisation, i.e. exporting remote images Timothy
2022-01-31 11:40 93% ` Timothy
2022-05-04 15:59 12% [PATCH] New LaTeX code export option: engraved Timothy
2022-05-05 7:52 ` Daniel Fleischer
2022-05-05 16:09 93% ` Timothy
2022-05-06 2:35 ` Ihor Radchenko
2022-05-06 11:23 86% ` Timothy
2022-05-05 8:48 ` Ihor Radchenko
2022-05-05 15:17 12% ` Timothy
2022-05-05 16:13 25% ` Timothy
2022-05-07 5:16 ` Ihor Radchenko
2022-05-07 6:57 7% ` Timothy
2022-05-07 10:40 8% ` Timothy
2022-05-07 11:33 ` Daniel Fleischer
2022-05-08 14:30 6% ` [PATCH] (v2) " Timothy
2022-05-09 6:20 ` Ihor Radchenko
2022-05-09 12:57 81% ` Timothy
2022-05-11 16:05 5% ` [PATCH] (v3) " Timothy
2022-05-12 16:40 ` Daniel Fleischer
2022-05-12 16:44 93% ` Timothy
2022-05-09 19:19 ` [PATCH] " Sébastien Miquel
2022-05-10 1:13 93% ` Timothy
2022-05-10 16:10 93% ` Timothy
2022-06-05 14:32 43% [PATCH] Support #+include-ing URLs Timothy
2022-06-05 15:01 ` Max Nikulin
2022-06-07 10:09 ` Fraga, Eric
2022-06-07 11:27 91% ` Timothy
2022-06-12 9:52 93% ` Timothy
2022-06-12 14:43 37% [PATCH] New remote resource download policy Timothy
2022-06-14 9:40 ` Robert Pluim
2022-06-22 9:58 93% ` Timothy
2022-06-15 12:35 ` Max Nikulin
2022-06-22 10:01 85% ` Timothy
2022-06-22 16:55 ` Max Nikulin
2022-06-29 15:27 93% ` Timothy
2022-06-30 16:57 ` Max Nikulin
2022-07-16 9:47 92% ` Timothy
2022-06-27 12:08 [PATCH] Improve look of agenda on graphical displays Stefan Kangas
2022-06-29 9:19 ` Ihor Radchenko
2022-06-29 10:20 ` Stefan Kangas
2022-06-29 12:46 ` [STYLE] :version tags in defcustom definitions (was: [PATCH] Improve look of agenda on graphical displays) Ihor Radchenko
2022-06-29 12:55 ` Stefan Kangas
2022-06-29 14:07 93% ` Timothy
2022-07-08 12:17 LaTeX export: when is it more useful to use LuaTeX instead of pdfTeX? Juan Manuel Macías
2022-07-08 18:49 ` Thomas S. Dye
2022-07-09 2:23 ` Max Nikulin
2022-07-09 10:42 ` Juan Manuel Macías
2022-07-09 12:15 ` Max Nikulin
2022-07-09 14:58 ` Juan Manuel Macías
[not found] ` <b58ee3cc-c58c-b627-9cc5-51993020db2c@gmail.com>
2022-07-09 20:22 ` Juan Manuel Macías
2022-07-10 20:23 ` [possible patch] Basic fontspec code for LuaLaTeX and XelaTeX (was "LaTeX export: when is it more useful...") Juan Manuel Macías
2022-07-11 2:19 ` Ihor Radchenko
2022-07-11 14:19 76% ` Timothy
2022-07-11 15:00 ` Juan Manuel Macías
2022-07-11 17:45 85% ` fontsets (was: [possible patch] Basic fontspec code for LuaLaTeX and XelaTeX (was "LaTeX export: when is it more useful...")) Timothy
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).