ox-koma-letter.el 36 KB

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