ox-koma-letter.el 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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 'cl-lib)
  160. (require 'ox-latex)
  161. ;; Install a default letter class.
  162. (unless (assoc "default-koma-letter" org-latex-classes)
  163. (add-to-list 'org-latex-classes
  164. '("default-koma-letter" "\\documentclass[11pt]{scrlttr2}")))
  165. ;;; User-Configurable Variables
  166. (defgroup org-export-koma-letter nil
  167. "Options for exporting to KOMA scrlttr2 class in LaTeX export."
  168. :tag "Org Koma-Letter"
  169. :group 'org-export)
  170. (defcustom org-koma-letter-class-option-file "NF"
  171. "Letter Class Option File.
  172. This option can also be set with the LCO keyword."
  173. :type 'string)
  174. (defcustom org-koma-letter-author 'user-full-name
  175. "Sender's name.
  176. This variable defaults to calling the function `user-full-name'
  177. which just returns the current function `user-full-name'.
  178. Alternatively a string, nil or a function may be given.
  179. Functions must return a string.
  180. This option can also be set with the AUTHOR keyword."
  181. :type '(radio (function-item user-full-name)
  182. (string)
  183. (function)
  184. (const :tag "Do not export author" nil)))
  185. (defcustom org-koma-letter-email 'org-koma-letter-email
  186. "Sender's email address.
  187. This variable defaults to the value `org-koma-letter-email' which
  188. returns `user-mail-address'. Alternatively a string, nil or
  189. a function may be given. Functions must return a string.
  190. This option can also be set with the EMAIL keyword."
  191. :type '(radio (function-item org-koma-letter-email)
  192. (string)
  193. (function)
  194. (const :tag "Do not export email" nil)))
  195. (defcustom org-koma-letter-from-address ""
  196. "Sender's address, as a string.
  197. This option can also be set with one or more FROM_ADDRESS
  198. keywords."
  199. :type 'string)
  200. (defcustom org-koma-letter-phone-number ""
  201. "Sender's phone number, as a string.
  202. This option can also be set with the PHONE_NUMBER keyword."
  203. :type 'string)
  204. (defcustom org-koma-letter-url ""
  205. "Sender's URL, e. g., the URL of her homepage.
  206. This option can also be set with the URL keyword."
  207. :type 'string
  208. :safe #'stringp)
  209. (defcustom org-koma-letter-from-logo ""
  210. "Commands for inserting the sender's logo, e. g., \\includegraphics{logo}.
  211. This option can also be set with the FROM_LOGO keyword."
  212. :type 'string
  213. :safe #'stringp)
  214. (defcustom org-koma-letter-place ""
  215. "Place from which the letter is sent, as a string.
  216. This option can also be set with the PLACE keyword."
  217. :type 'string)
  218. (defcustom org-koma-letter-location ""
  219. "Sender's extension field, as a string.
  220. This option can also be set with the LOCATION keyword.
  221. Moreover, when:
  222. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  223. or there is no LOCATION keyword or the LOCATION keyword is
  224. empty;
  225. (2) the letter contains a headline with the special
  226. tag \"location\";
  227. then the location will be set as the content of the location
  228. special heading.
  229. The location field is typically printed right of the address
  230. field (See Figure 4.9. in the English manual of 2015-10-03)."
  231. :type 'string)
  232. (defcustom org-koma-letter-opening ""
  233. "Letter's opening, as a string.
  234. This option can also be set with the OPENING keyword. Moreover,
  235. when:
  236. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  237. or the CLOSING keyword is empty
  238. (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
  239. (3) the letter contains a headline without a special
  240. tag (e.g. \"to\" or \"ps\");
  241. then the opening will be implicitly set as the untagged headline title."
  242. :type 'string)
  243. (defcustom org-koma-letter-closing ""
  244. "Letter's closing, as a string.
  245. This option can also be set with the CLOSING keyword. Moreover,
  246. when:
  247. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  248. or the CLOSING keyword is empty;
  249. (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
  250. (3) the letter contains a headline with the special
  251. tag \"closing\";
  252. then the opening will be set as the title of the closing special
  253. heading title."
  254. :type 'string)
  255. (defcustom org-koma-letter-signature ""
  256. "Signature, as a string.
  257. This option can also be set with the SIGNATURE keyword.
  258. Moreover, when:
  259. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  260. or there is no CLOSING keyword or the CLOSING keyword is empty;
  261. (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
  262. (3) the letter contains a headline with the special
  263. tag \"closing\";
  264. then the signature will be set as the content of the
  265. closing special heading.
  266. Note if the content is empty the signature will not be set."
  267. :type 'string)
  268. (defcustom org-koma-letter-prefer-special-headings nil
  269. "Non-nil means prefer headlines over keywords for TO and FROM.
  270. This option can also be set with the OPTIONS keyword, e.g.:
  271. \"special-headings:t\"."
  272. :type 'boolean)
  273. (defcustom org-koma-letter-subject-format t
  274. "Non-nil means include the subject.
  275. Support formatting options.
  276. When t, insert a subject using default options. When nil, do not
  277. insert a subject at all. It can also be a list of symbols among
  278. the following ones:
  279. `afteropening' Subject after opening
  280. `beforeopening' Subject before opening
  281. `centered' Subject centered
  282. `left' Subject left-justified
  283. `right' Subject right-justified
  284. `titled' Add title/description to subject
  285. `underlined' Set subject underlined
  286. `untitled' Do not add title/description to subject
  287. Please refer to the KOMA-script manual (Table 4.16. in the
  288. English manual of 2012-07-22).
  289. This option can also be set with the OPTIONS keyword, e.g.:
  290. \"subject:(underlined centered)\"."
  291. :type
  292. '(choice
  293. (const :tag "No export" nil)
  294. (const :tag "Default options" t)
  295. (set :tag "Configure options"
  296. (const :tag "Subject after opening" afteropening)
  297. (const :tag "Subject before opening" beforeopening)
  298. (const :tag "Subject centered" centered)
  299. (const :tag "Subject left-justified" left)
  300. (const :tag "Subject right-justified" right)
  301. (const :tag "Add title or description to subject" underlined)
  302. (const :tag "Set subject underlined" titled)
  303. (const :tag "Do not add title or description to subject" untitled))))
  304. (defcustom org-koma-letter-use-backaddress nil
  305. "Non-nil prints return address in line above to address.
  306. This option can also be set with the OPTIONS keyword, e.g.:
  307. \"backaddress:t\"."
  308. :type 'boolean)
  309. (defcustom org-koma-letter-use-foldmarks t
  310. "Configure appearance of folding marks.
  311. When t, activate default folding marks. When nil, do not insert
  312. folding marks at all. It can also be a list of symbols among the
  313. following ones:
  314. `B' Activate upper horizontal mark on left paper edge
  315. `b' Deactivate upper horizontal mark on left paper edge
  316. `H' Activate all horizontal marks on left paper edge
  317. `h' Deactivate all horizontal marks on left paper edge
  318. `L' Activate left vertical mark on upper paper edge
  319. `l' Deactivate left vertical mark on upper paper edge
  320. `M' Activate middle horizontal mark on left paper edge
  321. `m' Deactivate middle horizontal mark on left paper edge
  322. `P' Activate punch or center mark on left paper edge
  323. `p' Deactivate punch or center mark on left paper edge
  324. `T' Activate lower horizontal mark on left paper edge
  325. `t' Deactivate lower horizontal mark on left paper edge
  326. `V' Activate all vertical marks on upper paper edge
  327. `v' Deactivate all vertical marks on upper paper edge
  328. This option can also be set with the OPTIONS keyword, e.g.:
  329. \"foldmarks:(b l m t)\"."
  330. :type '(choice
  331. (const :tag "Activate default folding marks" t)
  332. (const :tag "Deactivate folding marks" nil)
  333. (set
  334. :tag "Configure folding marks"
  335. (const :tag "Activate upper horizontal mark on left paper edge" B)
  336. (const :tag "Deactivate upper horizontal mark on left paper edge" b)
  337. (const :tag "Activate all horizontal marks on left paper edge" H)
  338. (const :tag "Deactivate all horizontal marks on left paper edge" h)
  339. (const :tag "Activate left vertical mark on upper paper edge" L)
  340. (const :tag "Deactivate left vertical mark on upper paper edge" l)
  341. (const :tag "Activate middle horizontal mark on left paper edge" M)
  342. (const :tag "Deactivate middle horizontal mark on left paper edge" m)
  343. (const :tag "Activate punch or center mark on left paper edge" P)
  344. (const :tag "Deactivate punch or center mark on left paper edge" p)
  345. (const :tag "Activate lower horizontal mark on left paper edge" T)
  346. (const :tag "Deactivate lower horizontal mark on left paper edge" t)
  347. (const :tag "Activate all vertical marks on upper paper edge" V)
  348. (const :tag "Deactivate all vertical marks on upper paper edge" v))))
  349. (defcustom org-koma-letter-use-phone nil
  350. "Non-nil prints sender's phone number.
  351. This option can also be set with the OPTIONS keyword, e.g.:
  352. \"phone:t\"."
  353. :type 'boolean)
  354. (defcustom org-koma-letter-use-url nil
  355. "Non-nil prints sender's URL.
  356. This option can also be set with the OPTIONS keyword, e.g.:
  357. \"url:t\"."
  358. :type 'boolean
  359. :safe #'booleanp)
  360. (defcustom org-koma-letter-use-from-logo nil
  361. "Non-nil prints sender's FROM_LOGO.
  362. This option can also be set with the OPTIONS keyword, e.g.:
  363. \"from-logo:t\"."
  364. :type 'boolean
  365. :safe #'booleanp)
  366. (defcustom org-koma-letter-use-email nil
  367. "Non-nil prints sender's email address.
  368. This option can also be set with the OPTIONS keyword, e.g.:
  369. \"email:t\"."
  370. :type 'boolean)
  371. (defcustom org-koma-letter-use-place t
  372. "Non-nil prints the letter's place next to the date.
  373. This option can also be set with the OPTIONS keyword, e.g.:
  374. \"place:nil\"."
  375. :type 'boolean)
  376. (defcustom org-koma-letter-default-class "default-koma-letter"
  377. "Default class for `org-koma-letter'.
  378. The value must be a member of `org-latex-classes'."
  379. :type 'string)
  380. (defcustom org-koma-letter-headline-is-opening-maybe t
  381. "Non-nil means a headline may be used as an opening and closing.
  382. See also `org-koma-letter-opening' and
  383. `org-koma-letter-closing'."
  384. :type 'boolean)
  385. (defcustom org-koma-letter-prefer-subject nil
  386. "Non-nil means title should be interpreted as subject if subject is missing.
  387. This option can also be set with the OPTIONS keyword,
  388. e.g. \"title-subject:t\"."
  389. :type 'boolean)
  390. (defconst org-koma-letter-special-tags-in-letter '(to from closing location)
  391. "Header tags related to the letter itself.")
  392. (defconst org-koma-letter-special-tags-after-closing '(after_closing ps encl cc)
  393. "Header tags to be inserted in the letter after closing.")
  394. (defconst org-koma-letter-special-tags-as-macro '(ps encl cc)
  395. "Header tags to be inserted as macros.")
  396. (defconst org-koma-letter-special-tags-after-letter '(after_letter)
  397. "Header tags to be inserted after the letter.")
  398. (defvar org-koma-letter-special-contents nil
  399. "Holds special content temporarily.")
  400. ;;; Define Back-End
  401. (org-export-define-derived-backend 'koma-letter 'latex
  402. :options-alist
  403. '((:latex-class "LATEX_CLASS" nil org-koma-letter-default-class t)
  404. (:lco "LCO" nil org-koma-letter-class-option-file)
  405. (:author "AUTHOR" nil (org-koma-letter--get-value org-koma-letter-author) parse)
  406. (:author-changed-in-buffer-p "AUTHOR" nil nil t)
  407. (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
  408. (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
  409. (:url "URL" nil org-koma-letter-url)
  410. (:from-logo "FROM_LOGO" nil org-koma-letter-from-logo)
  411. (:email "EMAIL" nil (org-koma-letter--get-value org-koma-letter-email) t)
  412. (:to-address "TO_ADDRESS" nil nil newline)
  413. (:place "PLACE" nil org-koma-letter-place)
  414. (:location "LOCATION" nil org-koma-letter-location)
  415. (:subject "SUBJECT" nil nil parse)
  416. (:opening "OPENING" nil org-koma-letter-opening parse)
  417. (:closing "CLOSING" nil org-koma-letter-closing parse)
  418. (:signature "SIGNATURE" nil org-koma-letter-signature newline)
  419. (:special-headings nil "special-headings" org-koma-letter-prefer-special-headings)
  420. (:special-tags-as-macro nil nil org-koma-letter-special-tags-as-macro)
  421. (:special-tags-in-letter nil nil org-koma-letter-special-tags-in-letter)
  422. (:special-tags-after-closing nil "after-closing-order"
  423. org-koma-letter-special-tags-after-closing)
  424. (:special-tags-after-letter nil "after-letter-order"
  425. org-koma-letter-special-tags-after-letter)
  426. (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
  427. (:with-email nil "email" org-koma-letter-use-email)
  428. (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
  429. (:with-phone nil "phone" org-koma-letter-use-phone)
  430. (:with-url nil "url" org-koma-letter-use-url)
  431. (:with-from-logo nil "from-logo" org-koma-letter-use-from-logo)
  432. (:with-place nil "place" org-koma-letter-use-place)
  433. (:with-subject nil "subject" org-koma-letter-subject-format)
  434. (:with-title-as-subject nil "title-subject" org-koma-letter-prefer-subject)
  435. (:with-headline-opening nil nil org-koma-letter-headline-is-opening-maybe)
  436. ;; Special properties non-nil when a setting happened in buffer.
  437. ;; They are used to prioritize in-buffer settings over "lco"
  438. ;; files. See `org-koma-letter-template'.
  439. (:inbuffer-author "AUTHOR" nil 'koma-letter:empty)
  440. (:inbuffer-from "FROM" nil 'koma-letter:empty)
  441. (:inbuffer-email "EMAIL" nil 'koma-letter:empty)
  442. (:inbuffer-phone-number "PHONE_NUMBER" nil 'koma-letter:empty)
  443. (:inbuffer-url "URL" nil 'koma-letter:empty)
  444. (:inbuffer-from-logo "FROM_LOGO" nil 'koma-letter:empty)
  445. (:inbuffer-place "PLACE" nil 'koma-letter:empty)
  446. (:inbuffer-location "LOCATION" nil 'koma-letter:empty)
  447. (:inbuffer-signature "SIGNATURE" nil 'koma-letter:empty)
  448. (:inbuffer-with-backaddress nil "backaddress" 'koma-letter:empty)
  449. (:inbuffer-with-email nil "email" 'koma-letter:empty)
  450. (:inbuffer-with-foldmarks nil "foldmarks" 'koma-letter:empty)
  451. (:inbuffer-with-phone nil "phone" 'koma-letter:empty)
  452. (:inbuffer-with-url nil "url" 'koma-letter:empty)
  453. (:inbuffer-with-from-logo nil "from-logo" 'koma-letter:empty)
  454. (:inbuffer-with-place nil "place" 'koma-letter:empty))
  455. :translate-alist '((export-block . org-koma-letter-export-block)
  456. (export-snippet . org-koma-letter-export-snippet)
  457. (headline . org-koma-letter-headline)
  458. (keyword . org-koma-letter-keyword)
  459. (template . org-koma-letter-template))
  460. :menu-entry
  461. '(?k "Export with KOMA Scrlttr2"
  462. ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
  463. (?l "As LaTeX file" org-koma-letter-export-to-latex)
  464. (?p "As PDF file" org-koma-letter-export-to-pdf)
  465. (?o "As PDF file and open"
  466. (lambda (a s v b)
  467. (if a (org-koma-letter-export-to-pdf t s v b)
  468. (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
  469. ;;; Helper functions
  470. (defun org-koma-letter-email ()
  471. "Return the current `user-mail-address'."
  472. user-mail-address)
  473. ;; The following is taken from/inspired by ox-grof.el
  474. ;; Thanks, Luis!
  475. (defun org-koma-letter--get-tagged-contents (key)
  476. "Get contents from a headline tagged with KEY.
  477. The contents is stored in `org-koma-letter-special-contents'."
  478. (let ((value (cdr (assoc-string (org-koma-letter--get-value key)
  479. org-koma-letter-special-contents))))
  480. (when value (org-string-nw-p (org-trim value)))))
  481. (defun org-koma-letter--get-value (value)
  482. "Turn value into a string whenever possible.
  483. Determines if VALUE is nil, a string, a function or a symbol and
  484. return a string or nil."
  485. (when value
  486. (cond ((stringp value) value)
  487. ((functionp value) (funcall value))
  488. ((symbolp value) (symbol-name value))
  489. (t value))))
  490. (defun org-koma-letter--special-contents-inline (keywords info)
  491. "Process KEYWORDS members of `org-koma-letter-special-contents'.
  492. KEYWORDS is a list of symbols. Return them as a string to be
  493. formatted.
  494. The function is used for inserting content of special headings
  495. such as the one tagged with PS."
  496. (mapconcat
  497. (lambda (keyword)
  498. (let* ((name (org-koma-letter--get-value keyword))
  499. (value (org-koma-letter--get-tagged-contents name))
  500. (macrop (memq keyword (plist-get info :special-tags-as-macro))))
  501. (cond ((not value) nil)
  502. (macrop (format "\\%s{%s}\n" name value))
  503. (t value))))
  504. keywords
  505. "\n"))
  506. (defun org-koma-letter--add-latex-newlines (string)
  507. "Replace regular newlines with LaTeX newlines (i.e. `\\\\')."
  508. (let ((str (org-trim string)))
  509. (when (org-string-nw-p str)
  510. (replace-regexp-in-string "\n" "\\\\\\\\\n" str))))
  511. ;;; Transcode Functions
  512. ;;;; Export Block
  513. (defun org-koma-letter-export-block (export-block _contents _info)
  514. "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
  515. CONTENTS is nil. INFO is a plist used as a communication
  516. channel."
  517. (when (member (org-element-property :type export-block)
  518. '("KOMA-LETTER" "LATEX"))
  519. (org-remove-indentation (org-element-property :value export-block))))
  520. ;;;; Export Snippet
  521. (defun org-koma-letter-export-snippet (export-snippet _contents _info)
  522. "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
  523. CONTENTS is nil. INFO is a plist used as a communication
  524. channel."
  525. (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
  526. (org-element-property :value export-snippet)))
  527. ;;;; Keyword
  528. (defun org-koma-letter-keyword (keyword contents info)
  529. "Transcode a KEYWORD element into KOMA Scrlttr2 code.
  530. CONTENTS is nil. INFO is a plist used as a communication
  531. channel."
  532. (let ((key (org-element-property :key keyword))
  533. (value (org-element-property :value keyword)))
  534. ;; Handle specifically KOMA-LETTER keywords. Otherwise, fallback
  535. ;; to `latex' back-end.
  536. (if (equal key "KOMA-LETTER") value
  537. (org-export-with-backend 'latex keyword contents info))))
  538. ;; Headline
  539. (defun org-koma-letter-headline (headline contents info)
  540. "Transcode a HEADLINE element from Org to LaTeX.
  541. CONTENTS holds the contents of the headline. INFO is a plist
  542. holding contextual information.
  543. Note that if a headline is tagged with a tag from
  544. `org-koma-letter-special-tags' it will not be exported, but
  545. stored in `org-koma-letter-special-contents' and included at the
  546. appropriate place."
  547. (let ((special-tag (org-koma-letter--special-tag headline info)))
  548. (if (not special-tag)
  549. contents
  550. (push (cons special-tag contents) org-koma-letter-special-contents)
  551. "")))
  552. (defun org-koma-letter--special-tag (headline info)
  553. "Non-nil if HEADLINE is a special headline.
  554. INFO is a plist holding contextual information. Return first
  555. special tag headline."
  556. (let ((special-tags (append
  557. (plist-get info :special-tags-in-letter)
  558. (plist-get info :special-tags-after-closing)
  559. (plist-get info :special-tags-after-letter))))
  560. (cl-some (lambda (tag) (and (assoc-string tag special-tags) tag))
  561. (org-export-get-tags headline info))))
  562. (defun org-koma-letter--keyword-or-headline (plist-key pred info)
  563. "Return the correct version of opening or closing.
  564. PLIST-KEY should be a key in info, typically :opening
  565. or :closing. PRED is a predicate run on headline to determine
  566. which title to use which takes two arguments, a headline element
  567. and an info plist. INFO is a plist holding contextual
  568. information. Return the preferred candidate for the exported of
  569. PLIST-KEY."
  570. (let* ((keyword-candidate (plist-get info plist-key))
  571. (headline-candidate (when (and (plist-get info :with-headline-opening)
  572. (or (plist-get info :special-headings)
  573. (not keyword-candidate)))
  574. (org-element-map (plist-get info :parse-tree)
  575. 'headline
  576. (lambda (h)
  577. (and (funcall pred h info)
  578. (org-element-property :title h)))
  579. info t))))
  580. (org-export-data (or headline-candidate keyword-candidate "") info)))
  581. ;;;; Template
  582. (defun org-koma-letter-template (contents info)
  583. "Return complete document string after KOMA Scrlttr2 conversion.
  584. CONTENTS is the transcoded contents string. INFO is a plist
  585. holding export options."
  586. (concat
  587. ;; Time-stamp.
  588. (and (plist-get info :time-stamp-file)
  589. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  590. ;; LaTeX compiler
  591. (org-latex--insert-compiler info)
  592. ;; Document class and packages.
  593. (org-latex-make-preamble info)
  594. ;; Settings. They can come from three locations, in increasing
  595. ;; order of precedence: global variables, LCO files and in-buffer
  596. ;; settings. Thus, we first insert settings coming from global
  597. ;; variables, then we insert LCO files, and, eventually, we insert
  598. ;; settings coming from buffer keywords.
  599. (org-koma-letter--build-settings 'global info)
  600. (mapconcat (lambda (file) (format "\\LoadLetterOption{%s}\n" file))
  601. (split-string (or (plist-get info :lco) ""))
  602. "")
  603. (org-koma-letter--build-settings 'buffer info)
  604. ;; Date.
  605. (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
  606. ;; Hyperref, document start, and subject and title.
  607. (let* ((with-subject (plist-get info :with-subject))
  608. (with-title (plist-get info :with-title))
  609. (title-as-subject (and with-subject
  610. (plist-get info :with-title-as-subject)))
  611. (subject* (org-string-nw-p
  612. (org-export-data (plist-get info :subject) info)))
  613. (title* (and with-title
  614. (org-string-nw-p
  615. (org-export-data (plist-get info :title) info))))
  616. (subject (cond ((not with-subject) nil)
  617. (title-as-subject (or subject* title*))
  618. (t subject*)))
  619. (title (cond ((not with-title) nil)
  620. (title-as-subject (and subject* title*))
  621. (t title*)))
  622. (hyperref-template (plist-get info :latex-hyperref-template))
  623. (spec (append (list (cons ?t (or title subject "")))
  624. (org-latex--format-spec info))))
  625. (concat
  626. (when (and with-subject (not (eq with-subject t)))
  627. (format "\\KOMAoption{subject}{%s}\n"
  628. (if (symbolp with-subject) with-subject
  629. (mapconcat #'symbol-name with-subject ","))))
  630. ;; Hyperref.
  631. (and (stringp hyperref-template)
  632. (format-spec hyperref-template spec))
  633. ;; Document start.
  634. "\\begin{document}\n\n"
  635. ;; Subject and title.
  636. (when subject (format "\\setkomavar{subject}{%s}\n" subject))
  637. (when title (format "\\setkomavar{title}{%s}\n" title))
  638. (when (or (org-string-nw-p title) (org-string-nw-p subject)) "\n")))
  639. ;; Letter start.
  640. (let ((keyword-val (plist-get info :to-address))
  641. (heading-val (org-koma-letter--get-tagged-contents 'to)))
  642. (format "\\begin{letter}{%%\n%s}\n\n"
  643. (org-koma-letter--add-latex-newlines
  644. (or (if (plist-get info :special-headings)
  645. (or heading-val keyword-val)
  646. (or keyword-val heading-val))
  647. "\\mbox{}"))))
  648. ;; Opening.
  649. (format "\\opening{%s}\n\n"
  650. (org-koma-letter--keyword-or-headline
  651. :opening
  652. (lambda (h i)
  653. (not (org-koma-letter--special-tag h i)))
  654. info))
  655. ;; Letter body.
  656. contents
  657. ;; Closing.
  658. (format "\\closing{%s}\n"
  659. (org-koma-letter--keyword-or-headline
  660. :closing
  661. (lambda (h i)
  662. (let ((special-tag (org-koma-letter--special-tag h i)))
  663. (and special-tag
  664. (string= "closing" special-tag))))
  665. info))
  666. (org-koma-letter--special-contents-inline
  667. (plist-get info :special-tags-after-closing) info)
  668. ;; Letter end.
  669. "\n\\end{letter}\n"
  670. (org-koma-letter--special-contents-inline
  671. (plist-get info :special-tags-after-letter) info)
  672. ;; Document end.
  673. "\n\\end{document}"))
  674. (defun org-koma-letter--build-settings (scope info)
  675. "Build settings string according to type.
  676. SCOPE is either `global' or `buffer'. INFO is a plist used as
  677. a communication channel."
  678. (let* ((check-scope
  679. ;; Non-nil value when SETTING was defined in SCOPE.
  680. (lambda (setting)
  681. (let ((property (intern (format ":inbuffer-%s" setting))))
  682. (if (eq scope 'global)
  683. (eq (plist-get info property) 'koma-letter:empty)
  684. (not (eq (plist-get info property) 'koma-letter:empty))))))
  685. (heading-or-key-value
  686. (lambda (heading key &optional scoped)
  687. (let* ((heading-val
  688. (org-koma-letter--get-tagged-contents heading))
  689. (key-val (org-string-nw-p (plist-get info key)))
  690. (scopedp (funcall check-scope (or scoped heading))))
  691. (and (or (and key-val scopedp) heading-val)
  692. (not (and (eq scope 'global) heading-val))
  693. (if scopedp key-val heading-val))))))
  694. (concat
  695. ;; Name.
  696. (let ((author (plist-get info :author)))
  697. (and author
  698. (funcall check-scope 'author)
  699. (format "\\setkomavar{fromname}{%s}\n"
  700. (org-export-data author info))))
  701. ;; From.
  702. (let ((from (funcall heading-or-key-value 'from :from-address)))
  703. (and from
  704. (format "\\setkomavar{fromaddress}{%s}\n"
  705. (org-koma-letter--add-latex-newlines from))))
  706. ;; Email.
  707. (let ((email (plist-get info :email)))
  708. (and email
  709. (funcall check-scope 'email)
  710. (format "\\setkomavar{fromemail}{%s}\n" email)))
  711. (and (funcall check-scope 'with-email)
  712. (format "\\KOMAoption{fromemail}{%s}\n"
  713. (if (plist-get info :with-email) "true" "false")))
  714. ;; Phone number.
  715. (let ((phone-number (plist-get info :phone-number)))
  716. (and (org-string-nw-p phone-number)
  717. (funcall check-scope 'phone-number)
  718. (format "\\setkomavar{fromphone}{%s}\n" phone-number)))
  719. (and (funcall check-scope 'with-phone)
  720. (format "\\KOMAoption{fromphone}{%s}\n"
  721. (if (plist-get info :with-phone) "true" "false")))
  722. ;; URL
  723. (let ((url (plist-get info :url)))
  724. (and (org-string-nw-p url)
  725. (funcall check-scope 'url)
  726. (format "\\setkomavar{fromurl}{%s}\n" url)))
  727. (and (funcall check-scope 'with-url)
  728. (format "\\KOMAoption{fromurl}{%s}\n"
  729. (if (plist-get info :with-url) "true" "false")))
  730. ;; From Logo
  731. (let ((from-logo (plist-get info :from-logo)))
  732. (and (org-string-nw-p from-logo)
  733. (funcall check-scope 'from-logo)
  734. (format "\\setkomavar{fromlogo}{%s}\n" from-logo)))
  735. (and (funcall check-scope 'with-from-logo)
  736. (format "\\KOMAoption{fromlogo}{%s}\n"
  737. (if (plist-get info :with-from-logo) "true" "false")))
  738. ;; Signature.
  739. (let* ((heading-val
  740. (and (plist-get info :with-headline-opening)
  741. (pcase (org-koma-letter--get-tagged-contents 'closing)
  742. ((and (pred org-string-nw-p) closing) (org-trim closing))
  743. (_ nil))))
  744. (signature (org-string-nw-p (plist-get info :signature)))
  745. (signature-scope (funcall check-scope 'signature)))
  746. (and (or (and signature signature-scope)
  747. heading-val)
  748. (not (and (eq scope 'global) heading-val))
  749. (format "\\setkomavar{signature}{%s}\n"
  750. (if signature-scope signature heading-val))))
  751. ;; Back address.
  752. (and (funcall check-scope 'with-backaddress)
  753. (format "\\KOMAoption{backaddress}{%s}\n"
  754. (if (plist-get info :with-backaddress) "true" "false")))
  755. ;; Place.
  756. (let ((with-place-set (funcall check-scope 'with-place))
  757. (place-set (funcall check-scope 'place)))
  758. (and (or (and with-place-set place-set)
  759. (and (eq scope 'buffer) (or with-place-set place-set)))
  760. (format "\\setkomavar{place}{%s}\n"
  761. (if (plist-get info :with-place) (plist-get info :place)
  762. ""))))
  763. ;; Location.
  764. (let ((location (funcall heading-or-key-value 'location :location)))
  765. (and location
  766. (format "\\setkomavar{location}{%s}\n" location)))
  767. ;; Folding marks.
  768. (and (funcall check-scope 'with-foldmarks)
  769. (let ((foldmarks (plist-get info :with-foldmarks)))
  770. (cond ((consp foldmarks)
  771. (format "\\KOMAoptions{foldmarks=true,foldmarks=%s}\n"
  772. (mapconcat #'symbol-name foldmarks "")))
  773. (foldmarks "\\KOMAoptions{foldmarks=true}\n")
  774. (t "\\KOMAoptions{foldmarks=false}\n")))))))
  775. ;;; Commands
  776. ;;;###autoload
  777. (defun org-koma-letter-export-as-latex
  778. (&optional async subtreep visible-only body-only ext-plist)
  779. "Export current buffer as a KOMA Scrlttr2 letter.
  780. If narrowing is active in the current buffer, only export its
  781. narrowed part.
  782. If a region is active, export that region.
  783. A non-nil optional argument ASYNC means the process should happen
  784. asynchronously. The resulting buffer should be accessible
  785. through the `org-export-stack' interface.
  786. When optional argument SUBTREEP is non-nil, export the sub-tree
  787. at point, extracting information from the headline properties
  788. first.
  789. When optional argument VISIBLE-ONLY is non-nil, don't export
  790. contents of hidden elements.
  791. When optional argument BODY-ONLY is non-nil, only write code
  792. between \"\\begin{letter}\" and \"\\end{letter}\".
  793. EXT-PLIST, when provided, is a property list with external
  794. parameters overriding Org default settings, but still inferior to
  795. file-local settings.
  796. Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
  797. will be displayed if `org-export-show-temporary-export-buffer' is
  798. non-nil."
  799. (interactive)
  800. (let (org-koma-letter-special-contents)
  801. (org-export-to-buffer 'koma-letter "*Org KOMA-LETTER Export*"
  802. async subtreep visible-only body-only ext-plist
  803. (lambda () (LaTeX-mode)))))
  804. ;;;###autoload
  805. (defun org-koma-letter-export-to-latex
  806. (&optional async subtreep visible-only body-only ext-plist)
  807. "Export current buffer as a KOMA Scrlttr2 letter (tex).
  808. If narrowing is active in the current buffer, only export its
  809. narrowed part.
  810. If a region is active, export that region.
  811. A non-nil optional argument ASYNC means the process should happen
  812. asynchronously. The resulting file should be accessible through
  813. the `org-export-stack' interface.
  814. When optional argument SUBTREEP is non-nil, export the sub-tree
  815. at point, extracting information from the headline properties
  816. first.
  817. When optional argument VISIBLE-ONLY is non-nil, don't export
  818. contents of hidden elements.
  819. When optional argument BODY-ONLY is non-nil, only write code
  820. between \"\\begin{letter}\" and \"\\end{letter}\".
  821. EXT-PLIST, when provided, is a property list with external
  822. parameters overriding Org default settings, but still inferior to
  823. file-local settings.
  824. When optional argument PUB-DIR is set, use it as the publishing
  825. directory.
  826. Return output file's name."
  827. (interactive)
  828. (let ((outfile (org-export-output-file-name ".tex" subtreep))
  829. (org-koma-letter-special-contents))
  830. (org-export-to-file 'koma-letter outfile
  831. async subtreep visible-only body-only ext-plist)))
  832. ;;;###autoload
  833. (defun org-koma-letter-export-to-pdf
  834. (&optional async subtreep visible-only body-only ext-plist)
  835. "Export current buffer as a KOMA Scrlttr2 letter (pdf).
  836. If narrowing is active in the current buffer, only export its
  837. narrowed part.
  838. If a region is active, export that region.
  839. A non-nil optional argument ASYNC means the process should happen
  840. asynchronously. The resulting file should be accessible through
  841. the `org-export-stack' interface.
  842. When optional argument SUBTREEP is non-nil, export the sub-tree
  843. at point, extracting information from the headline properties
  844. first.
  845. When optional argument VISIBLE-ONLY is non-nil, don't export
  846. contents of hidden elements.
  847. When optional argument BODY-ONLY is non-nil, only write code
  848. between \"\\begin{letter}\" and \"\\end{letter}\".
  849. EXT-PLIST, when provided, is a property list with external
  850. parameters overriding Org default settings, but still inferior to
  851. file-local settings.
  852. Return PDF file's name."
  853. (interactive)
  854. (let ((file (org-export-output-file-name ".tex" subtreep))
  855. (org-koma-letter-special-contents))
  856. (org-export-to-file 'koma-letter file
  857. async subtreep visible-only body-only ext-plist
  858. #'org-latex-compile)))
  859. (provide 'ox-koma-letter)
  860. ;;; ox-koma-letter.el ends here