ox-koma-letter.el 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. ;;; ox-koma-letter.el --- KOMA Scrlttr2 Back-End for Org Export Engine -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2007-2022 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou AT gmail DOT com>
  4. ;; Alan Schmitt <alan.schmitt AT polytechnique DOT org>
  5. ;; Viktor Rosenfeld <listuser36 AT gmail DOT com>
  6. ;; Rasmus Pank Roulund <emacs AT pank DOT eu>
  7. ;; Maintainer: Marco Wahl <marcowahlsoft@gmail.com>
  8. ;; Keywords: org, wp, tex
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;;
  22. ;; This library implements a KOMA Scrlttr2 back-end, derived from the
  23. ;; LaTeX one.
  24. ;;
  25. ;; Depending on the desired output format, three commands are provided
  26. ;; for export: `org-koma-letter-export-as-latex' (temporary buffer),
  27. ;; `org-koma-letter-export-to-latex' ("tex" file) and
  28. ;; `org-koma-letter-export-to-pdf' ("pdf" file).
  29. ;;
  30. ;; On top of buffer keywords supported by `latex' back-end (see
  31. ;; `org-latex-options-alist'), this back-end introduces the following
  32. ;; keywords:
  33. ;; - CLOSING: see `org-koma-letter-closing',
  34. ;; - FROM_ADDRESS: see `org-koma-letter-from-address',
  35. ;; - LCO: see `org-koma-letter-class-option-file',
  36. ;; - OPENING: see `org-koma-letter-opening',
  37. ;; - PHONE_NUMBER: see `org-koma-letter-phone-number',
  38. ;; - URL: see `org-koma-letter-url',
  39. ;; - FROM_LOGO: see `org-koma-letter-from-logo',
  40. ;; - SIGNATURE: see `org-koma-letter-signature',
  41. ;; - PLACE: see `org-koma-letter-place',
  42. ;; - LOCATION: see `org-koma-letter-location',
  43. ;; - TO_ADDRESS: If unspecified this is set to "\mbox{}".
  44. ;;
  45. ;; TO_ADDRESS, FROM_ADDRESS, LOCATION, CLOSING, and SIGNATURE can also
  46. ;; be specified using "special headings" with the special tags
  47. ;; specified in `org-koma-letter-special-tags-in-letter'. LaTeX line
  48. ;; breaks are not necessary for TO_ADDRESS, FROM_ADDRESS and LOCATION.
  49. ;; If both a headline and a keyword specify a to or from address the
  50. ;; value is determined in accordance with
  51. ;; `org-koma-letter-prefer-special-headings'.
  52. ;;
  53. ;; A number of OPTIONS settings can be set to change which contents is
  54. ;; exported.
  55. ;; - backaddress (see `org-koma-letter-use-backaddress')
  56. ;; - foldmarks (see `org-koma-letter-use-foldmarks')
  57. ;; - phone (see `org-koma-letter-use-phone')
  58. ;; - url (see `org-koma-letter-use-url')
  59. ;; - from-logo (see `org-koma-letter-use-from-logo')
  60. ;; - email (see `org-koma-letter-use-email')
  61. ;; - place (see `org-koma-letter-use-place')
  62. ;; - location (see `org-koma-letter-use-location')
  63. ;; - subject, a list of format options
  64. ;; (see `org-koma-letter-subject-format')
  65. ;; - after-closing-order, a list of the ordering of headings with
  66. ;; special tags after closing (see
  67. ;; `org-koma-letter-special-tags-after-closing')
  68. ;; - after-letter-order, as above, but after the end of the letter
  69. ;; (see `org-koma-letter-special-tags-after-letter').
  70. ;;
  71. ;; The following variables works differently from the main LaTeX class
  72. ;; - AUTHOR: Default to user-full-name but may be disabled.
  73. ;; (See also `org-koma-letter-author'.)
  74. ;; - EMAIL: Same as AUTHOR. (See also `org-koma-letter-email'.)
  75. ;;
  76. ;; FROM_LOGO uses LaTeX markup. FROM_LOGO provides the
  77. ;; "includegraphics" command to tell LaTeX where to find the logo.
  78. ;; This command needs to know the logo's directory and file name. The
  79. ;; directory can either be relative or absolute, just as you would
  80. ;; expect. LaTeX can use three file types for the logo: PDF, JPEG, or
  81. ;; PNG. The logo can either include or exclude its extension, which
  82. ;; might surprise you. When you exclude its extension, LaTeX will
  83. ;; search the directory for the "best" quality graphics format. For
  84. ;; example if it finds both logo.pdf and logo.png then it will
  85. ;; identify the PDF as "better", and include "logo.pdf". This can be
  86. ;; useful, for example, when you are mocking up a logo in the PNG
  87. ;; raster format and then switch over to the higher quality PDF vector
  88. ;; format. When you include the file extension then LaTeX will
  89. ;; include it without searching for higher quality file types.
  90. ;; Whatever file type you choose, it will probably require a few
  91. ;; design iterations to get the best looking logo size for your
  92. ;; letter. Finally, the directory and file name are specified
  93. ;; *without* quotes. Here are some examples with commentary, in the
  94. ;; location of your letter, with a logo named "logo", to get you
  95. ;; started:
  96. ;;
  97. ;; Logo in the same directory: \includegraphics{logo}
  98. ;; or a sub-directory: \includegraphics{logos/production/logo}
  99. ;;
  100. ;; Logos specified using absolute paths on Linux or Windows:
  101. ;;
  102. ;; \includegraphics{~/correspondence/logo}
  103. ;; \includegraphics{~/correspondence/logos/production/logo}
  104. ;; \includegraphics{c:/you/correspondence/logo}
  105. ;; \includegraphics{c:/you/correspondence/logos/production/logo}
  106. ;;
  107. ;; Logos in the same directory where the "better" quality PDF will
  108. ;; be chosen over the JPG:
  109. ;;
  110. ;; \includegraphics{logo.pdf}
  111. ;; \includegraphics{logo.png}
  112. ;;
  113. ;; Headlines are in general ignored. However, headlines with special
  114. ;; tags can be used for specified contents like postscript (ps),
  115. ;; carbon copy (cc), enclosures (encl) and code to be inserted after
  116. ;; \end{letter} (after_letter). Specials tags are defined in
  117. ;; `org-koma-letter-special-tags-after-closing' and
  118. ;; `org-koma-letter-special-tags-after-letter'. Currently members of
  119. ;; `org-koma-letter-special-tags-after-closing' used as macros and the
  120. ;; content of the headline is the argument.
  121. ;;
  122. ;; Headlines with to and from may also be used rather than the keyword
  123. ;; approach described above. If both a keyword and a headline with
  124. ;; information is present precedence is determined by
  125. ;; `org-koma-letter-prefer-special-headings'.
  126. ;;
  127. ;; You need an appropriate association in `org-latex-classes' in order
  128. ;; to use the KOMA Scrlttr2 class. By default, a sparse scrlttr2
  129. ;; class is provided: "default-koma-letter". You can also add you own
  130. ;; letter class. For instance:
  131. ;;
  132. ;; (add-to-list 'org-latex-classes
  133. ;; '("my-letter"
  134. ;; "\\documentclass\[%
  135. ;; DIV=14,
  136. ;; fontsize=12pt,
  137. ;; parskip=half,
  138. ;; subject=titled,
  139. ;; backaddress=false,
  140. ;; fromalign=left,
  141. ;; fromemail=true,
  142. ;; fromphone=true\]\{scrlttr2\}
  143. ;; \[DEFAULT-PACKAGES]
  144. ;; \[PACKAGES]
  145. ;; \[EXTRA]"))
  146. ;;
  147. ;; Then, in your Org document, be sure to require the proper class
  148. ;; with:
  149. ;;
  150. ;; #+LATEX_CLASS: my-letter
  151. ;;
  152. ;; Or by setting `org-koma-letter-default-class'.
  153. ;;
  154. ;; You may have to load (LaTeX) Babel as well, e.g., by adding
  155. ;; it to `org-latex-packages-alist',
  156. ;;
  157. ;; (add-to-list 'org-latex-packages-alist '("AUTO" "babel" nil))
  158. ;;; Code:
  159. (require 'org-macs)
  160. (org-assert-version)
  161. (require 'cl-lib)
  162. (require 'ox-latex)
  163. ;; Install a default letter class.
  164. (unless (assoc "default-koma-letter" org-latex-classes)
  165. (add-to-list 'org-latex-classes
  166. '("default-koma-letter" "\\documentclass[11pt]{scrlttr2}")))
  167. ;;; User-Configurable Variables
  168. (defgroup org-export-koma-letter nil
  169. "Options for exporting to KOMA scrlttr2 class in LaTeX export."
  170. :tag "Org Koma-Letter"
  171. :group 'org-export)
  172. (defcustom org-koma-letter-class-option-file "NF"
  173. "Letter Class Option File.
  174. This option can also be set with the LCO keyword."
  175. :type 'string)
  176. (defcustom org-koma-letter-author 'user-full-name
  177. "Sender's name.
  178. This variable defaults to calling the function `user-full-name'
  179. which just returns the current function `user-full-name'.
  180. Alternatively a string, nil or a function may be given.
  181. Functions must return a string.
  182. This option can also be set with the AUTHOR keyword."
  183. :type '(radio (function-item user-full-name)
  184. (string)
  185. (function)
  186. (const :tag "Do not export author" nil)))
  187. (defcustom org-koma-letter-email 'org-koma-letter-email
  188. "Sender's email address.
  189. This variable defaults to the value `org-koma-letter-email' which
  190. returns `user-mail-address'. Alternatively a string, nil or
  191. a function may be given. Functions must return a string.
  192. This option can also be set with the EMAIL keyword."
  193. :type '(radio (function-item org-koma-letter-email)
  194. (string)
  195. (function)
  196. (const :tag "Do not export email" nil)))
  197. (defcustom org-koma-letter-from-address ""
  198. "Sender's address, as a string.
  199. This option can also be set with one or more FROM_ADDRESS
  200. keywords."
  201. :type 'string)
  202. (defcustom org-koma-letter-phone-number ""
  203. "Sender's phone number, as a string.
  204. This option can also be set with the PHONE_NUMBER keyword."
  205. :type 'string)
  206. (defcustom org-koma-letter-url ""
  207. "Sender's URL, e. g., the URL of her homepage.
  208. This option can also be set with the URL keyword."
  209. :type 'string
  210. :safe #'stringp)
  211. (defcustom org-koma-letter-from-logo ""
  212. "Commands for inserting the sender's logo, e. g., \\includegraphics{logo}.
  213. This option can also be set with the FROM_LOGO keyword."
  214. :type 'string
  215. :safe #'stringp)
  216. (defcustom org-koma-letter-place ""
  217. "Place from which the letter is sent, as a string.
  218. This option can also be set with the PLACE keyword."
  219. :type 'string)
  220. (defcustom org-koma-letter-location ""
  221. "Sender's extension field, as a string.
  222. This option can also be set with the LOCATION keyword.
  223. Moreover, when:
  224. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  225. or there is no LOCATION keyword or the LOCATION keyword is
  226. empty;
  227. (2) the letter contains a headline with the special
  228. tag \"location\";
  229. then the location will be set as the content of the location
  230. special heading.
  231. The location field is typically printed right of the address
  232. field (See Figure 4.9. in the English manual of 2015-10-03)."
  233. :type 'string)
  234. (defcustom org-koma-letter-opening ""
  235. "Letter's opening, as a string.
  236. This option can also be set with the OPENING keyword. Moreover,
  237. when:
  238. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  239. or the CLOSING keyword is empty
  240. (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
  241. (3) the letter contains a headline without a special
  242. tag (e.g. \"to\" or \"ps\");
  243. then the opening will be implicitly set as the untagged headline title."
  244. :type 'string)
  245. (defcustom org-koma-letter-closing ""
  246. "Letter's closing, as a string.
  247. This option can also be set with the CLOSING keyword. Moreover,
  248. when:
  249. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  250. or the CLOSING keyword is empty;
  251. (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
  252. (3) the letter contains a headline with the special
  253. tag \"closing\";
  254. then the opening will be set as the title of the closing special
  255. heading title."
  256. :type 'string)
  257. (defcustom org-koma-letter-signature ""
  258. "Signature, as a string.
  259. This option can also be set with the SIGNATURE keyword.
  260. Moreover, when:
  261. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  262. or there is no CLOSING keyword or the CLOSING keyword is empty;
  263. (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
  264. (3) the letter contains a headline with the special
  265. tag \"closing\";
  266. then the signature will be set as the content of the
  267. closing special heading.
  268. Note if the content is empty the signature will not be set."
  269. :type 'string)
  270. (defcustom org-koma-letter-prefer-special-headings nil
  271. "Non-nil means prefer headlines over keywords for TO and FROM.
  272. This option can also be set with the OPTIONS keyword, e.g.:
  273. \"special-headings:t\"."
  274. :type 'boolean)
  275. (defcustom org-koma-letter-subject-format t
  276. "Non-nil means include the subject.
  277. Support formatting options.
  278. When t, insert a subject using default options. When nil, do not
  279. insert a subject at all. It can also be a list of symbols among
  280. the following ones:
  281. `afteropening' Subject after opening
  282. `beforeopening' Subject before opening
  283. `centered' Subject centered
  284. `left' Subject left-justified
  285. `right' Subject right-justified
  286. `titled' Add title/description to subject
  287. `underlined' Set subject underlined
  288. `untitled' Do not add title/description to subject
  289. Please refer to the KOMA-script manual (Table 4.16. in the
  290. English manual of 2012-07-22).
  291. This option can also be set with the OPTIONS keyword, e.g.:
  292. \"subject:(underlined centered)\"."
  293. :type
  294. '(choice
  295. (const :tag "No export" nil)
  296. (const :tag "Default options" t)
  297. (set :tag "Configure options"
  298. (const :tag "Subject after opening" afteropening)
  299. (const :tag "Subject before opening" beforeopening)
  300. (const :tag "Subject centered" centered)
  301. (const :tag "Subject left-justified" left)
  302. (const :tag "Subject right-justified" right)
  303. (const :tag "Add title or description to subject" underlined)
  304. (const :tag "Set subject underlined" titled)
  305. (const :tag "Do not add title or description to subject" untitled))))
  306. (defcustom org-koma-letter-use-backaddress nil
  307. "Non-nil prints return address in line above to address.
  308. This option can also be set with the OPTIONS keyword, e.g.:
  309. \"backaddress:t\"."
  310. :type 'boolean)
  311. (defcustom org-koma-letter-use-foldmarks t
  312. "Configure appearance of folding marks.
  313. When t, activate default folding marks. When nil, do not insert
  314. folding marks at all. It can also be a list of symbols among the
  315. following ones:
  316. `B' Activate upper horizontal mark on left paper edge
  317. `b' Deactivate upper horizontal mark on left paper edge
  318. `H' Activate all horizontal marks on left paper edge
  319. `h' Deactivate all horizontal marks on left paper edge
  320. `L' Activate left vertical mark on upper paper edge
  321. `l' Deactivate left vertical mark on upper paper edge
  322. `M' Activate middle horizontal mark on left paper edge
  323. `m' Deactivate middle horizontal mark on left paper edge
  324. `P' Activate punch or center mark on left paper edge
  325. `p' Deactivate punch or center mark on left paper edge
  326. `T' Activate lower horizontal mark on left paper edge
  327. `t' Deactivate lower horizontal mark on left paper edge
  328. `V' Activate all vertical marks on upper paper edge
  329. `v' Deactivate all vertical marks on upper paper edge
  330. This option can also be set with the OPTIONS keyword, e.g.:
  331. \"foldmarks:(b l m t)\"."
  332. :type '(choice
  333. (const :tag "Activate default folding marks" t)
  334. (const :tag "Deactivate folding marks" nil)
  335. (set
  336. :tag "Configure folding marks"
  337. (const :tag "Activate upper horizontal mark on left paper edge" B)
  338. (const :tag "Deactivate upper horizontal mark on left paper edge" b)
  339. (const :tag "Activate all horizontal marks on left paper edge" H)
  340. (const :tag "Deactivate all horizontal marks on left paper edge" h)
  341. (const :tag "Activate left vertical mark on upper paper edge" L)
  342. (const :tag "Deactivate left vertical mark on upper paper edge" l)
  343. (const :tag "Activate middle horizontal mark on left paper edge" M)
  344. (const :tag "Deactivate middle horizontal mark on left paper edge" m)
  345. (const :tag "Activate punch or center mark on left paper edge" P)
  346. (const :tag "Deactivate punch or center mark on left paper edge" p)
  347. (const :tag "Activate lower horizontal mark on left paper edge" T)
  348. (const :tag "Deactivate lower horizontal mark on left paper edge" t)
  349. (const :tag "Activate all vertical marks on upper paper edge" V)
  350. (const :tag "Deactivate all vertical marks on upper paper edge" v))))
  351. (defcustom org-koma-letter-use-phone nil
  352. "Non-nil prints sender's phone number.
  353. This option can also be set with the OPTIONS keyword, e.g.:
  354. \"phone:t\"."
  355. :type 'boolean)
  356. (defcustom org-koma-letter-use-url nil
  357. "Non-nil prints sender's URL.
  358. This option can also be set with the OPTIONS keyword, e.g.:
  359. \"url:t\"."
  360. :type 'boolean
  361. :safe #'booleanp)
  362. (defcustom org-koma-letter-use-from-logo nil
  363. "Non-nil prints sender's FROM_LOGO.
  364. This option can also be set with the OPTIONS keyword, e.g.:
  365. \"from-logo:t\"."
  366. :type 'boolean
  367. :safe #'booleanp)
  368. (defcustom org-koma-letter-use-email nil
  369. "Non-nil prints sender's email address.
  370. This option can also be set with the OPTIONS keyword, e.g.:
  371. \"email:t\"."
  372. :type 'boolean)
  373. (defcustom org-koma-letter-use-place t
  374. "Non-nil prints the letter's place next to the date.
  375. This option can also be set with the OPTIONS keyword, e.g.:
  376. \"place:nil\"."
  377. :type 'boolean)
  378. (defcustom org-koma-letter-default-class "default-koma-letter"
  379. "Default class for `org-koma-letter'.
  380. The value must be a member of `org-latex-classes'."
  381. :type 'string)
  382. (defcustom org-koma-letter-headline-is-opening-maybe t
  383. "Non-nil means a headline may be used as an opening and closing.
  384. See also `org-koma-letter-opening' and
  385. `org-koma-letter-closing'."
  386. :type 'boolean)
  387. (defcustom org-koma-letter-prefer-subject nil
  388. "Non-nil means title should be interpreted as subject if subject is missing.
  389. This option can also be set with the OPTIONS keyword,
  390. e.g. \"title-subject:t\"."
  391. :type 'boolean)
  392. (defconst org-koma-letter-special-tags-in-letter '(to from closing location)
  393. "Header tags related to the letter itself.")
  394. (defconst org-koma-letter-special-tags-after-closing '(after_closing ps encl cc)
  395. "Header tags to be inserted in the letter after closing.")
  396. (defconst org-koma-letter-special-tags-as-macro '(ps encl cc)
  397. "Header tags to be inserted as macros.")
  398. (defconst org-koma-letter-special-tags-after-letter '(after_letter)
  399. "Header tags to be inserted after the letter.")
  400. (defvar org-koma-letter-special-contents nil
  401. "Holds special content temporarily.")
  402. ;;; Define Back-End
  403. (org-export-define-derived-backend 'koma-letter 'latex
  404. :options-alist
  405. '((:latex-class "LATEX_CLASS" nil org-koma-letter-default-class t)
  406. (:lco "LCO" nil org-koma-letter-class-option-file)
  407. (:author "AUTHOR" nil (org-koma-letter--get-value org-koma-letter-author) parse)
  408. (:author-changed-in-buffer-p "AUTHOR" nil nil t)
  409. (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
  410. (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
  411. (:url "URL" nil org-koma-letter-url)
  412. (:from-logo "FROM_LOGO" nil org-koma-letter-from-logo)
  413. (:email "EMAIL" nil (org-koma-letter--get-value org-koma-letter-email) t)
  414. (:to-address "TO_ADDRESS" nil nil newline)
  415. (:place "PLACE" nil org-koma-letter-place)
  416. (:location "LOCATION" nil org-koma-letter-location)
  417. (:subject "SUBJECT" nil nil parse)
  418. (:opening "OPENING" nil org-koma-letter-opening parse)
  419. (:closing "CLOSING" nil org-koma-letter-closing parse)
  420. (:signature "SIGNATURE" nil org-koma-letter-signature newline)
  421. (:special-headings nil "special-headings" org-koma-letter-prefer-special-headings)
  422. (:special-tags-as-macro nil nil org-koma-letter-special-tags-as-macro)
  423. (:special-tags-in-letter nil nil org-koma-letter-special-tags-in-letter)
  424. (:special-tags-after-closing nil "after-closing-order"
  425. org-koma-letter-special-tags-after-closing)
  426. (:special-tags-after-letter nil "after-letter-order"
  427. org-koma-letter-special-tags-after-letter)
  428. (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
  429. (:with-email nil "email" org-koma-letter-use-email)
  430. (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
  431. (:with-phone nil "phone" org-koma-letter-use-phone)
  432. (:with-url nil "url" org-koma-letter-use-url)
  433. (:with-from-logo nil "from-logo" org-koma-letter-use-from-logo)
  434. (:with-place nil "place" org-koma-letter-use-place)
  435. (:with-subject nil "subject" org-koma-letter-subject-format)
  436. (:with-title-as-subject nil "title-subject" org-koma-letter-prefer-subject)
  437. (:with-headline-opening nil nil org-koma-letter-headline-is-opening-maybe)
  438. ;; Special properties non-nil when a setting happened in buffer.
  439. ;; They are used to prioritize in-buffer settings over "lco"
  440. ;; files. See `org-koma-letter-template'.
  441. (:inbuffer-author "AUTHOR" nil 'koma-letter:empty)
  442. (:inbuffer-from "FROM" nil 'koma-letter:empty)
  443. (:inbuffer-email "EMAIL" nil 'koma-letter:empty)
  444. (:inbuffer-phone-number "PHONE_NUMBER" nil 'koma-letter:empty)
  445. (:inbuffer-url "URL" nil 'koma-letter:empty)
  446. (:inbuffer-from-logo "FROM_LOGO" nil 'koma-letter:empty)
  447. (:inbuffer-place "PLACE" nil 'koma-letter:empty)
  448. (:inbuffer-location "LOCATION" nil 'koma-letter:empty)
  449. (:inbuffer-signature "SIGNATURE" nil 'koma-letter:empty)
  450. (:inbuffer-with-backaddress nil "backaddress" 'koma-letter:empty)
  451. (:inbuffer-with-email nil "email" 'koma-letter:empty)
  452. (:inbuffer-with-foldmarks nil "foldmarks" 'koma-letter:empty)
  453. (:inbuffer-with-phone nil "phone" 'koma-letter:empty)
  454. (:inbuffer-with-url nil "url" 'koma-letter:empty)
  455. (:inbuffer-with-from-logo nil "from-logo" 'koma-letter:empty)
  456. (:inbuffer-with-place nil "place" 'koma-letter:empty))
  457. :translate-alist '((export-block . org-koma-letter-export-block)
  458. (export-snippet . org-koma-letter-export-snippet)
  459. (headline . org-koma-letter-headline)
  460. (keyword . org-koma-letter-keyword)
  461. (template . org-koma-letter-template))
  462. :menu-entry
  463. '(?k "Export with KOMA Scrlttr2"
  464. ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
  465. (?l "As LaTeX file" org-koma-letter-export-to-latex)
  466. (?p "As PDF file" org-koma-letter-export-to-pdf)
  467. (?o "As PDF file and open"
  468. (lambda (a s v b)
  469. (if a (org-koma-letter-export-to-pdf t s v b)
  470. (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
  471. ;;; Helper functions
  472. (defun org-koma-letter-email ()
  473. "Return the current `user-mail-address'."
  474. user-mail-address)
  475. ;; The following is taken from/inspired by ox-grof.el
  476. ;; Thanks, Luis!
  477. (defun org-koma-letter--get-tagged-contents (key)
  478. "Get contents from a headline tagged with KEY.
  479. The contents is stored in `org-koma-letter-special-contents'."
  480. (let ((value (cdr (assoc-string (org-koma-letter--get-value key)
  481. org-koma-letter-special-contents))))
  482. (when value (org-string-nw-p (org-trim value)))))
  483. (defun org-koma-letter--get-value (value)
  484. "Turn value into a string whenever possible.
  485. Determines if VALUE is nil, a string, a function or a symbol and
  486. return a string or nil."
  487. (when value
  488. (cond ((stringp value) value)
  489. ((functionp value) (funcall value))
  490. ((symbolp value) (symbol-name value))
  491. (t value))))
  492. (defun org-koma-letter--special-contents-inline (keywords info)
  493. "Process KEYWORDS members of `org-koma-letter-special-contents'.
  494. KEYWORDS is a list of symbols. Return them as a string to be
  495. formatted.
  496. The function is used for inserting content of special headings
  497. such as the one tagged with PS."
  498. (mapconcat
  499. (lambda (keyword)
  500. (let* ((name (org-koma-letter--get-value keyword))
  501. (value (org-koma-letter--get-tagged-contents name))
  502. (macrop (memq keyword (plist-get info :special-tags-as-macro))))
  503. (cond ((not value) nil)
  504. (macrop (format "\\%s{%s}\n" name value))
  505. (t value))))
  506. keywords
  507. "\n"))
  508. (defun org-koma-letter--add-latex-newlines (string)
  509. "Replace regular newlines with LaTeX newlines (i.e. `\\\\')."
  510. (let ((str (org-trim string)))
  511. (when (org-string-nw-p str)
  512. (replace-regexp-in-string "\n" "\\\\\\\\\n" str))))
  513. ;;; Transcode Functions
  514. ;;;; Export Block
  515. (defun org-koma-letter-export-block (export-block _contents _info)
  516. "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
  517. CONTENTS is nil. INFO is a plist used as a communication
  518. channel."
  519. (when (member (org-element-property :type export-block)
  520. '("KOMA-LETTER" "LATEX"))
  521. (org-remove-indentation (org-element-property :value export-block))))
  522. ;;;; Export Snippet
  523. (defun org-koma-letter-export-snippet (export-snippet _contents _info)
  524. "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
  525. CONTENTS is nil. INFO is a plist used as a communication
  526. channel."
  527. (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
  528. (org-element-property :value export-snippet)))
  529. ;;;; Keyword
  530. (defun org-koma-letter-keyword (keyword contents info)
  531. "Transcode a KEYWORD element into KOMA Scrlttr2 code.
  532. CONTENTS is nil. INFO is a plist used as a communication
  533. channel."
  534. (let ((key (org-element-property :key keyword))
  535. (value (org-element-property :value keyword)))
  536. ;; Handle specifically KOMA-LETTER keywords. Otherwise, fallback
  537. ;; to `latex' back-end.
  538. (if (equal key "KOMA-LETTER") value
  539. (org-export-with-backend 'latex keyword contents info))))
  540. ;; Headline
  541. (defun org-koma-letter-headline (headline contents info)
  542. "Transcode a HEADLINE element from Org to LaTeX.
  543. CONTENTS holds the contents of the headline. INFO is a plist
  544. holding contextual information.
  545. Note that if a headline is tagged with a tag from
  546. `org-koma-letter-special-tags' it will not be exported, but
  547. stored in `org-koma-letter-special-contents' and included at the
  548. appropriate place."
  549. (let ((special-tag (org-koma-letter--special-tag headline info)))
  550. (if (not special-tag)
  551. contents
  552. (push (cons special-tag contents) org-koma-letter-special-contents)
  553. "")))
  554. (defun org-koma-letter--special-tag (headline info)
  555. "Non-nil if HEADLINE is a special headline.
  556. INFO is a plist holding contextual information. Return first
  557. special tag headline."
  558. (let ((special-tags (append
  559. (plist-get info :special-tags-in-letter)
  560. (plist-get info :special-tags-after-closing)
  561. (plist-get info :special-tags-after-letter))))
  562. (cl-some (lambda (tag) (and (assoc-string tag special-tags) tag))
  563. (org-export-get-tags headline info))))
  564. (defun org-koma-letter--keyword-or-headline (plist-key pred info)
  565. "Return the correct version of opening or closing.
  566. PLIST-KEY should be a key in info, typically :opening
  567. or :closing. PRED is a predicate run on headline to determine
  568. which title to use which takes two arguments, a headline element
  569. and an info plist. INFO is a plist holding contextual
  570. information. Return the preferred candidate for the exported of
  571. PLIST-KEY."
  572. (let* ((keyword-candidate (plist-get info plist-key))
  573. (headline-candidate (when (and (plist-get info :with-headline-opening)
  574. (or (plist-get info :special-headings)
  575. (not keyword-candidate)))
  576. (org-element-map (plist-get info :parse-tree)
  577. 'headline
  578. (lambda (h)
  579. (and (funcall pred h info)
  580. (org-element-property :title h)))
  581. info t))))
  582. (org-export-data (or headline-candidate keyword-candidate "") info)))
  583. ;;;; Template
  584. (defun org-koma-letter-template (contents info)
  585. "Return complete document string after KOMA Scrlttr2 conversion.
  586. CONTENTS is the transcoded contents string. INFO is a plist
  587. holding export options."
  588. (concat
  589. ;; Time-stamp.
  590. (and (plist-get info :time-stamp-file)
  591. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  592. ;; LaTeX compiler
  593. (org-latex--insert-compiler info)
  594. ;; Document class and packages.
  595. (org-latex-make-preamble info)
  596. ;; Settings. They can come from three locations, in increasing
  597. ;; order of precedence: global variables, LCO files and in-buffer
  598. ;; settings. Thus, we first insert settings coming from global
  599. ;; variables, then we insert LCO files, and, eventually, we insert
  600. ;; settings coming from buffer keywords.
  601. (org-koma-letter--build-settings 'global info)
  602. (mapconcat (lambda (file) (format "\\LoadLetterOption{%s}\n" file))
  603. (split-string (or (plist-get info :lco) ""))
  604. "")
  605. (org-koma-letter--build-settings 'buffer info)
  606. ;; Date.
  607. (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
  608. ;; Hyperref, document start, and subject and title.
  609. (let* ((with-subject (plist-get info :with-subject))
  610. (with-title (plist-get info :with-title))
  611. (title-as-subject (and with-subject
  612. (plist-get info :with-title-as-subject)))
  613. (subject* (org-string-nw-p
  614. (org-export-data (plist-get info :subject) info)))
  615. (title* (and with-title
  616. (org-string-nw-p
  617. (org-export-data (plist-get info :title) info))))
  618. (subject (cond ((not with-subject) nil)
  619. (title-as-subject (or subject* title*))
  620. (t subject*)))
  621. (title (cond ((not with-title) nil)
  622. (title-as-subject (and subject* title*))
  623. (t title*)))
  624. (hyperref-template (plist-get info :latex-hyperref-template))
  625. (spec (append (list (cons ?t (or title subject "")))
  626. (org-latex--format-spec info))))
  627. (concat
  628. (when (and with-subject (not (eq with-subject t)))
  629. (format "\\KOMAoption{subject}{%s}\n"
  630. (if (symbolp with-subject) with-subject
  631. (mapconcat #'symbol-name with-subject ","))))
  632. ;; Hyperref.
  633. (and (stringp hyperref-template)
  634. (format-spec hyperref-template spec))
  635. ;; Document start.
  636. "\\begin{document}\n\n"
  637. ;; Subject and title.
  638. (when subject (format "\\setkomavar{subject}{%s}\n" subject))
  639. (when title (format "\\setkomavar{title}{%s}\n" title))
  640. (when (or (org-string-nw-p title) (org-string-nw-p subject)) "\n")))
  641. ;; Letter start.
  642. (let ((keyword-val (plist-get info :to-address))
  643. (heading-val (org-koma-letter--get-tagged-contents 'to)))
  644. (format "\\begin{letter}{%%\n%s}\n\n"
  645. (org-koma-letter--add-latex-newlines
  646. (or (if (plist-get info :special-headings)
  647. (or heading-val keyword-val)
  648. (or keyword-val heading-val))
  649. "\\mbox{}"))))
  650. ;; Opening.
  651. (format "\\opening{%s}\n\n"
  652. (org-koma-letter--keyword-or-headline
  653. :opening
  654. (lambda (h i)
  655. (not (org-koma-letter--special-tag h i)))
  656. info))
  657. ;; Letter body.
  658. contents
  659. ;; Closing.
  660. (format "\\closing{%s}\n"
  661. (org-koma-letter--keyword-or-headline
  662. :closing
  663. (lambda (h i)
  664. (let ((special-tag (org-koma-letter--special-tag h i)))
  665. (and special-tag
  666. (string= "closing" special-tag))))
  667. info))
  668. (org-koma-letter--special-contents-inline
  669. (plist-get info :special-tags-after-closing) info)
  670. ;; Letter end.
  671. "\n\\end{letter}\n"
  672. (org-koma-letter--special-contents-inline
  673. (plist-get info :special-tags-after-letter) info)
  674. ;; Document end.
  675. "\n\\end{document}"))
  676. (defun org-koma-letter--build-settings (scope info)
  677. "Build settings string according to type.
  678. SCOPE is either `global' or `buffer'. INFO is a plist used as
  679. a communication channel."
  680. (let* ((check-scope
  681. ;; Non-nil value when SETTING was defined in SCOPE.
  682. (lambda (setting)
  683. (let ((property (intern (format ":inbuffer-%s" setting))))
  684. (if (eq scope 'global)
  685. (eq (plist-get info property) 'koma-letter:empty)
  686. (not (eq (plist-get info property) 'koma-letter:empty))))))
  687. (heading-or-key-value
  688. (lambda (heading key &optional scoped)
  689. (let* ((heading-val
  690. (org-koma-letter--get-tagged-contents heading))
  691. (key-val (org-string-nw-p (plist-get info key)))
  692. (scopedp (funcall check-scope (or scoped heading))))
  693. (and (or (and key-val scopedp) heading-val)
  694. (not (and (eq scope 'global) heading-val))
  695. (if scopedp key-val heading-val))))))
  696. (concat
  697. ;; Name.
  698. (let ((author (plist-get info :author)))
  699. (and author
  700. (funcall check-scope 'author)
  701. (format "\\setkomavar{fromname}{%s}\n"
  702. (org-export-data author info))))
  703. ;; From.
  704. (let ((from (funcall heading-or-key-value 'from :from-address)))
  705. (and from
  706. (format "\\setkomavar{fromaddress}{%s}\n"
  707. (org-koma-letter--add-latex-newlines from))))
  708. ;; Email.
  709. (let ((email (plist-get info :email)))
  710. (and email
  711. (funcall check-scope 'email)
  712. (format "\\setkomavar{fromemail}{%s}\n" email)))
  713. (and (funcall check-scope 'with-email)
  714. (format "\\KOMAoption{fromemail}{%s}\n"
  715. (if (plist-get info :with-email) "true" "false")))
  716. ;; Phone number.
  717. (let ((phone-number (plist-get info :phone-number)))
  718. (and (org-string-nw-p phone-number)
  719. (funcall check-scope 'phone-number)
  720. (format "\\setkomavar{fromphone}{%s}\n" phone-number)))
  721. (and (funcall check-scope 'with-phone)
  722. (format "\\KOMAoption{fromphone}{%s}\n"
  723. (if (plist-get info :with-phone) "true" "false")))
  724. ;; URL
  725. (let ((url (plist-get info :url)))
  726. (and (org-string-nw-p url)
  727. (funcall check-scope 'url)
  728. (format "\\setkomavar{fromurl}{%s}\n" url)))
  729. (and (funcall check-scope 'with-url)
  730. (format "\\KOMAoption{fromurl}{%s}\n"
  731. (if (plist-get info :with-url) "true" "false")))
  732. ;; From Logo
  733. (let ((from-logo (plist-get info :from-logo)))
  734. (and (org-string-nw-p from-logo)
  735. (funcall check-scope 'from-logo)
  736. (format "\\setkomavar{fromlogo}{%s}\n" from-logo)))
  737. (and (funcall check-scope 'with-from-logo)
  738. (format "\\KOMAoption{fromlogo}{%s}\n"
  739. (if (plist-get info :with-from-logo) "true" "false")))
  740. ;; Signature.
  741. (let* ((heading-val
  742. (and (plist-get info :with-headline-opening)
  743. (pcase (org-koma-letter--get-tagged-contents 'closing)
  744. ((and (pred org-string-nw-p) closing) (org-trim closing))
  745. (_ nil))))
  746. (signature (org-string-nw-p (plist-get info :signature)))
  747. (signature-scope (funcall check-scope 'signature)))
  748. (and (or (and signature signature-scope)
  749. heading-val)
  750. (not (and (eq scope 'global) heading-val))
  751. (format "\\setkomavar{signature}{%s}\n"
  752. (if signature-scope signature heading-val))))
  753. ;; Back address.
  754. (and (funcall check-scope 'with-backaddress)
  755. (format "\\KOMAoption{backaddress}{%s}\n"
  756. (if (plist-get info :with-backaddress) "true" "false")))
  757. ;; Place.
  758. (let ((with-place-set (funcall check-scope 'with-place))
  759. (place-set (funcall check-scope 'place)))
  760. (and (or (and with-place-set place-set)
  761. (and (eq scope 'buffer) (or with-place-set place-set)))
  762. (format "\\setkomavar{place}{%s}\n"
  763. (if (plist-get info :with-place) (plist-get info :place)
  764. ""))))
  765. ;; Location.
  766. (let ((location (funcall heading-or-key-value 'location :location)))
  767. (and location
  768. (format "\\setkomavar{location}{%s}\n" location)))
  769. ;; Folding marks.
  770. (and (funcall check-scope 'with-foldmarks)
  771. (let ((foldmarks (plist-get info :with-foldmarks)))
  772. (cond ((consp foldmarks)
  773. (format "\\KOMAoptions{foldmarks=true,foldmarks=%s}\n"
  774. (mapconcat #'symbol-name foldmarks "")))
  775. (foldmarks "\\KOMAoptions{foldmarks=true}\n")
  776. (t "\\KOMAoptions{foldmarks=false}\n")))))))
  777. ;;; Commands
  778. ;;;###autoload
  779. (defun org-koma-letter-export-as-latex
  780. (&optional async subtreep visible-only body-only ext-plist)
  781. "Export current buffer as a KOMA Scrlttr2 letter.
  782. If narrowing is active in the current buffer, only export its
  783. narrowed part.
  784. If a region is active, export that region.
  785. A non-nil optional argument ASYNC means the process should happen
  786. asynchronously. The resulting buffer should be accessible
  787. through the `org-export-stack' interface.
  788. When optional argument SUBTREEP is non-nil, export the sub-tree
  789. at point, extracting information from the headline properties
  790. first.
  791. When optional argument VISIBLE-ONLY is non-nil, don't export
  792. contents of hidden elements.
  793. When optional argument BODY-ONLY is non-nil, only write code
  794. between \"\\begin{letter}\" and \"\\end{letter}\".
  795. EXT-PLIST, when provided, is a property list with external
  796. parameters overriding Org default settings, but still inferior to
  797. file-local settings.
  798. Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
  799. will be displayed if `org-export-show-temporary-export-buffer' is
  800. non-nil."
  801. (interactive)
  802. (let (org-koma-letter-special-contents)
  803. (org-export-to-buffer 'koma-letter "*Org KOMA-LETTER Export*"
  804. async subtreep visible-only body-only ext-plist
  805. (lambda () (LaTeX-mode)))))
  806. ;;;###autoload
  807. (defun org-koma-letter-export-to-latex
  808. (&optional async subtreep visible-only body-only ext-plist)
  809. "Export current buffer as a KOMA Scrlttr2 letter (tex).
  810. If narrowing is active in the current buffer, only export its
  811. narrowed part.
  812. If a region is active, export that region.
  813. A non-nil optional argument ASYNC means the process should happen
  814. asynchronously. The resulting file should be accessible through
  815. the `org-export-stack' interface.
  816. When optional argument SUBTREEP is non-nil, export the sub-tree
  817. at point, extracting information from the headline properties
  818. first.
  819. When optional argument VISIBLE-ONLY is non-nil, don't export
  820. contents of hidden elements.
  821. When optional argument BODY-ONLY is non-nil, only write code
  822. between \"\\begin{letter}\" and \"\\end{letter}\".
  823. EXT-PLIST, when provided, is a property list with external
  824. parameters overriding Org default settings, but still inferior to
  825. file-local settings.
  826. When optional argument PUB-DIR is set, use it as the publishing
  827. directory.
  828. Return output file's name."
  829. (interactive)
  830. (let ((outfile (org-export-output-file-name ".tex" subtreep))
  831. (org-koma-letter-special-contents))
  832. (org-export-to-file 'koma-letter outfile
  833. async subtreep visible-only body-only ext-plist)))
  834. ;;;###autoload
  835. (defun org-koma-letter-export-to-pdf
  836. (&optional async subtreep visible-only body-only ext-plist)
  837. "Export current buffer as a KOMA Scrlttr2 letter (pdf).
  838. If narrowing is active in the current buffer, only export its
  839. narrowed part.
  840. If a region is active, export that region.
  841. A non-nil optional argument ASYNC means the process should happen
  842. asynchronously. The resulting file should be accessible through
  843. the `org-export-stack' interface.
  844. When optional argument SUBTREEP is non-nil, export the sub-tree
  845. at point, extracting information from the headline properties
  846. first.
  847. When optional argument VISIBLE-ONLY is non-nil, don't export
  848. contents of hidden elements.
  849. When optional argument BODY-ONLY is non-nil, only write code
  850. between \"\\begin{letter}\" and \"\\end{letter}\".
  851. EXT-PLIST, when provided, is a property list with external
  852. parameters overriding Org default settings, but still inferior to
  853. file-local settings.
  854. Return PDF file's name."
  855. (interactive)
  856. (let ((file (org-export-output-file-name ".tex" subtreep))
  857. (org-koma-letter-special-contents))
  858. (org-export-to-file 'koma-letter file
  859. async subtreep visible-only body-only ext-plist
  860. #'org-latex-compile)))
  861. (provide 'ox-koma-letter)
  862. ;;; ox-koma-letter.el ends here