ox-koma-letter.el 37 KB

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