Export back-ends: add entry in `org-export-dispatch' when loaded
[org-mode.git] / contrib / lisp / org-e-groff.el
blob9651e23f34a3e4f548808b5b6bd714fe1757bde3
1 ;; org-e-groff.el --- Groff Back-End For Org Export Engine
3 ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Author: Luis R Anaya <papoanaya aroba hot mail punto com>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;;
10 ;; This program 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.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; This library implements a Groff Memorandum Macro back-end for
25 ;; Org generic exporter.
27 ;; To test it, run
29 ;; M-: (org-export-to-buffer 'e-groff "*Test e-Groff*") RET
31 ;; in an org-mode buffer then switch to the buffer to see the Groff
32 ;; export. See contrib/lisp/org-export.el for more details on how
33 ;; this exporter works.
35 ;; It introduces two new buffer keywords: "GROFF_CLASS" and
36 ;; "GROFF_CLASS_OPTIONS".
38 ;;; Code:
40 (eval-when-compile (require 'cl))
42 (defvar org-export-groff-default-packages-alist)
43 (defvar org-export-groff-packages-alist)
45 (require 'org-export)
48 ;;; Define Back-End
50 (org-export-define-backend e-groff
51 '((babel-call . org-e-groff-babel-call)
52 (bold . org-e-groff-bold)
53 (center-block . org-e-groff-center-block)
54 (clock . org-e-groff-clock)
55 (code . org-e-groff-code)
56 (comment . org-e-groff-comment)
57 (comment-block . org-e-groff-comment-block)
58 (drawer . org-e-groff-drawer)
59 (dynamic-block . org-e-groff-dynamic-block)
60 (entity . org-e-groff-entity)
61 (example-block . org-e-groff-example-block)
62 (export-block . org-e-groff-export-block)
63 (export-snippet . org-e-groff-export-snippet)
64 (fixed-width . org-e-groff-fixed-width)
65 (footnote-definition . org-e-groff-footnote-definition)
66 (footnote-reference . org-e-groff-footnote-reference)
67 (headline . org-e-groff-headline)
68 (horizontal-rule . org-e-groff-horizontal-rule)
69 (inline-babel-call . org-e-groff-inline-babel-call)
70 (inline-src-block . org-e-groff-inline-src-block)
71 (inlinetask . org-e-groff-inlinetask)
72 (italic . org-e-groff-italic)
73 (item . org-e-groff-item)
74 (keyword . org-e-groff-keyword)
75 (groff-environment . org-e-groff-groff-environment)
76 (groff-fragment . org-e-groff-groff-fragment)
77 (line-break . org-e-groff-line-break)
78 (link . org-e-groff-link)
79 (paragraph . org-e-groff-paragraph)
80 (plain-list . org-e-groff-plain-list)
81 (plain-text . org-e-groff-plain-text)
82 (planning . org-e-groff-planning)
83 (property-drawer . org-e-groff-property-drawer)
84 (quote-block . org-e-groff-quote-block)
85 (quote-section . org-e-groff-quote-section)
86 (radio-target . org-e-groff-radio-target)
87 (section . org-e-groff-section)
88 (special-block . org-e-groff-special-block)
89 (src-block . org-e-groff-src-block)
90 (statistics-cookie . org-e-groff-statistics-cookie)
91 (strike-through . org-e-groff-strike-through)
92 (subscript . org-e-groff-subscript)
93 (superscript . org-e-groff-superscript)
94 (table . org-e-groff-table)
95 (table-cell . org-e-groff-table-cell)
96 (table-row . org-e-groff-table-row)
97 (target . org-e-groff-target)
98 (template . org-e-groff-template)
99 (timestamp . org-e-groff-timestamp)
100 (underline . org-e-groff-underline)
101 (verbatim . org-e-groff-verbatim)
102 (verse-block . org-e-groff-verse-block))
103 :export-block "GROFF"
104 :menu-entry
105 (?g "Export to GROFF"
106 ((?g "As GROFF file" org-e-groff-export-to-groff)
107 (?p "As PDF file" org-e-groff-export-to-pdf)
108 (?o "As PDF file and open"
109 (lambda (s v b) (org-open-file (org-e-groff-export-to-pdf s v b))))))
110 :options-alist
111 ((:date "DATE" nil org-e-groff-date-format t)
112 (:groff-class "GROFF_CLASS" nil org-e-groff-default-class t)
113 (:groff-class-options "GROFF_CLASS_OPTIONS" nil nil t)
114 (:groff-header-extra "GROFF_HEADER" nil nil newline)))
118 ;;; User Configurable Variables
120 (defgroup org-export-e-groff nil
121 "Options for exporting Org mode files to Groff."
122 :tag "Org Export Groff"
123 :group 'org-export)
125 ;;; Preamble
127 (defcustom org-e-groff-default-class "internal"
128 "The default Groff class."
129 :group 'org-export-e-groff
130 :type '(string :tag "Groff class"))
132 (defcustom org-e-groff-classes
133 '(("file" ".MT 1"
134 (:heading 'default :type "memo" :last-section "toc"))
135 ("internal" ".MT 0"
136 (:heading 'default :type "memo" :last-section "toc"))
137 ("programmer" ".MT 2"
138 (:heading 'default :type "memo" :last-section "toc"))
139 ("engineer" ".MT 3"
140 (:heading 'default :type "memo" :last-section "toc"))
141 ("external" ".MT 4"
142 (:heading 'default :type "memo" :last-section "toc"))
143 ("letter" ".MT 5"
144 (:heading 'default :type "memo" :last-section "sign"))
145 ("custom" ".so file"
146 (:heading custom-function :type "custom" :last-section "toc"))
147 ("dummy" ""
148 (:heading 'default :type "memo"))
149 ("ms" "ms"
150 (:heading 'default :type "cover" :last-section "toc"))
151 ("se_ms" "se_ms"
152 (:heading 'default :type "cover" :last-section "toc"))
153 ("block" "BL"
154 (:heading 'default :type "letter" :last-section "sign"))
155 ("semiblock" "SB"
156 (:heading 'default :type "letter" :last-section "sign"))
157 ("fullblock" "FB"
158 (:heading 'default :type "letter" :last-section "sign"))
159 ("simplified" "SP"
160 (:heading 'default :type "letter" :last-section "sign"))
161 ("none" "" (:heading 'default :type "custom")))
163 ;; none means, no Cover or Memorandum Type and no calls to AU, AT, ND and TL
164 ;; This is to facilitate the creation of custom pages.
166 ;; dummy means, no Cover or Memorandum Type but calls to AU, AT, ND and TL
167 ;; are made. This is to facilitate Abstract Insertion.
169 "This list describes the attributes for the documents being created.
170 It allows for the creation of new "
171 :group 'org-export-e-groff
172 :type '(repeat
173 (list (string :tag "Document Type")
174 (string :tag "Header")
175 (repeat :tag "Options" :inline t
176 (choice
177 (list :tag "Heading")
178 (function :tag "Hook computing sectioning"))))))
181 (defcustom org-e-groff-date-format
182 (format-time-string "%Y-%m-%d")
183 "Format string for .ND "
184 :group 'org-export-e-groff
185 :type 'boolean)
187 ;;; Headline
189 (defconst org-e-groff-special-tags
190 '("FROM" "TO" "ABSTRACT" "APPENDIX" "BODY" "NS"))
192 (defcustom org-e-groff-format-headline-function nil
193 "Function to format headline text.
195 This function will be called with 5 arguments:
196 TODO the todo keyword (string or nil).
197 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
198 PRIORITY the priority of the headline (integer or nil)
199 TEXT the main headline text (string).
200 TAGS the tags as a list of strings (list of strings or nil).
202 The function result will be used in the section format string.
204 As an example, one could set the variable to the following, in
205 order to reproduce the default set-up:
207 \(defun org-e-groff-format-headline (todo todo-type priority text tags)
208 \"Default format function for an headline.\"
209 \(concat (when todo
210 \(format \"\\fB%s\\fP \" todo))
211 \(when priority
212 \(format \"[\\#%c] \" priority))
213 text
214 \(when tags
215 \(format \" %s \"
216 \(mapconcat 'identity tags \":\"))))"
217 :group 'org-export-e-groff
218 :type 'function)
220 ;;; Timestamps
222 (defcustom org-e-groff-active-timestamp-format "\\fI%s\\fP"
223 "A printf format string to be applied to active timestamps."
224 :group 'org-export-e-groff
225 :type 'string)
227 (defcustom org-e-groff-inactive-timestamp-format "\\fI%s\\fP"
228 "A printf format string to be applied to inactive timestamps."
229 :group 'org-export-e-groff
230 :type 'string)
232 (defcustom org-e-groff-diary-timestamp-format "\\fI%s\\fP"
233 "A printf format string to be applied to diary timestamps."
234 :group 'org-export-e-groff
235 :type 'string)
237 ;;; Links
239 (defcustom org-e-groff-inline-image-rules
240 '(("file" . "\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'")
241 ("fuzzy" . "\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'"))
242 "Rules characterizing image files that can be inlined into Groff.
244 A rule consists in an association whose key is the type of link
245 to consider, and value is a regexp that will be matched against
246 link's path.
248 Note that, by default, the image extensions actually allowed
249 depend on the way the Groff file is processed. When used with
250 pdfgroff, pdf, jpg and png images are OK. When processing
251 through dvi to Postscript, only ps and eps are allowed. The
252 default we use here encompasses both."
253 :group 'org-export-e-groff
254 :type '(alist :key-type (string :tag "Type")
255 :value-type (regexp :tag "Path")))
257 (defcustom org-e-groff-link-with-unknown-path-format "\\fI%s\\fP"
258 "Format string for links with unknown path type."
259 :group 'org-export-groff
260 :type 'string)
262 ;;; Tables
264 (defcustom org-e-groff-tables-centered t
265 "When non-nil, tables are exported in a center environment."
266 :group 'org-export-e-groff
267 :type 'boolean)
269 (defcustom org-e-groff-tables-verbatim nil
270 "When non-nil, tables are exported verbatim."
271 :group 'org-export-e-groff
272 :type 'boolean)
274 (defcustom org-e-groff-table-scientific-notation "%sE%s"
275 "Format string to display numbers in scientific notation.
276 The format should have \"%s\" twice, for mantissa and exponent
277 \(i.e. \"%s\\\\times10^{%s}\").
279 When nil, no transformation is made."
280 :group 'org-export-e-groff
281 :type '(choice
282 (string :tag "Format string")
283 (const :tag "No formatting")))
285 ;;; Text markup
287 (defcustom org-e-groff-text-markup-alist
288 '((bold . "\\fB%s\\fP")
289 (code . "\\fC%s\\fP")
290 (italic . "\\fI%s\\fP")
291 (strike-through . "\\fC%s\\fP") ; Strike through and underline
292 (underline . "\\fI%s\\fP") ; need to be revised.
293 (verbatim . "protectedtexttt"))
294 "Alist of Groff expressions to convert text markup.
296 The key must be a symbol among `bold', `code', `italic',
297 `strike-through', `underline' and `verbatim'. The value is
298 a formatting string to wrap fontified text with it.
300 If no association can be found for a given markup, text will be
301 returned as-is."
302 :group 'org-export-e-groff
303 :type 'alist
304 :options '(bold code italic strike-through underline verbatim))
306 ;;; Drawers
308 (defcustom org-e-groff-format-drawer-function nil
309 "Function called to format a drawer in Groff code.
311 The function must accept two parameters:
312 NAME the drawer name, like \"LOGBOOK\"
313 CONTENTS the contents of the drawer.
315 The function should return the string to be exported.
317 For example, the variable could be set to the following function
318 in order to mimic default behaviour:
320 \(defun org-e-groff-format-drawer-default \(name contents\)
321 \"Format a drawer element for Groff export.\"
322 contents\)"
323 :group 'org-export-e-groff
324 :type 'function)
326 ;;; Inlinetasks
328 (defcustom org-e-groff-format-inlinetask-function nil
329 "Function called to format an inlinetask in Groff code.
331 The function must accept six parameters:
332 TODO the todo keyword, as a string
333 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
334 PRIORITY the inlinetask priority, as a string
335 NAME the inlinetask name, as a string.
336 TAGS the inlinetask tags, as a list of strings.
337 CONTENTS the contents of the inlinetask, as a string.
339 The function should return the string to be exported.
341 For example, the variable could be set to the following function
342 in order to mimic default behaviour:
344 \(defun org-e-groff-format-inlinetask \(todo type priority name tags contents\)
345 \"Format an inline task element for Groff export.\"
346 \(let ((full-title
347 \(concat
348 \(when todo
349 \(format \"\\fB%s\\fP \" todo))
350 \(when priority (format \"[\\#%c] \" priority))
351 title
352 \(when tags
353 \(format \":%s:\"
354 \(mapconcat 'identity tags \":\")))))
355 \(format (concat \".DS L\\n\"
356 \"%s\\n\\n\"
357 \"%s\"
358 \".DE\")
359 full-title contents))"
360 :group 'org-export-e-groff
361 :type 'function)
363 ;; Src blocks
365 (defcustom org-e-groff-source-highlight nil
366 "Use GNU source highlight to embellish source blocks "
367 :group 'org-export-e-groff
368 :type 'boolean)
370 (defcustom org-e-groff-source-highlight-langs
371 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
372 (scheme "scheme")
373 (c "c") (cc "cpp") (csharp "csharp") (d "d")
374 (fortran "fortran") (cobol "cobol") (pascal "pascal")
375 (ada "ada") (asm "asm")
376 (perl "perl") (cperl "perl")
377 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
378 (java "java") (javascript "javascript")
379 (tex "latex")
380 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
381 (ocaml "caml") (caml "caml")
382 (sql "sql") (sqlite "sql")
383 (html "html") (css "css") (xml "xml")
384 (bat "bat") (bison "bison") (clipper "clipper")
385 (ldap "ldap") (opa "opa")
386 (php "php") (postscript "postscript") (prolog "prolog")
387 (properties "properties") (makefile "makefile")
388 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
389 "Alist mapping languages to their listing language counterpart.
390 The key is a symbol, the major mode symbol without the \"-mode\".
391 The value is the string that should be inserted as the language
392 parameter for the listings package. If the mode name and the
393 listings name are the same, the language does not need an entry
394 in this list - but it does not hurt if it is present."
395 :group 'org-export-e-groff
396 :type '(repeat
397 (list
398 (symbol :tag "Major mode ")
399 (string :tag "Listings language"))))
401 (defcustom org-e-groff-source-highlight-options nil
402 "Association list of options for the groff listings package.
404 These options are supplied as a comma-separated list to the
405 \\lstset command. Each element of the association list should be
406 a list containing two strings: the name of the option, and the
407 value. For example,
409 (setq org-e-groff-source-highlight-options
410 '((\"basicstyle\" \"\\small\")
411 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
413 will typeset the code in a small size font with underlined, bold
414 black keywords.
416 Note that the same options will be applied to blocks of all
417 languages."
418 :group 'org-export-e-groff
419 :type '(repeat
420 (list
421 (string :tag "Listings option name ")
422 (string :tag "Listings option value"))))
424 (defvar org-e-groff-custom-lang-environments nil
425 "Alist mapping languages to language-specific Groff environments.
427 It is used during export of src blocks by the listings and
428 groff packages. For example,
430 \(setq org-e-groff-custom-lang-environments
431 '\(\(python \"pythoncode\"\)\)\)
433 would have the effect that if org encounters begin_src python
434 during groff export it will use pythoncode as the source-highlight
435 language.")
437 ;;; Plain text
439 (defcustom org-e-groff-quotes
440 '(("fr"
441 ("\\(\\s-\\|[[(]\\|^\\)\"" . "«~")
442 ("\\(\\S-\\)\"" . "~»")
443 ("\\(\\s-\\|(\\|^\\)'" . "'"))
444 ("en"
445 ("\\(\\s-\\|[[(]\\|^\\)\"" . "``")
446 ("\\(\\S-\\)\"" . "''")
447 ("\\(\\s-\\|(\\|^\\)'" . "`")))
448 "Alist for quotes to use when converting english double-quotes.
450 The CAR of each item in this alist is the language code.
451 The CDR of each item in this alist is a list of three CONS:
452 - the first CONS defines the opening quote;
453 - the second CONS defines the closing quote;
454 - the last CONS defines single quotes.
456 For each item in a CONS, the first string is a regexp
457 for allowed characters before/after the quote, the second
458 string defines the replacement string for this quote."
459 :group 'org-export-e-groff
460 :type '(list
461 (cons :tag "Opening quote"
462 (string :tag "Regexp for char before")
463 (string :tag "Replacement quote "))
464 (cons :tag "Closing quote"
465 (string :tag "Regexp for char after ")
466 (string :tag "Replacement quote "))
467 (cons :tag "Single quote"
468 (string :tag "Regexp for char before")
469 (string :tag "Replacement quote "))))
471 (defcustom org-e-groff-special-char
472 '(("(c)" . "\\\\(co")
473 ("(tm)" . "\\\\(tm")
474 ("(rg)" . "\\\\(rg"))
475 "CONS list in which the value of the car
476 is replace on the value of the CDR. "
477 :group 'org-export-e-groff
478 :type '(list
479 (cons :tag "Character Subtitute"
480 (string :tag "Original Character Group")
481 (string :tag "Replacement Character"))))
483 ;;; Compilation
485 (defcustom org-e-groff-pdf-process
486 '("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
487 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
488 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf")
490 "Commands to process a Groff file to a PDF file.
491 This is a list of strings, each of them will be given to the
492 shell as a command. %f in the command will be replaced by the
493 full file name, %b by the file base name \(i.e. without
494 extension) and %o by the base directory of the file."
495 :group 'org-export-pdf
496 :type '(choice
497 (repeat :tag "Shell command sequence"
498 (string :tag "Shell command"))
499 (const :tag "2 runs of pdfgroff"
500 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
501 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
502 (const :tag "3 runs of pdfgroff"
503 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
504 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
505 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
506 (function)))
508 (defcustom org-e-groff-logfiles-extensions
509 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
510 "The list of file extensions to consider as Groff logfiles."
511 :group 'org-export-e-groff
512 :type '(repeat (string :tag "Extension")))
514 (defcustom org-e-groff-remove-logfiles t
515 "Non-nil means remove the logfiles produced by PDF production.
516 These are the .aux, .log, .out, and .toc files."
517 :group 'org-export-e-groff
518 :type 'boolean)
520 (defcustom org-e-groff-organization "Org User"
521 "Name of the organization used to populate the .AF command."
522 :group 'org-export-e-groff
523 :type 'string)
525 (defcustom org-e-groff-raster-to-ps nil
526 "Command used to convert raster to EPS. Nil for no conversion. Make sure that
527 `org-e-groff-inline-image-rules' is adjusted accordingly if not conversion is being
528 done. In this case, remove the entries for jpg and png in the file and fuzzy lists."
529 :group 'org-export-e-groff
530 :type '(choice
531 (repeat :tag "Shell Command Sequence" (string :tag "Shell Command"))
532 (const :tag "sam2p" "a=%s;b=%s;sam2p ${a} ${b} ;grep -v BeginData ${b} > b_${b};mv b_${b} ${b}" )
533 (const :tag "NetPNM" "a=%s;b=%s;pngtopnm ${a} | pnmtops -noturn > ${b}" )
534 (const :tag "None" nil)))
536 (defvar org-e-groff-registered-references nil)
537 (defvar org-e-groff-special-content nil)
541 ;;; Internal Functions
543 (defun org-e-groff--caption/label-string (caption label info)
544 "Return caption and label Groff string for floats.
546 CAPTION is a cons cell of secondary strings, the car being the
547 standard caption and the cdr its short form. LABEL is a string
548 representing the label. INFO is a plist holding contextual
549 information.
551 If there's no caption nor label, return the empty string.
553 For non-floats, see `org-e-groff--wrap-label'."
554 (let ((label-str ""))
555 (cond
556 ((and (not caption) (not label)) "")
557 ((not caption) (format "\\fI%s\\fP" label))
558 ;; Option caption format with short name.
559 ((cdr caption)
560 (format "%s\n.br\n%s - %s\n"
561 (org-export-data (cdr caption) info)
562 label-str
563 (org-export-data (car caption) info)))
564 ;; Standard caption format.
565 (t (format "\\fR%s\\fP"
566 (org-export-data (car caption) info))))))
568 (defun org-e-groff--quotation-marks (text info)
569 "Export quotation marks depending on language conventions.
570 TEXT is a string containing quotation marks to be replaced. INFO
571 is a plist used as a communication channel."
572 (mapc (lambda(l)
573 (let ((start 0))
574 (while (setq start (string-match (car l) text start))
575 (let ((new-quote (concat (match-string 1 text) (cdr l))))
576 (setq text (replace-match new-quote t t text))))))
577 (cdr (or (assoc (plist-get info :language) org-e-groff-quotes)
578 ;; Falls back on English.
579 (assoc "en" org-e-groff-quotes))))
580 text)
582 (defun org-e-groff--wrap-label (element output)
583 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
584 This function shouldn't be used for floats. See
585 `org-e-groff--caption/label-string'."
586 (let ((label (org-element-property :name element)))
587 (if (or (not output) (not label) (string= output "") (string= label ""))
588 output
589 (concat (format "%s\n.br\n" label) output))))
591 (defun org-e-groff--text-markup (text markup)
592 "Format TEXT depending on MARKUP text markup.
593 See `org-e-groff-text-markup-alist' for details."
594 (let ((fmt (cdr (assq markup org-e-groff-text-markup-alist))))
595 (cond
596 ;; No format string: Return raw text.
597 ((not fmt) text)
598 ((string= "protectedtexttt" fmt)
599 (let ((start 0)
600 (trans '(("\\" . "\\")))
601 (rtn "")
602 char)
603 (while (string-match "[\\{}$%&_#~^]" text)
604 (setq char (match-string 0 text))
605 (if (> (match-beginning 0) 0)
606 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
607 (setq text (substring text (1+ (match-beginning 0))))
608 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
609 rtn (concat rtn char)))
610 (setq text (concat rtn text))
611 (format "\\fC%s\\fP" text)))
612 ;; Else use format string.
613 (t (format fmt text)))))
616 (defun org-e-groff--get-tagged-content (tag info)
617 (cdr (assoc tag org-e-groff-special-content)))
619 (defun org-e-groff--mt-head (title contents attr info)
620 (concat
622 ;; 1. Insert Organization
623 (let ((firm-option (plist-get attr :firm)))
624 (cond
625 ((stringp firm-option)
626 (format ".AF \"%s\" \n" firm-option))
627 (t (format ".AF \"%s\" \n" (or org-e-groff-organization "")))))
629 ;; 2. Title
630 (let ((subtitle1 (plist-get attr :subtitle1))
631 (subtitle2 (plist-get attr :subtitle2)))
633 (cond
634 ((string= "" title)
635 (format ".TL \"%s\" \"%s\" \n%s\n"
636 (or subtitle1 "")
637 (or subtitle2 "") " "))
639 ((not (or subtitle1 subtitle2))
640 (format ".TL\n%s\n"
641 (or title "")))
643 (format ".TL \"%s\" \"%s \" \n%s\n"
644 (or subtitle1 "")
645 (or subtitle2 "") title))))
647 ;; 3. Author.
648 ;; In Groff, .AU *MUST* be placed after .TL
649 ;; If From, populate with data from From else
651 (let ((author (and (plist-get info :with-author)
652 (let ((auth (plist-get info :author)))
653 (and auth (org-export-data auth info)))))
654 (email (and (plist-get info :with-email)
655 (org-export-data (plist-get info :email) info)))
656 (from-data (org-e-groff--get-tagged-content "FROM" info))
658 (to-data (org-e-groff--get-tagged-content "TO" info)))
660 (cond
661 ((and author from-data)
662 (let ((au-line
663 (mapconcat
664 (lambda (from-line)
665 (format " \"%s\" " from-line))
666 (split-string
667 (setq from-data
668 (replace-regexp-in-string "\\.P\n" "" from-data)) "\n") "")))
670 (concat
671 (format ".AU \"%s\" " author) au-line "\n")))
673 ((and author email (not (string= "" email)))
674 (format ".AU \"%s\" \"%s\"\n" author email))
676 (author (format ".AU \"%s\"\n" author))
678 (t ".AU \"\" \n")))
681 ;; 4. Author Title, if present
682 (let ((at-item (plist-get attr :author-title)))
683 (if (and at-item (stringp at-item))
684 (format ".AT \"%s\" \n" at-item)
685 ""))
687 ;; 5. Date.
688 (let ((date (org-export-data (plist-get info :date) info)))
689 (and date (format ".ND \"%s\"\n" date)))
692 ;; If Abstract, then Populate Abstract
695 (let ((abstract-data (org-e-groff--get-tagged-content "ABSTRACT" info))
696 (to-data (org-e-groff--get-tagged-content "TO" info)))
697 (cond
698 (abstract-data
699 (format ".AS\n%s\n.AE\n" abstract-data))
700 (to-data
701 (format ".AS\n%s\n.AE\n" to-data))))))
703 (defun org-e-groff--letter-head (title contents attr info)
704 (let ((author (and (plist-get info :with-author)
705 (let ((auth (plist-get info :author)))
706 (and auth (org-export-data auth info)))))
707 (email (and (plist-get info :with-email)
708 (org-export-data (plist-get info :email) info)))
709 (from-data (org-e-groff--get-tagged-content "FROM" info))
710 (at-item (plist-get attr :author-title))
711 (to-data (org-e-groff--get-tagged-content "TO" info)))
714 ;; If FROM then get data from FROM
715 (if from-data
716 (setq from-data
717 (replace-regexp-in-string "\\.P\n" "" from-data))
718 (setq from-data ""))
720 (if to-data
721 (setq to-data
722 (replace-regexp-in-string "\\.P\n" "" to-data))
723 (setq from-data ""))
725 (concat
726 (cond
727 (from-data
728 (format ".WA \"%s\" \"%s\" \n%s\n.WE\n" author (or at-item "") from-data))
729 ((and author email (not (string= "" email)))
730 (format ".WA \"%s\"\n \"%s\"\n.WE\n" author email))
731 (author (format ".WA \"%s\"\n.WE\n" author))
732 (t ".WA \"\" \n.WE\n"))
734 ;; If TO then get data from TO
736 (when to-data
737 (format ".IA \n%s\n.IE\n" to-data)))))
740 ;;; Template
742 (defun org-e-groff-template (contents info)
743 "Return complete document string after Groff conversion.
744 CONTENTS is the transcoded contents string. INFO is a plist
745 holding export options."
746 (let* ((title (org-export-data (plist-get info :title) info))
747 (attr (read
748 (format "(%s)"
749 (mapconcat
750 #'identity
751 (list (plist-get info :groff-class-options))
752 " "))))
753 (class (plist-get info :groff-class))
754 (class-options (plist-get info :groff-class-options))
755 (classes (assoc class org-e-groff-classes))
756 (classes-options (car (last classes)))
757 (heading-option (plist-get classes-options :heading))
758 (type-option (plist-get classes-options :type))
759 (last-option (plist-get classes-options :last-section))
760 (hyphenate (plist-get attr :hyphenate))
761 (justify-right (plist-get attr :justify-right))
763 (document-class-string
764 (progn
765 (org-element-normalize-string
766 (let* ((header (nth 1 (assoc class org-e-groff-classes)))
767 (document-class-item (if (stringp header) header "")))
768 document-class-item)))))
771 (concat
772 (if justify-right
773 (case justify-right
774 ('yes ".SA 1 \n")
775 ('no ".SA 0 \n")
776 (t ""))
779 (if hyphenate
780 (case hyphenate
781 ('yes ".nr Hy 1 \n")
782 ('no ".nr Hy 0 \n")
783 (t ""))
786 (cond
787 ((string= type-option "custom") "")
789 ((and (stringp document-class-string)
790 (string= type-option "cover"))
792 (concat
793 (format ".COVER %s\n" document-class-string)
794 (org-e-groff--mt-head title contents attr info)
795 ".COVEND\n"))
797 ((string= type-option "memo")
798 (concat
799 (org-e-groff--mt-head title contents attr info)
800 document-class-string))
801 ((string= type-option "letter")
802 (concat
803 (org-e-groff--letter-head title contents attr info)
804 (let ((sa-item (plist-get attr :salutation))
805 (cn-item (plist-get attr :confidential))
806 (sj-item (plist-get attr :subject))
807 (rn-item (plist-get attr :reference))
808 (at-item (plist-get attr :attention)))
810 (concat
812 (if (stringp sa-item)
813 (format ".LO SA \"%s\" \n" sa-item)
814 ".LO SA\n")
816 (when cn-item
817 (if (stringp cn-item)
818 (format ".LO CN \"%s\"\n" cn-item)
819 ".LO CN\n"))
821 (when (and at-item (stringp at-item))
822 (format ".LO AT \"%s\" \n" at-item))
823 (when (and title rn-item)
824 (format ".LO RN \"%s\"\n" title))
826 (when (and sj-item (stringp sj-item))
827 (format ".LO SJ \"%s\" \n" sj-item))
830 ".LT " document-class-string "\n"))))
832 (t ""))
834 contents
836 (cond
837 ((string= last-option "toc")
838 ".TC")
839 ((string= last-option "sign")
840 (let ((fc-item (plist-get attr :closing)))
841 (concat (if (stringp fc-item)
842 (format ".FC \"%s\" \n" fc-item)
843 ".FC\n")
844 ".SG\n")))
845 (t ""))
847 (progn
848 (mapconcat
849 (lambda (item)
850 (when (string= (car item) "NS")
851 (replace-regexp-in-string
852 "\\.P\n" "" (cdr item))))
853 (reverse org-e-groff-special-content) "\n")))))
857 ;;; Transcode Functions
859 ;;; Babel Call
861 ;; Babel Calls are ignored.
864 ;;; Bold
866 (defun org-e-groff-bold (bold contents info)
867 "Transcode BOLD from Org to Groff.
868 CONTENTS is the text with bold markup. INFO is a plist holding
869 contextual information."
870 (org-e-groff--text-markup contents 'bold))
872 ;;; Center Block
874 (defun org-e-groff-center-block (center-block contents info)
875 "Transcode a CENTER-BLOCK element from Org to Groff.
876 CONTENTS holds the contents of the center block. INFO is a plist
877 holding contextual information."
878 (org-e-groff--wrap-label
879 center-block
880 (format ".DS C \n%s\n.DE" contents)))
882 ;;; Clock
884 (defun org-e-groff-clock (clock contents info)
885 "Transcode a CLOCK element from Org to Groff.
886 CONTENTS is nil. INFO is a plist holding contextual
887 information."
888 (concat
889 (format "\\fB%s\\fP " org-clock-string)
890 (format org-e-groff-inactive-timestamp-format
891 (concat (org-translate-time (org-element-property :value clock))
892 (let ((time (org-element-property :time clock)))
893 (and time (format " (%s)" time)))))))
895 ;;; Code
897 (defun org-e-groff-code (code contents info)
898 "Transcode a CODE object from Org to Groff.
899 CONTENTS is nil. INFO is a plist used as a communication
900 channel."
901 (org-e-groff--text-markup (org-element-property :value code) 'code))
903 ;;; Comments and Comment Blocks are ignored.
905 ;;; Drawer
907 (defun org-e-groff-drawer (drawer contents info)
908 "Transcode a DRAWER element from Org to Groff.
909 CONTENTS holds the contents of the block. INFO is a plist
910 holding contextual information."
911 (let* ((name (org-element-property :drawer-name drawer))
912 (output (if (functionp org-e-groff-format-drawer-function)
913 (funcall org-e-groff-format-drawer-function
914 name contents)
915 ;; If there's no user defined function: simply
916 ;; display contents of the drawer.
917 contents)))
918 (org-e-groff--wrap-label drawer output)))
920 ;;; Dynamic Block
922 (defun org-e-groff-dynamic-block (dynamic-block contents info)
923 "Transcode a DYNAMIC-BLOCK element from Org to Groff.
924 CONTENTS holds the contents of the block. INFO is a plist
925 holding contextual information. See `org-export-data'."
926 (org-e-groff--wrap-label dynamic-block contents))
928 ;;; Entity
930 (defun org-e-groff-entity (entity contents info)
931 "Transcode an ENTITY object from Org to Groff.
932 CONTENTS are the definition itself. INFO is a plist holding
933 contextual information."
934 (let ((ent (org-element-property :utf8 entity))) ent))
936 ;;; Example Block
938 (defun org-e-groff-example-block (example-block contents info)
939 "Transcode an EXAMPLE-BLOCK element from Org to Groff.
940 CONTENTS is nil. INFO is a plist holding contextual
941 information."
942 (org-e-groff--wrap-label
943 example-block
944 (format ".DS L\n%s\n.DE"
945 (org-export-format-code-default example-block info))))
947 ;;; Export Block
949 (defun org-e-groff-export-block (export-block contents info)
950 "Transcode a EXPORT-BLOCK element from Org to Groff.
951 CONTENTS is nil. INFO is a plist holding contextual information."
952 (when (string= (org-element-property :type export-block) "GROFF")
953 (org-remove-indentation (org-element-property :value export-block))))
955 ;;; Export Snippet
957 (defun org-e-groff-export-snippet (export-snippet contents info)
958 "Transcode a EXPORT-SNIPPET object from Org to Groff.
959 CONTENTS is nil. INFO is a plist holding contextual information."
960 (when (eq (org-export-snippet-backend export-snippet) 'e-groff)
961 (org-element-property :value export-snippet)))
963 ;;; Fixed Width
965 (defun org-e-groff-fixed-width (fixed-width contents info)
966 "Transcode a FIXED-WIDTH element from Org to Groff.
967 CONTENTS is nil. INFO is a plist holding contextual information."
968 (org-e-groff--wrap-label
969 fixed-width
970 (format "\\fC\n%s\\fP"
971 (org-remove-indentation
972 (org-element-property :value fixed-width)))))
974 ;;; Footnote Definition
976 ;; Footnote Definitions are ignored.
978 ;; Footnotes are handled automatically in GROFF. Although manual
979 ;; references can be added, not really required.
981 (defun org-e-groff-footnote-reference (footnote-reference contents info)
982 ;; Changing from info to footnote-reference
983 (let* ((raw (org-export-get-footnote-definition footnote-reference info))
984 (n (org-export-get-footnote-number footnote-reference info))
985 (data (org-trim (org-export-data raw info)))
986 (ref-id (plist-get (nth 1 footnote-reference) :label)))
987 ;; It is a reference
988 (if (string-match "fn:rl" ref-id)
989 (if (member ref-id org-e-groff-registered-references)
990 (format "\\*[%s]" ref-id)
991 (progn
992 (push ref-id org-e-groff-registered-references)
993 (format "\\*(Rf\n.RS \"%s\" \n%s\n.RF\n" ref-id data)))
994 ;; else it is a footnote
995 (format "\\u\\s-2%s\\d\\s+2\n.FS %s\n%s\n.FE\n" n n data))))
997 ;;; Headline
999 (defun org-e-groff-headline (headline contents info)
1000 "Transcode an HEADLINE element from Org to Groff.
1001 CONTENTS holds the contents of the headline. INFO is a plist
1002 holding contextual information."
1003 (let* ((class (plist-get info :groff-class))
1004 (level (org-export-get-relative-level headline info))
1005 (numberedp (org-export-numbered-headline-p headline info))
1006 ;; Section formatting will set two placeholders: one for the
1007 ;; title and the other for the contents.
1008 (classes (assoc class org-e-groff-classes))
1009 (classes-options (car (last classes)))
1010 (heading-option (plist-get classes-options :heading))
1011 (section-fmt
1012 (progn
1013 (cond
1014 ((and (symbolp heading-option)
1015 (fboundp heading-option))
1016 (funcall heading-option level numberedp))
1017 ((> level 7) nil)
1018 (t (if numberedp
1019 (concat ".H " (number-to-string level) " \"%s\"\n%s")
1020 ".HU \"%s\"\n%s")))))
1021 ;; End of section-fmt
1022 (text (org-export-data (org-element-property :title headline) info))
1023 (todo
1024 (and (plist-get info :with-todo-keywords)
1025 (let ((todo (org-element-property :todo-keyword headline)))
1026 (and todo (org-export-data todo info)))))
1027 (todo-type (and todo (org-element-property :todo-type headline)))
1028 (tags (and (plist-get info :with-tags)
1029 (org-export-get-tags headline info)))
1030 (priority (and (plist-get info :with-priority)
1031 (org-element-property :priority headline)))
1032 ;; Create the headline text along with a no-tag version. The
1033 ;; latter is required to remove tags from table of contents.
1034 (full-text (if (functionp org-e-groff-format-headline-function)
1035 ;; User-defined formatting function.
1036 (funcall org-e-groff-format-headline-function
1037 todo todo-type priority text tags)
1038 ;; Default formatting.
1039 (concat
1040 (when todo
1041 (format "\\fB%s\\fP " todo))
1042 (when priority (format " [\\#%c] " priority))
1043 text
1044 (when tags
1045 (format " \\fC:%s:\\fP "
1046 (mapconcat 'identity tags ":"))))))
1047 (full-text-no-tag
1048 (if (functionp org-e-groff-format-headline-function)
1049 ;; User-defined formatting function.
1050 (funcall org-e-groff-format-headline-function
1051 todo todo-type priority text nil)
1052 ;; Default formatting.
1053 (concat
1054 (when todo (format "\\fB%s\\fP " todo))
1055 (when priority (format " [\\#%c] " priority))
1056 text)))
1057 ;; Associate some \label to the headline for internal links.
1058 ;; (headline-label
1059 ;; (format "\\label{sec-%s}\n"
1060 ;; (mapconcat 'number-to-string
1061 ;; (org-export-get-headline-number headline info)
1062 ;; "-")))
1063 (headline-label "")
1064 (pre-blanks
1065 (make-string (org-element-property :pre-blank headline) 10)))
1067 (cond
1068 ;; Case 1: Special Tag
1069 ((member (car tags) org-e-groff-special-tags)
1070 (cond
1071 ((string= (car tags) "BODY") contents)
1073 ((string= (car tags) "NS")
1074 (progn
1075 (push (cons (car tags)
1076 (format ".NS \"%s\" 1 \n%s"
1077 (car (org-element-property :title headline))
1078 (or contents " ")))
1079 org-e-groff-special-content) nil))
1082 (progn
1083 (push (cons (car tags) contents) org-e-groff-special-content)
1084 nil))))
1086 ;; Case 2: This is a footnote section: ignore it.
1087 ((org-element-property :footnote-section-p headline) nil)
1089 ;; Case 3: This is a deep sub-tree: export it as a list item.
1090 ;; Also export as items headlines for which no section
1091 ;; format has been found.
1092 ((or (not section-fmt) (org-export-low-level-p headline info))
1093 ;; Build the real contents of the sub-tree.
1094 (let ((low-level-body
1095 (concat
1096 ;; If the headline is the first sibling, start a list.
1097 (when (org-export-first-sibling-p headline info)
1098 (format "%s\n" (if numberedp ".AL 1\n" ".DL \n")))
1099 ;; Itemize headline
1100 ".LI\n" full-text "\n" headline-label pre-blanks contents)))
1101 ;; If headline is not the last sibling simply return
1102 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1103 ;; blank line.
1104 (if (not (org-export-last-sibling-p headline info)) low-level-body
1105 (replace-regexp-in-string
1106 "[ \t\n]*\\'"
1107 (concat "\n.LE")
1108 low-level-body))))
1110 ;; Case 4. Standard headline. Export it as a section.
1112 (format section-fmt full-text
1113 (concat headline-label pre-blanks contents))))))
1115 ;;; Horizontal Rule
1116 ;; Not supported
1118 ;;; Inline Babel Call
1120 ;; Inline Babel Calls are ignored.
1122 ;;; Inline Src Block
1124 (defun org-e-groff-inline-src-block (inline-src-block contents info)
1125 "Transcode an INLINE-SRC-BLOCK element from Org to Groff.
1126 CONTENTS holds the contents of the item. INFO is a plist holding
1127 contextual information."
1128 (let* ((code (org-element-property :value inline-src-block)))
1129 (cond
1130 (org-e-groff-source-highlight
1131 (let* ((tmpdir (if (featurep 'xemacs)
1132 temp-directory
1133 temporary-file-directory))
1134 (in-file (make-temp-name
1135 (expand-file-name "srchilite" tmpdir)))
1136 (out-file (make-temp-name
1137 (expand-file-name "reshilite" tmpdir)))
1138 (org-lang (org-element-property :language inline-src-block))
1139 (lst-lang (cadr (assq (intern org-lang)
1140 org-e-groff-source-highlight-langs)))
1142 (cmd (concat (expand-file-name "source-highlight")
1143 " -s " lst-lang
1144 " -f groff_mm_color "
1145 " -i " in-file
1146 " -o " out-file)))
1147 (if lst-lang
1148 (let ((code-block ""))
1149 (with-temp-file in-file (insert code))
1150 (shell-command cmd)
1151 (setq code-block (org-file-contents out-file))
1152 (delete-file in-file)
1153 (delete-file out-file)
1154 code-block)
1155 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1156 code))))
1158 ;; Do not use a special package: transcode it verbatim.
1160 (concat ".DS I\n" "\\fC" code "\\fP\n.DE\n")))))
1162 ;;; Inlinetask
1164 (defun org-e-groff-inlinetask (inlinetask contents info)
1165 "Transcode an INLINETASK element from Org to Groff.
1166 CONTENTS holds the contents of the block. INFO is a plist
1167 holding contextual information."
1168 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1169 (todo (and (plist-get info :with-todo-keywords)
1170 (let ((todo (org-element-property :todo-keyword inlinetask)))
1171 (and todo (org-export-data todo info)))))
1172 (todo-type (org-element-property :todo-type inlinetask))
1173 (tags (and (plist-get info :with-tags)
1174 (org-export-get-tags inlinetask info)))
1175 (priority (and (plist-get info :with-priority)
1176 (org-element-property :priority inlinetask))))
1177 ;; If `org-e-groff-format-inlinetask-function' is provided, call it
1178 ;; with appropriate arguments.
1179 (if (functionp org-e-groff-format-inlinetask-function)
1180 (funcall org-e-groff-format-inlinetask-function
1181 todo todo-type priority title tags contents)
1182 ;; Otherwise, use a default template.
1183 (org-e-groff--wrap-label
1184 inlinetask
1185 (let ((full-title
1186 (concat
1187 (when todo (format "\\fB%s\\fP " todo))
1188 (when priority (format " [\\#%c] " priority))
1189 title
1190 (when tags (format " \\fC:%s:\\fP "
1191 (mapconcat 'identity tags ":"))))))
1192 (format (concat "\n.DS I\n"
1193 "%s\n"
1194 ".sp"
1195 "%s\n"
1196 ".DE")
1197 full-title contents))))))
1199 ;;; Italic
1201 (defun org-e-groff-italic (italic contents info)
1202 "Transcode ITALIC from Org to Groff.
1203 CONTENTS is the text with italic markup. INFO is a plist holding
1204 contextual information."
1205 (org-e-groff--text-markup contents 'italic))
1207 ;;; Item
1209 (defun org-e-groff-item (item contents info)
1210 "Transcode an ITEM element from Org to Groff.
1211 CONTENTS holds the contents of the item. INFO is a plist holding
1212 contextual information."
1213 (let* ((bullet (org-element-property :bullet item))
1214 (type (org-element-property
1215 :type (org-element-property :parent item)))
1216 (checkbox (case (org-element-property :checkbox item)
1217 (on "\\o'\\(sq\\(mu'")
1218 (off "\\(sq")
1219 (trans "\\o'\\(sq\\(mi'")))
1220 (tag (let ((tag (org-element-property :tag item)))
1221 ;; Check-boxes must belong to the tag.
1222 (and tag (format "%s"
1223 (concat checkbox
1224 (org-export-data tag info)))))))
1226 (cond
1227 ((or checkbox tag)
1228 (concat ".LI ""\"" (or tag (concat "\\ " checkbox)) "\""
1229 "\n"
1230 (org-trim (or contents " "))))
1231 ((eq type 'ordered)
1232 (concat ".LI"
1233 "\n"
1234 (org-trim (or contents " "))))
1236 (let* ((bullet (org-trim bullet))
1237 (marker (cond ((string= "-" bullet) "\\(em")
1238 ((string= "*" bullet) "\\(bu")
1239 (t "\\(dg"))))
1240 (concat ".LI " marker "\n"
1241 (org-trim (or contents " "))))))))
1243 ;;; Keyword
1245 (defun org-e-groff-keyword (keyword contents info)
1246 "Transcode a KEYWORD element from Org to Groff.
1247 CONTENTS is nil. INFO is a plist holding contextual information."
1248 (let ((key (org-element-property :key keyword))
1249 (value (org-element-property :value keyword)))
1250 (cond
1251 ((string= key "GROFF") value)
1252 (t nil))))
1254 ;;; Groff Environment
1256 (defun org-e-groff-groff-environment (groff-environment contents info)
1257 "Transcode a GROFF-ENVIRONMENT element from Org to Groff.
1258 CONTENTS is nil. INFO is a plist holding contextual information."
1259 (let ((label (org-element-property :name groff-environment))
1260 (value (org-remove-indentation
1261 (org-element-property :value groff-environment))))
1262 (if (not (org-string-nw-p label)) value
1263 ;; Environment is labelled: label must be within the environment
1264 ;; (otherwise, a reference pointing to that element will count
1265 ;; the section instead).
1266 (with-temp-buffer
1267 (insert value)
1268 (goto-char (point-min))
1269 (forward-line)
1270 (insert (format "%s\n" label))
1271 (buffer-string)))))
1273 ;;; Groff Fragment
1275 (defun org-e-groff-groff-fragment (groff-fragment contents info)
1276 "Transcode a GROFF-FRAGMENT object from Org to Groff.
1277 CONTENTS is nil. INFO is a plist holding contextual information."
1278 (org-element-property :value groff-fragment))
1280 ;;; Line Break
1282 (defun org-e-groff-line-break (line-break contents info)
1283 "Transcode a LINE-BREAK object from Org to Groff.
1284 CONTENTS is nil. INFO is a plist holding contextual information."
1285 ".br\n")
1287 ;;; Link
1288 ;; Inline images just place a call to .PSPIC or .PS/.PE
1289 ;; and load the graph.
1291 (defun org-e-groff-link--inline-image (link info)
1292 "Return Groff code for an inline image.
1293 LINK is the link pointing to the inline image. INFO is a plist
1294 used as a communication channel."
1295 (let* ((parent (org-export-get-parent-element link))
1296 (path (let ((raw-path (org-element-property :path link)))
1297 (if (not (file-name-absolute-p raw-path)) raw-path
1298 (expand-file-name raw-path))))
1299 (attr (read (format "(%s)"
1300 (mapconcat
1301 #'identity
1302 (org-element-property :attr_groff parent)
1303 " "))))
1304 (placement
1305 (case (plist-get attr :position)
1306 ('center "")
1307 ('left "-L")
1308 ('right "-R")
1309 (t "")))
1310 (width (or (plist-get attr :width) ""))
1311 (height (or (plist-get attr :height) ""))
1313 (disable-caption (plist-get attr :disable-caption))
1315 (caption
1316 (org-e-groff--caption/label-string
1317 (org-element-property :caption parent)
1318 (org-element-property :name parent)
1319 info)))
1321 ;; Now clear ATTR from any special keyword and set a default value
1322 ;; if nothing is left. Return proper string.
1323 (concat
1324 (cond
1325 ((and org-e-groff-raster-to-ps
1326 (or (string-match ".\.png$" path)
1327 (string-match ".\.jpg$" path)))
1328 (let ((eps-path (concat path ".eps")))
1329 (shell-command (format org-e-groff-raster-to-ps path eps-path))
1330 (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1331 placement eps-path width height)))
1332 ((string-match ".\.pic$" path)
1333 (format "\n.PS\ncopy \"%s\"\n.PE" path))
1334 (t (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1335 placement path width height)))
1336 (unless disable-caption (format "\n.FG \"%s\"" caption)))))
1338 (defun org-e-groff-link (link desc info)
1339 "Transcode a LINK object from Org to Groff.
1341 DESC is the description part of the link, or the empty string.
1342 INFO is a plist holding contextual information. See
1343 `org-export-data'."
1345 (let* ((type (org-element-property :type link))
1346 (raw-path (org-element-property :path link))
1347 ;; Ensure DESC really exists, or set it to nil.
1348 (desc (and (not (string= desc "")) desc))
1349 (imagep (org-export-inline-image-p
1350 link org-e-groff-inline-image-rules))
1351 (path (cond
1352 ((member type '("http" "https" "ftp" "mailto"))
1353 (concat type ":" raw-path))
1354 ((string= type "file")
1355 (when (string-match "\\(.+\\)::.+" raw-path)
1356 (setq raw-path (match-string 1 raw-path)))
1357 (if (file-name-absolute-p raw-path)
1358 (concat "file://" (expand-file-name raw-path))
1359 (concat "file://" raw-path)))
1360 (t raw-path)))
1361 protocol)
1362 (cond
1363 ;; Image file.
1364 (imagep (org-e-groff-link--inline-image link info))
1365 ;; import groff files
1366 ((and (string= type "file")
1367 (string-match ".\.groff$" raw-path))
1368 (concat ".so " raw-path "\n"))
1369 ;; Radio link: transcode target's contents and use them as link's
1370 ;; description.
1371 ((string= type "radio")
1372 (let ((destination (org-export-resolve-radio-link link info)))
1373 (when destination
1374 (format "\\fI [%s] \\fP"
1375 (org-export-solidify-link-text path)))))
1377 ;; Links pointing to an headline: find destination and build
1378 ;; appropriate referencing command.
1379 ((member type '("custom-id" "fuzzy" "id"))
1380 (let ((destination (if (string= type "fuzzy")
1381 (org-export-resolve-fuzzy-link link info)
1382 (org-export-resolve-id-link link info))))
1383 (case (org-element-type destination)
1384 ;; Id link points to an external file.
1385 (plain-text
1386 (if desc (format "%s \\fBat\\fP \\fIfile://%s\\fP" desc destination)
1387 (format "\\fI file://%s \\fP" destination)))
1388 ;; Fuzzy link points nowhere.
1389 ('nil
1390 (format org-e-groff-link-with-unknown-path-format
1391 (or desc
1392 (org-export-data
1393 (org-element-property :raw-link link) info))))
1394 ;; Fuzzy link points to an invisible target.
1395 (keyword nil)
1396 ;; LINK points to an headline. If headlines are numbered
1397 ;; and the link has no description, display headline's
1398 ;; number. Otherwise, display description or headline's
1399 ;; title.
1400 (headline
1401 (let ((label ""))
1402 (if (and (plist-get info :section-numbers) (not desc))
1403 (format "\\fI%s\\fP" label)
1404 (format "\\fI%s\\fP"
1405 (or desc
1406 (org-export-data
1407 (org-element-property :title destination) info))))))
1408 ;; Fuzzy link points to a target. Do as above.
1409 (otherwise
1410 (let ((path (org-export-solidify-link-text path)))
1411 (if (not desc) (format "\\fI%s\\fP" path)
1412 (format "%s \\fBat\\fP \\fI%s\\fP" desc path)))))))
1413 ;; External link with a description part.
1414 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
1415 ;; External link without a description part.
1416 (path (format "\\fI%s\\fP" path))
1417 ;; No path, only description. Try to do something useful.
1418 (t (format org-e-groff-link-with-unknown-path-format desc)))))
1420 ;;; Paragraph
1422 (defun org-e-groff-paragraph (paragraph contents info)
1423 "Transcode a PARAGRAPH element from Org to Groff.
1424 CONTENTS is the contents of the paragraph, as a string. INFO is
1425 the plist used as a communication channel."
1426 (let ((parent (plist-get (nth 1 paragraph) :parent)))
1427 (when parent
1428 (let* ((parent-type (car parent))
1429 (fixed-paragraph "")
1430 (class (plist-get info :groff-class))
1431 (class-options (plist-get info :groff-class-options))
1432 (classes (assoc class org-e-groff-classes))
1433 (classes-options (car (last classes)))
1434 (paragraph-option (plist-get classes-options :paragraph)))
1435 (cond
1436 ((and (symbolp paragraph-option)
1437 (fboundp paragraph-option))
1438 (funcall paragraph-option parent-type parent contents))
1439 ((and (eq parent-type 'item)
1440 (plist-get (nth 1 parent) :bullet))
1441 (setq fixed-paragraph (concat "" contents)))
1442 ((eq parent-type 'section)
1443 (setq fixed-paragraph (concat ".P\n" contents)))
1444 ((eq parent-type 'footnote-definition)
1445 (setq fixed-paragraph (concat "" contents)))
1446 (t (setq fixed-paragraph (concat "" contents))))
1447 fixed-paragraph))))
1449 ;;; Plain List
1451 (defun org-e-groff-plain-list (plain-list contents info)
1452 "Transcode a PLAIN-LIST element from Org to Groff.
1453 CONTENTS is the contents of the list. INFO is a plist holding
1454 contextual information."
1455 (let* ((type (org-element-property :type plain-list))
1456 (attr (mapconcat #'identity
1457 (org-element-property :attr_groff plain-list)
1458 " "))
1459 (groff-type (cond
1460 ((eq type 'ordered) ".AL")
1461 ((eq type 'unordered) ".BL")
1462 ((eq type 'descriptive) ".VL 2.0i"))))
1463 (org-e-groff--wrap-label
1464 plain-list
1465 (format "%s\n%s\n.LE" groff-type contents))))
1467 ;;; Plain Text
1469 (defun org-e-groff-plain-text (text info)
1470 "Transcode a TEXT string from Org to Groff.
1471 TEXT is the string to transcode. INFO is a plist holding
1472 contextual information."
1473 ;; Protect
1474 (setq text (replace-regexp-in-string
1475 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1476 "$\\" text nil t 1))
1477 ;; Handle quotation marks
1478 (setq text (org-e-groff--quotation-marks text info))
1479 ;; Handle Special Characters
1480 (if org-e-groff-special-char
1481 (dolist (special-char-list org-e-groff-special-char)
1482 (setq text
1483 (replace-regexp-in-string (car special-char-list)
1484 (cdr special-char-list) text))))
1485 ;; Handle break preservation if required.
1486 (when (plist-get info :preserve-breaks)
1487 (setq text (replace-regexp-in-string
1488 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" text)))
1489 ;; Return value.
1490 text)
1492 ;;; Planning
1494 (defun org-e-groff-planning (planning contents info)
1495 "Transcode a PLANNING element from Org to Groff.
1496 CONTENTS is nil. INFO is a plist holding contextual
1497 information."
1498 (concat
1499 (mapconcat
1500 'identity
1501 (delq nil
1502 (list
1503 (let ((closed (org-element-property :closed planning)))
1504 (when closed
1505 (concat
1506 (format "\\fR %s \\fP" org-closed-string)
1507 (format org-e-groff-inactive-timestamp-format
1508 (org-translate-time closed)))))
1509 (let ((deadline (org-element-property :deadline planning)))
1510 (when deadline
1511 (concat
1512 (format "\\fB %s \\fP" org-deadline-string)
1513 (format org-e-groff-active-timestamp-format
1514 (org-translate-time deadline)))))
1515 (let ((scheduled (org-element-property :scheduled planning)))
1516 (when scheduled
1517 (concat
1518 (format "\\fR %s \\fP" org-scheduled-string)
1519 (format org-e-groff-active-timestamp-format
1520 (org-translate-time scheduled)))))))
1522 ""))
1524 ;;; Property Drawer
1526 (defun org-e-groff-property-drawer (property-drawer contents info)
1527 "Transcode a PROPERTY-DRAWER element from Org to Groff.
1528 CONTENTS is nil. INFO is a plist holding contextual
1529 information."
1530 ;; The property drawer isn't exported but we want separating blank
1531 ;; lines nonetheless.
1534 ;;; Quote Block
1536 (defun org-e-groff-quote-block (quote-block contents info)
1537 "Transcode a QUOTE-BLOCK element from Org to Groff.
1538 CONTENTS holds the contents of the block. INFO is a plist
1539 holding contextual information."
1540 (org-e-groff--wrap-label
1541 quote-block
1542 (format ".DS I\n.I\n%s\n.R\n.DE" contents)))
1544 ;;; Quote Section
1546 (defun org-e-groff-quote-section (quote-section contents info)
1547 "Transcode a QUOTE-SECTION element from Org to Groff.
1548 CONTENTS is nil. INFO is a plist holding contextual information."
1549 (let ((value (org-remove-indentation
1550 (org-element-property :value quote-section))))
1551 (when value (format ".DS L\n\\fI%s\\fP\n.DE\n" value))))
1553 ;;; Radio Target
1555 (defun org-e-groff-radio-target (radio-target text info)
1556 "Transcode a RADIO-TARGET object from Org to Groff.
1557 TEXT is the text of the target. INFO is a plist holding
1558 contextual information."
1559 (format "%s - %s"
1560 (org-export-solidify-link-text
1561 (org-element-property :value radio-target))
1562 text))
1564 ;;; Section
1566 (defun org-e-groff-section (section contents info)
1567 "Transcode a SECTION element from Org to Groff.
1568 CONTENTS holds the contents of the section. INFO is a plist
1569 holding contextual information."
1570 contents)
1572 ;;; Special Block
1574 (defun org-e-groff-special-block (special-block contents info)
1575 "Transcode a SPECIAL-BLOCK element from Org to Groff.
1576 CONTENTS holds the contents of the block. INFO is a plist
1577 holding contextual information."
1578 (let ((type (downcase (org-element-property :type special-block))))
1579 (org-e-groff--wrap-label
1580 special-block
1581 (format "%s\n" contents))))
1583 ;;; Src Block
1585 (defun org-e-groff-src-block (src-block contents info)
1586 "Transcode a SRC-BLOCK element from Org to Groff.
1587 CONTENTS holds the contents of the item. INFO is a plist holding
1588 contextual information."
1589 (let* ((lang (org-element-property :language src-block))
1590 (caption (org-element-property :caption src-block))
1591 (label (org-element-property :name src-block))
1592 (code (org-element-property :value src-block))
1593 (custom-env (and lang
1594 (cadr (assq (intern lang)
1595 org-e-groff-custom-lang-environments))))
1596 (num-start (case (org-element-property :number-lines src-block)
1597 (continued (org-export-get-loc src-block info))
1598 (new 0)))
1599 (retain-labels (org-element-property :retain-labels src-block))
1600 (attr
1601 (read (format "(%s)"
1602 (mapconcat #'identity
1603 (org-element-property :attr_groff src-block)
1604 " "))))
1605 (disable-caption (plist-get attr :disable-caption)))
1607 (cond
1608 ;; Case 1. No source fontification.
1609 ((not org-e-groff-source-highlight)
1610 (let ((caption-str (org-e-groff--caption/label-string caption label info)))
1611 (concat
1612 (format ".DS I\n\\fC%s\\fP\n.DE\n"
1613 (org-export-format-code-default src-block info))
1614 (unless disable-caption (format ".EX \"%s\" " caption-str)))))
1616 ;; Case 2. Source fontification.
1617 (org-e-groff-source-highlight
1618 (let* ((tmpdir (if (featurep 'xemacs)
1619 temp-directory
1620 temporary-file-directory))
1621 (caption-str (org-e-groff--caption/label-string caption label info))
1622 (in-file (make-temp-name
1623 (expand-file-name "srchilite" tmpdir)))
1624 (out-file (make-temp-name
1625 (expand-file-name "reshilite" tmpdir)))
1627 (org-lang (org-element-property :language src-block))
1628 (lst-lang (cadr (assq (intern org-lang)
1629 org-e-groff-source-highlight-langs)))
1631 (cmd (concat "source-highlight"
1632 " -s " lst-lang
1633 " -f groff_mm_color "
1634 " -i " in-file
1635 " -o " out-file)))
1637 (concat
1638 (if lst-lang
1639 (let ((code-block ""))
1640 (with-temp-file in-file (insert code))
1641 (shell-command cmd)
1642 (setq code-block (org-file-contents out-file))
1643 (delete-file in-file)
1644 (delete-file out-file)
1645 (format "%s\n" code-block))
1646 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1647 code))
1648 (unless disable-caption (format ".EX \"%s\" " caption-str))))))))
1651 ;;; Statistics Cookie
1653 (defun org-e-groff-statistics-cookie (statistics-cookie contents info)
1654 "Transcode a STATISTICS-COOKIE object from Org to Groff.
1655 CONTENTS is nil. INFO is a plist holding contextual information."
1656 (org-element-property :value statistics-cookie))
1659 ;;; Strike-Through
1661 (defun org-e-groff-strike-through (strike-through contents info)
1662 "Transcode STRIKE-THROUGH from Org to Groff.
1663 CONTENTS is the text with strike-through markup. INFO is a plist
1664 holding contextual information."
1665 (org-e-groff--text-markup contents 'strike-through))
1667 ;;; Subscript
1669 (defun org-e-groff-subscript (subscript contents info)
1670 "Transcode a SUBSCRIPT object from Org to Groff.
1671 CONTENTS is the contents of the object. INFO is a plist holding
1672 contextual information."
1673 (format "\\d\\s-2%s\\s+2\\u" contents))
1675 ;;; Superscript "^_%s$
1677 (defun org-e-groff-superscript (superscript contents info)
1678 "Transcode a SUPERSCRIPT object from Org to Groff.
1679 CONTENTS is the contents of the object. INFO is a plist holding
1680 contextual information."
1681 (format "\\u\\s-2%s\\s+2\\d" contents))
1684 ;;; Table
1686 ;; `org-e-groff-table' is the entry point for table transcoding. It
1687 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1688 ;; delegates the job to `org-e-groff-table--org-table' function,
1689 ;; depending of the type of the table.
1691 ;; `org-e-groff-table--align-string' is a subroutine used to build
1692 ;; alignment string for Org tables.
1694 (defun org-e-groff-table (table contents info)
1695 "Transcode a TABLE element from Org to Groff.
1696 CONTENTS is the contents of the table. INFO is a plist holding
1697 contextual information."
1698 (cond
1699 ;; Case 1: verbatim table.
1700 ((or org-e-groff-tables-verbatim
1701 (let ((attr (read (format "(%s)"
1702 (mapconcat
1703 #'identity
1704 (org-element-property :attr_groff table) " ")))))
1705 (and attr (plist-get attr :verbatim))))
1707 (format ".DS L\n\\fC%s\\fP\n.DE"
1708 ;; Re-create table, without affiliated keywords.
1709 (org-trim
1710 (org-element-interpret-data
1711 `(table nil ,@(org-element-contents table))))))
1713 ;; Case 2: Standard table.
1714 (t (org-e-groff-table--org-table table contents info))))
1716 (defun org-e-groff-table--align-string (divider table info)
1717 "Return an appropriate Groff alignment string.
1718 TABLE is the considered table. INFO is a plist used as
1719 a communication channel."
1720 (let (alignment)
1721 ;; Extract column groups and alignment from first (non-rule)
1722 ;; row.
1723 (org-element-map
1724 (org-element-map
1725 table 'table-row
1726 (lambda (row)
1727 (and (eq (org-element-property :type row) 'standard) row))
1728 info 'first-match)
1729 'table-cell
1730 (lambda (cell)
1731 (let* ((borders (org-export-table-cell-borders cell info))
1732 (raw-width (org-export-table-cell-width cell info))
1733 (width-cm (when raw-width (/ raw-width 5)))
1734 (width (if raw-width (format "w(%dc)"
1735 (if (< width-cm 1) 1 width-cm)) "")))
1736 ;; Check left border for the first cell only.
1737 ;; Alignment is nil on assignment
1739 (when (and (memq 'left borders) (not alignment))
1740 (push "|" alignment))
1741 (push
1742 (case (org-export-table-cell-alignment cell info)
1743 (left (concat "l" width divider))
1744 (right (concat "r" width divider))
1745 (center (concat "c" width divider)))
1746 alignment)
1747 (when (memq 'right borders) (push "|" alignment))))
1748 info)
1749 (apply 'concat (reverse alignment))))
1751 (defun org-e-groff-table--org-table (table contents info)
1752 "Return appropriate Groff code for an Org table.
1754 TABLE is the table type element to transcode. CONTENTS is its
1755 contents, as a string. INFO is a plist used as a communication
1756 channel.
1758 This function assumes TABLE has `org' as its `:type' attribute."
1759 (let* ((label (org-element-property :name table))
1760 (caption (org-e-groff--caption/label-string
1761 (org-element-property :caption table) label info))
1762 (attr (read (format "(%s)"
1763 (mapconcat #'identity
1764 (org-element-property :attr_groff table)
1765 " "))))
1766 (divider (if (plist-get attr :divider) "|" " "))
1768 ;; Determine alignment string.
1769 (alignment (org-e-groff-table--align-string divider table info))
1771 ;; Extract others display options.
1773 (lines (org-split-string contents "\n"))
1775 (attr-list
1776 (let (result-list)
1777 (dolist (attr-item
1778 (list
1779 (if (plist-get attr :expand)
1780 "expand" nil)
1782 (case (plist-get attr :placement)
1783 ('center "center")
1784 ('left nil)
1786 (if org-e-groff-tables-centered
1787 "center" "")))
1789 (case (plist-get attr :boxtype)
1790 ('box "box")
1791 ('doublebox "doublebox")
1792 ('allbox "allbox")
1793 ('none nil)
1794 (t "box"))))
1796 (if (not (null attr-item))
1797 (add-to-list 'result-list attr-item)))
1798 result-list))
1800 (title-line (plist-get attr :title-line))
1801 (disable-caption (plist-get attr :disable-caption))
1802 (long-cells (plist-get attr :long-cells))
1804 (table-format
1805 (concat
1806 (format "%s"
1807 (or (car attr-list) ""))
1809 (let (output-list)
1810 (when (cdr attr-list)
1811 (dolist (attr-item (cdr attr-list))
1812 (setq output-list (concat output-list
1813 (format ",%s" attr-item)))))
1814 output-list) "")))
1815 (first-line
1816 (when lines (org-split-string (car lines) "\t"))))
1817 ;; Prepare the final format string for the table.
1820 (cond
1821 ;; Others.
1822 (lines
1823 (concat ".TS\n " table-format ";\n"
1824 (format "%s.\n"
1825 (let ((final-line ""))
1826 (when title-line
1827 (dotimes (i (length first-line))
1828 (setq final-line (concat final-line "cb" divider))))
1830 (setq final-line (concat final-line "\n"))
1832 (if alignment
1833 (setq final-line (concat final-line alignment))
1834 (dotimes (i (length first-line))
1835 (setq final-line (concat final-line "c" divider))))
1836 final-line))
1838 (format "%s.TE\n"
1839 (let ((final-line "")
1840 (long-line "")
1841 (lines (org-split-string contents "\n")))
1843 (dolist (line-item lines)
1844 (setq long-line "")
1846 (if long-cells
1847 (progn
1848 (if (string= line-item "_")
1849 (setq long-line (format "%s\n" line-item))
1850 ;; else string =
1851 (let ((cell-item-list (org-split-string line-item "\t")))
1852 (dolist (cell-item cell-item-list)
1854 (cond ((eq cell-item (car (last cell-item-list)))
1855 (setq long-line (concat long-line
1856 (format "T{\n%s\nT}\t\n" cell-item))))
1858 (setq long-line (concat long-line
1859 (format "T{\n%s\nT}\t" cell-item))))))
1860 long-line))
1861 ;; else long cells
1862 (setq final-line (concat final-line long-line)))
1864 (setq final-line (concat final-line line-item "\n"))))
1865 final-line))
1867 (if (not disable-caption)
1868 (format ".TB \"%s\""
1869 caption) ""))))))
1871 ;;; Table Cell
1873 (defun org-e-groff-table-cell (table-cell contents info)
1874 "Transcode a TABLE-CELL element from Org to Groff
1875 CONTENTS is the cell contents. INFO is a plist used as
1876 a communication channel."
1877 (progn
1878 (concat (if (and contents
1879 org-e-groff-table-scientific-notation
1880 (string-match orgtbl-exp-regexp contents))
1881 ;; Use appropriate format string for scientific
1882 ;; notation.
1883 (format org-e-groff-table-scientific-notation
1884 (match-string 1 contents)
1885 (match-string 2 contents))
1886 contents)
1887 (when (org-export-get-next-element table-cell info) "\t"))))
1890 ;;; Table Row
1892 (defun org-e-groff-table-row (table-row contents info)
1893 "Transcode a TABLE-ROW element from Org to Groff
1894 CONTENTS is the contents of the row. INFO is a plist used as
1895 a communication channel."
1896 ;; Rules are ignored since table separators are deduced from
1897 ;; borders of the current row.
1898 (when (eq (org-element-property :type table-row) 'standard)
1899 (let* ((attr (mapconcat 'identity
1900 (org-element-property
1901 :attr_groff (org-export-get-parent table-row))
1902 " "))
1903 ;; TABLE-ROW's borders are extracted from its first cell.
1904 (borders
1905 (org-export-table-cell-borders
1906 (car (org-element-contents table-row)) info)))
1907 (concat
1908 ;; Mark horizontal lines
1909 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1910 contents
1911 (cond
1912 ;; When BOOKTABS are activated enforce bottom rule even when
1913 ;; no hline was specifically marked.
1914 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1915 ((memq 'below borders) "\n_"))))))
1917 ;;; Target
1919 (defun org-e-groff-target (target contents info)
1920 "Transcode a TARGET object from Org to Groff.
1921 CONTENTS is nil. INFO is a plist holding contextual
1922 information."
1923 (format "\\fI%s\\fP"
1924 (org-export-solidify-link-text (org-element-property :value target))))
1926 ;;; Timestamp
1928 (defun org-e-groff-timestamp (timestamp contents info)
1929 "Transcode a TIMESTAMP object from Org to Groff.
1930 CONTENTS is nil. INFO is a plist holding contextual
1931 information."
1932 (let ((value (org-translate-time (org-element-property :value timestamp)))
1933 (type (org-element-property :type timestamp)))
1934 (cond ((memq type '(active active-range))
1935 (format org-e-groff-active-timestamp-format value))
1936 ((memq type '(inactive inactive-range))
1937 (format org-e-groff-inactive-timestamp-format value))
1938 (t (format org-e-groff-diary-timestamp-format value)))))
1940 ;;; Underline
1942 (defun org-e-groff-underline (underline contents info)
1943 "Transcode UNDERLINE from Org to Groff.
1944 CONTENTS is the text with underline markup. INFO is a plist
1945 holding contextual information."
1946 (org-e-groff--text-markup contents 'underline))
1948 ;;; Verbatim
1950 (defun org-e-groff-verbatim (verbatim contents info)
1951 "Transcode a VERBATIM object from Org to Groff.
1952 CONTENTS is nil. INFO is a plist used as a communication
1953 channel."
1954 (org-e-groff--text-markup (org-element-property :value verbatim) 'verbatim))
1956 ;;; Verse Block
1958 (defun org-e-groff-verse-block (verse-block contents info)
1959 "Transcode a VERSE-BLOCK element from Org to Groff.
1960 CONTENTS is verse block contents. INFO is a plist holding
1961 contextual information."
1962 (format ".DS C\n.ft HI\n%s\n.ft\n.DE" contents))
1965 ;;; Interactive functions
1967 (defun org-e-groff-export-to-groff
1968 (&optional subtreep visible-only body-only ext-plist pub-dir)
1969 "Export current buffer to a Groff file.
1971 If narrowing is active in the current buffer, only export its
1972 narrowed part.
1974 If a region is active, export that region.
1976 When optional argument SUBTREEP is non-nil, export the sub-tree
1977 at point, extracting information from the headline properties
1978 first.
1980 When optional argument VISIBLE-ONLY is non-nil, don't export
1981 contents of hidden elements.
1983 EXT-PLIST, when provided, is a property list with external
1984 parameters overriding Org default settings, but still inferior to
1985 file-local settings.
1987 When optional argument PUB-DIR is set, use it as the publishing
1988 directory.
1990 Return output file's name."
1991 (interactive)
1992 (setq org-e-groff-registered-references nil)
1993 (setq org-e-groff-special-content nil)
1994 (let ((outfile (org-export-output-file-name ".groff" subtreep pub-dir)))
1995 (org-export-to-file
1996 'e-groff outfile subtreep visible-only body-only ext-plist)))
1998 (defun org-e-groff-export-to-pdf
1999 (&optional subtreep visible-only body-only ext-plist pub-dir)
2000 "Export current buffer to Groff then process through to PDF.
2002 If narrowing is active in the current buffer, only export its
2003 narrowed part.
2005 If a region is active, export that region.
2007 When optional argument SUBTREEP is non-nil, export the sub-tree
2008 at point, extracting information from the headline properties
2009 first.
2011 When optional argument VISIBLE-ONLY is non-nil, don't export
2012 contents of hidden elements.
2014 EXT-PLIST, when provided, is a property list with external
2015 parameters overriding Org default settings, but still inferior to
2016 file-local settings.
2018 When optional argument PUB-DIR is set, use it as the publishing
2019 directory.
2021 Return PDF file's name."
2022 (interactive)
2023 (org-e-groff-compile
2024 (org-e-groff-export-to-groff
2025 subtreep visible-only body-only ext-plist pub-dir)))
2027 (defun org-e-groff-compile (grofffile)
2028 "Compile a Groff file.
2030 GROFFFILE is the name of the file being compiled. Processing is
2031 done through the command specified in `org-e-groff-pdf-process'.
2033 Return PDF file name or an error if it couldn't be produced."
2034 (let* ((wconfig (current-window-configuration))
2035 (grofffile (file-truename grofffile))
2036 (base (file-name-sans-extension grofffile))
2037 errors)
2038 (message (format "Processing Groff file %s ..." grofffile))
2039 (unwind-protect
2040 (progn
2041 (cond
2042 ;; A function is provided: Apply it.
2043 ((functionp org-e-groff-pdf-process)
2044 (funcall org-e-groff-pdf-process (shell-quote-argument grofffile)))
2045 ;; A list is provided: Replace %b, %f and %o with appropriate
2046 ;; values in each command before applying it. Output is
2047 ;; redirected to "*Org PDF Groff Output*" buffer.
2048 ((consp org-e-groff-pdf-process)
2049 (let* ((out-dir (or (file-name-directory grofffile) "./"))
2050 (outbuf (get-buffer-create "*Org PDF Groff Output*")))
2051 (mapc
2052 (lambda (command)
2053 (shell-command
2054 (replace-regexp-in-string
2055 "%b" (shell-quote-argument base)
2056 (replace-regexp-in-string
2057 "%f" (shell-quote-argument grofffile)
2058 (replace-regexp-in-string
2059 "%o" (shell-quote-argument out-dir) command t t)
2060 t t) t t)
2061 outbuf))
2062 org-e-groff-pdf-process)
2063 ;; Collect standard errors from output buffer.
2064 (setq errors (org-e-groff-collect-errors outbuf))))
2065 (t (error "No valid command to process to PDF")))
2066 (let ((pdffile (concat base ".pdf")))
2067 ;; Check for process failure. Provide collected errors if
2068 ;; possible.
2069 (if (not (file-exists-p pdffile))
2070 (error (concat (format "PDF file %s wasn't produced" pdffile)
2071 (when errors (concat ": " errors))))
2072 ;; Else remove log files, when specified, and signal end of
2073 ;; process to user, along with any error encountered.
2074 (when org-e-groff-remove-logfiles
2075 (dolist (ext org-e-groff-logfiles-extensions)
2076 (let ((file (concat base "." ext)))
2077 (when (file-exists-p file) (delete-file file)))))
2078 (message (concat "Process completed"
2079 (if (not errors) "."
2080 (concat " with errors: " errors)))))
2081 ;; Return output file name.
2082 pdffile))
2083 (set-window-configuration wconfig))))
2085 (defun org-e-groff-collect-errors (buffer)
2086 "Collect some kind of errors from \"groff\" output
2087 BUFFER is the buffer containing output.
2088 Return collected error types as a string, or nil if there was
2089 none."
2090 (with-current-buffer buffer
2091 (save-excursion
2092 (goto-char (point-max))
2093 ;; Find final run
2094 nil)))
2097 (provide 'org-e-groff)
2098 ;;; org-e-groff.el ends here