Export back-ends: Update timestamp export
[org-mode.git] / contrib / lisp / org-e-groff.el
blob7fe9eb7ca2a237473c1084d87edaff393628cfca
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))
41 (require 'org-export)
43 (defvar orgtbl-exp-regexp)
46 ;;; Define Back-End
48 (org-export-define-backend e-groff
49 ((bold . org-e-groff-bold)
50 (center-block . org-e-groff-center-block)
51 (clock . org-e-groff-clock)
52 (code . org-e-groff-code)
53 (drawer . org-e-groff-drawer)
54 (dynamic-block . org-e-groff-dynamic-block)
55 (entity . org-e-groff-entity)
56 (example-block . org-e-groff-example-block)
57 (export-block . org-e-groff-export-block)
58 (export-snippet . org-e-groff-export-snippet)
59 (fixed-width . org-e-groff-fixed-width)
60 (footnote-definition . org-e-groff-footnote-definition)
61 (footnote-reference . org-e-groff-footnote-reference)
62 (headline . org-e-groff-headline)
63 (horizontal-rule . org-e-groff-horizontal-rule)
64 (inline-src-block . org-e-groff-inline-src-block)
65 (inlinetask . org-e-groff-inlinetask)
66 (italic . org-e-groff-italic)
67 (item . org-e-groff-item)
68 (keyword . org-e-groff-keyword)
69 ;; (latex-environment . org-e-groff-latex-environment)
70 ;; (latex-fragment . org-e-groff-latex-fragment)
71 (line-break . org-e-groff-line-break)
72 (link . org-e-groff-link)
73 (paragraph . org-e-groff-paragraph)
74 (plain-list . org-e-groff-plain-list)
75 (plain-text . org-e-groff-plain-text)
76 (planning . org-e-groff-planning)
77 (property-drawer . org-e-groff-property-drawer)
78 (quote-block . org-e-groff-quote-block)
79 (quote-section . org-e-groff-quote-section)
80 (radio-target . org-e-groff-radio-target)
81 (section . org-e-groff-section)
82 (special-block . org-e-groff-special-block)
83 (src-block . org-e-groff-src-block)
84 (statistics-cookie . org-e-groff-statistics-cookie)
85 (strike-through . org-e-groff-strike-through)
86 (subscript . org-e-groff-subscript)
87 (superscript . org-e-groff-superscript)
88 (table . org-e-groff-table)
89 (table-cell . org-e-groff-table-cell)
90 (table-row . org-e-groff-table-row)
91 (target . org-e-groff-target)
92 (template . org-e-groff-template)
93 (timestamp . org-e-groff-timestamp)
94 (underline . org-e-groff-underline)
95 (verbatim . org-e-groff-verbatim)
96 (verse-block . org-e-groff-verse-block))
97 :export-block "GROFF"
98 :menu-entry
99 (?g "Export to GROFF"
100 ((?g "As GROFF file" org-e-groff-export-to-groff)
101 (?p "As PDF file" org-e-groff-export-to-pdf)
102 (?o "As PDF file and open"
103 (lambda (s v b) (org-open-file (org-e-groff-export-to-pdf s v b))))))
104 :options-alist
105 ((:date "DATE" nil org-e-groff-date-format t)
106 (:groff-class "GROFF_CLASS" nil org-e-groff-default-class t)
107 (:groff-class-options "GROFF_CLASS_OPTIONS" nil nil t)
108 (:groff-header-extra "GROFF_HEADER" nil nil newline)))
112 ;;; User Configurable Variables
114 (defgroup org-export-e-groff nil
115 "Options for exporting Org mode files to Groff."
116 :tag "Org Export Groff"
117 :group 'org-export)
119 ;;; Preamble
121 (defcustom org-e-groff-default-class "internal"
122 "The default Groff class."
123 :group 'org-export-e-groff
124 :type '(string :tag "Groff class"))
126 (defcustom org-e-groff-classes
127 '(("file" ".MT 1"
128 (:heading 'default :type "memo" :last-section "toc"))
129 ("internal" ".MT 0"
130 (:heading 'default :type "memo" :last-section "toc"))
131 ("programmer" ".MT 2"
132 (:heading 'default :type "memo" :last-section "toc"))
133 ("engineer" ".MT 3"
134 (:heading 'default :type "memo" :last-section "toc"))
135 ("external" ".MT 4"
136 (:heading 'default :type "memo" :last-section "toc"))
137 ("letter" ".MT 5"
138 (:heading 'default :type "memo" :last-section "sign"))
139 ("custom" ".so file"
140 (:heading custom-function :type "custom" :last-section "toc"))
141 ("dummy" ""
142 (:heading 'default :type "memo"))
143 ("ms" "ms"
144 (:heading 'default :type "cover" :last-section "toc"))
145 ("se_ms" "se_ms"
146 (:heading 'default :type "cover" :last-section "toc"))
147 ("block" "BL"
148 (:heading 'default :type "letter" :last-section "sign"))
149 ("semiblock" "SB"
150 (:heading 'default :type "letter" :last-section "sign"))
151 ("fullblock" "FB"
152 (:heading 'default :type "letter" :last-section "sign"))
153 ("simplified" "SP"
154 (:heading 'default :type "letter" :last-section "sign"))
155 ("none" "" (:heading 'default :type "custom")))
157 ;; none means, no Cover or Memorandum Type and no calls to AU, AT, ND and TL
158 ;; This is to facilitate the creation of custom pages.
160 ;; dummy means, no Cover or Memorandum Type but calls to AU, AT, ND and TL
161 ;; are made. This is to facilitate Abstract Insertion.
163 "This list describes the attributes for the documents being created.
164 It allows for the creation of new "
165 :group 'org-export-e-groff
166 :type '(repeat
167 (list (string :tag "Document Type")
168 (string :tag "Header")
169 (repeat :tag "Options" :inline t
170 (choice
171 (list :tag "Heading")
172 (function :tag "Hook computing sectioning"))))))
175 (defcustom org-e-groff-date-format
176 (format-time-string "%Y-%m-%d")
177 "Format string for .ND "
178 :group 'org-export-e-groff
179 :type 'boolean)
181 ;;; Headline
183 (defconst org-e-groff-special-tags
184 '("FROM" "TO" "ABSTRACT" "APPENDIX" "BODY" "NS"))
186 (defcustom org-e-groff-format-headline-function nil
187 "Function to format headline text.
189 This function will be called with 5 arguments:
190 TODO the todo keyword (string or nil).
191 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
192 PRIORITY the priority of the headline (integer or nil)
193 TEXT the main headline text (string).
194 TAGS the tags as a list of strings (list of strings or nil).
196 The function result will be used in the section format string.
198 As an example, one could set the variable to the following, in
199 order to reproduce the default set-up:
201 \(defun org-e-groff-format-headline (todo todo-type priority text tags)
202 \"Default format function for an headline.\"
203 \(concat (when todo
204 \(format \"\\fB%s\\fP \" todo))
205 \(when priority
206 \(format \"[\\#%c] \" priority))
207 text
208 \(when tags
209 \(format \" %s \"
210 \(mapconcat 'identity tags \":\"))))"
211 :group 'org-export-e-groff
212 :type 'function)
214 ;;; Timestamps
216 (defcustom org-e-groff-active-timestamp-format "\\fI%s\\fP"
217 "A printf format string to be applied to active timestamps."
218 :group 'org-export-e-groff
219 :type 'string)
221 (defcustom org-e-groff-inactive-timestamp-format "\\fI%s\\fP"
222 "A printf format string to be applied to inactive timestamps."
223 :group 'org-export-e-groff
224 :type 'string)
226 (defcustom org-e-groff-diary-timestamp-format "\\fI%s\\fP"
227 "A printf format string to be applied to diary timestamps."
228 :group 'org-export-e-groff
229 :type 'string)
231 ;;; Links
233 (defcustom org-e-groff-inline-image-rules
234 '(("file" . "\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'")
235 ("fuzzy" . "\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'"))
236 "Rules characterizing image files that can be inlined into Groff.
238 A rule consists in an association whose key is the type of link
239 to consider, and value is a regexp that will be matched against
240 link's path.
242 Note that, by default, the image extensions actually allowed
243 depend on the way the Groff file is processed. When used with
244 pdfgroff, pdf, jpg and png images are OK. When processing
245 through dvi to Postscript, only ps and eps are allowed. The
246 default we use here encompasses both."
247 :group 'org-export-e-groff
248 :type '(alist :key-type (string :tag "Type")
249 :value-type (regexp :tag "Path")))
251 (defcustom org-e-groff-link-with-unknown-path-format "\\fI%s\\fP"
252 "Format string for links with unknown path type."
253 :group 'org-export-groff
254 :type 'string)
256 ;;; Tables
258 (defcustom org-e-groff-tables-centered t
259 "When non-nil, tables are exported in a center environment."
260 :group 'org-export-e-groff
261 :type 'boolean)
263 (defcustom org-e-groff-tables-verbatim nil
264 "When non-nil, tables are exported verbatim."
265 :group 'org-export-e-groff
266 :type 'boolean)
268 (defcustom org-e-groff-table-scientific-notation "%sE%s"
269 "Format string to display numbers in scientific notation.
270 The format should have \"%s\" twice, for mantissa and exponent
271 \(i.e. \"%s\\\\times10^{%s}\").
273 When nil, no transformation is made."
274 :group 'org-export-e-groff
275 :type '(choice
276 (string :tag "Format string")
277 (const :tag "No formatting")))
279 ;;; Text markup
281 (defcustom org-e-groff-text-markup-alist
282 '((bold . "\\fB%s\\fP")
283 (code . "\\fC%s\\fP")
284 (italic . "\\fI%s\\fP")
285 (strike-through . "\\fC%s\\fP") ; Strike through and underline
286 (underline . "\\fI%s\\fP") ; need to be revised.
287 (verbatim . "protectedtexttt"))
288 "Alist of Groff expressions to convert text markup.
290 The key must be a symbol among `bold', `code', `italic',
291 `strike-through', `underline' and `verbatim'. The value is
292 a formatting string to wrap fontified text with it.
294 If no association can be found for a given markup, text will be
295 returned as-is."
296 :group 'org-export-e-groff
297 :type 'alist
298 :options '(bold code italic strike-through underline verbatim))
300 ;;; Drawers
302 (defcustom org-e-groff-format-drawer-function nil
303 "Function called to format a drawer in Groff code.
305 The function must accept two parameters:
306 NAME the drawer name, like \"LOGBOOK\"
307 CONTENTS the contents of the drawer.
309 The function should return the string to be exported.
311 For example, the variable could be set to the following function
312 in order to mimic default behaviour:
314 \(defun org-e-groff-format-drawer-default \(name contents\)
315 \"Format a drawer element for Groff export.\"
316 contents\)"
317 :group 'org-export-e-groff
318 :type 'function)
320 ;;; Inlinetasks
322 (defcustom org-e-groff-format-inlinetask-function nil
323 "Function called to format an inlinetask in Groff code.
325 The function must accept six parameters:
326 TODO the todo keyword, as a string
327 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
328 PRIORITY the inlinetask priority, as a string
329 NAME the inlinetask name, as a string.
330 TAGS the inlinetask tags, as a list of strings.
331 CONTENTS the contents of the inlinetask, as a string.
333 The function should return the string to be exported.
335 For example, the variable could be set to the following function
336 in order to mimic default behaviour:
338 \(defun org-e-groff-format-inlinetask \(todo type priority name tags contents\)
339 \"Format an inline task element for Groff export.\"
340 \(let ((full-title
341 \(concat
342 \(when todo
343 \(format \"\\fB%s\\fP \" todo))
344 \(when priority (format \"[\\#%c] \" priority))
345 title
346 \(when tags
347 \(format \":%s:\"
348 \(mapconcat 'identity tags \":\")))))
349 \(format (concat \".DS L\\n\"
350 \"%s\\n\\n\"
351 \"%s\"
352 \".DE\")
353 full-title contents))"
354 :group 'org-export-e-groff
355 :type 'function)
357 ;; Src blocks
359 (defcustom org-e-groff-source-highlight nil
360 "Use GNU source highlight to embellish source blocks "
361 :group 'org-export-e-groff
362 :type 'boolean)
364 (defcustom org-e-groff-source-highlight-langs
365 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
366 (scheme "scheme")
367 (c "c") (cc "cpp") (csharp "csharp") (d "d")
368 (fortran "fortran") (cobol "cobol") (pascal "pascal")
369 (ada "ada") (asm "asm")
370 (perl "perl") (cperl "perl")
371 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
372 (java "java") (javascript "javascript")
373 (tex "latex")
374 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
375 (ocaml "caml") (caml "caml")
376 (sql "sql") (sqlite "sql")
377 (html "html") (css "css") (xml "xml")
378 (bat "bat") (bison "bison") (clipper "clipper")
379 (ldap "ldap") (opa "opa")
380 (php "php") (postscript "postscript") (prolog "prolog")
381 (properties "properties") (makefile "makefile")
382 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
383 "Alist mapping languages to their listing language counterpart.
384 The key is a symbol, the major mode symbol without the \"-mode\".
385 The value is the string that should be inserted as the language
386 parameter for the listings package. If the mode name and the
387 listings name are the same, the language does not need an entry
388 in this list - but it does not hurt if it is present."
389 :group 'org-export-e-groff
390 :type '(repeat
391 (list
392 (symbol :tag "Major mode ")
393 (string :tag "Listings language"))))
395 (defcustom org-e-groff-source-highlight-options nil
396 "Association list of options for the groff listings package.
398 These options are supplied as a comma-separated list to the
399 \\lstset command. Each element of the association list should be
400 a list containing two strings: the name of the option, and the
401 value. For example,
403 (setq org-e-groff-source-highlight-options
404 '((\"basicstyle\" \"\\small\")
405 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
407 will typeset the code in a small size font with underlined, bold
408 black keywords.
410 Note that the same options will be applied to blocks of all
411 languages."
412 :group 'org-export-e-groff
413 :type '(repeat
414 (list
415 (string :tag "Listings option name ")
416 (string :tag "Listings option value"))))
418 (defvar org-e-groff-custom-lang-environments nil
419 "Alist mapping languages to language-specific Groff environments.
421 It is used during export of src blocks by the listings and
422 groff packages. For example,
424 \(setq org-e-groff-custom-lang-environments
425 '\(\(python \"pythoncode\"\)\)\)
427 would have the effect that if org encounters begin_src python
428 during groff export it will use pythoncode as the source-highlight
429 language.")
431 ;;; Plain text
433 (defcustom org-e-groff-special-char
434 '(("(c)" . "\\\\(co")
435 ("(tm)" . "\\\\(tm")
436 ("(rg)" . "\\\\(rg"))
437 "CONS list in which the value of the car
438 is replace on the value of the CDR. "
439 :group 'org-export-e-groff
440 :type '(list
441 (cons :tag "Character Subtitute"
442 (string :tag "Original Character Group")
443 (string :tag "Replacement Character"))))
445 ;;; Compilation
447 (defcustom org-e-groff-pdf-process
448 '("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
449 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
450 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf")
452 "Commands to process a Groff file to a PDF file.
453 This is a list of strings, each of them will be given to the
454 shell as a command. %f in the command will be replaced by the
455 full file name, %b by the file base name \(i.e. without
456 extension) and %o by the base directory of the file."
457 :group 'org-export-pdf
458 :type '(choice
459 (repeat :tag "Shell command sequence"
460 (string :tag "Shell command"))
461 (const :tag "2 runs of pdfgroff"
462 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
463 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
464 (const :tag "3 runs of pdfgroff"
465 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
466 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
467 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
468 (function)))
470 (defcustom org-e-groff-logfiles-extensions
471 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
472 "The list of file extensions to consider as Groff logfiles."
473 :group 'org-export-e-groff
474 :type '(repeat (string :tag "Extension")))
476 (defcustom org-e-groff-remove-logfiles t
477 "Non-nil means remove the logfiles produced by PDF production.
478 These are the .aux, .log, .out, and .toc files."
479 :group 'org-export-e-groff
480 :type 'boolean)
482 (defcustom org-e-groff-organization "Org User"
483 "Name of the organization used to populate the .AF command."
484 :group 'org-export-e-groff
485 :type 'string)
487 (defcustom org-e-groff-raster-to-ps nil
488 "Command used to convert raster to EPS. Nil for no conversion. Make sure that
489 `org-e-groff-inline-image-rules' is adjusted accordingly if not conversion is being
490 done. In this case, remove the entries for jpg and png in the file and fuzzy lists."
491 :group 'org-export-e-groff
492 :type '(choice
493 (repeat :tag "Shell Command Sequence" (string :tag "Shell Command"))
494 (const :tag "sam2p" "a=%s;b=%s;sam2p ${a} ${b} ;grep -v BeginData ${b} > b_${b};mv b_${b} ${b}" )
495 (const :tag "NetPNM" "a=%s;b=%s;pngtopnm ${a} | pnmtops -noturn > ${b}" )
496 (const :tag "None" nil)))
498 (defvar org-e-groff-registered-references nil)
499 (defvar org-e-groff-special-content nil)
503 ;;; Internal Functions
505 (defun org-e-groff--caption/label-string (element info)
506 "Return caption and label Groff string for ELEMENT.
508 INFO is a plist holding contextual information. If there's no
509 caption nor label, return the empty string.
511 For non-floats, see `org-e-groff--wrap-label'."
512 (let ((main (org-export-get-caption element))
513 (short (org-export-get-caption element t))
514 (label (org-element-property :name element)))
515 (cond ((and (not main) (not label)) "")
516 ((not main) (format "\\fI%s\\fP" label))
517 ;; Option caption format with short name.
518 (short (format "%s\n.br\n - %s\n"
519 (org-export-data short info)
520 (org-export-data main info)))
521 ;; Standard caption format.
522 (t (format "\\fR%s\\fP" (org-export-data main info))))))
524 (defun org-e-groff--wrap-label (element output)
525 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
526 This function shouldn't be used for floats. See
527 `org-e-groff--caption/label-string'."
528 (let ((label (org-element-property :name element)))
529 (if (or (not output) (not label) (string= output "") (string= label ""))
530 output
531 (concat (format "%s\n.br\n" label) output))))
533 (defun org-e-groff--text-markup (text markup)
534 "Format TEXT depending on MARKUP text markup.
535 See `org-e-groff-text-markup-alist' for details."
536 (let ((fmt (cdr (assq markup org-e-groff-text-markup-alist))))
537 (cond
538 ;; No format string: Return raw text.
539 ((not fmt) text)
540 ((string= "protectedtexttt" fmt)
541 (let ((start 0)
542 (trans '(("\\" . "\\")))
543 (rtn "")
544 char)
545 (while (string-match "[\\{}$%&_#~^]" text)
546 (setq char (match-string 0 text))
547 (if (> (match-beginning 0) 0)
548 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
549 (setq text (substring text (1+ (match-beginning 0))))
550 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
551 rtn (concat rtn char)))
552 (setq text (concat rtn text))
553 (format "\\fC%s\\fP" text)))
554 ;; Else use format string.
555 (t (format fmt text)))))
558 (defun org-e-groff--get-tagged-content (tag info)
559 (cdr (assoc tag org-e-groff-special-content)))
561 (defun org-e-groff--mt-head (title contents attr info)
562 (concat
564 ;; 1. Insert Organization
565 (let ((firm-option (plist-get attr :firm)))
566 (cond
567 ((stringp firm-option)
568 (format ".AF \"%s\" \n" firm-option))
569 (t (format ".AF \"%s\" \n" (or org-e-groff-organization "")))))
571 ;; 2. Title
572 (let ((subtitle1 (plist-get attr :subtitle1))
573 (subtitle2 (plist-get attr :subtitle2)))
575 (cond
576 ((string= "" title)
577 (format ".TL \"%s\" \"%s\" \n%s\n"
578 (or subtitle1 "")
579 (or subtitle2 "") " "))
581 ((not (or subtitle1 subtitle2))
582 (format ".TL\n%s\n"
583 (or title "")))
585 (format ".TL \"%s\" \"%s \" \n%s\n"
586 (or subtitle1 "")
587 (or subtitle2 "") title))))
589 ;; 3. Author.
590 ;; In Groff, .AU *MUST* be placed after .TL
591 ;; If From, populate with data from From else
593 (let ((author (and (plist-get info :with-author)
594 (let ((auth (plist-get info :author)))
595 (and auth (org-export-data auth info)))))
596 (email (and (plist-get info :with-email)
597 (org-export-data (plist-get info :email) info)))
598 (from-data (org-e-groff--get-tagged-content "FROM" info))
600 (to-data (org-e-groff--get-tagged-content "TO" info)))
602 (cond
603 ((and author from-data)
604 (let ((au-line
605 (mapconcat
606 (lambda (from-line)
607 (format " \"%s\" " from-line))
608 (split-string
609 (setq from-data
610 (replace-regexp-in-string "\\.P\n" "" from-data)) "\n") "")))
612 (concat
613 (format ".AU \"%s\" " author) au-line "\n")))
615 ((and author email (not (string= "" email)))
616 (format ".AU \"%s\" \"%s\"\n" author email))
618 (author (format ".AU \"%s\"\n" author))
620 (t ".AU \"\" \n")))
623 ;; 4. Author Title, if present
624 (let ((at-item (plist-get attr :author-title)))
625 (if (and at-item (stringp at-item))
626 (format ".AT \"%s\" \n" at-item)
627 ""))
629 ;; 5. Date.
630 (let ((date (org-export-data (plist-get info :date) info)))
631 (and date (format ".ND \"%s\"\n" date)))
634 ;; If Abstract, then Populate Abstract
637 (let ((abstract-data (org-e-groff--get-tagged-content "ABSTRACT" info))
638 (to-data (org-e-groff--get-tagged-content "TO" info)))
639 (cond
640 (abstract-data
641 (format ".AS\n%s\n.AE\n" abstract-data))
642 (to-data
643 (format ".AS\n%s\n.AE\n" to-data))))))
645 (defun org-e-groff--letter-head (title contents attr info)
646 (let ((author (and (plist-get info :with-author)
647 (let ((auth (plist-get info :author)))
648 (and auth (org-export-data auth info)))))
649 (email (and (plist-get info :with-email)
650 (org-export-data (plist-get info :email) info)))
651 (from-data (org-e-groff--get-tagged-content "FROM" info))
652 (at-item (plist-get attr :author-title))
653 (to-data (org-e-groff--get-tagged-content "TO" info)))
656 ;; If FROM then get data from FROM
657 (if from-data
658 (setq from-data
659 (replace-regexp-in-string "\\.P\n" "" from-data))
660 (setq from-data ""))
662 (if to-data
663 (setq to-data
664 (replace-regexp-in-string "\\.P\n" "" to-data))
665 (setq from-data ""))
667 (concat
668 (cond
669 (from-data
670 (format ".WA \"%s\" \"%s\" \n%s\n.WE\n" author (or at-item "") from-data))
671 ((and author email (not (string= "" email)))
672 (format ".WA \"%s\"\n \"%s\"\n.WE\n" author email))
673 (author (format ".WA \"%s\"\n.WE\n" author))
674 (t ".WA \"\" \n.WE\n"))
676 ;; If TO then get data from TO
678 (when to-data
679 (format ".IA \n%s\n.IE\n" to-data)))))
682 ;;; Template
684 (defun org-e-groff-template (contents info)
685 "Return complete document string after Groff conversion.
686 CONTENTS is the transcoded contents string. INFO is a plist
687 holding export options."
688 (let* ((title (org-export-data (plist-get info :title) info))
689 (attr (read
690 (format "(%s)"
691 (mapconcat
692 #'identity
693 (list (plist-get info :groff-class-options))
694 " "))))
695 (class (plist-get info :groff-class))
696 (class-options (plist-get info :groff-class-options))
697 (classes (assoc class org-e-groff-classes))
698 (classes-options (car (last classes)))
699 (heading-option (plist-get classes-options :heading))
700 (type-option (plist-get classes-options :type))
701 (last-option (plist-get classes-options :last-section))
702 (hyphenate (plist-get attr :hyphenate))
703 (justify-right (plist-get attr :justify-right))
705 (document-class-string
706 (progn
707 (org-element-normalize-string
708 (let* ((header (nth 1 (assoc class org-e-groff-classes)))
709 (document-class-item (if (stringp header) header "")))
710 document-class-item)))))
713 (concat
714 (if justify-right
715 (case justify-right
716 ('yes ".SA 1 \n")
717 ('no ".SA 0 \n")
718 (t ""))
721 (if hyphenate
722 (case hyphenate
723 ('yes ".nr Hy 1 \n")
724 ('no ".nr Hy 0 \n")
725 (t ""))
728 (cond
729 ((string= type-option "custom") "")
731 ((and (stringp document-class-string)
732 (string= type-option "cover"))
734 (concat
735 (format ".COVER %s\n" document-class-string)
736 (org-e-groff--mt-head title contents attr info)
737 ".COVEND\n"))
739 ((string= type-option "memo")
740 (concat
741 (org-e-groff--mt-head title contents attr info)
742 document-class-string))
743 ((string= type-option "letter")
744 (concat
745 (org-e-groff--letter-head title contents attr info)
746 (let ((sa-item (plist-get attr :salutation))
747 (cn-item (plist-get attr :confidential))
748 (sj-item (plist-get attr :subject))
749 (rn-item (plist-get attr :reference))
750 (at-item (plist-get attr :attention)))
752 (concat
754 (if (stringp sa-item)
755 (format ".LO SA \"%s\" \n" sa-item)
756 ".LO SA\n")
758 (when cn-item
759 (if (stringp cn-item)
760 (format ".LO CN \"%s\"\n" cn-item)
761 ".LO CN\n"))
763 (when (and at-item (stringp at-item))
764 (format ".LO AT \"%s\" \n" at-item))
765 (when (and title rn-item)
766 (format ".LO RN \"%s\"\n" title))
768 (when (and sj-item (stringp sj-item))
769 (format ".LO SJ \"%s\" \n" sj-item))
772 ".LT " document-class-string "\n"))))
774 (t ""))
776 contents
778 (cond
779 ((string= last-option "toc")
780 ".TC")
781 ((string= last-option "sign")
782 (let ((fc-item (plist-get attr :closing)))
783 (concat (if (stringp fc-item)
784 (format ".FC \"%s\" \n" fc-item)
785 ".FC\n")
786 ".SG\n")))
787 (t ""))
789 (progn
790 (mapconcat
791 (lambda (item)
792 (when (string= (car item) "NS")
793 (replace-regexp-in-string
794 "\\.P\n" "" (cdr item))))
795 (reverse org-e-groff-special-content) "\n")))))
799 ;;; Transcode Functions
801 ;;; Babel Call
803 ;; Babel Calls are ignored.
806 ;;; Bold
808 (defun org-e-groff-bold (bold contents info)
809 "Transcode BOLD from Org to Groff.
810 CONTENTS is the text with bold markup. INFO is a plist holding
811 contextual information."
812 (org-e-groff--text-markup contents 'bold))
814 ;;; Center Block
816 (defun org-e-groff-center-block (center-block contents info)
817 "Transcode a CENTER-BLOCK element from Org to Groff.
818 CONTENTS holds the contents of the center block. INFO is a plist
819 holding contextual information."
820 (org-e-groff--wrap-label
821 center-block
822 (format ".DS C \n%s\n.DE" contents)))
824 ;;; Clock
826 (defun org-e-groff-clock (clock contents info)
827 "Transcode a CLOCK element from Org to Groff.
828 CONTENTS is nil. INFO is a plist holding contextual
829 information."
830 (concat
831 (format "\\fB%s\\fP " org-clock-string)
832 (format org-e-groff-inactive-timestamp-format
833 (concat (org-translate-time (org-element-property :value clock))
834 (let ((time (org-element-property :time clock)))
835 (and time (format " (%s)" time)))))))
837 ;;; Code
839 (defun org-e-groff-code (code contents info)
840 "Transcode a CODE object from Org to Groff.
841 CONTENTS is nil. INFO is a plist used as a communication
842 channel."
843 (org-e-groff--text-markup (org-element-property :value code) 'code))
845 ;;; Comments and Comment Blocks are ignored.
847 ;;; Drawer
849 (defun org-e-groff-drawer (drawer contents info)
850 "Transcode a DRAWER element from Org to Groff.
851 CONTENTS holds the contents of the block. INFO is a plist
852 holding contextual information."
853 (let* ((name (org-element-property :drawer-name drawer))
854 (output (if (functionp org-e-groff-format-drawer-function)
855 (funcall org-e-groff-format-drawer-function
856 name contents)
857 ;; If there's no user defined function: simply
858 ;; display contents of the drawer.
859 contents)))
860 (org-e-groff--wrap-label drawer output)))
862 ;;; Dynamic Block
864 (defun org-e-groff-dynamic-block (dynamic-block contents info)
865 "Transcode a DYNAMIC-BLOCK element from Org to Groff.
866 CONTENTS holds the contents of the block. INFO is a plist
867 holding contextual information. See `org-export-data'."
868 (org-e-groff--wrap-label dynamic-block contents))
870 ;;; Entity
872 (defun org-e-groff-entity (entity contents info)
873 "Transcode an ENTITY object from Org to Groff.
874 CONTENTS are the definition itself. INFO is a plist holding
875 contextual information."
876 (org-element-property :utf-8 entity))
878 ;;; Example Block
880 (defun org-e-groff-example-block (example-block contents info)
881 "Transcode an EXAMPLE-BLOCK element from Org to Groff.
882 CONTENTS is nil. INFO is a plist holding contextual
883 information."
884 (org-e-groff--wrap-label
885 example-block
886 (format ".DS L\n%s\n.DE"
887 (org-export-format-code-default example-block info))))
889 ;;; Export Block
891 (defun org-e-groff-export-block (export-block contents info)
892 "Transcode a EXPORT-BLOCK element from Org to Groff.
893 CONTENTS is nil. INFO is a plist holding contextual information."
894 (when (string= (org-element-property :type export-block) "GROFF")
895 (org-remove-indentation (org-element-property :value export-block))))
897 ;;; Export Snippet
899 (defun org-e-groff-export-snippet (export-snippet contents info)
900 "Transcode a EXPORT-SNIPPET object from Org to Groff.
901 CONTENTS is nil. INFO is a plist holding contextual information."
902 (when (eq (org-export-snippet-backend export-snippet) 'e-groff)
903 (org-element-property :value export-snippet)))
905 ;;; Fixed Width
907 (defun org-e-groff-fixed-width (fixed-width contents info)
908 "Transcode a FIXED-WIDTH element from Org to Groff.
909 CONTENTS is nil. INFO is a plist holding contextual information."
910 (org-e-groff--wrap-label
911 fixed-width
912 (format "\\fC\n%s\\fP"
913 (org-remove-indentation
914 (org-element-property :value fixed-width)))))
916 ;;; Footnote Definition
918 ;; Footnote Definitions are ignored.
920 ;; Footnotes are handled automatically in GROFF. Although manual
921 ;; references can be added, not really required.
923 (defun org-e-groff-footnote-reference (footnote-reference contents info)
924 ;; Changing from info to footnote-reference
925 (let* ((raw (org-export-get-footnote-definition footnote-reference info))
926 (n (org-export-get-footnote-number footnote-reference info))
927 (data (org-trim (org-export-data raw info)))
928 (ref-id (plist-get (nth 1 footnote-reference) :label)))
929 ;; It is a reference
930 (if (string-match "fn:rl" ref-id)
931 (if (member ref-id org-e-groff-registered-references)
932 (format "\\*[%s]" ref-id)
933 (progn
934 (push ref-id org-e-groff-registered-references)
935 (format "\\*(Rf\n.RS \"%s\" \n%s\n.RF\n" ref-id data)))
936 ;; else it is a footnote
937 (format "\\u\\s-2%s\\d\\s+2\n.FS %s\n%s\n.FE\n" n n data))))
939 ;;; Headline
941 (defun org-e-groff-headline (headline contents info)
942 "Transcode an HEADLINE element from Org to Groff.
943 CONTENTS holds the contents of the headline. INFO is a plist
944 holding contextual information."
945 (let* ((class (plist-get info :groff-class))
946 (level (org-export-get-relative-level headline info))
947 (numberedp (org-export-numbered-headline-p headline info))
948 ;; Section formatting will set two placeholders: one for the
949 ;; title and the other for the contents.
950 (classes (assoc class org-e-groff-classes))
951 (classes-options (car (last classes)))
952 (heading-option (plist-get classes-options :heading))
953 (section-fmt
954 (progn
955 (cond
956 ((and (symbolp heading-option)
957 (fboundp heading-option))
958 (funcall heading-option level numberedp))
959 ((> level 7) nil)
960 (t (if numberedp
961 (concat ".H " (number-to-string level) " \"%s\"\n%s")
962 ".HU \"%s\"\n%s")))))
963 ;; End of section-fmt
964 (text (org-export-data (org-element-property :title headline) info))
965 (todo
966 (and (plist-get info :with-todo-keywords)
967 (let ((todo (org-element-property :todo-keyword headline)))
968 (and todo (org-export-data todo info)))))
969 (todo-type (and todo (org-element-property :todo-type headline)))
970 (tags (and (plist-get info :with-tags)
971 (org-export-get-tags headline info)))
972 (priority (and (plist-get info :with-priority)
973 (org-element-property :priority headline)))
974 ;; Create the headline text along with a no-tag version. The
975 ;; latter is required to remove tags from table of contents.
976 (full-text (if (functionp org-e-groff-format-headline-function)
977 ;; User-defined formatting function.
978 (funcall org-e-groff-format-headline-function
979 todo todo-type priority text tags)
980 ;; Default formatting.
981 (concat
982 (when todo
983 (format "\\fB%s\\fP " todo))
984 (when priority (format " [\\#%c] " priority))
985 text
986 (when tags
987 (format " \\fC:%s:\\fP "
988 (mapconcat 'identity tags ":"))))))
989 (full-text-no-tag
990 (if (functionp org-e-groff-format-headline-function)
991 ;; User-defined formatting function.
992 (funcall org-e-groff-format-headline-function
993 todo todo-type priority text nil)
994 ;; Default formatting.
995 (concat
996 (when todo (format "\\fB%s\\fP " todo))
997 (when priority (format " [\\#%c] " priority))
998 text)))
999 ;; Associate some \label to the headline for internal links.
1000 ;; (headline-label
1001 ;; (format "\\label{sec-%s}\n"
1002 ;; (mapconcat 'number-to-string
1003 ;; (org-export-get-headline-number headline info)
1004 ;; "-")))
1005 (headline-label "")
1006 (pre-blanks
1007 (make-string (org-element-property :pre-blank headline) 10)))
1009 (cond
1010 ;; Case 1: Special Tag
1011 ((member (car tags) org-e-groff-special-tags)
1012 (cond
1013 ((string= (car tags) "BODY") contents)
1015 ((string= (car tags) "NS")
1016 (progn
1017 (push (cons (car tags)
1018 (format ".NS \"%s\" 1 \n%s"
1019 (car (org-element-property :title headline))
1020 (or contents " ")))
1021 org-e-groff-special-content) nil))
1024 (progn
1025 (push (cons (car tags) contents) org-e-groff-special-content)
1026 nil))))
1028 ;; Case 2: This is a footnote section: ignore it.
1029 ((org-element-property :footnote-section-p headline) nil)
1031 ;; Case 3: This is a deep sub-tree: export it as a list item.
1032 ;; Also export as items headlines for which no section
1033 ;; format has been found.
1034 ((or (not section-fmt) (org-export-low-level-p headline info))
1035 ;; Build the real contents of the sub-tree.
1036 (let ((low-level-body
1037 (concat
1038 ;; If the headline is the first sibling, start a list.
1039 (when (org-export-first-sibling-p headline info)
1040 (format "%s\n" (if numberedp ".AL 1\n" ".DL \n")))
1041 ;; Itemize headline
1042 ".LI\n" full-text "\n" headline-label pre-blanks contents)))
1043 ;; If headline is not the last sibling simply return
1044 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1045 ;; blank line.
1046 (if (not (org-export-last-sibling-p headline info)) low-level-body
1047 (replace-regexp-in-string
1048 "[ \t\n]*\\'"
1049 (concat "\n.LE")
1050 low-level-body))))
1052 ;; Case 4. Standard headline. Export it as a section.
1054 (format section-fmt full-text
1055 (concat headline-label pre-blanks contents))))))
1057 ;;; Horizontal Rule
1058 ;; Not supported
1060 ;;; Inline Babel Call
1062 ;; Inline Babel Calls are ignored.
1064 ;;; Inline Src Block
1066 (defun org-e-groff-inline-src-block (inline-src-block contents info)
1067 "Transcode an INLINE-SRC-BLOCK element from Org to Groff.
1068 CONTENTS holds the contents of the item. INFO is a plist holding
1069 contextual information."
1070 (let* ((code (org-element-property :value inline-src-block)))
1071 (cond
1072 (org-e-groff-source-highlight
1073 (let* ((tmpdir (if (featurep 'xemacs)
1074 temp-directory
1075 temporary-file-directory))
1076 (in-file (make-temp-name
1077 (expand-file-name "srchilite" tmpdir)))
1078 (out-file (make-temp-name
1079 (expand-file-name "reshilite" tmpdir)))
1080 (org-lang (org-element-property :language inline-src-block))
1081 (lst-lang (cadr (assq (intern org-lang)
1082 org-e-groff-source-highlight-langs)))
1084 (cmd (concat (expand-file-name "source-highlight")
1085 " -s " lst-lang
1086 " -f groff_mm_color "
1087 " -i " in-file
1088 " -o " out-file)))
1089 (if lst-lang
1090 (let ((code-block ""))
1091 (with-temp-file in-file (insert code))
1092 (shell-command cmd)
1093 (setq code-block (org-file-contents out-file))
1094 (delete-file in-file)
1095 (delete-file out-file)
1096 code-block)
1097 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1098 code))))
1100 ;; Do not use a special package: transcode it verbatim.
1102 (concat ".DS I\n" "\\fC" code "\\fP\n.DE\n")))))
1104 ;;; Inlinetask
1106 (defun org-e-groff-inlinetask (inlinetask contents info)
1107 "Transcode an INLINETASK element from Org to Groff.
1108 CONTENTS holds the contents of the block. INFO is a plist
1109 holding contextual information."
1110 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1111 (todo (and (plist-get info :with-todo-keywords)
1112 (let ((todo (org-element-property :todo-keyword inlinetask)))
1113 (and todo (org-export-data todo info)))))
1114 (todo-type (org-element-property :todo-type inlinetask))
1115 (tags (and (plist-get info :with-tags)
1116 (org-export-get-tags inlinetask info)))
1117 (priority (and (plist-get info :with-priority)
1118 (org-element-property :priority inlinetask))))
1119 ;; If `org-e-groff-format-inlinetask-function' is provided, call it
1120 ;; with appropriate arguments.
1121 (if (functionp org-e-groff-format-inlinetask-function)
1122 (funcall org-e-groff-format-inlinetask-function
1123 todo todo-type priority title tags contents)
1124 ;; Otherwise, use a default template.
1125 (org-e-groff--wrap-label
1126 inlinetask
1127 (let ((full-title
1128 (concat
1129 (when todo (format "\\fB%s\\fP " todo))
1130 (when priority (format " [\\#%c] " priority))
1131 title
1132 (when tags (format " \\fC:%s:\\fP "
1133 (mapconcat 'identity tags ":"))))))
1134 (format (concat "\n.DS I\n"
1135 "%s\n"
1136 ".sp"
1137 "%s\n"
1138 ".DE")
1139 full-title contents))))))
1141 ;;; Italic
1143 (defun org-e-groff-italic (italic contents info)
1144 "Transcode ITALIC from Org to Groff.
1145 CONTENTS is the text with italic markup. INFO is a plist holding
1146 contextual information."
1147 (org-e-groff--text-markup contents 'italic))
1149 ;;; Item
1151 (defun org-e-groff-item (item contents info)
1152 "Transcode an ITEM element from Org to Groff.
1153 CONTENTS holds the contents of the item. INFO is a plist holding
1154 contextual information."
1155 (let* ((bullet (org-element-property :bullet item))
1156 (type (org-element-property
1157 :type (org-element-property :parent item)))
1158 (checkbox (case (org-element-property :checkbox item)
1159 (on "\\o'\\(sq\\(mu'")
1160 (off "\\(sq")
1161 (trans "\\o'\\(sq\\(mi'")))
1162 (tag (let ((tag (org-element-property :tag item)))
1163 ;; Check-boxes must belong to the tag.
1164 (and tag (format "%s"
1165 (concat checkbox
1166 (org-export-data tag info)))))))
1168 (cond
1169 ((or checkbox tag)
1170 (concat ".LI ""\"" (or tag (concat "\\ " checkbox)) "\""
1171 "\n"
1172 (org-trim (or contents " "))))
1173 ((eq type 'ordered)
1174 (concat ".LI"
1175 "\n"
1176 (org-trim (or contents " "))))
1178 (let* ((bullet (org-trim bullet))
1179 (marker (cond ((string= "-" bullet) "\\(em")
1180 ((string= "*" bullet) "\\(bu")
1181 (t "\\(dg"))))
1182 (concat ".LI " marker "\n"
1183 (org-trim (or contents " "))))))))
1185 ;;; Keyword
1187 (defun org-e-groff-keyword (keyword contents info)
1188 "Transcode a KEYWORD element from Org to Groff.
1189 CONTENTS is nil. INFO is a plist holding contextual information."
1190 (let ((key (org-element-property :key keyword))
1191 (value (org-element-property :value keyword)))
1192 (cond
1193 ((string= key "GROFF") value)
1194 (t nil))))
1196 ;;; LaTeX Environment
1198 (defun org-e-groff-latex-environment (latex-environment contents info)
1199 "Transcode a LATEX-ENVIRONMENT element from Org to Groff.
1200 CONTENTS is nil. INFO is a plist holding contextual information."
1201 (let ((label (org-element-property :name latex-environment))
1202 (value (org-remove-indentation
1203 (org-element-property :value latex-environment))))
1204 (if (not (org-string-nw-p label)) value
1205 ;; Environment is labelled: label must be within the environment
1206 ;; (otherwise, a reference pointing to that element will count
1207 ;; the section instead).
1208 (with-temp-buffer
1209 (insert value)
1210 (goto-char (point-min))
1211 (forward-line)
1212 (insert (format "%s\n" label))
1213 (buffer-string)))))
1215 ;;; LaTeX Fragment
1217 (defun org-e-groff-latex-fragment (latex-fragment contents info)
1218 "Transcode a LATEX-FRAGMENT object from Org to Groff.
1219 CONTENTS is nil. INFO is a plist holding contextual information."
1220 (org-element-property :value latex-fragment))
1222 ;;; Line Break
1224 (defun org-e-groff-line-break (line-break contents info)
1225 "Transcode a LINE-BREAK object from Org to Groff.
1226 CONTENTS is nil. INFO is a plist holding contextual information."
1227 ".br\n")
1229 ;;; Link
1230 ;; Inline images just place a call to .PSPIC or .PS/.PE
1231 ;; and load the graph.
1233 (defun org-e-groff-link--inline-image (link info)
1234 "Return Groff code for an inline image.
1235 LINK is the link pointing to the inline image. INFO is a plist
1236 used as a communication channel."
1237 (let* ((parent (org-export-get-parent-element link))
1238 (path (let ((raw-path (org-element-property :path link)))
1239 (if (not (file-name-absolute-p raw-path)) raw-path
1240 (expand-file-name raw-path))))
1241 (attr (org-export-read-attribute :attr_groff link))
1242 (placement
1243 (case (plist-get attr :position)
1244 ('center "")
1245 ('left "-L")
1246 ('right "-R")
1247 (t "")))
1248 (width (or (plist-get attr :width) ""))
1249 (height (or (plist-get attr :height) ""))
1250 (caption (and (not (plist-get attr :disable-caption))
1251 (org-e-groff--caption/label-string parent info))))
1252 ;; Now clear ATTR from any special keyword and set a default value
1253 ;; if nothing is left. Return proper string.
1254 (concat
1255 (cond
1256 ((and org-e-groff-raster-to-ps
1257 (or (string-match ".\.png$" path)
1258 (string-match ".\.jpg$" path)))
1259 (let ((eps-path (concat path ".eps")))
1260 (shell-command (format org-e-groff-raster-to-ps path eps-path))
1261 (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1262 placement eps-path width height)))
1263 ((string-match ".\.pic$" path)
1264 (format "\n.PS\ncopy \"%s\"\n.PE" path))
1265 (t (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1266 placement path width height)))
1267 (and caption (format "\n.FG \"%s\"" caption)))))
1269 (defun org-e-groff-link (link desc info)
1270 "Transcode a LINK object from Org to Groff.
1272 DESC is the description part of the link, or the empty string.
1273 INFO is a plist holding contextual information. See
1274 `org-export-data'."
1276 (let* ((type (org-element-property :type link))
1277 (raw-path (org-element-property :path link))
1278 ;; Ensure DESC really exists, or set it to nil.
1279 (desc (and (not (string= desc "")) desc))
1280 (imagep (org-export-inline-image-p
1281 link org-e-groff-inline-image-rules))
1282 (path (cond
1283 ((member type '("http" "https" "ftp" "mailto"))
1284 (concat type ":" raw-path))
1285 ((string= type "file")
1286 (when (string-match "\\(.+\\)::.+" raw-path)
1287 (setq raw-path (match-string 1 raw-path)))
1288 (if (file-name-absolute-p raw-path)
1289 (concat "file://" (expand-file-name raw-path))
1290 (concat "file://" raw-path)))
1291 (t raw-path)))
1292 protocol)
1293 (cond
1294 ;; Image file.
1295 (imagep (org-e-groff-link--inline-image link info))
1296 ;; import groff files
1297 ((and (string= type "file")
1298 (string-match ".\.groff$" raw-path))
1299 (concat ".so " raw-path "\n"))
1300 ;; Radio link: transcode target's contents and use them as link's
1301 ;; description.
1302 ((string= type "radio")
1303 (let ((destination (org-export-resolve-radio-link link info)))
1304 (when destination
1305 (format "\\fI [%s] \\fP"
1306 (org-export-solidify-link-text path)))))
1308 ;; Links pointing to an headline: find destination and build
1309 ;; appropriate referencing command.
1310 ((member type '("custom-id" "fuzzy" "id"))
1311 (let ((destination (if (string= type "fuzzy")
1312 (org-export-resolve-fuzzy-link link info)
1313 (org-export-resolve-id-link link info))))
1314 (case (org-element-type destination)
1315 ;; Id link points to an external file.
1316 (plain-text
1317 (if desc (format "%s \\fBat\\fP \\fIfile://%s\\fP" desc destination)
1318 (format "\\fI file://%s \\fP" destination)))
1319 ;; Fuzzy link points nowhere.
1320 ('nil
1321 (format org-e-groff-link-with-unknown-path-format
1322 (or desc
1323 (org-export-data
1324 (org-element-property :raw-link link) info))))
1325 ;; Fuzzy link points to an invisible target.
1326 (keyword nil)
1327 ;; LINK points to an headline. If headlines are numbered
1328 ;; and the link has no description, display headline's
1329 ;; number. Otherwise, display description or headline's
1330 ;; title.
1331 (headline
1332 (let ((label ""))
1333 (if (and (plist-get info :section-numbers) (not desc))
1334 (format "\\fI%s\\fP" label)
1335 (format "\\fI%s\\fP"
1336 (or desc
1337 (org-export-data
1338 (org-element-property :title destination) info))))))
1339 ;; Fuzzy link points to a target. Do as above.
1340 (otherwise
1341 (let ((path (org-export-solidify-link-text path)))
1342 (if (not desc) (format "\\fI%s\\fP" path)
1343 (format "%s \\fBat\\fP \\fI%s\\fP" desc path)))))))
1344 ;; External link with a description part.
1345 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
1346 ;; External link without a description part.
1347 (path (format "\\fI%s\\fP" path))
1348 ;; No path, only description. Try to do something useful.
1349 (t (format org-e-groff-link-with-unknown-path-format desc)))))
1351 ;;; Paragraph
1353 (defun org-e-groff-paragraph (paragraph contents info)
1354 "Transcode a PARAGRAPH element from Org to Groff.
1355 CONTENTS is the contents of the paragraph, as a string. INFO is
1356 the plist used as a communication channel."
1357 (let ((parent (plist-get (nth 1 paragraph) :parent)))
1358 (when parent
1359 (let* ((parent-type (car parent))
1360 (fixed-paragraph "")
1361 (class (plist-get info :groff-class))
1362 (class-options (plist-get info :groff-class-options))
1363 (classes (assoc class org-e-groff-classes))
1364 (classes-options (car (last classes)))
1365 (paragraph-option (plist-get classes-options :paragraph)))
1366 (cond
1367 ((and (symbolp paragraph-option)
1368 (fboundp paragraph-option))
1369 (funcall paragraph-option parent-type parent contents))
1370 ((and (eq parent-type 'item)
1371 (plist-get (nth 1 parent) :bullet))
1372 (setq fixed-paragraph (concat "" contents)))
1373 ((eq parent-type 'section)
1374 (setq fixed-paragraph (concat ".P\n" contents)))
1375 ((eq parent-type 'footnote-definition)
1376 (setq fixed-paragraph (concat "" contents)))
1377 (t (setq fixed-paragraph (concat "" contents))))
1378 fixed-paragraph))))
1380 ;;; Plain List
1382 (defun org-e-groff-plain-list (plain-list contents info)
1383 "Transcode a PLAIN-LIST element from Org to Groff.
1384 CONTENTS is the contents of the list. INFO is a plist holding
1385 contextual information."
1386 (let* ((type (org-element-property :type plain-list))
1387 (attr (mapconcat #'identity
1388 (org-element-property :attr_groff plain-list)
1389 " "))
1390 (groff-type (cond
1391 ((eq type 'ordered) ".AL")
1392 ((eq type 'unordered) ".BL")
1393 ((eq type 'descriptive) ".VL 2.0i"))))
1394 (org-e-groff--wrap-label
1395 plain-list
1396 (format "%s\n%s\n.LE" groff-type contents))))
1398 ;;; Plain Text
1400 (defun org-e-groff-plain-text (text info)
1401 "Transcode a TEXT string from Org to Groff.
1402 TEXT is the string to transcode. INFO is a plist holding
1403 contextual information."
1404 (let ((output text))
1405 ;; Protect various characters.
1406 (setq output (replace-regexp-in-string
1407 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1408 "$\\" output nil t 1))
1409 ;; Activate smart quotes. Be sure to provide original TEXT string
1410 ;; since OUTPUT may have been modified.
1411 (when (plist-get info :with-smart-quotes)
1412 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
1413 ;; Handle Special Characters
1414 (if org-e-groff-special-char
1415 (dolist (special-char-list org-e-groff-special-char)
1416 (setq output
1417 (replace-regexp-in-string (car special-char-list)
1418 (cdr special-char-list) output))))
1419 ;; Handle break preservation if required.
1420 (when (plist-get info :preserve-breaks)
1421 (setq output (replace-regexp-in-string
1422 "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n" output)))
1423 ;; Return value.
1424 output))
1426 ;;; Planning
1428 (defun org-e-groff-planning (planning contents info)
1429 "Transcode a PLANNING element from Org to Groff.
1430 CONTENTS is nil. INFO is a plist holding contextual
1431 information."
1432 (concat
1433 (mapconcat
1434 'identity
1435 (delq nil
1436 (list
1437 (let ((closed (org-element-property :closed planning)))
1438 (when closed
1439 (concat
1440 (format "\\fR %s \\fP" org-closed-string)
1441 (format org-e-groff-inactive-timestamp-format
1442 (org-translate-time closed)))))
1443 (let ((deadline (org-element-property :deadline planning)))
1444 (when deadline
1445 (concat
1446 (format "\\fB %s \\fP" org-deadline-string)
1447 (format org-e-groff-active-timestamp-format
1448 (org-translate-time deadline)))))
1449 (let ((scheduled (org-element-property :scheduled planning)))
1450 (when scheduled
1451 (concat
1452 (format "\\fR %s \\fP" org-scheduled-string)
1453 (format org-e-groff-active-timestamp-format
1454 (org-translate-time scheduled)))))))
1456 ""))
1458 ;;; Property Drawer
1460 (defun org-e-groff-property-drawer (property-drawer contents info)
1461 "Transcode a PROPERTY-DRAWER element from Org to Groff.
1462 CONTENTS is nil. INFO is a plist holding contextual
1463 information."
1464 ;; The property drawer isn't exported but we want separating blank
1465 ;; lines nonetheless.
1468 ;;; Quote Block
1470 (defun org-e-groff-quote-block (quote-block contents info)
1471 "Transcode a QUOTE-BLOCK element from Org to Groff.
1472 CONTENTS holds the contents of the block. INFO is a plist
1473 holding contextual information."
1474 (org-e-groff--wrap-label
1475 quote-block
1476 (format ".DS I\n.I\n%s\n.R\n.DE" contents)))
1478 ;;; Quote Section
1480 (defun org-e-groff-quote-section (quote-section contents info)
1481 "Transcode a QUOTE-SECTION element from Org to Groff.
1482 CONTENTS is nil. INFO is a plist holding contextual information."
1483 (let ((value (org-remove-indentation
1484 (org-element-property :value quote-section))))
1485 (when value (format ".DS L\n\\fI%s\\fP\n.DE\n" value))))
1487 ;;; Radio Target
1489 (defun org-e-groff-radio-target (radio-target text info)
1490 "Transcode a RADIO-TARGET object from Org to Groff.
1491 TEXT is the text of the target. INFO is a plist holding
1492 contextual information."
1493 (format "%s - %s"
1494 (org-export-solidify-link-text
1495 (org-element-property :value radio-target))
1496 text))
1498 ;;; Section
1500 (defun org-e-groff-section (section contents info)
1501 "Transcode a SECTION element from Org to Groff.
1502 CONTENTS holds the contents of the section. INFO is a plist
1503 holding contextual information."
1504 contents)
1506 ;;; Special Block
1508 (defun org-e-groff-special-block (special-block contents info)
1509 "Transcode a SPECIAL-BLOCK element from Org to Groff.
1510 CONTENTS holds the contents of the block. INFO is a plist
1511 holding contextual information."
1512 (let ((type (downcase (org-element-property :type special-block))))
1513 (org-e-groff--wrap-label
1514 special-block
1515 (format "%s\n" contents))))
1517 ;;; Src Block
1519 (defun org-e-groff-src-block (src-block contents info)
1520 "Transcode a SRC-BLOCK element from Org to Groff.
1521 CONTENTS holds the contents of the item. INFO is a plist holding
1522 contextual information."
1523 (let* ((lang (org-element-property :language src-block))
1524 (label (org-element-property :name src-block))
1525 (code (org-element-property :value src-block))
1526 (custom-env (and lang
1527 (cadr (assq (intern lang)
1528 org-e-groff-custom-lang-environments))))
1529 (num-start (case (org-element-property :number-lines src-block)
1530 (continued (org-export-get-loc src-block info))
1531 (new 0)))
1532 (retain-labels (org-element-property :retain-labels src-block))
1533 (caption (and (not (org-export-read-attribute
1534 :attr_groff src-block :disable-caption))
1535 (org-e-groff--caption/label-string src-block info))))
1537 (cond
1538 ;; Case 1. No source fontification.
1539 ((not org-e-groff-source-highlight)
1540 (concat
1541 (format ".DS I\n\\fC%s\\fP\n.DE\n"
1542 (org-export-format-code-default src-block info))
1543 (and caption (format ".EX \"%s\" " caption))))
1545 ;; Case 2. Source fontification.
1546 (org-e-groff-source-highlight
1547 (let* ((tmpdir (if (featurep 'xemacs)
1548 temp-directory
1549 temporary-file-directory))
1550 (in-file (make-temp-name
1551 (expand-file-name "srchilite" tmpdir)))
1552 (out-file (make-temp-name
1553 (expand-file-name "reshilite" tmpdir)))
1555 (org-lang (org-element-property :language src-block))
1556 (lst-lang (cadr (assq (intern org-lang)
1557 org-e-groff-source-highlight-langs)))
1559 (cmd (concat "source-highlight"
1560 " -s " lst-lang
1561 " -f groff_mm_color "
1562 " -i " in-file
1563 " -o " out-file)))
1565 (concat
1566 (if lst-lang
1567 (let ((code-block ""))
1568 (with-temp-file in-file (insert code))
1569 (shell-command cmd)
1570 (setq code-block (org-file-contents out-file))
1571 (delete-file in-file)
1572 (delete-file out-file)
1573 (format "%s\n" code-block))
1574 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1575 code))
1576 (and caption (format ".EX \"%s\" " caption))))))))
1579 ;;; Statistics Cookie
1581 (defun org-e-groff-statistics-cookie (statistics-cookie contents info)
1582 "Transcode a STATISTICS-COOKIE object from Org to Groff.
1583 CONTENTS is nil. INFO is a plist holding contextual information."
1584 (org-element-property :value statistics-cookie))
1587 ;;; Strike-Through
1589 (defun org-e-groff-strike-through (strike-through contents info)
1590 "Transcode STRIKE-THROUGH from Org to Groff.
1591 CONTENTS is the text with strike-through markup. INFO is a plist
1592 holding contextual information."
1593 (org-e-groff--text-markup contents 'strike-through))
1595 ;;; Subscript
1597 (defun org-e-groff-subscript (subscript contents info)
1598 "Transcode a SUBSCRIPT object from Org to Groff.
1599 CONTENTS is the contents of the object. INFO is a plist holding
1600 contextual information."
1601 (format "\\d\\s-2%s\\s+2\\u" contents))
1603 ;;; Superscript "^_%s$
1605 (defun org-e-groff-superscript (superscript contents info)
1606 "Transcode a SUPERSCRIPT object from Org to Groff.
1607 CONTENTS is the contents of the object. INFO is a plist holding
1608 contextual information."
1609 (format "\\u\\s-2%s\\s+2\\d" contents))
1612 ;;; Table
1614 ;; `org-e-groff-table' is the entry point for table transcoding. It
1615 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1616 ;; delegates the job to `org-e-groff-table--org-table' function,
1617 ;; depending of the type of the table.
1619 ;; `org-e-groff-table--align-string' is a subroutine used to build
1620 ;; alignment string for Org tables.
1622 (defun org-e-groff-table (table contents info)
1623 "Transcode a TABLE element from Org to Groff.
1624 CONTENTS is the contents of the table. INFO is a plist holding
1625 contextual information."
1626 (cond
1627 ;; Case 1: verbatim table.
1628 ((or org-e-groff-tables-verbatim
1629 (let ((attr (read (format "(%s)"
1630 (mapconcat
1631 #'identity
1632 (org-element-property :attr_groff table) " ")))))
1633 (and attr (plist-get attr :verbatim))))
1635 (format ".DS L\n\\fC%s\\fP\n.DE"
1636 ;; Re-create table, without affiliated keywords.
1637 (org-trim
1638 (org-element-interpret-data
1639 `(table nil ,@(org-element-contents table))))))
1641 ;; Case 2: Standard table.
1642 (t (org-e-groff-table--org-table table contents info))))
1644 (defun org-e-groff-table--align-string (divider table info)
1645 "Return an appropriate Groff alignment string.
1646 TABLE is the considered table. INFO is a plist used as
1647 a communication channel."
1648 (let (alignment)
1649 ;; Extract column groups and alignment from first (non-rule)
1650 ;; row.
1651 (org-element-map
1652 (org-element-map
1653 table 'table-row
1654 (lambda (row)
1655 (and (eq (org-element-property :type row) 'standard) row))
1656 info 'first-match)
1657 'table-cell
1658 (lambda (cell)
1659 (let* ((borders (org-export-table-cell-borders cell info))
1660 (raw-width (org-export-table-cell-width cell info))
1661 (width-cm (when raw-width (/ raw-width 5)))
1662 (width (if raw-width (format "w(%dc)"
1663 (if (< width-cm 1) 1 width-cm)) "")))
1664 ;; Check left border for the first cell only.
1665 ;; Alignment is nil on assignment
1667 (when (and (memq 'left borders) (not alignment))
1668 (push "|" alignment))
1669 (push
1670 (case (org-export-table-cell-alignment cell info)
1671 (left (concat "l" width divider))
1672 (right (concat "r" width divider))
1673 (center (concat "c" width divider)))
1674 alignment)
1675 (when (memq 'right borders) (push "|" alignment))))
1676 info)
1677 (apply 'concat (reverse alignment))))
1679 (defun org-e-groff-table--org-table (table contents info)
1680 "Return appropriate Groff code for an Org table.
1682 TABLE is the table type element to transcode. CONTENTS is its
1683 contents, as a string. INFO is a plist used as a communication
1684 channel.
1686 This function assumes TABLE has `org' as its `:type' attribute."
1687 (let* ((attr (org-export-read-attribute :attr_groff table))
1688 (label (org-element-property :name table))
1689 (caption (and (not (plist-get attr :disable-caption))
1690 (org-e-groff--caption/label-string table info)))
1691 (divider (if (plist-get attr :divider) "|" " "))
1693 ;; Determine alignment string.
1694 (alignment (org-e-groff-table--align-string divider table info))
1696 ;; Extract others display options.
1698 (lines (org-split-string contents "\n"))
1700 (attr-list
1701 (let (result-list)
1702 (dolist (attr-item
1703 (list
1704 (if (plist-get attr :expand)
1705 "expand" nil)
1707 (case (plist-get attr :placement)
1708 ('center "center")
1709 ('left nil)
1711 (if org-e-groff-tables-centered
1712 "center" "")))
1714 (case (plist-get attr :boxtype)
1715 ('box "box")
1716 ('doublebox "doublebox")
1717 ('allbox "allbox")
1718 ('none nil)
1719 (t "box"))))
1721 (if (not (null attr-item))
1722 (add-to-list 'result-list attr-item)))
1723 result-list))
1725 (title-line (plist-get attr :title-line))
1726 (long-cells (plist-get attr :long-cells))
1728 (table-format
1729 (concat
1730 (format "%s"
1731 (or (car attr-list) ""))
1733 (let (output-list)
1734 (when (cdr attr-list)
1735 (dolist (attr-item (cdr attr-list))
1736 (setq output-list (concat output-list
1737 (format ",%s" attr-item)))))
1738 output-list) "")))
1739 (first-line
1740 (when lines (org-split-string (car lines) "\t"))))
1741 ;; Prepare the final format string for the table.
1744 (cond
1745 ;; Others.
1746 (lines
1747 (concat ".TS\n " table-format ";\n"
1748 (format "%s.\n"
1749 (let ((final-line ""))
1750 (when title-line
1751 (dotimes (i (length first-line))
1752 (setq final-line (concat final-line "cb" divider))))
1754 (setq final-line (concat final-line "\n"))
1756 (if alignment
1757 (setq final-line (concat final-line alignment))
1758 (dotimes (i (length first-line))
1759 (setq final-line (concat final-line "c" divider))))
1760 final-line))
1762 (format "%s.TE\n"
1763 (let ((final-line "")
1764 (long-line "")
1765 (lines (org-split-string contents "\n")))
1767 (dolist (line-item lines)
1768 (setq long-line "")
1770 (if long-cells
1771 (progn
1772 (if (string= line-item "_")
1773 (setq long-line (format "%s\n" line-item))
1774 ;; else string =
1775 (let ((cell-item-list (org-split-string line-item "\t")))
1776 (dolist (cell-item cell-item-list)
1778 (cond ((eq cell-item (car (last cell-item-list)))
1779 (setq long-line (concat long-line
1780 (format "T{\n%s\nT}\t\n" cell-item))))
1782 (setq long-line (concat long-line
1783 (format "T{\n%s\nT}\t" cell-item))))))
1784 long-line))
1785 ;; else long cells
1786 (setq final-line (concat final-line long-line)))
1788 (setq final-line (concat final-line line-item "\n"))))
1789 final-line))
1791 (if caption (format ".TB \"%s\"" caption) ""))))))
1793 ;;; Table Cell
1795 (defun org-e-groff-table-cell (table-cell contents info)
1796 "Transcode a TABLE-CELL element from Org to Groff
1797 CONTENTS is the cell contents. INFO is a plist used as
1798 a communication channel."
1799 (progn
1800 (concat (if (and contents
1801 org-e-groff-table-scientific-notation
1802 (string-match orgtbl-exp-regexp contents))
1803 ;; Use appropriate format string for scientific
1804 ;; notation.
1805 (format org-e-groff-table-scientific-notation
1806 (match-string 1 contents)
1807 (match-string 2 contents))
1808 contents)
1809 (when (org-export-get-next-element table-cell info) "\t"))))
1812 ;;; Table Row
1814 (defun org-e-groff-table-row (table-row contents info)
1815 "Transcode a TABLE-ROW element from Org to Groff
1816 CONTENTS is the contents of the row. INFO is a plist used as
1817 a communication channel."
1818 ;; Rules are ignored since table separators are deduced from
1819 ;; borders of the current row.
1820 (when (eq (org-element-property :type table-row) 'standard)
1821 (let* ((attr (mapconcat 'identity
1822 (org-element-property
1823 :attr_groff (org-export-get-parent table-row))
1824 " "))
1825 ;; TABLE-ROW's borders are extracted from its first cell.
1826 (borders
1827 (org-export-table-cell-borders
1828 (car (org-element-contents table-row)) info)))
1829 (concat
1830 ;; Mark horizontal lines
1831 (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
1832 contents
1833 (cond
1834 ;; When BOOKTABS are activated enforce bottom rule even when
1835 ;; no hline was specifically marked.
1836 ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
1837 ((memq 'below borders) "\n_"))))))
1839 ;;; Target
1841 (defun org-e-groff-target (target contents info)
1842 "Transcode a TARGET object from Org to Groff.
1843 CONTENTS is nil. INFO is a plist holding contextual
1844 information."
1845 (format "\\fI%s\\fP"
1846 (org-export-solidify-link-text (org-element-property :value target))))
1848 ;;; Timestamp
1850 (defun org-e-groff-timestamp (timestamp contents info)
1851 "Transcode a TIMESTAMP object from Org to Groff.
1852 CONTENTS is nil. INFO is a plist holding contextual
1853 information."
1854 (let ((value (org-translate-time
1855 (org-element-property :raw-value timestamp))))
1856 (case (org-element-property :type timestamp)
1857 ((active active-range)
1858 (format org-e-groff-active-timestamp-format value))
1859 ((inactive inactive-range)
1860 (format org-e-groff-inactive-timestamp-format value))
1861 (t (format org-e-groff-diary-timestamp-format value)))))
1863 ;;; Underline
1865 (defun org-e-groff-underline (underline contents info)
1866 "Transcode UNDERLINE from Org to Groff.
1867 CONTENTS is the text with underline markup. INFO is a plist
1868 holding contextual information."
1869 (org-e-groff--text-markup contents 'underline))
1871 ;;; Verbatim
1873 (defun org-e-groff-verbatim (verbatim contents info)
1874 "Transcode a VERBATIM object from Org to Groff.
1875 CONTENTS is nil. INFO is a plist used as a communication
1876 channel."
1877 (org-e-groff--text-markup (org-element-property :value verbatim) 'verbatim))
1879 ;;; Verse Block
1881 (defun org-e-groff-verse-block (verse-block contents info)
1882 "Transcode a VERSE-BLOCK element from Org to Groff.
1883 CONTENTS is verse block contents. INFO is a plist holding
1884 contextual information."
1885 (format ".DS C\n.ft HI\n%s\n.ft\n.DE" contents))
1888 ;;; Interactive functions
1890 (defun org-e-groff-export-to-groff
1891 (&optional subtreep visible-only body-only ext-plist pub-dir)
1892 "Export current buffer to a Groff file.
1894 If narrowing is active in the current buffer, only export its
1895 narrowed part.
1897 If a region is active, export that region.
1899 When optional argument SUBTREEP is non-nil, export the sub-tree
1900 at point, extracting information from the headline properties
1901 first.
1903 When optional argument VISIBLE-ONLY is non-nil, don't export
1904 contents of hidden elements.
1906 EXT-PLIST, when provided, is a property list with external
1907 parameters overriding Org default settings, but still inferior to
1908 file-local settings.
1910 When optional argument PUB-DIR is set, use it as the publishing
1911 directory.
1913 Return output file's name."
1914 (interactive)
1915 (setq org-e-groff-registered-references nil)
1916 (setq org-e-groff-special-content nil)
1917 (let ((outfile (org-export-output-file-name ".groff" subtreep pub-dir)))
1918 (org-export-to-file
1919 'e-groff outfile subtreep visible-only body-only ext-plist)))
1921 (defun org-e-groff-export-to-pdf
1922 (&optional subtreep visible-only body-only ext-plist pub-dir)
1923 "Export current buffer to Groff then process through to PDF.
1925 If narrowing is active in the current buffer, only export its
1926 narrowed part.
1928 If a region is active, export that region.
1930 When optional argument SUBTREEP is non-nil, export the sub-tree
1931 at point, extracting information from the headline properties
1932 first.
1934 When optional argument VISIBLE-ONLY is non-nil, don't export
1935 contents of hidden elements.
1937 EXT-PLIST, when provided, is a property list with external
1938 parameters overriding Org default settings, but still inferior to
1939 file-local settings.
1941 When optional argument PUB-DIR is set, use it as the publishing
1942 directory.
1944 Return PDF file's name."
1945 (interactive)
1946 (org-e-groff-compile
1947 (org-e-groff-export-to-groff
1948 subtreep visible-only body-only ext-plist pub-dir)))
1950 (defun org-e-groff-compile (file)
1951 "Compile a Groff file.
1953 FILE is the name of the file being compiled. Processing is done
1954 through the command specified in `org-e-groff-pdf-process'.
1956 Return PDF file name or an error if it couldn't be produced."
1957 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1958 (full-name (file-truename file))
1959 (out-dir (file-name-directory file))
1960 ;; Make sure `default-directory' is set to FILE directory,
1961 ;; not to whatever value the current buffer may have.
1962 (default-directory (file-name-directory full-name))
1963 errors)
1964 (message (format "Processing Groff file %s ..." file))
1965 (save-window-excursion
1966 (cond
1967 ;; A function is provided: Apply it.
1968 ((functionp org-e-groff-pdf-process)
1969 (funcall org-e-groff-pdf-process (shell-quote-argument file)))
1970 ;; A list is provided: Replace %b, %f and %o with appropriate
1971 ;; values in each command before applying it. Output is
1972 ;; redirected to "*Org PDF Groff Output*" buffer.
1973 ((consp org-e-groff-pdf-process)
1974 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1975 (mapc
1976 (lambda (command)
1977 (shell-command
1978 (replace-regexp-in-string
1979 "%b" (shell-quote-argument base-name)
1980 (replace-regexp-in-string
1981 "%f" (shell-quote-argument full-name)
1982 (replace-regexp-in-string
1983 "%o" (shell-quote-argument out-dir) command t t)
1984 t t) t t)
1985 outbuf))
1986 org-e-groff-pdf-process)
1987 ;; Collect standard errors from output buffer.
1988 (setq errors (org-e-groff-collect-errors outbuf))))
1989 (t (error "No valid command to process to PDF")))
1990 (let ((pdffile (concat out-dir base-name ".pdf")))
1991 ;; Check for process failure. Provide collected errors if
1992 ;; possible.
1993 (if (not (file-exists-p pdffile))
1994 (error (concat (format "PDF file %s wasn't produced" pdffile)
1995 (when errors (concat ": " errors))))
1996 ;; Else remove log files, when specified, and signal end of
1997 ;; process to user, along with any error encountered.
1998 (when org-e-groff-remove-logfiles
1999 (dolist (ext org-e-groff-logfiles-extensions)
2000 (let ((file (concat out-dir base-name "." ext)))
2001 (when (file-exists-p file) (delete-file file)))))
2002 (message (concat "Process completed"
2003 (if (not errors) "."
2004 (concat " with errors: " errors)))))
2005 ;; Return output file name.
2006 pdffile))))
2008 (defun org-e-groff-collect-errors (buffer)
2009 "Collect some kind of errors from \"groff\" output
2010 BUFFER is the buffer containing output.
2011 Return collected error types as a string, or nil if there was
2012 none."
2013 (with-current-buffer buffer
2014 (save-excursion
2015 (goto-char (point-max))
2016 ;; Find final run
2017 nil)))
2020 (provide 'org-e-groff)
2021 ;;; org-e-groff.el ends here