org-agenda.el: New options to limit the number of displayed entries
[org-mode.git] / contrib / lisp / org-e-groff.el
blobc7fbeaca1fc077626ed00abb21a2e733ae151f7b
1 ;; org-e-groff.el --- Groff Back-End For Org Export Engine
3 ;; Copyright (C) 2011-2013 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 (a s v b)
104 (if a (org-e-groff-export-to-pdf t s v b)
105 (org-open-file (org-e-groff-export-to-pdf nil s v b)))))))
106 :options-alist
107 ((:groff-class "GROFF_CLASS" nil org-e-groff-default-class t)
108 (:groff-class-options "GROFF_CLASS_OPTIONS" nil nil t)
109 (:groff-header-extra "GROFF_HEADER" nil nil newline)))
113 ;;; User Configurable Variables
115 (defgroup org-export-e-groff nil
116 "Options for exporting Org mode files to Groff."
117 :tag "Org Export Groff"
118 :group 'org-export)
120 ;;; Preamble
122 (defcustom org-e-groff-default-class "internal"
123 "The default Groff class."
124 :group 'org-export-e-groff
125 :type '(string :tag "Groff class"))
127 (defcustom org-e-groff-classes
128 '(("file" ".MT 1"
129 (:heading 'default :type "memo" :last-section "toc"))
130 ("internal" ".MT 0"
131 (:heading 'default :type "memo" :last-section "toc"))
132 ("programmer" ".MT 2"
133 (:heading 'default :type "memo" :last-section "toc"))
134 ("engineer" ".MT 3"
135 (:heading 'default :type "memo" :last-section "toc"))
136 ("external" ".MT 4"
137 (:heading 'default :type "memo" :last-section "toc"))
138 ("letter" ".MT 5"
139 (:heading 'default :type "memo" :last-section "sign"))
140 ("custom" ".so file"
141 (:heading custom-function :type "custom" :last-section "toc"))
142 ("dummy" ""
143 (:heading 'default :type "memo"))
144 ("ms" "ms"
145 (:heading 'default :type "cover" :last-section "toc"))
146 ("se_ms" "se_ms"
147 (:heading 'default :type "cover" :last-section "toc"))
148 ("block" "BL"
149 (:heading 'default :type "letter" :last-section "sign"))
150 ("semiblock" "SB"
151 (:heading 'default :type "letter" :last-section "sign"))
152 ("fullblock" "FB"
153 (:heading 'default :type "letter" :last-section "sign"))
154 ("simplified" "SP"
155 (:heading 'default :type "letter" :last-section "sign"))
156 ("none" "" (:heading 'default :type "custom")))
158 ;; none means, no Cover or Memorandum Type and no calls to AU, AT, ND and TL
159 ;; This is to facilitate the creation of custom pages.
161 ;; dummy means, no Cover or Memorandum Type but calls to AU, AT, ND and TL
162 ;; are made. This is to facilitate Abstract Insertion.
164 "This list describes the attributes for the documents being created.
165 It allows for the creation of new "
166 :group 'org-export-e-groff
167 :type '(repeat
168 (list (string :tag "Document Type")
169 (string :tag "Header")
170 (repeat :tag "Options" :inline t
171 (choice
172 (list :tag "Heading")
173 (function :tag "Hook computing sectioning"))))))
175 ;;; Headline
177 (defconst org-e-groff-special-tags
178 '("FROM" "TO" "ABSTRACT" "APPENDIX" "BODY" "NS"))
180 (defcustom org-e-groff-format-headline-function nil
181 "Function to format headline text.
183 This function will be called with 5 arguments:
184 TODO the todo keyword (string or nil).
185 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
186 PRIORITY the priority of the headline (integer or nil)
187 TEXT the main headline text (string).
188 TAGS the tags as a list of strings (list of strings or nil).
190 The function result will be used in the section format string.
192 As an example, one could set the variable to the following, in
193 order to reproduce the default set-up:
195 \(defun org-e-groff-format-headline (todo todo-type priority text tags)
196 \"Default format function for an headline.\"
197 \(concat (when todo
198 \(format \"\\fB%s\\fP \" todo))
199 \(when priority
200 \(format \"[\\#%c] \" priority))
201 text
202 \(when tags
203 \(format \" %s \"
204 \(mapconcat 'identity tags \":\"))))"
205 :group 'org-export-e-groff
206 :type 'function)
208 ;;; Timestamps
210 (defcustom org-e-groff-active-timestamp-format "\\fI%s\\fP"
211 "A printf format string to be applied to active timestamps."
212 :group 'org-export-e-groff
213 :type 'string)
215 (defcustom org-e-groff-inactive-timestamp-format "\\fI%s\\fP"
216 "A printf format string to be applied to inactive timestamps."
217 :group 'org-export-e-groff
218 :type 'string)
220 (defcustom org-e-groff-diary-timestamp-format "\\fI%s\\fP"
221 "A printf format string to be applied to diary timestamps."
222 :group 'org-export-e-groff
223 :type 'string)
225 ;;; Links
227 (defcustom org-e-groff-inline-image-rules
228 '(("file" . "\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'")
229 ("fuzzy" . "\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'"))
230 "Rules characterizing image files that can be inlined into Groff.
232 A rule consists in an association whose key is the type of link
233 to consider, and value is a regexp that will be matched against
234 link's path.
236 Note that, by default, the image extensions actually allowed
237 depend on the way the Groff file is processed. When used with
238 pdfgroff, pdf, jpg and png images are OK. When processing
239 through dvi to Postscript, only ps and eps are allowed. The
240 default we use here encompasses both."
241 :group 'org-export-e-groff
242 :type '(alist :key-type (string :tag "Type")
243 :value-type (regexp :tag "Path")))
245 (defcustom org-e-groff-link-with-unknown-path-format "\\fI%s\\fP"
246 "Format string for links with unknown path type."
247 :group 'org-export-groff
248 :type 'string)
250 ;;; Tables
252 (defcustom org-e-groff-tables-centered t
253 "When non-nil, tables are exported in a center environment."
254 :group 'org-export-e-groff
255 :type 'boolean)
257 (defcustom org-e-groff-tables-verbatim nil
258 "When non-nil, tables are exported verbatim."
259 :group 'org-export-e-groff
260 :type 'boolean)
262 (defcustom org-e-groff-table-scientific-notation "%sE%s"
263 "Format string to display numbers in scientific notation.
264 The format should have \"%s\" twice, for mantissa and exponent
265 \(i.e. \"%s\\\\times10^{%s}\").
267 When nil, no transformation is made."
268 :group 'org-export-e-groff
269 :type '(choice
270 (string :tag "Format string")
271 (const :tag "No formatting")))
273 ;;; Text markup
275 (defcustom org-e-groff-text-markup-alist
276 '((bold . "\\fB%s\\fP")
277 (code . "\\fC%s\\fP")
278 (italic . "\\fI%s\\fP")
279 (strike-through . "\\fC%s\\fP") ; Strike through and underline
280 (underline . "\\fI%s\\fP") ; need to be revised.
281 (verbatim . "protectedtexttt"))
282 "Alist of Groff expressions to convert text markup.
284 The key must be a symbol among `bold', `code', `italic',
285 `strike-through', `underline' and `verbatim'. The value is
286 a formatting string to wrap fontified text with it.
288 If no association can be found for a given markup, text will be
289 returned as-is."
290 :group 'org-export-e-groff
291 :type 'alist
292 :options '(bold code italic strike-through underline verbatim))
294 ;;; Drawers
296 (defcustom org-e-groff-format-drawer-function nil
297 "Function called to format a drawer in Groff code.
299 The function must accept two parameters:
300 NAME the drawer name, like \"LOGBOOK\"
301 CONTENTS the contents of the drawer.
303 The function should return the string to be exported.
305 For example, the variable could be set to the following function
306 in order to mimic default behaviour:
308 \(defun org-e-groff-format-drawer-default \(name contents\)
309 \"Format a drawer element for Groff export.\"
310 contents\)"
311 :group 'org-export-e-groff
312 :type 'function)
314 ;;; Inlinetasks
316 (defcustom org-e-groff-format-inlinetask-function nil
317 "Function called to format an inlinetask in Groff code.
319 The function must accept six parameters:
320 TODO the todo keyword, as a string
321 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
322 PRIORITY the inlinetask priority, as a string
323 NAME the inlinetask name, as a string.
324 TAGS the inlinetask tags, as a list of strings.
325 CONTENTS the contents of the inlinetask, as a string.
327 The function should return the string to be exported.
329 For example, the variable could be set to the following function
330 in order to mimic default behaviour:
332 \(defun org-e-groff-format-inlinetask \(todo type priority name tags contents\)
333 \"Format an inline task element for Groff export.\"
334 \(let ((full-title
335 \(concat
336 \(when todo
337 \(format \"\\fB%s\\fP \" todo))
338 \(when priority (format \"[\\#%c] \" priority))
339 title
340 \(when tags
341 \(format \":%s:\"
342 \(mapconcat 'identity tags \":\")))))
343 \(format (concat \".DS L\\n\"
344 \"%s\\n\\n\"
345 \"%s\"
346 \".DE\")
347 full-title contents))"
348 :group 'org-export-e-groff
349 :type 'function)
351 ;; Src blocks
353 (defcustom org-e-groff-source-highlight nil
354 "Use GNU source highlight to embellish source blocks "
355 :group 'org-export-e-groff
356 :type 'boolean)
358 (defcustom org-e-groff-source-highlight-langs
359 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
360 (scheme "scheme")
361 (c "c") (cc "cpp") (csharp "csharp") (d "d")
362 (fortran "fortran") (cobol "cobol") (pascal "pascal")
363 (ada "ada") (asm "asm")
364 (perl "perl") (cperl "perl")
365 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
366 (java "java") (javascript "javascript")
367 (tex "latex")
368 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
369 (ocaml "caml") (caml "caml")
370 (sql "sql") (sqlite "sql")
371 (html "html") (css "css") (xml "xml")
372 (bat "bat") (bison "bison") (clipper "clipper")
373 (ldap "ldap") (opa "opa")
374 (php "php") (postscript "postscript") (prolog "prolog")
375 (properties "properties") (makefile "makefile")
376 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
377 "Alist mapping languages to their listing language counterpart.
378 The key is a symbol, the major mode symbol without the \"-mode\".
379 The value is the string that should be inserted as the language
380 parameter for the listings package. If the mode name and the
381 listings name are the same, the language does not need an entry
382 in this list - but it does not hurt if it is present."
383 :group 'org-export-e-groff
384 :type '(repeat
385 (list
386 (symbol :tag "Major mode ")
387 (string :tag "Listings language"))))
389 (defcustom org-e-groff-source-highlight-options nil
390 "Association list of options for the groff listings package.
392 These options are supplied as a comma-separated list to the
393 \\lstset command. Each element of the association list should be
394 a list containing two strings: the name of the option, and the
395 value. For example,
397 (setq org-e-groff-source-highlight-options
398 '((\"basicstyle\" \"\\small\")
399 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
401 will typeset the code in a small size font with underlined, bold
402 black keywords.
404 Note that the same options will be applied to blocks of all
405 languages."
406 :group 'org-export-e-groff
407 :type '(repeat
408 (list
409 (string :tag "Listings option name ")
410 (string :tag "Listings option value"))))
412 (defvar org-e-groff-custom-lang-environments nil
413 "Alist mapping languages to language-specific Groff environments.
415 It is used during export of src blocks by the listings and
416 groff packages. For example,
418 \(setq org-e-groff-custom-lang-environments
419 '\(\(python \"pythoncode\"\)\)\)
421 would have the effect that if org encounters begin_src python
422 during groff export it will use pythoncode as the source-highlight
423 language.")
425 ;;; Plain text
427 (defcustom org-e-groff-special-char
428 '(("(c)" . "\\\\(co")
429 ("(tm)" . "\\\\(tm")
430 ("(rg)" . "\\\\(rg"))
431 "CONS list in which the value of the car
432 is replace on the value of the CDR. "
433 :group 'org-export-e-groff
434 :type '(list
435 (cons :tag "Character Subtitute"
436 (string :tag "Original Character Group")
437 (string :tag "Replacement Character"))))
439 ;;; Compilation
441 (defcustom org-e-groff-pdf-process
442 '("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
443 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
444 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf")
446 "Commands to process a Groff file to a PDF file.
447 This is a list of strings, each of them will be given to the
448 shell as a command. %f in the command will be replaced by the
449 full file name, %b by the file base name \(i.e. without
450 extension) and %o by the base directory of the file."
451 :group 'org-export-pdf
452 :type '(choice
453 (repeat :tag "Shell command sequence"
454 (string :tag "Shell command"))
455 (const :tag "2 runs of pdfgroff"
456 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
457 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
458 (const :tag "3 runs of pdfgroff"
459 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
460 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
461 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
462 (function)))
464 (defcustom org-e-groff-logfiles-extensions
465 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
466 "The list of file extensions to consider as Groff logfiles."
467 :group 'org-export-e-groff
468 :type '(repeat (string :tag "Extension")))
470 (defcustom org-e-groff-remove-logfiles t
471 "Non-nil means remove the logfiles produced by PDF production.
472 These are the .aux, .log, .out, and .toc files."
473 :group 'org-export-e-groff
474 :type 'boolean)
476 (defcustom org-e-groff-organization "Org User"
477 "Name of the organization used to populate the .AF command."
478 :group 'org-export-e-groff
479 :type 'string)
481 (defcustom org-e-groff-raster-to-ps nil
482 "Command used to convert raster to EPS. Nil for no conversion. Make sure that
483 `org-e-groff-inline-image-rules' is adjusted accordingly if not conversion is being
484 done. In this case, remove the entries for jpg and png in the file and fuzzy lists."
485 :group 'org-export-e-groff
486 :type '(choice
487 (repeat :tag "Shell Command Sequence" (string :tag "Shell Command"))
488 (const :tag "sam2p" "a=%s;b=%s;sam2p ${a} ${b} ;grep -v BeginData ${b} > b_${b};mv b_${b} ${b}" )
489 (const :tag "NetPNM" "a=%s;b=%s;pngtopnm ${a} | pnmtops -noturn > ${b}" )
490 (const :tag "None" nil)))
492 (defvar org-e-groff-registered-references nil)
493 (defvar org-e-groff-special-content nil)
497 ;;; Internal Functions
499 (defun org-e-groff--caption/label-string (element info)
500 "Return caption and label Groff string for ELEMENT.
502 INFO is a plist holding contextual information. If there's no
503 caption nor label, return the empty string.
505 For non-floats, see `org-e-groff--wrap-label'."
506 (let ((main (org-export-get-caption element))
507 (short (org-export-get-caption element t))
508 (label (org-element-property :name element)))
509 (cond ((and (not main) (not label)) "")
510 ((not main) (format "\\fI%s\\fP" label))
511 ;; Option caption format with short name.
512 (short (format "%s\n.br\n - %s\n"
513 (org-export-data short info)
514 (org-export-data main info)))
515 ;; Standard caption format.
516 (t (format "\\fR%s\\fP" (org-export-data main info))))))
518 (defun org-e-groff--wrap-label (element output)
519 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
520 This function shouldn't be used for floats. See
521 `org-e-groff--caption/label-string'."
522 (let ((label (org-element-property :name element)))
523 (if (or (not output) (not label) (string= output "") (string= label ""))
524 output
525 (concat (format "%s\n.br\n" label) output))))
527 (defun org-e-groff--text-markup (text markup)
528 "Format TEXT depending on MARKUP text markup.
529 See `org-e-groff-text-markup-alist' for details."
530 (let ((fmt (cdr (assq markup org-e-groff-text-markup-alist))))
531 (cond
532 ;; No format string: Return raw text.
533 ((not fmt) text)
534 ((string= "protectedtexttt" fmt)
535 (let ((start 0)
536 (trans '(("\\" . "\\")))
537 (rtn "")
538 char)
539 (while (string-match "[\\{}$%&_#~^]" text)
540 (setq char (match-string 0 text))
541 (if (> (match-beginning 0) 0)
542 (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
543 (setq text (substring text (1+ (match-beginning 0))))
544 (setq char (or (cdr (assoc char trans)) (concat "\\" char))
545 rtn (concat rtn char)))
546 (setq text (concat rtn text))
547 (format "\\fC%s\\fP" text)))
548 ;; Else use format string.
549 (t (format fmt text)))))
552 (defun org-e-groff--get-tagged-content (tag info)
553 (cdr (assoc tag org-e-groff-special-content)))
555 (defun org-e-groff--mt-head (title contents attr info)
556 (concat
558 ;; 1. Insert Organization
559 (let ((firm-option (plist-get attr :firm)))
560 (cond
561 ((stringp firm-option)
562 (format ".AF \"%s\" \n" firm-option))
563 (t (format ".AF \"%s\" \n" (or org-e-groff-organization "")))))
565 ;; 2. Title
566 (let ((subtitle1 (plist-get attr :subtitle1))
567 (subtitle2 (plist-get attr :subtitle2)))
569 (cond
570 ((string= "" title)
571 (format ".TL \"%s\" \"%s\" \n%s\n"
572 (or subtitle1 "")
573 (or subtitle2 "") " "))
575 ((not (or subtitle1 subtitle2))
576 (format ".TL\n%s\n"
577 (or title "")))
579 (format ".TL \"%s\" \"%s \" \n%s\n"
580 (or subtitle1 "")
581 (or subtitle2 "") title))))
583 ;; 3. Author.
584 ;; In Groff, .AU *MUST* be placed after .TL
585 ;; If From, populate with data from From else
587 (let ((author (and (plist-get info :with-author)
588 (let ((auth (plist-get info :author)))
589 (and auth (org-export-data auth info)))))
590 (email (and (plist-get info :with-email)
591 (org-export-data (plist-get info :email) info)))
592 (from-data (org-e-groff--get-tagged-content "FROM" info))
594 (to-data (org-e-groff--get-tagged-content "TO" info)))
596 (cond
597 ((and author from-data)
598 (let ((au-line
599 (mapconcat
600 (lambda (from-line)
601 (format " \"%s\" " from-line))
602 (split-string
603 (setq from-data
604 (replace-regexp-in-string "\\.P\n" "" from-data)) "\n") "")))
606 (concat
607 (format ".AU \"%s\" " author) au-line "\n")))
609 ((and author email (not (string= "" email)))
610 (format ".AU \"%s\" \"%s\"\n" author email))
612 (author (format ".AU \"%s\"\n" author))
614 (t ".AU \"\" \n")))
617 ;; 4. Author Title, if present
618 (let ((at-item (plist-get attr :author-title)))
619 (if (and at-item (stringp at-item))
620 (format ".AT \"%s\" \n" at-item)
621 ""))
623 ;; 5. Date.
624 (when (plist-get info :with-date)
625 (let ((date (org-export-data (plist-get info :date) info)))
626 (and date (format ".ND \"%s\"\n" date))))
629 ;; If Abstract, then Populate Abstract
632 (let ((abstract-data (org-e-groff--get-tagged-content "ABSTRACT" info))
633 (to-data (org-e-groff--get-tagged-content "TO" info)))
634 (cond
635 (abstract-data
636 (format ".AS\n%s\n.AE\n" abstract-data))
637 (to-data
638 (format ".AS\n%s\n.AE\n" to-data))))))
640 (defun org-e-groff--letter-head (title contents attr info)
641 (let ((author (and (plist-get info :with-author)
642 (let ((auth (plist-get info :author)))
643 (and auth (org-export-data auth info)))))
644 (email (and (plist-get info :with-email)
645 (org-export-data (plist-get info :email) info)))
646 (from-data (org-e-groff--get-tagged-content "FROM" info))
647 (at-item (plist-get attr :author-title))
648 (to-data (org-e-groff--get-tagged-content "TO" info)))
651 ;; If FROM then get data from FROM
652 (if from-data
653 (setq from-data
654 (replace-regexp-in-string "\\.P\n" "" from-data))
655 (setq from-data ""))
657 (if to-data
658 (setq to-data
659 (replace-regexp-in-string "\\.P\n" "" to-data))
660 (setq from-data ""))
662 (concat
663 (cond
664 (from-data
665 (format ".WA \"%s\" \"%s\" \n%s\n.WE\n" author (or at-item "") from-data))
666 ((and author email (not (string= "" email)))
667 (format ".WA \"%s\"\n \"%s\"\n.WE\n" author email))
668 (author (format ".WA \"%s\"\n.WE\n" author))
669 (t ".WA \"\" \n.WE\n"))
671 ;; If TO then get data from TO
673 (when to-data
674 (format ".IA \n%s\n.IE\n" to-data)))))
677 ;;; Template
679 (defun org-e-groff-template (contents info)
680 "Return complete document string after Groff conversion.
681 CONTENTS is the transcoded contents string. INFO is a plist
682 holding export options."
683 (let* ((title (org-export-data (plist-get info :title) info))
684 (attr (read
685 (format "(%s)"
686 (mapconcat
687 #'identity
688 (list (plist-get info :groff-class-options))
689 " "))))
690 (class (plist-get info :groff-class))
691 (class-options (plist-get info :groff-class-options))
692 (classes (assoc class org-e-groff-classes))
693 (classes-options (car (last classes)))
694 (heading-option (plist-get classes-options :heading))
695 (type-option (plist-get classes-options :type))
696 (last-option (plist-get classes-options :last-section))
697 (hyphenate (plist-get attr :hyphenate))
698 (justify-right (plist-get attr :justify-right))
700 (document-class-string
701 (progn
702 (org-element-normalize-string
703 (let* ((header (nth 1 (assoc class org-e-groff-classes)))
704 (document-class-item (if (stringp header) header "")))
705 document-class-item)))))
708 (concat
709 (if justify-right
710 (case justify-right
711 ('yes ".SA 1 \n")
712 ('no ".SA 0 \n")
713 (t ""))
716 (if hyphenate
717 (case hyphenate
718 ('yes ".nr Hy 1 \n")
719 ('no ".nr Hy 0 \n")
720 (t ""))
723 (cond
724 ((string= type-option "custom") "")
726 ((and (stringp document-class-string)
727 (string= type-option "cover"))
729 (concat
730 (format ".COVER %s\n" document-class-string)
731 (org-e-groff--mt-head title contents attr info)
732 ".COVEND\n"))
734 ((string= type-option "memo")
735 (concat
736 (org-e-groff--mt-head title contents attr info)
737 document-class-string))
738 ((string= type-option "letter")
739 (concat
740 (org-e-groff--letter-head title contents attr info)
741 (let ((sa-item (plist-get attr :salutation))
742 (cn-item (plist-get attr :confidential))
743 (sj-item (plist-get attr :subject))
744 (rn-item (plist-get attr :reference))
745 (at-item (plist-get attr :attention)))
747 (concat
749 (if (stringp sa-item)
750 (format ".LO SA \"%s\" \n" sa-item)
751 ".LO SA\n")
753 (when cn-item
754 (if (stringp cn-item)
755 (format ".LO CN \"%s\"\n" cn-item)
756 ".LO CN\n"))
758 (when (and at-item (stringp at-item))
759 (format ".LO AT \"%s\" \n" at-item))
760 (when (and title rn-item)
761 (format ".LO RN \"%s\"\n" title))
763 (when (and sj-item (stringp sj-item))
764 (format ".LO SJ \"%s\" \n" sj-item))
767 ".LT " document-class-string "\n"))))
769 (t ""))
771 contents
773 (cond
774 ((string= last-option "toc")
775 ".TC")
776 ((string= last-option "sign")
777 (let ((fc-item (plist-get attr :closing)))
778 (concat (if (stringp fc-item)
779 (format ".FC \"%s\" \n" fc-item)
780 ".FC\n")
781 ".SG\n")))
782 (t ""))
784 (progn
785 (mapconcat
786 (lambda (item)
787 (when (string= (car item) "NS")
788 (replace-regexp-in-string
789 "\\.P\n" "" (cdr item))))
790 (reverse org-e-groff-special-content) "\n")))))
794 ;;; Transcode Functions
796 ;;; Babel Call
798 ;; Babel Calls are ignored.
801 ;;; Bold
803 (defun org-e-groff-bold (bold contents info)
804 "Transcode BOLD from Org to Groff.
805 CONTENTS is the text with bold markup. INFO is a plist holding
806 contextual information."
807 (org-e-groff--text-markup contents 'bold))
809 ;;; Center Block
811 (defun org-e-groff-center-block (center-block contents info)
812 "Transcode a CENTER-BLOCK element from Org to Groff.
813 CONTENTS holds the contents of the center block. INFO is a plist
814 holding contextual information."
815 (org-e-groff--wrap-label
816 center-block
817 (format ".DS C \n%s\n.DE" contents)))
819 ;;; Clock
821 (defun org-e-groff-clock (clock contents info)
822 "Transcode a CLOCK element from Org to Groff.
823 CONTENTS is nil. INFO is a plist holding contextual
824 information."
825 (concat
826 (format "\\fB%s\\fP " org-clock-string)
827 (format org-e-groff-inactive-timestamp-format
828 (concat (org-translate-time
829 (org-element-property :raw-value
830 (org-element-property :value clock)))
831 (let ((time (org-element-property :duration clock)))
832 (and time (format " (%s)" time)))))))
834 ;;; Code
836 (defun org-e-groff-code (code contents info)
837 "Transcode a CODE object from Org to Groff.
838 CONTENTS is nil. INFO is a plist used as a communication
839 channel."
840 (org-e-groff--text-markup (org-element-property :value code) 'code))
842 ;;; Comments and Comment Blocks are ignored.
844 ;;; Drawer
846 (defun org-e-groff-drawer (drawer contents info)
847 "Transcode a DRAWER element from Org to Groff.
848 CONTENTS holds the contents of the block. INFO is a plist
849 holding contextual information."
850 (let* ((name (org-element-property :drawer-name drawer))
851 (output (if (functionp org-e-groff-format-drawer-function)
852 (funcall org-e-groff-format-drawer-function
853 name contents)
854 ;; If there's no user defined function: simply
855 ;; display contents of the drawer.
856 contents)))
857 (org-e-groff--wrap-label drawer output)))
859 ;;; Dynamic Block
861 (defun org-e-groff-dynamic-block (dynamic-block contents info)
862 "Transcode a DYNAMIC-BLOCK element from Org to Groff.
863 CONTENTS holds the contents of the block. INFO is a plist
864 holding contextual information. See `org-export-data'."
865 (org-e-groff--wrap-label dynamic-block contents))
867 ;;; Entity
869 (defun org-e-groff-entity (entity contents info)
870 "Transcode an ENTITY object from Org to Groff.
871 CONTENTS are the definition itself. INFO is a plist holding
872 contextual information."
873 (org-element-property :utf-8 entity))
875 ;;; Example Block
877 (defun org-e-groff-example-block (example-block contents info)
878 "Transcode an EXAMPLE-BLOCK element from Org to Groff.
879 CONTENTS is nil. INFO is a plist holding contextual
880 information."
881 (org-e-groff--wrap-label
882 example-block
883 (format ".DS L\n%s\n.DE"
884 (org-export-format-code-default example-block info))))
886 ;;; Export Block
888 (defun org-e-groff-export-block (export-block contents info)
889 "Transcode a EXPORT-BLOCK element from Org to Groff.
890 CONTENTS is nil. INFO is a plist holding contextual information."
891 (when (string= (org-element-property :type export-block) "GROFF")
892 (org-remove-indentation (org-element-property :value export-block))))
894 ;;; Export Snippet
896 (defun org-e-groff-export-snippet (export-snippet contents info)
897 "Transcode a EXPORT-SNIPPET object from Org to Groff.
898 CONTENTS is nil. INFO is a plist holding contextual information."
899 (when (eq (org-export-snippet-backend export-snippet) 'e-groff)
900 (org-element-property :value export-snippet)))
902 ;;; Fixed Width
904 (defun org-e-groff-fixed-width (fixed-width contents info)
905 "Transcode a FIXED-WIDTH element from Org to Groff.
906 CONTENTS is nil. INFO is a plist holding contextual information."
907 (org-e-groff--wrap-label
908 fixed-width
909 (format "\\fC\n%s\\fP"
910 (org-remove-indentation
911 (org-element-property :value fixed-width)))))
913 ;;; Footnote Definition
915 ;; Footnote Definitions are ignored.
917 ;; Footnotes are handled automatically in GROFF. Although manual
918 ;; references can be added, not really required.
920 (defun org-e-groff-footnote-reference (footnote-reference contents info)
921 ;; Changing from info to footnote-reference
922 (let* ((raw (org-export-get-footnote-definition footnote-reference info))
923 (n (org-export-get-footnote-number footnote-reference info))
924 (data (org-trim (org-export-data raw info)))
925 (ref-id (plist-get (nth 1 footnote-reference) :label)))
926 ;; It is a reference
927 (if (string-match "fn:rl" ref-id)
928 (if (member ref-id org-e-groff-registered-references)
929 (format "\\*[%s]" ref-id)
930 (progn
931 (push ref-id org-e-groff-registered-references)
932 (format "\\*(Rf\n.RS \"%s\" \n%s\n.RF\n" ref-id data)))
933 ;; else it is a footnote
934 (format "\\u\\s-2%s\\d\\s+2\n.FS %s\n%s\n.FE\n" n n data))))
936 ;;; Headline
938 (defun org-e-groff-headline (headline contents info)
939 "Transcode an HEADLINE element from Org to Groff.
940 CONTENTS holds the contents of the headline. INFO is a plist
941 holding contextual information."
942 (let* ((class (plist-get info :groff-class))
943 (level (org-export-get-relative-level headline info))
944 (numberedp (org-export-numbered-headline-p headline info))
945 ;; Section formatting will set two placeholders: one for the
946 ;; title and the other for the contents.
947 (classes (assoc class org-e-groff-classes))
948 (classes-options (car (last classes)))
949 (heading-option (plist-get classes-options :heading))
950 (section-fmt
951 (progn
952 (cond
953 ((and (symbolp heading-option)
954 (fboundp heading-option))
955 (funcall heading-option level numberedp))
956 ((> level 7) nil)
957 (t (if numberedp
958 (concat ".H " (number-to-string level) " \"%s\"\n%s")
959 ".HU \"%s\"\n%s")))))
960 ;; End of section-fmt
961 (text (org-export-data (org-element-property :title headline) info))
962 (todo
963 (and (plist-get info :with-todo-keywords)
964 (let ((todo (org-element-property :todo-keyword headline)))
965 (and todo (org-export-data todo info)))))
966 (todo-type (and todo (org-element-property :todo-type headline)))
967 (tags (and (plist-get info :with-tags)
968 (org-export-get-tags headline info)))
969 (priority (and (plist-get info :with-priority)
970 (org-element-property :priority headline)))
971 ;; Create the headline text along with a no-tag version. The
972 ;; latter is required to remove tags from table of contents.
973 (full-text (if (functionp org-e-groff-format-headline-function)
974 ;; User-defined formatting function.
975 (funcall org-e-groff-format-headline-function
976 todo todo-type priority text tags)
977 ;; Default formatting.
978 (concat
979 (when todo
980 (format "\\fB%s\\fP " todo))
981 (when priority (format " [\\#%c] " priority))
982 text
983 (when tags
984 (format " \\fC:%s:\\fP "
985 (mapconcat 'identity tags ":"))))))
986 (full-text-no-tag
987 (if (functionp org-e-groff-format-headline-function)
988 ;; User-defined formatting function.
989 (funcall org-e-groff-format-headline-function
990 todo todo-type priority text nil)
991 ;; Default formatting.
992 (concat
993 (when todo (format "\\fB%s\\fP " todo))
994 (when priority (format " [\\#%c] " priority))
995 text)))
996 ;; Associate some \label to the headline for internal links.
997 ;; (headline-label
998 ;; (format "\\label{sec-%s}\n"
999 ;; (mapconcat 'number-to-string
1000 ;; (org-export-get-headline-number headline info)
1001 ;; "-")))
1002 (headline-label "")
1003 (pre-blanks
1004 (make-string (org-element-property :pre-blank headline) 10)))
1006 (cond
1007 ;; Case 1: Special Tag
1008 ((member (car tags) org-e-groff-special-tags)
1009 (cond
1010 ((string= (car tags) "BODY") contents)
1012 ((string= (car tags) "NS")
1013 (progn
1014 (push (cons (car tags)
1015 (format ".NS \"%s\" 1 \n%s"
1016 (car (org-element-property :title headline))
1017 (or contents " ")))
1018 org-e-groff-special-content) nil))
1021 (progn
1022 (push (cons (car tags) contents) org-e-groff-special-content)
1023 nil))))
1025 ;; Case 2: This is a footnote section: ignore it.
1026 ((org-element-property :footnote-section-p headline) nil)
1028 ;; Case 3: This is a deep sub-tree: export it as a list item.
1029 ;; Also export as items headlines for which no section
1030 ;; format has been found.
1031 ((or (not section-fmt) (org-export-low-level-p headline info))
1032 ;; Build the real contents of the sub-tree.
1033 (let ((low-level-body
1034 (concat
1035 ;; If the headline is the first sibling, start a list.
1036 (when (org-export-first-sibling-p headline info)
1037 (format "%s\n" (if numberedp ".AL 1\n" ".DL \n")))
1038 ;; Itemize headline
1039 ".LI\n" full-text "\n" headline-label pre-blanks contents)))
1040 ;; If headline is not the last sibling simply return
1041 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1042 ;; blank line.
1043 (if (not (org-export-last-sibling-p headline info)) low-level-body
1044 (replace-regexp-in-string
1045 "[ \t\n]*\\'"
1046 (concat "\n.LE")
1047 low-level-body))))
1049 ;; Case 4. Standard headline. Export it as a section.
1051 (format section-fmt full-text
1052 (concat headline-label pre-blanks contents))))))
1054 ;;; Horizontal Rule
1055 ;; Not supported
1057 ;;; Inline Babel Call
1059 ;; Inline Babel Calls are ignored.
1061 ;;; Inline Src Block
1063 (defun org-e-groff-inline-src-block (inline-src-block contents info)
1064 "Transcode an INLINE-SRC-BLOCK element from Org to Groff.
1065 CONTENTS holds the contents of the item. INFO is a plist holding
1066 contextual information."
1067 (let* ((code (org-element-property :value inline-src-block)))
1068 (cond
1069 (org-e-groff-source-highlight
1070 (let* ((tmpdir (if (featurep 'xemacs)
1071 temp-directory
1072 temporary-file-directory))
1073 (in-file (make-temp-name
1074 (expand-file-name "srchilite" tmpdir)))
1075 (out-file (make-temp-name
1076 (expand-file-name "reshilite" tmpdir)))
1077 (org-lang (org-element-property :language inline-src-block))
1078 (lst-lang (cadr (assq (intern org-lang)
1079 org-e-groff-source-highlight-langs)))
1081 (cmd (concat (expand-file-name "source-highlight")
1082 " -s " lst-lang
1083 " -f groff_mm_color "
1084 " -i " in-file
1085 " -o " out-file)))
1086 (if lst-lang
1087 (let ((code-block ""))
1088 (with-temp-file in-file (insert code))
1089 (shell-command cmd)
1090 (setq code-block (org-file-contents out-file))
1091 (delete-file in-file)
1092 (delete-file out-file)
1093 code-block)
1094 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1095 code))))
1097 ;; Do not use a special package: transcode it verbatim.
1099 (concat ".DS I\n" "\\fC" code "\\fP\n.DE\n")))))
1101 ;;; Inlinetask
1103 (defun org-e-groff-inlinetask (inlinetask contents info)
1104 "Transcode an INLINETASK element from Org to Groff.
1105 CONTENTS holds the contents of the block. INFO is a plist
1106 holding contextual information."
1107 (let ((title (org-export-data (org-element-property :title inlinetask) info))
1108 (todo (and (plist-get info :with-todo-keywords)
1109 (let ((todo (org-element-property :todo-keyword inlinetask)))
1110 (and todo (org-export-data todo info)))))
1111 (todo-type (org-element-property :todo-type inlinetask))
1112 (tags (and (plist-get info :with-tags)
1113 (org-export-get-tags inlinetask info)))
1114 (priority (and (plist-get info :with-priority)
1115 (org-element-property :priority inlinetask))))
1116 ;; If `org-e-groff-format-inlinetask-function' is provided, call it
1117 ;; with appropriate arguments.
1118 (if (functionp org-e-groff-format-inlinetask-function)
1119 (funcall org-e-groff-format-inlinetask-function
1120 todo todo-type priority title tags contents)
1121 ;; Otherwise, use a default template.
1122 (org-e-groff--wrap-label
1123 inlinetask
1124 (let ((full-title
1125 (concat
1126 (when todo (format "\\fB%s\\fP " todo))
1127 (when priority (format " [\\#%c] " priority))
1128 title
1129 (when tags (format " \\fC:%s:\\fP "
1130 (mapconcat 'identity tags ":"))))))
1131 (format (concat "\n.DS I\n"
1132 "%s\n"
1133 ".sp"
1134 "%s\n"
1135 ".DE")
1136 full-title contents))))))
1138 ;;; Italic
1140 (defun org-e-groff-italic (italic contents info)
1141 "Transcode ITALIC from Org to Groff.
1142 CONTENTS is the text with italic markup. INFO is a plist holding
1143 contextual information."
1144 (org-e-groff--text-markup contents 'italic))
1146 ;;; Item
1148 (defun org-e-groff-item (item contents info)
1149 "Transcode an ITEM element from Org to Groff.
1150 CONTENTS holds the contents of the item. INFO is a plist holding
1151 contextual information."
1152 (let* ((bullet (org-element-property :bullet item))
1153 (type (org-element-property
1154 :type (org-element-property :parent item)))
1155 (checkbox (case (org-element-property :checkbox item)
1156 (on "\\o'\\(sq\\(mu'")
1157 (off "\\(sq")
1158 (trans "\\o'\\(sq\\(mi'")))
1159 (tag (let ((tag (org-element-property :tag item)))
1160 ;; Check-boxes must belong to the tag.
1161 (and tag (format "%s"
1162 (concat checkbox
1163 (org-export-data tag info)))))))
1165 (cond
1166 ((or checkbox tag)
1167 (concat ".LI ""\"" (or tag (concat "\\ " checkbox)) "\""
1168 "\n"
1169 (org-trim (or contents " "))))
1170 ((eq type 'ordered)
1171 (concat ".LI"
1172 "\n"
1173 (org-trim (or contents " "))))
1175 (let* ((bullet (org-trim bullet))
1176 (marker (cond ((string= "-" bullet) "\\(em")
1177 ((string= "*" bullet) "\\(bu")
1178 (t "\\(dg"))))
1179 (concat ".LI " marker "\n"
1180 (org-trim (or contents " "))))))))
1182 ;;; Keyword
1184 (defun org-e-groff-keyword (keyword contents info)
1185 "Transcode a KEYWORD element from Org to Groff.
1186 CONTENTS is nil. INFO is a plist holding contextual information."
1187 (let ((key (org-element-property :key keyword))
1188 (value (org-element-property :value keyword)))
1189 (cond
1190 ((string= key "GROFF") value)
1191 (t nil))))
1193 ;;; LaTeX Environment
1195 (defun org-e-groff-latex-environment (latex-environment contents info)
1196 "Transcode a LATEX-ENVIRONMENT element from Org to Groff.
1197 CONTENTS is nil. INFO is a plist holding contextual information."
1198 (let ((label (org-element-property :name latex-environment))
1199 (value (org-remove-indentation
1200 (org-element-property :value latex-environment))))
1201 (if (not (org-string-nw-p label)) value
1202 ;; Environment is labelled: label must be within the environment
1203 ;; (otherwise, a reference pointing to that element will count
1204 ;; the section instead).
1205 (with-temp-buffer
1206 (insert value)
1207 (goto-char (point-min))
1208 (forward-line)
1209 (insert (format "%s\n" label))
1210 (buffer-string)))))
1212 ;;; LaTeX Fragment
1214 (defun org-e-groff-latex-fragment (latex-fragment contents info)
1215 "Transcode a LATEX-FRAGMENT object from Org to Groff.
1216 CONTENTS is nil. INFO is a plist holding contextual information."
1217 (org-element-property :value latex-fragment))
1219 ;;; Line Break
1221 (defun org-e-groff-line-break (line-break contents info)
1222 "Transcode a LINE-BREAK object from Org to Groff.
1223 CONTENTS is nil. INFO is a plist holding contextual information."
1224 ".br\n")
1226 ;;; Link
1227 ;; Inline images just place a call to .PSPIC or .PS/.PE
1228 ;; and load the graph.
1230 (defun org-e-groff-link--inline-image (link info)
1231 "Return Groff code for an inline image.
1232 LINK is the link pointing to the inline image. INFO is a plist
1233 used as a communication channel."
1234 (let* ((parent (org-export-get-parent-element link))
1235 (path (let ((raw-path (org-element-property :path link)))
1236 (if (not (file-name-absolute-p raw-path)) raw-path
1237 (expand-file-name raw-path))))
1238 (attr (org-export-read-attribute :attr_groff link))
1239 (placement
1240 (case (plist-get attr :position)
1241 ('center "")
1242 ('left "-L")
1243 ('right "-R")
1244 (t "")))
1245 (width (or (plist-get attr :width) ""))
1246 (height (or (plist-get attr :height) ""))
1247 (caption (and (not (plist-get attr :disable-caption))
1248 (org-e-groff--caption/label-string parent info))))
1249 ;; Now clear ATTR from any special keyword and set a default value
1250 ;; if nothing is left. Return proper string.
1251 (concat
1252 (cond
1253 ((and org-e-groff-raster-to-ps
1254 (or (string-match ".\.png$" path)
1255 (string-match ".\.jpg$" path)))
1256 (let ((eps-path (concat path ".eps")))
1257 (shell-command (format org-e-groff-raster-to-ps path eps-path))
1258 (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1259 placement eps-path width height)))
1260 ((string-match ".\.pic$" path)
1261 (format "\n.PS\ncopy \"%s\"\n.PE" path))
1262 (t (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1263 placement path width height)))
1264 (and caption (format "\n.FG \"%s\"" caption)))))
1266 (defun org-e-groff-link (link desc info)
1267 "Transcode a LINK object from Org to Groff.
1269 DESC is the description part of the link, or the empty string.
1270 INFO is a plist holding contextual information. See
1271 `org-export-data'."
1273 (let* ((type (org-element-property :type link))
1274 (raw-path (org-element-property :path link))
1275 ;; Ensure DESC really exists, or set it to nil.
1276 (desc (and (not (string= desc "")) desc))
1277 (imagep (org-export-inline-image-p
1278 link org-e-groff-inline-image-rules))
1279 (path (cond
1280 ((member type '("http" "https" "ftp" "mailto"))
1281 (concat type ":" raw-path))
1282 ((string= type "file")
1283 (when (string-match "\\(.+\\)::.+" raw-path)
1284 (setq raw-path (match-string 1 raw-path)))
1285 (if (file-name-absolute-p raw-path)
1286 (concat "file://" (expand-file-name raw-path))
1287 (concat "file://" raw-path)))
1288 (t raw-path)))
1289 protocol)
1290 (cond
1291 ;; Image file.
1292 (imagep (org-e-groff-link--inline-image link info))
1293 ;; import groff files
1294 ((and (string= type "file")
1295 (string-match ".\.groff$" raw-path))
1296 (concat ".so " raw-path "\n"))
1297 ;; Radio link: transcode target's contents and use them as link's
1298 ;; description.
1299 ((string= type "radio")
1300 (let ((destination (org-export-resolve-radio-link link info)))
1301 (when destination
1302 (format "\\fI [%s] \\fP"
1303 (org-export-solidify-link-text path)))))
1305 ;; Links pointing to an headline: find destination and build
1306 ;; appropriate referencing command.
1307 ((member type '("custom-id" "fuzzy" "id"))
1308 (let ((destination (if (string= type "fuzzy")
1309 (org-export-resolve-fuzzy-link link info)
1310 (org-export-resolve-id-link link info))))
1311 (case (org-element-type destination)
1312 ;; Id link points to an external file.
1313 (plain-text
1314 (if desc (format "%s \\fBat\\fP \\fIfile://%s\\fP" desc destination)
1315 (format "\\fI file://%s \\fP" destination)))
1316 ;; Fuzzy link points nowhere.
1317 ('nil
1318 (format org-e-groff-link-with-unknown-path-format
1319 (or desc
1320 (org-export-data
1321 (org-element-property :raw-link link) info))))
1322 ;; Fuzzy link points to an invisible target.
1323 (keyword nil)
1324 ;; LINK points to an headline. If headlines are numbered
1325 ;; and the link has no description, display headline's
1326 ;; number. Otherwise, display description or headline's
1327 ;; title.
1328 (headline
1329 (let ((label ""))
1330 (if (and (plist-get info :section-numbers) (not desc))
1331 (format "\\fI%s\\fP" label)
1332 (format "\\fI%s\\fP"
1333 (or desc
1334 (org-export-data
1335 (org-element-property :title destination) info))))))
1336 ;; Fuzzy link points to a target. Do as above.
1337 (otherwise
1338 (let ((path (org-export-solidify-link-text path)))
1339 (if (not desc) (format "\\fI%s\\fP" path)
1340 (format "%s \\fBat\\fP \\fI%s\\fP" desc path)))))))
1341 ;; External link with a description part.
1342 ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
1343 ;; External link without a description part.
1344 (path (format "\\fI%s\\fP" path))
1345 ;; No path, only description. Try to do something useful.
1346 (t (format org-e-groff-link-with-unknown-path-format desc)))))
1348 ;;; Paragraph
1350 (defun org-e-groff-paragraph (paragraph contents info)
1351 "Transcode a PARAGRAPH element from Org to Groff.
1352 CONTENTS is the contents of the paragraph, as a string. INFO is
1353 the plist used as a communication channel."
1354 (let ((parent (plist-get (nth 1 paragraph) :parent)))
1355 (when parent
1356 (let* ((parent-type (car parent))
1357 (fixed-paragraph "")
1358 (class (plist-get info :groff-class))
1359 (class-options (plist-get info :groff-class-options))
1360 (classes (assoc class org-e-groff-classes))
1361 (classes-options (car (last classes)))
1362 (paragraph-option (plist-get classes-options :paragraph)))
1363 (cond
1364 ((and (symbolp paragraph-option)
1365 (fboundp paragraph-option))
1366 (funcall paragraph-option parent-type parent contents))
1367 ((and (eq parent-type 'item)
1368 (plist-get (nth 1 parent) :bullet))
1369 (setq fixed-paragraph (concat "" contents)))
1370 ((eq parent-type 'section)
1371 (setq fixed-paragraph (concat ".P\n" contents)))
1372 ((eq parent-type 'footnote-definition)
1373 (setq fixed-paragraph (concat "" contents)))
1374 (t (setq fixed-paragraph (concat "" contents))))
1375 fixed-paragraph))))
1377 ;;; Plain List
1379 (defun org-e-groff-plain-list (plain-list contents info)
1380 "Transcode a PLAIN-LIST element from Org to Groff.
1381 CONTENTS is the contents of the list. INFO is a plist holding
1382 contextual information."
1383 (let* ((type (org-element-property :type plain-list))
1384 (attr (mapconcat #'identity
1385 (org-element-property :attr_groff plain-list)
1386 " "))
1387 (groff-type (cond
1388 ((eq type 'ordered) ".AL")
1389 ((eq type 'unordered) ".BL")
1390 ((eq type 'descriptive) ".VL 2.0i"))))
1391 (org-e-groff--wrap-label
1392 plain-list
1393 (format "%s\n%s\n.LE" groff-type contents))))
1395 ;;; Plain Text
1397 (defun org-e-groff-plain-text (text info)
1398 "Transcode a TEXT string from Org to Groff.
1399 TEXT is the string to transcode. INFO is a plist holding
1400 contextual information."
1401 (let ((output text))
1402 ;; Protect various characters.
1403 (setq output (replace-regexp-in-string
1404 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1405 "$\\" output nil t 1))
1406 ;; Activate smart quotes. Be sure to provide original TEXT string
1407 ;; since OUTPUT may have been modified.
1408 (when (plist-get info :with-smart-quotes)
1409 (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
1410 ;; Handle Special Characters
1411 (if org-e-groff-special-char
1412 (dolist (special-char-list org-e-groff-special-char)
1413 (setq output
1414 (replace-regexp-in-string (car special-char-list)
1415 (cdr special-char-list) output))))
1416 ;; Handle break preservation if required.
1417 (when (plist-get info :preserve-breaks)
1418 (setq output (replace-regexp-in-string
1419 "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n" output)))
1420 ;; Return value.
1421 output))
1423 ;;; Planning
1425 (defun org-e-groff-planning (planning contents info)
1426 "Transcode a PLANNING element from Org to Groff.
1427 CONTENTS is nil. INFO is a plist holding contextual
1428 information."
1429 (concat
1430 (mapconcat
1431 'identity
1432 (delq nil
1433 (list
1434 (let ((closed (org-element-property :closed planning)))
1435 (when closed
1436 (concat
1437 (format "\\fR %s \\fP" org-closed-string)
1438 (format org-e-groff-inactive-timestamp-format
1439 (org-translate-time
1440 (org-element-property :raw-value closed))))))
1441 (let ((deadline (org-element-property :deadline planning)))
1442 (when deadline
1443 (concat
1444 (format "\\fB %s \\fP" org-deadline-string)
1445 (format org-e-groff-active-timestamp-format
1446 (org-translate-time
1447 (org-element-property :raw-value deadline))))))
1448 (let ((scheduled (org-element-property :scheduled planning)))
1449 (when scheduled
1450 (concat
1451 (format "\\fR %s \\fP" org-scheduled-string)
1452 (format org-e-groff-active-timestamp-format
1453 (org-translate-time
1454 (org-element-property :raw-value 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-e-groff-plain-text
1855 (org-timestamp-translate timestamp) info)))
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 async subtreep visible-only body-only ext-plist)
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 A non-nil optional argument ASYNC means the process should happen
1900 asynchronously. The resulting file should be accessible through
1901 the `org-export-stack' interface.
1903 When optional argument SUBTREEP is non-nil, export the sub-tree
1904 at point, extracting information from the headline properties
1905 first.
1907 When optional argument VISIBLE-ONLY is non-nil, don't export
1908 contents of hidden elements.
1910 EXT-PLIST, when provided, is a property list with external
1911 parameters overriding Org default settings, but still inferior to
1912 file-local settings.
1914 Return output file's name."
1915 (interactive)
1916 (let ((outfile (org-export-output-file-name ".groff" subtreep)))
1917 (if async
1918 (org-export-async-start
1919 (lambda (f) (org-export-add-to-stack f 'e-groff))
1920 (let ((org-e-groff-registered-references nil)
1921 (org-e-groff-special-content nil))
1922 `(expand-file-name
1923 (org-export-to-file
1924 'e-groff ,outfile ,subtreep ,visible-only ,body-only
1925 ',ext-plist))))
1926 (let ((org-e-groff-registered-references nil)
1927 (org-e-groff-special-content nil))
1928 (org-export-to-file
1929 'e-groff outfile subtreep visible-only body-only ext-plist)))))
1931 (defun org-e-groff-export-to-pdf
1932 (&optional async subtreep visible-only body-only ext-plist)
1933 "Export current buffer to Groff then process through to PDF.
1935 If narrowing is active in the current buffer, only export its
1936 narrowed part.
1938 If a region is active, export that region.
1940 A non-nil optional argument ASYNC means the process should happen
1941 asynchronously. The resulting file should be accessible through
1942 the `org-export-stack' interface.
1944 When optional argument SUBTREEP is non-nil, export the sub-tree
1945 at point, extracting information from the headline properties
1946 first.
1948 When optional argument VISIBLE-ONLY is non-nil, don't export
1949 contents of hidden elements.
1951 EXT-PLIST, when provided, is a property list with external
1952 parameters overriding Org default settings, but still inferior to
1953 file-local settings.
1955 Return PDF file's name."
1956 (interactive)
1957 (if async
1958 (let ((outfile (org-export-output-file-name ".groff" subtreep)))
1959 (org-export-async-start
1960 (lambda (f) (org-export-add-to-stack f 'e-groff))
1961 `(expand-file-name
1962 (org-e-groff-compile
1963 (org-export-to-file
1964 'e-groff ,outfile ,subtreep ,visible-only ,body-only
1965 ',ext-plist)))))
1966 (org-e-groff-compile
1967 (org-e-groff-export-to-groff
1968 nil subtreep visible-only body-only ext-plist))))
1970 (defun org-e-groff-compile (file)
1971 "Compile a Groff file.
1973 FILE is the name of the file being compiled. Processing is done
1974 through the command specified in `org-e-groff-pdf-process'.
1976 Return PDF file name or an error if it couldn't be produced."
1977 (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
1978 (full-name (file-truename file))
1979 (out-dir (file-name-directory file))
1980 ;; Make sure `default-directory' is set to FILE directory,
1981 ;; not to whatever value the current buffer may have.
1982 (default-directory (file-name-directory full-name))
1983 errors)
1984 (message (format "Processing Groff file %s ..." file))
1985 (save-window-excursion
1986 (cond
1987 ;; A function is provided: Apply it.
1988 ((functionp org-e-groff-pdf-process)
1989 (funcall org-e-groff-pdf-process (shell-quote-argument file)))
1990 ;; A list is provided: Replace %b, %f and %o with appropriate
1991 ;; values in each command before applying it. Output is
1992 ;; redirected to "*Org PDF Groff Output*" buffer.
1993 ((consp org-e-groff-pdf-process)
1994 (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
1995 (mapc
1996 (lambda (command)
1997 (shell-command
1998 (replace-regexp-in-string
1999 "%b" (shell-quote-argument base-name)
2000 (replace-regexp-in-string
2001 "%f" (shell-quote-argument full-name)
2002 (replace-regexp-in-string
2003 "%o" (shell-quote-argument out-dir) command t t)
2004 t t) t t)
2005 outbuf))
2006 org-e-groff-pdf-process)
2007 ;; Collect standard errors from output buffer.
2008 (setq errors (org-e-groff-collect-errors outbuf))))
2009 (t (error "No valid command to process to PDF")))
2010 (let ((pdffile (concat out-dir base-name ".pdf")))
2011 ;; Check for process failure. Provide collected errors if
2012 ;; possible.
2013 (if (not (file-exists-p pdffile))
2014 (error (concat (format "PDF file %s wasn't produced" pdffile)
2015 (when errors (concat ": " errors))))
2016 ;; Else remove log files, when specified, and signal end of
2017 ;; process to user, along with any error encountered.
2018 (when org-e-groff-remove-logfiles
2019 (dolist (ext org-e-groff-logfiles-extensions)
2020 (let ((file (concat out-dir base-name "." ext)))
2021 (when (file-exists-p file) (delete-file file)))))
2022 (message (concat "Process completed"
2023 (if (not errors) "."
2024 (concat " with errors: " errors)))))
2025 ;; Return output file name.
2026 pdffile))))
2028 (defun org-e-groff-collect-errors (buffer)
2029 "Collect some kind of errors from \"groff\" output
2030 BUFFER is the buffer containing output.
2031 Return collected error types as a string, or nil if there was
2032 none."
2033 (with-current-buffer buffer
2034 (save-excursion
2035 (goto-char (point-max))
2036 ;; Find final run
2037 nil)))
2040 (provide 'org-e-groff)
2041 ;;; org-e-groff.el ends here