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
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/>.
24 ;; This library implements a Groff Memorandum Macro back-end for
25 ;; Org generic exporter.
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".
40 (eval-when-compile (require 'cl
))
42 (defvar org-export-groff-default-packages-alist
)
43 (defvar org-export-groff-packages-alist
)
50 (org-export-define-backend e-groff
51 '((babel-call . org-e-groff-babel-call
)
52 (bold . org-e-groff-bold
)
53 (center-block . org-e-groff-center-block
)
54 (clock . org-e-groff-clock
)
55 (code . org-e-groff-code
)
56 (comment . org-e-groff-comment
)
57 (comment-block . org-e-groff-comment-block
)
58 (drawer . org-e-groff-drawer
)
59 (dynamic-block . org-e-groff-dynamic-block
)
60 (entity . org-e-groff-entity
)
61 (example-block . org-e-groff-example-block
)
62 (export-block . org-e-groff-export-block
)
63 (export-snippet . org-e-groff-export-snippet
)
64 (fixed-width . org-e-groff-fixed-width
)
65 (footnote-definition . org-e-groff-footnote-definition
)
66 (footnote-reference . org-e-groff-footnote-reference
)
67 (headline . org-e-groff-headline
)
68 (horizontal-rule . org-e-groff-horizontal-rule
)
69 (inline-babel-call . org-e-groff-inline-babel-call
)
70 (inline-src-block . org-e-groff-inline-src-block
)
71 (inlinetask . org-e-groff-inlinetask
)
72 (italic . org-e-groff-italic
)
73 (item . org-e-groff-item
)
74 (keyword . org-e-groff-keyword
)
75 (groff-environment . org-e-groff-groff-environment
)
76 (groff-fragment . org-e-groff-groff-fragment
)
77 (line-break . org-e-groff-line-break
)
78 (link . org-e-groff-link
)
79 (paragraph . org-e-groff-paragraph
)
80 (plain-list . org-e-groff-plain-list
)
81 (plain-text . org-e-groff-plain-text
)
82 (planning . org-e-groff-planning
)
83 (property-drawer . org-e-groff-property-drawer
)
84 (quote-block . org-e-groff-quote-block
)
85 (quote-section . org-e-groff-quote-section
)
86 (radio-target . org-e-groff-radio-target
)
87 (section . org-e-groff-section
)
88 (special-block . org-e-groff-special-block
)
89 (src-block . org-e-groff-src-block
)
90 (statistics-cookie . org-e-groff-statistics-cookie
)
91 (strike-through . org-e-groff-strike-through
)
92 (subscript . org-e-groff-subscript
)
93 (superscript . org-e-groff-superscript
)
94 (table . org-e-groff-table
)
95 (table-cell . org-e-groff-table-cell
)
96 (table-row . org-e-groff-table-row
)
97 (target . org-e-groff-target
)
98 (template . org-e-groff-template
)
99 (timestamp . org-e-groff-timestamp
)
100 (underline . org-e-groff-underline
)
101 (verbatim . org-e-groff-verbatim
)
102 (verse-block . org-e-groff-verse-block
))
103 :export-block
"GROFF"
105 (?g
"Export to GROFF"
106 ((?g
"As GROFF file" org-e-groff-export-to-groff
)
107 (?p
"As PDF file" org-e-groff-export-to-pdf
)
108 (?o
"As PDF file and open"
109 (lambda (s v b
) (org-open-file (org-e-groff-export-to-pdf s v b
))))))
111 ((:date
"DATE" nil org-e-groff-date-format t
)
112 (:groff-class
"GROFF_CLASS" nil org-e-groff-default-class t
)
113 (:groff-class-options
"GROFF_CLASS_OPTIONS" nil nil t
)
114 (:groff-header-extra
"GROFF_HEADER" nil nil newline
)))
118 ;;; User Configurable Variables
120 (defgroup org-export-e-groff nil
121 "Options for exporting Org mode files to Groff."
122 :tag
"Org Export Groff"
127 (defcustom org-e-groff-default-class
"internal"
128 "The default Groff class."
129 :group
'org-export-e-groff
130 :type
'(string :tag
"Groff class"))
132 (defcustom org-e-groff-classes
134 (:heading
'default
:type
"memo" :last-section
"toc"))
136 (:heading
'default
:type
"memo" :last-section
"toc"))
137 ("programmer" ".MT 2"
138 (:heading
'default
:type
"memo" :last-section
"toc"))
140 (:heading
'default
:type
"memo" :last-section
"toc"))
142 (:heading
'default
:type
"memo" :last-section
"toc"))
144 (:heading
'default
:type
"memo" :last-section
"sign"))
146 (:heading custom-function
:type
"custom" :last-section
"toc"))
148 (:heading
'default
:type
"memo"))
150 (:heading
'default
:type
"cover" :last-section
"toc"))
152 (:heading
'default
:type
"cover" :last-section
"toc"))
154 (:heading
'default
:type
"letter" :last-section
"sign"))
156 (:heading
'default
:type
"letter" :last-section
"sign"))
158 (:heading
'default
:type
"letter" :last-section
"sign"))
160 (:heading
'default
:type
"letter" :last-section
"sign"))
161 ("none" "" (:heading
'default
:type
"custom")))
163 ;; none means, no Cover or Memorandum Type and no calls to AU, AT, ND and TL
164 ;; This is to facilitate the creation of custom pages.
166 ;; dummy means, no Cover or Memorandum Type but calls to AU, AT, ND and TL
167 ;; are made. This is to facilitate Abstract Insertion.
169 "This list describes the attributes for the documents being created.
170 It allows for the creation of new "
171 :group
'org-export-e-groff
173 (list (string :tag
"Document Type")
174 (string :tag
"Header")
175 (repeat :tag
"Options" :inline t
177 (list :tag
"Heading")
178 (function :tag
"Hook computing sectioning"))))))
181 (defcustom org-e-groff-date-format
182 (format-time-string "%Y-%m-%d")
183 "Format string for .ND "
184 :group
'org-export-e-groff
189 (defconst org-e-groff-special-tags
190 '("FROM" "TO" "ABSTRACT" "APPENDIX" "BODY" "NS"))
192 (defcustom org-e-groff-format-headline-function nil
193 "Function to format headline text.
195 This function will be called with 5 arguments:
196 TODO the todo keyword (string or nil).
197 TODO-TYPE the type of todo (symbol: `todo', `done', nil)
198 PRIORITY the priority of the headline (integer or nil)
199 TEXT the main headline text (string).
200 TAGS the tags as a list of strings (list of strings or nil).
202 The function result will be used in the section format string.
204 As an example, one could set the variable to the following, in
205 order to reproduce the default set-up:
207 \(defun org-e-groff-format-headline (todo todo-type priority text tags)
208 \"Default format function for an headline.\"
210 \(format \"\\fB%s\\fP \" todo))
212 \(format \"[\\#%c] \" priority))
216 \(mapconcat 'identity tags \":\"))))"
217 :group
'org-export-e-groff
222 (defcustom org-e-groff-active-timestamp-format
"\\fI%s\\fP"
223 "A printf format string to be applied to active timestamps."
224 :group
'org-export-e-groff
227 (defcustom org-e-groff-inactive-timestamp-format
"\\fI%s\\fP"
228 "A printf format string to be applied to inactive timestamps."
229 :group
'org-export-e-groff
232 (defcustom org-e-groff-diary-timestamp-format
"\\fI%s\\fP"
233 "A printf format string to be applied to diary timestamps."
234 :group
'org-export-e-groff
239 (defcustom org-e-groff-inline-image-rules
240 '(("file" .
"\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'")
241 ("fuzzy" .
"\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'"))
242 "Rules characterizing image files that can be inlined into Groff.
244 A rule consists in an association whose key is the type of link
245 to consider, and value is a regexp that will be matched against
248 Note that, by default, the image extensions actually allowed
249 depend on the way the Groff file is processed. When used with
250 pdfgroff, pdf, jpg and png images are OK. When processing
251 through dvi to Postscript, only ps and eps are allowed. The
252 default we use here encompasses both."
253 :group
'org-export-e-groff
254 :type
'(alist :key-type
(string :tag
"Type")
255 :value-type
(regexp :tag
"Path")))
257 (defcustom org-e-groff-link-with-unknown-path-format
"\\fI%s\\fP"
258 "Format string for links with unknown path type."
259 :group
'org-export-groff
264 (defcustom org-e-groff-tables-centered t
265 "When non-nil, tables are exported in a center environment."
266 :group
'org-export-e-groff
269 (defcustom org-e-groff-tables-verbatim nil
270 "When non-nil, tables are exported verbatim."
271 :group
'org-export-e-groff
274 (defcustom org-e-groff-table-scientific-notation
"%sE%s"
275 "Format string to display numbers in scientific notation.
276 The format should have \"%s\" twice, for mantissa and exponent
277 \(i.e. \"%s\\\\times10^{%s}\").
279 When nil, no transformation is made."
280 :group
'org-export-e-groff
282 (string :tag
"Format string")
283 (const :tag
"No formatting")))
287 (defcustom org-e-groff-text-markup-alist
288 '((bold .
"\\fB%s\\fP")
289 (code .
"\\fC%s\\fP")
290 (italic .
"\\fI%s\\fP")
291 (strike-through .
"\\fC%s\\fP") ; Strike through and underline
292 (underline .
"\\fI%s\\fP") ; need to be revised.
293 (verbatim .
"protectedtexttt"))
294 "Alist of Groff expressions to convert text markup.
296 The key must be a symbol among `bold', `code', `italic',
297 `strike-through', `underline' and `verbatim'. The value is
298 a formatting string to wrap fontified text with it.
300 If no association can be found for a given markup, text will be
302 :group
'org-export-e-groff
304 :options
'(bold code italic strike-through underline verbatim
))
308 (defcustom org-e-groff-format-drawer-function nil
309 "Function called to format a drawer in Groff code.
311 The function must accept two parameters:
312 NAME the drawer name, like \"LOGBOOK\"
313 CONTENTS the contents of the drawer.
315 The function should return the string to be exported.
317 For example, the variable could be set to the following function
318 in order to mimic default behaviour:
320 \(defun org-e-groff-format-drawer-default \(name contents\)
321 \"Format a drawer element for Groff export.\"
323 :group
'org-export-e-groff
328 (defcustom org-e-groff-format-inlinetask-function nil
329 "Function called to format an inlinetask in Groff code.
331 The function must accept six parameters:
332 TODO the todo keyword, as a string
333 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
334 PRIORITY the inlinetask priority, as a string
335 NAME the inlinetask name, as a string.
336 TAGS the inlinetask tags, as a list of strings.
337 CONTENTS the contents of the inlinetask, as a string.
339 The function should return the string to be exported.
341 For example, the variable could be set to the following function
342 in order to mimic default behaviour:
344 \(defun org-e-groff-format-inlinetask \(todo type priority name tags contents\)
345 \"Format an inline task element for Groff export.\"
349 \(format \"\\fB%s\\fP \" todo))
350 \(when priority (format \"[\\#%c] \" priority))
354 \(mapconcat 'identity tags \":\")))))
355 \(format (concat \".DS L\\n\"
359 full-title contents))"
360 :group
'org-export-e-groff
365 (defcustom org-e-groff-source-highlight nil
366 "Use GNU source highlight to embellish source blocks "
367 :group
'org-export-e-groff
370 (defcustom org-e-groff-source-highlight-langs
371 '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
373 (c "c") (cc "cpp") (csharp "csharp") (d "d")
374 (fortran "fortran") (cobol "cobol") (pascal "pascal")
375 (ada "ada") (asm "asm")
376 (perl "perl") (cperl "perl")
377 (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
378 (java "java") (javascript "javascript")
380 (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
381 (ocaml "caml") (caml "caml")
382 (sql "sql") (sqlite "sql")
383 (html "html") (css "css") (xml "xml")
384 (bat "bat") (bison "bison") (clipper "clipper")
385 (ldap "ldap") (opa "opa")
386 (php "php") (postscript "postscript") (prolog "prolog")
387 (properties "properties") (makefile "makefile")
388 (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
389 "Alist mapping languages to their listing language counterpart.
390 The key is a symbol, the major mode symbol without the \"-mode\".
391 The value is the string that should be inserted as the language
392 parameter for the listings package. If the mode name and the
393 listings name are the same, the language does not need an entry
394 in this list - but it does not hurt if it is present."
395 :group
'org-export-e-groff
398 (symbol :tag
"Major mode ")
399 (string :tag
"Listings language"))))
401 (defcustom org-e-groff-source-highlight-options nil
402 "Association list of options for the groff listings package.
404 These options are supplied as a comma-separated list to the
405 \\lstset command. Each element of the association list should be
406 a list containing two strings: the name of the option, and the
409 (setq org-e-groff-source-highlight-options
410 '((\"basicstyle\" \"\\small\")
411 (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
413 will typeset the code in a small size font with underlined, bold
416 Note that the same options will be applied to blocks of all
418 :group
'org-export-e-groff
421 (string :tag
"Listings option name ")
422 (string :tag
"Listings option value"))))
424 (defvar org-e-groff-custom-lang-environments nil
425 "Alist mapping languages to language-specific Groff environments.
427 It is used during export of src blocks by the listings and
428 groff packages. For example,
430 \(setq org-e-groff-custom-lang-environments
431 '\(\(python \"pythoncode\"\)\)\)
433 would have the effect that if org encounters begin_src python
434 during groff export it will use pythoncode as the source-highlight
439 (defcustom org-e-groff-quotes
441 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"«~")
442 ("\\(\\S-\\)\"" .
"~»")
443 ("\\(\\s-\\|(\\|^\\)'" .
"'"))
445 ("\\(\\s-\\|[[(]\\|^\\)\"" .
"``")
446 ("\\(\\S-\\)\"" .
"''")
447 ("\\(\\s-\\|(\\|^\\)'" .
"`")))
448 "Alist for quotes to use when converting english double-quotes.
450 The CAR of each item in this alist is the language code.
451 The CDR of each item in this alist is a list of three CONS:
452 - the first CONS defines the opening quote;
453 - the second CONS defines the closing quote;
454 - the last CONS defines single quotes.
456 For each item in a CONS, the first string is a regexp
457 for allowed characters before/after the quote, the second
458 string defines the replacement string for this quote."
459 :group
'org-export-e-groff
461 (cons :tag
"Opening quote"
462 (string :tag
"Regexp for char before")
463 (string :tag
"Replacement quote "))
464 (cons :tag
"Closing quote"
465 (string :tag
"Regexp for char after ")
466 (string :tag
"Replacement quote "))
467 (cons :tag
"Single quote"
468 (string :tag
"Regexp for char before")
469 (string :tag
"Replacement quote "))))
471 (defcustom org-e-groff-special-char
472 '(("(c)" .
"\\\\(co")
474 ("(rg)" .
"\\\\(rg"))
475 "CONS list in which the value of the car
476 is replace on the value of the CDR. "
477 :group
'org-export-e-groff
479 (cons :tag
"Character Subtitute"
480 (string :tag
"Original Character Group")
481 (string :tag
"Replacement Character"))))
485 (defcustom org-e-groff-pdf-process
486 '("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
487 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
488 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf")
490 "Commands to process a Groff file to a PDF file.
491 This is a list of strings, each of them will be given to the
492 shell as a command. %f in the command will be replaced by the
493 full file name, %b by the file base name \(i.e. without
494 extension) and %o by the base directory of the file."
495 :group
'org-export-pdf
497 (repeat :tag
"Shell command sequence"
498 (string :tag
"Shell command"))
499 (const :tag
"2 runs of pdfgroff"
500 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
501 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
502 (const :tag
"3 runs of pdfgroff"
503 ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
504 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
505 "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
508 (defcustom org-e-groff-logfiles-extensions
509 '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
510 "The list of file extensions to consider as Groff logfiles."
511 :group
'org-export-e-groff
512 :type
'(repeat (string :tag
"Extension")))
514 (defcustom org-e-groff-remove-logfiles t
515 "Non-nil means remove the logfiles produced by PDF production.
516 These are the .aux, .log, .out, and .toc files."
517 :group
'org-export-e-groff
520 (defcustom org-e-groff-organization
"Org User"
521 "Name of the organization used to populate the .AF command."
522 :group
'org-export-e-groff
525 (defcustom org-e-groff-raster-to-ps nil
526 "Command used to convert raster to EPS. Nil for no conversion. Make sure that
527 `org-e-groff-inline-image-rules' is adjusted accordingly if not conversion is being
528 done. In this case, remove the entries for jpg and png in the file and fuzzy lists."
529 :group
'org-export-e-groff
531 (repeat :tag
"Shell Command Sequence" (string :tag
"Shell Command"))
532 (const :tag
"sam2p" "a=%s;b=%s;sam2p ${a} ${b} ;grep -v BeginData ${b} > b_${b};mv b_${b} ${b}" )
533 (const :tag
"NetPNM" "a=%s;b=%s;pngtopnm ${a} | pnmtops -noturn > ${b}" )
534 (const :tag
"None" nil
)))
536 (defvar org-e-groff-registered-references nil
)
537 (defvar org-e-groff-special-content nil
)
541 ;;; Internal Functions
543 (defun org-e-groff--caption/label-string
(caption label info
)
544 "Return caption and label Groff string for floats.
546 CAPTION is a cons cell of secondary strings, the car being the
547 standard caption and the cdr its short form. LABEL is a string
548 representing the label. INFO is a plist holding contextual
551 If there's no caption nor label, return the empty string.
553 For non-floats, see `org-e-groff--wrap-label'."
554 (let ((label-str ""))
556 ((and (not caption
) (not label
)) "")
557 ((not caption
) (format "\\fI%s\\fP" label
))
558 ;; Option caption format with short name.
560 (format "%s\n.br\n%s - %s\n"
561 (org-export-data (cdr caption
) info
)
563 (org-export-data (car caption
) info
)))
564 ;; Standard caption format.
565 (t (format "\\fR%s\\fP"
566 (org-export-data (car caption
) info
))))))
568 (defun org-e-groff--quotation-marks (text info
)
569 "Export quotation marks depending on language conventions.
570 TEXT is a string containing quotation marks to be replaced. INFO
571 is a plist used as a communication channel."
574 (while (setq start
(string-match (car l
) text start
))
575 (let ((new-quote (concat (match-string 1 text
) (cdr l
))))
576 (setq text
(replace-match new-quote t t text
))))))
577 (cdr (or (assoc (plist-get info
:language
) org-e-groff-quotes
)
578 ;; Falls back on English.
579 (assoc "en" org-e-groff-quotes
))))
582 (defun org-e-groff--wrap-label (element output
)
583 "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
584 This function shouldn't be used for floats. See
585 `org-e-groff--caption/label-string'."
586 (let ((label (org-element-property :name element
)))
587 (if (or (not output
) (not label
) (string= output
"") (string= label
""))
589 (concat (format "%s\n.br\n" label
) output
))))
591 (defun org-e-groff--text-markup (text markup
)
592 "Format TEXT depending on MARKUP text markup.
593 See `org-e-groff-text-markup-alist' for details."
594 (let ((fmt (cdr (assq markup org-e-groff-text-markup-alist
))))
596 ;; No format string: Return raw text.
598 ((string= "protectedtexttt" fmt
)
600 (trans '(("\\" .
"\\")))
603 (while (string-match "[\\{}$%&_#~^]" text
)
604 (setq char
(match-string 0 text
))
605 (if (> (match-beginning 0) 0)
606 (setq rtn
(concat rtn
(substring text
0 (match-beginning 0)))))
607 (setq text
(substring text
(1+ (match-beginning 0))))
608 (setq char
(or (cdr (assoc char trans
)) (concat "\\" char
))
609 rtn
(concat rtn char
)))
610 (setq text
(concat rtn text
))
611 (format "\\fC%s\\fP" text
)))
612 ;; Else use format string.
613 (t (format fmt text
)))))
616 (defun org-e-groff--get-tagged-content (tag info
)
617 (cdr (assoc tag org-e-groff-special-content
)))
619 (defun org-e-groff--mt-head (title contents attr info
)
622 ;; 1. Insert Organization
623 (let ((firm-option (plist-get attr
:firm
)))
625 ((stringp firm-option
)
626 (format ".AF \"%s\" \n" firm-option
))
627 (t (format ".AF \"%s\" \n" (or org-e-groff-organization
"")))))
630 (let ((subtitle1 (plist-get attr
:subtitle1
))
631 (subtitle2 (plist-get attr
:subtitle2
)))
635 (format ".TL \"%s\" \"%s\" \n%s\n"
637 (or subtitle2
"") " "))
639 ((not (or subtitle1 subtitle2
))
643 (format ".TL \"%s\" \"%s \" \n%s\n"
645 (or subtitle2
"") title
))))
648 ;; In Groff, .AU *MUST* be placed after .TL
649 ;; If From, populate with data from From else
651 (let ((author (and (plist-get info
:with-author
)
652 (let ((auth (plist-get info
:author
)))
653 (and auth
(org-export-data auth info
)))))
654 (email (and (plist-get info
:with-email
)
655 (org-export-data (plist-get info
:email
) info
)))
656 (from-data (org-e-groff--get-tagged-content "FROM" info
))
658 (to-data (org-e-groff--get-tagged-content "TO" info
)))
661 ((and author from-data
)
665 (format " \"%s\" " from-line
))
668 (replace-regexp-in-string "\\.P\n" "" from-data
)) "\n") "")))
671 (format ".AU \"%s\" " author
) au-line
"\n")))
673 ((and author email
(not (string= "" email
)))
674 (format ".AU \"%s\" \"%s\"\n" author email
))
676 (author (format ".AU \"%s\"\n" author
))
681 ;; 4. Author Title, if present
682 (let ((at-item (plist-get attr
:author-title
)))
683 (if (and at-item
(stringp at-item
))
684 (format ".AT \"%s\" \n" at-item
)
688 (let ((date (org-export-data (plist-get info
:date
) info
)))
689 (and date
(format ".ND \"%s\"\n" date
)))
692 ;; If Abstract, then Populate Abstract
695 (let ((abstract-data (org-e-groff--get-tagged-content "ABSTRACT" info
))
696 (to-data (org-e-groff--get-tagged-content "TO" info
)))
699 (format ".AS\n%s\n.AE\n" abstract-data
))
701 (format ".AS\n%s\n.AE\n" to-data
))))))
703 (defun org-e-groff--letter-head (title contents attr info
)
704 (let ((author (and (plist-get info
:with-author
)
705 (let ((auth (plist-get info
:author
)))
706 (and auth
(org-export-data auth info
)))))
707 (email (and (plist-get info
:with-email
)
708 (org-export-data (plist-get info
:email
) info
)))
709 (from-data (org-e-groff--get-tagged-content "FROM" info
))
710 (at-item (plist-get attr
:author-title
))
711 (to-data (org-e-groff--get-tagged-content "TO" info
)))
714 ;; If FROM then get data from FROM
717 (replace-regexp-in-string "\\.P\n" "" from-data
))
722 (replace-regexp-in-string "\\.P\n" "" to-data
))
728 (format ".WA \"%s\" \"%s\" \n%s\n.WE\n" author
(or at-item
"") from-data
))
729 ((and author email
(not (string= "" email
)))
730 (format ".WA \"%s\"\n \"%s\"\n.WE\n" author email
))
731 (author (format ".WA \"%s\"\n.WE\n" author
))
732 (t ".WA \"\" \n.WE\n"))
734 ;; If TO then get data from TO
737 (format ".IA \n%s\n.IE\n" to-data
)))))
742 (defun org-e-groff-template (contents info
)
743 "Return complete document string after Groff conversion.
744 CONTENTS is the transcoded contents string. INFO is a plist
745 holding export options."
746 (let* ((title (org-export-data (plist-get info
:title
) info
))
751 (list (plist-get info
:groff-class-options
))
753 (class (plist-get info
:groff-class
))
754 (class-options (plist-get info
:groff-class-options
))
755 (classes (assoc class org-e-groff-classes
))
756 (classes-options (car (last classes
)))
757 (heading-option (plist-get classes-options
:heading
))
758 (type-option (plist-get classes-options
:type
))
759 (last-option (plist-get classes-options
:last-section
))
760 (hyphenate (plist-get attr
:hyphenate
))
761 (justify-right (plist-get attr
:justify-right
))
763 (document-class-string
765 (org-element-normalize-string
766 (let* ((header (nth 1 (assoc class org-e-groff-classes
)))
767 (document-class-item (if (stringp header
) header
"")))
768 document-class-item
)))))
787 ((string= type-option
"custom") "")
789 ((and (stringp document-class-string
)
790 (string= type-option
"cover"))
793 (format ".COVER %s\n" document-class-string
)
794 (org-e-groff--mt-head title contents attr info
)
797 ((string= type-option
"memo")
799 (org-e-groff--mt-head title contents attr info
)
800 document-class-string
))
801 ((string= type-option
"letter")
803 (org-e-groff--letter-head title contents attr info
)
804 (let ((sa-item (plist-get attr
:salutation
))
805 (cn-item (plist-get attr
:confidential
))
806 (sj-item (plist-get attr
:subject
))
807 (rn-item (plist-get attr
:reference
))
808 (at-item (plist-get attr
:attention
)))
812 (if (stringp sa-item
)
813 (format ".LO SA \"%s\" \n" sa-item
)
817 (if (stringp cn-item
)
818 (format ".LO CN \"%s\"\n" cn-item
)
821 (when (and at-item
(stringp at-item
))
822 (format ".LO AT \"%s\" \n" at-item
))
823 (when (and title rn-item
)
824 (format ".LO RN \"%s\"\n" title
))
826 (when (and sj-item
(stringp sj-item
))
827 (format ".LO SJ \"%s\" \n" sj-item
))
830 ".LT " document-class-string
"\n"))))
837 ((string= last-option
"toc")
839 ((string= last-option
"sign")
840 (let ((fc-item (plist-get attr
:closing
)))
841 (concat (if (stringp fc-item
)
842 (format ".FC \"%s\" \n" fc-item
)
850 (when (string= (car item
) "NS")
851 (replace-regexp-in-string
852 "\\.P\n" "" (cdr item
))))
853 (reverse org-e-groff-special-content
) "\n")))))
857 ;;; Transcode Functions
861 ;; Babel Calls are ignored.
866 (defun org-e-groff-bold (bold contents info
)
867 "Transcode BOLD from Org to Groff.
868 CONTENTS is the text with bold markup. INFO is a plist holding
869 contextual information."
870 (org-e-groff--text-markup contents
'bold
))
874 (defun org-e-groff-center-block (center-block contents info
)
875 "Transcode a CENTER-BLOCK element from Org to Groff.
876 CONTENTS holds the contents of the center block. INFO is a plist
877 holding contextual information."
878 (org-e-groff--wrap-label
880 (format ".DS C \n%s\n.DE" contents
)))
884 (defun org-e-groff-clock (clock contents info
)
885 "Transcode a CLOCK element from Org to Groff.
886 CONTENTS is nil. INFO is a plist holding contextual
889 (format "\\fB%s\\fP " org-clock-string
)
890 (format org-e-groff-inactive-timestamp-format
891 (concat (org-translate-time (org-element-property :value clock
))
892 (let ((time (org-element-property :time clock
)))
893 (and time
(format " (%s)" time
)))))))
897 (defun org-e-groff-code (code contents info
)
898 "Transcode a CODE object from Org to Groff.
899 CONTENTS is nil. INFO is a plist used as a communication
901 (org-e-groff--text-markup (org-element-property :value code
) 'code
))
903 ;;; Comments and Comment Blocks are ignored.
907 (defun org-e-groff-drawer (drawer contents info
)
908 "Transcode a DRAWER element from Org to Groff.
909 CONTENTS holds the contents of the block. INFO is a plist
910 holding contextual information."
911 (let* ((name (org-element-property :drawer-name drawer
))
912 (output (if (functionp org-e-groff-format-drawer-function
)
913 (funcall org-e-groff-format-drawer-function
915 ;; If there's no user defined function: simply
916 ;; display contents of the drawer.
918 (org-e-groff--wrap-label drawer output
)))
922 (defun org-e-groff-dynamic-block (dynamic-block contents info
)
923 "Transcode a DYNAMIC-BLOCK element from Org to Groff.
924 CONTENTS holds the contents of the block. INFO is a plist
925 holding contextual information. See `org-export-data'."
926 (org-e-groff--wrap-label dynamic-block contents
))
930 (defun org-e-groff-entity (entity contents info
)
931 "Transcode an ENTITY object from Org to Groff.
932 CONTENTS are the definition itself. INFO is a plist holding
933 contextual information."
934 (let ((ent (org-element-property :utf8 entity
))) ent
))
938 (defun org-e-groff-example-block (example-block contents info
)
939 "Transcode an EXAMPLE-BLOCK element from Org to Groff.
940 CONTENTS is nil. INFO is a plist holding contextual
942 (org-e-groff--wrap-label
944 (format ".DS L\n%s\n.DE"
945 (org-export-format-code-default example-block info
))))
949 (defun org-e-groff-export-block (export-block contents info
)
950 "Transcode a EXPORT-BLOCK element from Org to Groff.
951 CONTENTS is nil. INFO is a plist holding contextual information."
952 (when (string= (org-element-property :type export-block
) "GROFF")
953 (org-remove-indentation (org-element-property :value export-block
))))
957 (defun org-e-groff-export-snippet (export-snippet contents info
)
958 "Transcode a EXPORT-SNIPPET object from Org to Groff.
959 CONTENTS is nil. INFO is a plist holding contextual information."
960 (when (eq (org-export-snippet-backend export-snippet
) 'e-groff
)
961 (org-element-property :value export-snippet
)))
965 (defun org-e-groff-fixed-width (fixed-width contents info
)
966 "Transcode a FIXED-WIDTH element from Org to Groff.
967 CONTENTS is nil. INFO is a plist holding contextual information."
968 (org-e-groff--wrap-label
970 (format "\\fC\n%s\\fP"
971 (org-remove-indentation
972 (org-element-property :value fixed-width
)))))
974 ;;; Footnote Definition
976 ;; Footnote Definitions are ignored.
978 ;; Footnotes are handled automatically in GROFF. Although manual
979 ;; references can be added, not really required.
981 (defun org-e-groff-footnote-reference (footnote-reference contents info
)
982 ;; Changing from info to footnote-reference
983 (let* ((raw (org-export-get-footnote-definition footnote-reference info
))
984 (n (org-export-get-footnote-number footnote-reference info
))
985 (data (org-trim (org-export-data raw info
)))
986 (ref-id (plist-get (nth 1 footnote-reference
) :label
)))
988 (if (string-match "fn:rl" ref-id
)
989 (if (member ref-id org-e-groff-registered-references
)
990 (format "\\*[%s]" ref-id
)
992 (push ref-id org-e-groff-registered-references
)
993 (format "\\*(Rf\n.RS \"%s\" \n%s\n.RF\n" ref-id data
)))
994 ;; else it is a footnote
995 (format "\\u\\s-2%s\\d\\s+2\n.FS %s\n%s\n.FE\n" n n data
))))
999 (defun org-e-groff-headline (headline contents info
)
1000 "Transcode an HEADLINE element from Org to Groff.
1001 CONTENTS holds the contents of the headline. INFO is a plist
1002 holding contextual information."
1003 (let* ((class (plist-get info
:groff-class
))
1004 (level (org-export-get-relative-level headline info
))
1005 (numberedp (org-export-numbered-headline-p headline info
))
1006 ;; Section formatting will set two placeholders: one for the
1007 ;; title and the other for the contents.
1008 (classes (assoc class org-e-groff-classes
))
1009 (classes-options (car (last classes
)))
1010 (heading-option (plist-get classes-options
:heading
))
1014 ((and (symbolp heading-option
)
1015 (fboundp heading-option
))
1016 (funcall heading-option level numberedp
))
1019 (concat ".H " (number-to-string level
) " \"%s\"\n%s")
1020 ".HU \"%s\"\n%s")))))
1021 ;; End of section-fmt
1022 (text (org-export-data (org-element-property :title headline
) info
))
1024 (and (plist-get info
:with-todo-keywords
)
1025 (let ((todo (org-element-property :todo-keyword headline
)))
1026 (and todo
(org-export-data todo info
)))))
1027 (todo-type (and todo
(org-element-property :todo-type headline
)))
1028 (tags (and (plist-get info
:with-tags
)
1029 (org-export-get-tags headline info
)))
1030 (priority (and (plist-get info
:with-priority
)
1031 (org-element-property :priority headline
)))
1032 ;; Create the headline text along with a no-tag version. The
1033 ;; latter is required to remove tags from table of contents.
1034 (full-text (if (functionp org-e-groff-format-headline-function
)
1035 ;; User-defined formatting function.
1036 (funcall org-e-groff-format-headline-function
1037 todo todo-type priority text tags
)
1038 ;; Default formatting.
1041 (format "\\fB%s\\fP " todo
))
1042 (when priority
(format " [\\#%c] " priority
))
1045 (format " \\fC:%s:\\fP "
1046 (mapconcat 'identity tags
":"))))))
1048 (if (functionp org-e-groff-format-headline-function
)
1049 ;; User-defined formatting function.
1050 (funcall org-e-groff-format-headline-function
1051 todo todo-type priority text nil
)
1052 ;; Default formatting.
1054 (when todo
(format "\\fB%s\\fP " todo
))
1055 (when priority
(format " [\\#%c] " priority
))
1057 ;; Associate some \label to the headline for internal links.
1059 ;; (format "\\label{sec-%s}\n"
1060 ;; (mapconcat 'number-to-string
1061 ;; (org-export-get-headline-number headline info)
1065 (make-string (org-element-property :pre-blank headline
) 10)))
1068 ;; Case 1: Special Tag
1069 ((member (car tags
) org-e-groff-special-tags
)
1071 ((string= (car tags
) "BODY") contents
)
1073 ((string= (car tags
) "NS")
1075 (push (cons (car tags
)
1076 (format ".NS \"%s\" 1 \n%s"
1077 (car (org-element-property :title headline
))
1079 org-e-groff-special-content
) nil
))
1083 (push (cons (car tags
) contents
) org-e-groff-special-content
)
1086 ;; Case 2: This is a footnote section: ignore it.
1087 ((org-element-property :footnote-section-p headline
) nil
)
1089 ;; Case 3: This is a deep sub-tree: export it as a list item.
1090 ;; Also export as items headlines for which no section
1091 ;; format has been found.
1092 ((or (not section-fmt
) (org-export-low-level-p headline info
))
1093 ;; Build the real contents of the sub-tree.
1094 (let ((low-level-body
1096 ;; If the headline is the first sibling, start a list.
1097 (when (org-export-first-sibling-p headline info
)
1098 (format "%s\n" (if numberedp
".AL 1\n" ".DL \n")))
1100 ".LI\n" full-text
"\n" headline-label pre-blanks contents
)))
1101 ;; If headline is not the last sibling simply return
1102 ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
1104 (if (not (org-export-last-sibling-p headline info
)) low-level-body
1105 (replace-regexp-in-string
1110 ;; Case 4. Standard headline. Export it as a section.
1112 (format section-fmt full-text
1113 (concat headline-label pre-blanks contents
))))))
1118 ;;; Inline Babel Call
1120 ;; Inline Babel Calls are ignored.
1122 ;;; Inline Src Block
1124 (defun org-e-groff-inline-src-block (inline-src-block contents info
)
1125 "Transcode an INLINE-SRC-BLOCK element from Org to Groff.
1126 CONTENTS holds the contents of the item. INFO is a plist holding
1127 contextual information."
1128 (let* ((code (org-element-property :value inline-src-block
)))
1130 (org-e-groff-source-highlight
1131 (let* ((tmpdir (if (featurep 'xemacs
)
1133 temporary-file-directory
))
1134 (in-file (make-temp-name
1135 (expand-file-name "srchilite" tmpdir
)))
1136 (out-file (make-temp-name
1137 (expand-file-name "reshilite" tmpdir
)))
1138 (org-lang (org-element-property :language inline-src-block
))
1139 (lst-lang (cadr (assq (intern org-lang
)
1140 org-e-groff-source-highlight-langs
)))
1142 (cmd (concat (expand-file-name "source-highlight")
1144 " -f groff_mm_color "
1148 (let ((code-block ""))
1149 (with-temp-file in-file
(insert code
))
1151 (setq code-block
(org-file-contents out-file
))
1152 (delete-file in-file
)
1153 (delete-file out-file
)
1155 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1158 ;; Do not use a special package: transcode it verbatim.
1160 (concat ".DS I\n" "\\fC" code
"\\fP\n.DE\n")))))
1164 (defun org-e-groff-inlinetask (inlinetask contents info
)
1165 "Transcode an INLINETASK element from Org to Groff.
1166 CONTENTS holds the contents of the block. INFO is a plist
1167 holding contextual information."
1168 (let ((title (org-export-data (org-element-property :title inlinetask
) info
))
1169 (todo (and (plist-get info
:with-todo-keywords
)
1170 (let ((todo (org-element-property :todo-keyword inlinetask
)))
1171 (and todo
(org-export-data todo info
)))))
1172 (todo-type (org-element-property :todo-type inlinetask
))
1173 (tags (and (plist-get info
:with-tags
)
1174 (org-export-get-tags inlinetask info
)))
1175 (priority (and (plist-get info
:with-priority
)
1176 (org-element-property :priority inlinetask
))))
1177 ;; If `org-e-groff-format-inlinetask-function' is provided, call it
1178 ;; with appropriate arguments.
1179 (if (functionp org-e-groff-format-inlinetask-function
)
1180 (funcall org-e-groff-format-inlinetask-function
1181 todo todo-type priority title tags contents
)
1182 ;; Otherwise, use a default template.
1183 (org-e-groff--wrap-label
1187 (when todo
(format "\\fB%s\\fP " todo
))
1188 (when priority
(format " [\\#%c] " priority
))
1190 (when tags
(format " \\fC:%s:\\fP "
1191 (mapconcat 'identity tags
":"))))))
1192 (format (concat "\n.DS I\n"
1197 full-title contents
))))))
1201 (defun org-e-groff-italic (italic contents info
)
1202 "Transcode ITALIC from Org to Groff.
1203 CONTENTS is the text with italic markup. INFO is a plist holding
1204 contextual information."
1205 (org-e-groff--text-markup contents
'italic
))
1209 (defun org-e-groff-item (item contents info
)
1210 "Transcode an ITEM element from Org to Groff.
1211 CONTENTS holds the contents of the item. INFO is a plist holding
1212 contextual information."
1213 (let* ((bullet (org-element-property :bullet item
))
1214 (type (org-element-property
1215 :type
(org-element-property :parent item
)))
1216 (checkbox (case (org-element-property :checkbox item
)
1217 (on "\\o'\\(sq\\(mu'")
1219 (trans "\\o'\\(sq\\(mi'")))
1220 (tag (let ((tag (org-element-property :tag item
)))
1221 ;; Check-boxes must belong to the tag.
1222 (and tag
(format "%s"
1224 (org-export-data tag info
)))))))
1228 (concat ".LI ""\"" (or tag
(concat "\\ " checkbox
)) "\""
1230 (org-trim (or contents
" "))))
1234 (org-trim (or contents
" "))))
1236 (let* ((bullet (org-trim bullet
))
1237 (marker (cond ((string= "-" bullet
) "\\(em")
1238 ((string= "*" bullet
) "\\(bu")
1240 (concat ".LI " marker
"\n"
1241 (org-trim (or contents
" "))))))))
1245 (defun org-e-groff-keyword (keyword contents info
)
1246 "Transcode a KEYWORD element from Org to Groff.
1247 CONTENTS is nil. INFO is a plist holding contextual information."
1248 (let ((key (org-element-property :key keyword
))
1249 (value (org-element-property :value keyword
)))
1251 ((string= key
"GROFF") value
)
1254 ;;; Groff Environment
1256 (defun org-e-groff-groff-environment (groff-environment contents info
)
1257 "Transcode a GROFF-ENVIRONMENT element from Org to Groff.
1258 CONTENTS is nil. INFO is a plist holding contextual information."
1259 (let ((label (org-element-property :name groff-environment
))
1260 (value (org-remove-indentation
1261 (org-element-property :value groff-environment
))))
1262 (if (not (org-string-nw-p label
)) value
1263 ;; Environment is labelled: label must be within the environment
1264 ;; (otherwise, a reference pointing to that element will count
1265 ;; the section instead).
1268 (goto-char (point-min))
1270 (insert (format "%s\n" label
))
1275 (defun org-e-groff-groff-fragment (groff-fragment contents info
)
1276 "Transcode a GROFF-FRAGMENT object from Org to Groff.
1277 CONTENTS is nil. INFO is a plist holding contextual information."
1278 (org-element-property :value groff-fragment
))
1282 (defun org-e-groff-line-break (line-break contents info
)
1283 "Transcode a LINE-BREAK object from Org to Groff.
1284 CONTENTS is nil. INFO is a plist holding contextual information."
1288 ;; Inline images just place a call to .PSPIC or .PS/.PE
1289 ;; and load the graph.
1291 (defun org-e-groff-link--inline-image (link info
)
1292 "Return Groff code for an inline image.
1293 LINK is the link pointing to the inline image. INFO is a plist
1294 used as a communication channel."
1295 (let* ((parent (org-export-get-parent-element link
))
1296 (path (let ((raw-path (org-element-property :path link
)))
1297 (if (not (file-name-absolute-p raw-path
)) raw-path
1298 (expand-file-name raw-path
))))
1299 (attr (read (format "(%s)"
1302 (org-element-property :attr_groff parent
)
1305 (case (plist-get attr
:position
)
1310 (width (or (plist-get attr
:width
) ""))
1311 (height (or (plist-get attr
:height
) ""))
1313 (disable-caption (plist-get attr
:disable-caption
))
1316 (org-e-groff--caption/label-string
1317 (org-element-property :caption parent
)
1318 (org-element-property :name parent
)
1321 ;; Now clear ATTR from any special keyword and set a default value
1322 ;; if nothing is left. Return proper string.
1325 ((and org-e-groff-raster-to-ps
1326 (or (string-match ".\.png$" path
)
1327 (string-match ".\.jpg$" path
)))
1328 (let ((eps-path (concat path
".eps")))
1329 (shell-command (format org-e-groff-raster-to-ps path eps-path
))
1330 (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1331 placement eps-path width height
)))
1332 ((string-match ".\.pic$" path
)
1333 (format "\n.PS\ncopy \"%s\"\n.PE" path
))
1334 (t (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
1335 placement path width height
)))
1336 (unless disable-caption
(format "\n.FG \"%s\"" caption
)))))
1338 (defun org-e-groff-link (link desc info
)
1339 "Transcode a LINK object from Org to Groff.
1341 DESC is the description part of the link, or the empty string.
1342 INFO is a plist holding contextual information. See
1345 (let* ((type (org-element-property :type link
))
1346 (raw-path (org-element-property :path link
))
1347 ;; Ensure DESC really exists, or set it to nil.
1348 (desc (and (not (string= desc
"")) desc
))
1349 (imagep (org-export-inline-image-p
1350 link org-e-groff-inline-image-rules
))
1352 ((member type
'("http" "https" "ftp" "mailto"))
1353 (concat type
":" raw-path
))
1354 ((string= type
"file")
1355 (when (string-match "\\(.+\\)::.+" raw-path
)
1356 (setq raw-path
(match-string 1 raw-path
)))
1357 (if (file-name-absolute-p raw-path
)
1358 (concat "file://" (expand-file-name raw-path
))
1359 (concat "file://" raw-path
)))
1364 (imagep (org-e-groff-link--inline-image link info
))
1365 ;; import groff files
1366 ((and (string= type
"file")
1367 (string-match ".\.groff$" raw-path
))
1368 (concat ".so " raw-path
"\n"))
1369 ;; Radio link: transcode target's contents and use them as link's
1371 ((string= type
"radio")
1372 (let ((destination (org-export-resolve-radio-link link info
)))
1374 (format "\\fI [%s] \\fP"
1375 (org-export-solidify-link-text path
)))))
1377 ;; Links pointing to an headline: find destination and build
1378 ;; appropriate referencing command.
1379 ((member type
'("custom-id" "fuzzy" "id"))
1380 (let ((destination (if (string= type
"fuzzy")
1381 (org-export-resolve-fuzzy-link link info
)
1382 (org-export-resolve-id-link link info
))))
1383 (case (org-element-type destination
)
1384 ;; Id link points to an external file.
1386 (if desc
(format "%s \\fBat\\fP \\fIfile://%s\\fP" desc destination
)
1387 (format "\\fI file://%s \\fP" destination
)))
1388 ;; Fuzzy link points nowhere.
1390 (format org-e-groff-link-with-unknown-path-format
1393 (org-element-property :raw-link link
) info
))))
1394 ;; Fuzzy link points to an invisible target.
1396 ;; LINK points to an headline. If headlines are numbered
1397 ;; and the link has no description, display headline's
1398 ;; number. Otherwise, display description or headline's
1402 (if (and (plist-get info
:section-numbers
) (not desc
))
1403 (format "\\fI%s\\fP" label
)
1404 (format "\\fI%s\\fP"
1407 (org-element-property :title destination
) info
))))))
1408 ;; Fuzzy link points to a target. Do as above.
1410 (let ((path (org-export-solidify-link-text path
)))
1411 (if (not desc
) (format "\\fI%s\\fP" path
)
1412 (format "%s \\fBat\\fP \\fI%s\\fP" desc path
)))))))
1413 ;; External link with a description part.
1414 ((and path desc
) (format "%s \\fBat\\fP \\fI%s\\fP" path desc
))
1415 ;; External link without a description part.
1416 (path (format "\\fI%s\\fP" path
))
1417 ;; No path, only description. Try to do something useful.
1418 (t (format org-e-groff-link-with-unknown-path-format desc
)))))
1422 (defun org-e-groff-paragraph (paragraph contents info
)
1423 "Transcode a PARAGRAPH element from Org to Groff.
1424 CONTENTS is the contents of the paragraph, as a string. INFO is
1425 the plist used as a communication channel."
1426 (let ((parent (plist-get (nth 1 paragraph
) :parent
)))
1428 (let* ((parent-type (car parent
))
1429 (fixed-paragraph "")
1430 (class (plist-get info
:groff-class
))
1431 (class-options (plist-get info
:groff-class-options
))
1432 (classes (assoc class org-e-groff-classes
))
1433 (classes-options (car (last classes
)))
1434 (paragraph-option (plist-get classes-options
:paragraph
)))
1436 ((and (symbolp paragraph-option
)
1437 (fboundp paragraph-option
))
1438 (funcall paragraph-option parent-type parent contents
))
1439 ((and (eq parent-type
'item
)
1440 (plist-get (nth 1 parent
) :bullet
))
1441 (setq fixed-paragraph
(concat "" contents
)))
1442 ((eq parent-type
'section
)
1443 (setq fixed-paragraph
(concat ".P\n" contents
)))
1444 ((eq parent-type
'footnote-definition
)
1445 (setq fixed-paragraph
(concat "" contents
)))
1446 (t (setq fixed-paragraph
(concat "" contents
))))
1451 (defun org-e-groff-plain-list (plain-list contents info
)
1452 "Transcode a PLAIN-LIST element from Org to Groff.
1453 CONTENTS is the contents of the list. INFO is a plist holding
1454 contextual information."
1455 (let* ((type (org-element-property :type plain-list
))
1456 (attr (mapconcat #'identity
1457 (org-element-property :attr_groff plain-list
)
1460 ((eq type
'ordered
) ".AL")
1461 ((eq type
'unordered
) ".BL")
1462 ((eq type
'descriptive
) ".VL 2.0i"))))
1463 (org-e-groff--wrap-label
1465 (format "%s\n%s\n.LE" groff-type contents
))))
1469 (defun org-e-groff-plain-text (text info
)
1470 "Transcode a TEXT string from Org to Groff.
1471 TEXT is the string to transcode. INFO is a plist holding
1472 contextual information."
1474 (setq text
(replace-regexp-in-string
1475 "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
1476 "$\\" text nil t
1))
1477 ;; Handle quotation marks
1478 (setq text
(org-e-groff--quotation-marks text info
))
1479 ;; Handle Special Characters
1480 (if org-e-groff-special-char
1481 (dolist (special-char-list org-e-groff-special-char
)
1483 (replace-regexp-in-string (car special-char-list
)
1484 (cdr special-char-list
) text
))))
1485 ;; Handle break preservation if required.
1486 (when (plist-get info
:preserve-breaks
)
1487 (setq text
(replace-regexp-in-string
1488 "\\(\\\\\\\\\\)?[ \t]*\n" " \\\\\\\\\n" text
)))
1494 (defun org-e-groff-planning (planning contents info
)
1495 "Transcode a PLANNING element from Org to Groff.
1496 CONTENTS is nil. INFO is a plist holding contextual
1503 (let ((closed (org-element-property :closed planning
)))
1506 (format "\\fR %s \\fP" org-closed-string
)
1507 (format org-e-groff-inactive-timestamp-format
1508 (org-translate-time closed
)))))
1509 (let ((deadline (org-element-property :deadline planning
)))
1512 (format "\\fB %s \\fP" org-deadline-string
)
1513 (format org-e-groff-active-timestamp-format
1514 (org-translate-time deadline
)))))
1515 (let ((scheduled (org-element-property :scheduled planning
)))
1518 (format "\\fR %s \\fP" org-scheduled-string
)
1519 (format org-e-groff-active-timestamp-format
1520 (org-translate-time scheduled
)))))))
1526 (defun org-e-groff-property-drawer (property-drawer contents info
)
1527 "Transcode a PROPERTY-DRAWER element from Org to Groff.
1528 CONTENTS is nil. INFO is a plist holding contextual
1530 ;; The property drawer isn't exported but we want separating blank
1531 ;; lines nonetheless.
1536 (defun org-e-groff-quote-block (quote-block contents info
)
1537 "Transcode a QUOTE-BLOCK element from Org to Groff.
1538 CONTENTS holds the contents of the block. INFO is a plist
1539 holding contextual information."
1540 (org-e-groff--wrap-label
1542 (format ".DS I\n.I\n%s\n.R\n.DE" contents
)))
1546 (defun org-e-groff-quote-section (quote-section contents info
)
1547 "Transcode a QUOTE-SECTION element from Org to Groff.
1548 CONTENTS is nil. INFO is a plist holding contextual information."
1549 (let ((value (org-remove-indentation
1550 (org-element-property :value quote-section
))))
1551 (when value
(format ".DS L\n\\fI%s\\fP\n.DE\n" value
))))
1555 (defun org-e-groff-radio-target (radio-target text info
)
1556 "Transcode a RADIO-TARGET object from Org to Groff.
1557 TEXT is the text of the target. INFO is a plist holding
1558 contextual information."
1560 (org-export-solidify-link-text
1561 (org-element-property :value radio-target
))
1566 (defun org-e-groff-section (section contents info
)
1567 "Transcode a SECTION element from Org to Groff.
1568 CONTENTS holds the contents of the section. INFO is a plist
1569 holding contextual information."
1574 (defun org-e-groff-special-block (special-block contents info
)
1575 "Transcode a SPECIAL-BLOCK element from Org to Groff.
1576 CONTENTS holds the contents of the block. INFO is a plist
1577 holding contextual information."
1578 (let ((type (downcase (org-element-property :type special-block
))))
1579 (org-e-groff--wrap-label
1581 (format "%s\n" contents
))))
1585 (defun org-e-groff-src-block (src-block contents info
)
1586 "Transcode a SRC-BLOCK element from Org to Groff.
1587 CONTENTS holds the contents of the item. INFO is a plist holding
1588 contextual information."
1589 (let* ((lang (org-element-property :language src-block
))
1590 (caption (org-element-property :caption src-block
))
1591 (label (org-element-property :name src-block
))
1592 (code (org-element-property :value src-block
))
1593 (custom-env (and lang
1594 (cadr (assq (intern lang
)
1595 org-e-groff-custom-lang-environments
))))
1596 (num-start (case (org-element-property :number-lines src-block
)
1597 (continued (org-export-get-loc src-block info
))
1599 (retain-labels (org-element-property :retain-labels src-block
))
1601 (read (format "(%s)"
1602 (mapconcat #'identity
1603 (org-element-property :attr_groff src-block
)
1605 (disable-caption (plist-get attr
:disable-caption
)))
1608 ;; Case 1. No source fontification.
1609 ((not org-e-groff-source-highlight
)
1610 (let ((caption-str (org-e-groff--caption/label-string caption label info
)))
1612 (format ".DS I\n\\fC%s\\fP\n.DE\n"
1613 (org-export-format-code-default src-block info
))
1614 (unless disable-caption
(format ".EX \"%s\" " caption-str
)))))
1616 ;; Case 2. Source fontification.
1617 (org-e-groff-source-highlight
1618 (let* ((tmpdir (if (featurep 'xemacs
)
1620 temporary-file-directory
))
1621 (caption-str (org-e-groff--caption/label-string caption label info
))
1622 (in-file (make-temp-name
1623 (expand-file-name "srchilite" tmpdir
)))
1624 (out-file (make-temp-name
1625 (expand-file-name "reshilite" tmpdir
)))
1627 (org-lang (org-element-property :language src-block
))
1628 (lst-lang (cadr (assq (intern org-lang
)
1629 org-e-groff-source-highlight-langs
)))
1631 (cmd (concat "source-highlight"
1633 " -f groff_mm_color "
1639 (let ((code-block ""))
1640 (with-temp-file in-file
(insert code
))
1642 (setq code-block
(org-file-contents out-file
))
1643 (delete-file in-file
)
1644 (delete-file out-file
)
1645 (format "%s\n" code-block
))
1646 (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
1648 (unless disable-caption
(format ".EX \"%s\" " caption-str
))))))))
1651 ;;; Statistics Cookie
1653 (defun org-e-groff-statistics-cookie (statistics-cookie contents info
)
1654 "Transcode a STATISTICS-COOKIE object from Org to Groff.
1655 CONTENTS is nil. INFO is a plist holding contextual information."
1656 (org-element-property :value statistics-cookie
))
1661 (defun org-e-groff-strike-through (strike-through contents info
)
1662 "Transcode STRIKE-THROUGH from Org to Groff.
1663 CONTENTS is the text with strike-through markup. INFO is a plist
1664 holding contextual information."
1665 (org-e-groff--text-markup contents
'strike-through
))
1669 (defun org-e-groff-subscript (subscript contents info
)
1670 "Transcode a SUBSCRIPT object from Org to Groff.
1671 CONTENTS is the contents of the object. INFO is a plist holding
1672 contextual information."
1673 (format "\\d\\s-2%s\\s+2\\u" contents
))
1675 ;;; Superscript "^_%s$
1677 (defun org-e-groff-superscript (superscript contents info
)
1678 "Transcode a SUPERSCRIPT object from Org to Groff.
1679 CONTENTS is the contents of the object. INFO is a plist holding
1680 contextual information."
1681 (format "\\u\\s-2%s\\s+2\\d" contents
))
1686 ;; `org-e-groff-table' is the entry point for table transcoding. It
1687 ;; takes care of tables with a "verbatim" attribute. Otherwise, it
1688 ;; delegates the job to `org-e-groff-table--org-table' function,
1689 ;; depending of the type of the table.
1691 ;; `org-e-groff-table--align-string' is a subroutine used to build
1692 ;; alignment string for Org tables.
1694 (defun org-e-groff-table (table contents info
)
1695 "Transcode a TABLE element from Org to Groff.
1696 CONTENTS is the contents of the table. INFO is a plist holding
1697 contextual information."
1699 ;; Case 1: verbatim table.
1700 ((or org-e-groff-tables-verbatim
1701 (let ((attr (read (format "(%s)"
1704 (org-element-property :attr_groff table
) " ")))))
1705 (and attr
(plist-get attr
:verbatim
))))
1707 (format ".DS L\n\\fC%s\\fP\n.DE"
1708 ;; Re-create table, without affiliated keywords.
1710 (org-element-interpret-data
1711 `(table nil
,@(org-element-contents table
))))))
1713 ;; Case 2: Standard table.
1714 (t (org-e-groff-table--org-table table contents info
))))
1716 (defun org-e-groff-table--align-string (divider table info
)
1717 "Return an appropriate Groff alignment string.
1718 TABLE is the considered table. INFO is a plist used as
1719 a communication channel."
1721 ;; Extract column groups and alignment from first (non-rule)
1727 (and (eq (org-element-property :type row
) 'standard
) row
))
1731 (let* ((borders (org-export-table-cell-borders cell info
))
1732 (raw-width (org-export-table-cell-width cell info
))
1733 (width-cm (when raw-width
(/ raw-width
5)))
1734 (width (if raw-width
(format "w(%dc)"
1735 (if (< width-cm
1) 1 width-cm
)) "")))
1736 ;; Check left border for the first cell only.
1737 ;; Alignment is nil on assignment
1739 (when (and (memq 'left borders
) (not alignment
))
1740 (push "|" alignment
))
1742 (case (org-export-table-cell-alignment cell info
)
1743 (left (concat "l" width divider
))
1744 (right (concat "r" width divider
))
1745 (center (concat "c" width divider
)))
1747 (when (memq 'right borders
) (push "|" alignment
))))
1749 (apply 'concat
(reverse alignment
))))
1751 (defun org-e-groff-table--org-table (table contents info
)
1752 "Return appropriate Groff code for an Org table.
1754 TABLE is the table type element to transcode. CONTENTS is its
1755 contents, as a string. INFO is a plist used as a communication
1758 This function assumes TABLE has `org' as its `:type' attribute."
1759 (let* ((label (org-element-property :name table
))
1760 (caption (org-e-groff--caption/label-string
1761 (org-element-property :caption table
) label info
))
1762 (attr (read (format "(%s)"
1763 (mapconcat #'identity
1764 (org-element-property :attr_groff table
)
1766 (divider (if (plist-get attr
:divider
) "|" " "))
1768 ;; Determine alignment string.
1769 (alignment (org-e-groff-table--align-string divider table info
))
1771 ;; Extract others display options.
1773 (lines (org-split-string contents
"\n"))
1779 (if (plist-get attr
:expand
)
1782 (case (plist-get attr
:placement
)
1786 (if org-e-groff-tables-centered
1789 (case (plist-get attr
:boxtype
)
1791 ('doublebox
"doublebox")
1796 (if (not (null attr-item
))
1797 (add-to-list 'result-list attr-item
)))
1800 (title-line (plist-get attr
:title-line
))
1801 (disable-caption (plist-get attr
:disable-caption
))
1802 (long-cells (plist-get attr
:long-cells
))
1807 (or (car attr-list
) ""))
1810 (when (cdr attr-list
)
1811 (dolist (attr-item (cdr attr-list
))
1812 (setq output-list
(concat output-list
1813 (format ",%s" attr-item
)))))
1816 (when lines
(org-split-string (car lines
) "\t"))))
1817 ;; Prepare the final format string for the table.
1823 (concat ".TS\n " table-format
";\n"
1825 (let ((final-line ""))
1827 (dotimes (i (length first-line
))
1828 (setq final-line
(concat final-line
"cb" divider
))))
1830 (setq final-line
(concat final-line
"\n"))
1833 (setq final-line
(concat final-line alignment
))
1834 (dotimes (i (length first-line
))
1835 (setq final-line
(concat final-line
"c" divider
))))
1839 (let ((final-line "")
1841 (lines (org-split-string contents
"\n")))
1843 (dolist (line-item lines
)
1848 (if (string= line-item
"_")
1849 (setq long-line
(format "%s\n" line-item
))
1851 (let ((cell-item-list (org-split-string line-item
"\t")))
1852 (dolist (cell-item cell-item-list
)
1854 (cond ((eq cell-item
(car (last cell-item-list
)))
1855 (setq long-line
(concat long-line
1856 (format "T{\n%s\nT}\t\n" cell-item
))))
1858 (setq long-line
(concat long-line
1859 (format "T{\n%s\nT}\t" cell-item
))))))
1862 (setq final-line
(concat final-line long-line
)))
1864 (setq final-line
(concat final-line line-item
"\n"))))
1867 (if (not disable-caption
)
1868 (format ".TB \"%s\""
1873 (defun org-e-groff-table-cell (table-cell contents info
)
1874 "Transcode a TABLE-CELL element from Org to Groff
1875 CONTENTS is the cell contents. INFO is a plist used as
1876 a communication channel."
1878 (concat (if (and contents
1879 org-e-groff-table-scientific-notation
1880 (string-match orgtbl-exp-regexp contents
))
1881 ;; Use appropriate format string for scientific
1883 (format org-e-groff-table-scientific-notation
1884 (match-string 1 contents
)
1885 (match-string 2 contents
))
1887 (when (org-export-get-next-element table-cell info
) "\t"))))
1892 (defun org-e-groff-table-row (table-row contents info
)
1893 "Transcode a TABLE-ROW element from Org to Groff
1894 CONTENTS is the contents of the row. INFO is a plist used as
1895 a communication channel."
1896 ;; Rules are ignored since table separators are deduced from
1897 ;; borders of the current row.
1898 (when (eq (org-element-property :type table-row
) 'standard
)
1899 (let* ((attr (mapconcat 'identity
1900 (org-element-property
1901 :attr_groff
(org-export-get-parent table-row
))
1903 ;; TABLE-ROW's borders are extracted from its first cell.
1905 (org-export-table-cell-borders
1906 (car (org-element-contents table-row
)) info
)))
1908 ;; Mark horizontal lines
1909 (cond ((and (memq 'top borders
) (memq 'above borders
)) "_\n"))
1912 ;; When BOOKTABS are activated enforce bottom rule even when
1913 ;; no hline was specifically marked.
1914 ((and (memq 'bottom borders
) (memq 'below borders
)) "\n_")
1915 ((memq 'below borders
) "\n_"))))))
1919 (defun org-e-groff-target (target contents info
)
1920 "Transcode a TARGET object from Org to Groff.
1921 CONTENTS is nil. INFO is a plist holding contextual
1923 (format "\\fI%s\\fP"
1924 (org-export-solidify-link-text (org-element-property :value target
))))
1928 (defun org-e-groff-timestamp (timestamp contents info
)
1929 "Transcode a TIMESTAMP object from Org to Groff.
1930 CONTENTS is nil. INFO is a plist holding contextual
1932 (let ((value (org-translate-time (org-element-property :value timestamp
)))
1933 (type (org-element-property :type timestamp
)))
1934 (cond ((memq type
'(active active-range
))
1935 (format org-e-groff-active-timestamp-format value
))
1936 ((memq type
'(inactive inactive-range
))
1937 (format org-e-groff-inactive-timestamp-format value
))
1938 (t (format org-e-groff-diary-timestamp-format value
)))))
1942 (defun org-e-groff-underline (underline contents info
)
1943 "Transcode UNDERLINE from Org to Groff.
1944 CONTENTS is the text with underline markup. INFO is a plist
1945 holding contextual information."
1946 (org-e-groff--text-markup contents
'underline
))
1950 (defun org-e-groff-verbatim (verbatim contents info
)
1951 "Transcode a VERBATIM object from Org to Groff.
1952 CONTENTS is nil. INFO is a plist used as a communication
1954 (org-e-groff--text-markup (org-element-property :value verbatim
) 'verbatim
))
1958 (defun org-e-groff-verse-block (verse-block contents info
)
1959 "Transcode a VERSE-BLOCK element from Org to Groff.
1960 CONTENTS is verse block contents. INFO is a plist holding
1961 contextual information."
1962 (format ".DS C\n.ft HI\n%s\n.ft\n.DE" contents
))
1965 ;;; Interactive functions
1967 (defun org-e-groff-export-to-groff
1968 (&optional subtreep visible-only body-only ext-plist pub-dir
)
1969 "Export current buffer to a Groff file.
1971 If narrowing is active in the current buffer, only export its
1974 If a region is active, export that region.
1976 When optional argument SUBTREEP is non-nil, export the sub-tree
1977 at point, extracting information from the headline properties
1980 When optional argument VISIBLE-ONLY is non-nil, don't export
1981 contents of hidden elements.
1983 EXT-PLIST, when provided, is a property list with external
1984 parameters overriding Org default settings, but still inferior to
1985 file-local settings.
1987 When optional argument PUB-DIR is set, use it as the publishing
1990 Return output file's name."
1992 (setq org-e-groff-registered-references nil
)
1993 (setq org-e-groff-special-content nil
)
1994 (let ((outfile (org-export-output-file-name ".groff" subtreep pub-dir
)))
1996 'e-groff outfile subtreep visible-only body-only ext-plist
)))
1998 (defun org-e-groff-export-to-pdf
1999 (&optional subtreep visible-only body-only ext-plist pub-dir
)
2000 "Export current buffer to Groff then process through to PDF.
2002 If narrowing is active in the current buffer, only export its
2005 If a region is active, export that region.
2007 When optional argument SUBTREEP is non-nil, export the sub-tree
2008 at point, extracting information from the headline properties
2011 When optional argument VISIBLE-ONLY is non-nil, don't export
2012 contents of hidden elements.
2014 EXT-PLIST, when provided, is a property list with external
2015 parameters overriding Org default settings, but still inferior to
2016 file-local settings.
2018 When optional argument PUB-DIR is set, use it as the publishing
2021 Return PDF file's name."
2023 (org-e-groff-compile
2024 (org-e-groff-export-to-groff
2025 subtreep visible-only body-only ext-plist pub-dir
)))
2027 (defun org-e-groff-compile (grofffile)
2028 "Compile a Groff file.
2030 GROFFFILE is the name of the file being compiled. Processing is
2031 done through the command specified in `org-e-groff-pdf-process'.
2033 Return PDF file name or an error if it couldn't be produced."
2034 (let* ((wconfig (current-window-configuration))
2035 (grofffile (file-truename grofffile
))
2036 (base (file-name-sans-extension grofffile
))
2038 (message (format "Processing Groff file %s ..." grofffile
))
2042 ;; A function is provided: Apply it.
2043 ((functionp org-e-groff-pdf-process
)
2044 (funcall org-e-groff-pdf-process
(shell-quote-argument grofffile
)))
2045 ;; A list is provided: Replace %b, %f and %o with appropriate
2046 ;; values in each command before applying it. Output is
2047 ;; redirected to "*Org PDF Groff Output*" buffer.
2048 ((consp org-e-groff-pdf-process
)
2049 (let* ((out-dir (or (file-name-directory grofffile
) "./"))
2050 (outbuf (get-buffer-create "*Org PDF Groff Output*")))
2054 (replace-regexp-in-string
2055 "%b" (shell-quote-argument base
)
2056 (replace-regexp-in-string
2057 "%f" (shell-quote-argument grofffile
)
2058 (replace-regexp-in-string
2059 "%o" (shell-quote-argument out-dir
) command t t
)
2062 org-e-groff-pdf-process
)
2063 ;; Collect standard errors from output buffer.
2064 (setq errors
(org-e-groff-collect-errors outbuf
))))
2065 (t (error "No valid command to process to PDF")))
2066 (let ((pdffile (concat base
".pdf")))
2067 ;; Check for process failure. Provide collected errors if
2069 (if (not (file-exists-p pdffile
))
2070 (error (concat (format "PDF file %s wasn't produced" pdffile
)
2071 (when errors
(concat ": " errors
))))
2072 ;; Else remove log files, when specified, and signal end of
2073 ;; process to user, along with any error encountered.
2074 (when org-e-groff-remove-logfiles
2075 (dolist (ext org-e-groff-logfiles-extensions
)
2076 (let ((file (concat base
"." ext
)))
2077 (when (file-exists-p file
) (delete-file file
)))))
2078 (message (concat "Process completed"
2079 (if (not errors
) "."
2080 (concat " with errors: " errors
)))))
2081 ;; Return output file name.
2083 (set-window-configuration wconfig
))))
2085 (defun org-e-groff-collect-errors (buffer)
2086 "Collect some kind of errors from \"groff\" output
2087 BUFFER is the buffer containing output.
2088 Return collected error types as a string, or nil if there was
2090 (with-current-buffer buffer
2092 (goto-char (point-max))
2097 (provide 'org-e-groff
)
2098 ;;; org-e-groff.el ends here