;;; howto.el: Minor mode defining some functions for DocBook v3.1 DTD. ;;; Copyright (C) 2000 Philippe Martin ;; Author : Philippe Martin ; $Id: howto.el,v 1.7 2000/06/12 20:42:16 feloy Exp feloy $ ;; This file is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 2, or (at your option) ;; any later version. ;; This file is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;;; INSTRUCTIONS ;; To fully install this, add the following lines to the appropriate ;; init file: ;; ;; (add-hook 'sgml-mode-hook ;; '(lambda () "Defaults for SGML mode" ;; (load-library "howto.el") ;; )) ;; ;; ;; LIST OF COMMANDS ;; ;; howto-mode ;; howto-new-document (C-c h d) ;; howto-insert-section (C-c h s) ;; howto-insert-itemized-list (C-c h i) ;; howto-insert-numbered-list (C-c h n) ;; howto-insert-figure (C-c h f) ;; howto-insert-email (C-c h e) ;; howto-insert-key-sequence (C-c h k) ;; howto-insert-quote (C-c h q) ;; howto-insert-url (C-c h u) ;; howto-insert-footnote (C-c h n) ;; howto-insert-indexterm (C-c h t) ;; howto-insert-simple-table ;; ;; LIST OF USER SETTABLE VARIABLES ;; ;; howto-insert-comments ;; howto-section-with-id ;; howto-insert-index ;;; Code: (defvar howto-insert-comments nil "If non-nil, insert comments for article header, sections and subsections.") (defvar howto-section-with-id nil "If non-nil, query an id for sections.") (defvar howto-insert-index nil "If non-nil, insert an index at the end of the document.") (defvar howto-mode nil "Non-nil if HowTo minor mode is enabled.") (make-variable-buffer-local 'howto-mode) (or (assq 'howto-mode minor-mode-alist) (setq minor-mode-alist (cons '(howto-mode " HowTo") minor-mode-alist))) (defvar howto-minor-keymap (let ((map (make-sparse-keymap))) (define-key map "\C-chd" 'howto-new-document) (define-key map "\C-chs" 'howto-insert-section) (define-key map "\C-chi" 'howto-insert-itemized-list) (define-key map "\C-chn" 'howto-insert-numbered-list) (define-key map "\C-chf" 'howto-insert-figure) (define-key map "\C-che" 'howto-insert-email) (define-key map "\C-chk" 'howto-insert-key-sequence) (define-key map "\C-chq" 'howto-insert-quote) (define-key map "\C-chu" 'howto-insert-url) (define-key map "\C-chn" 'howto-insert-footnote) (define-key map "\C-cht" 'howto-insert-indexterm) map) "Keymap used for HowTo minor mode.") (or (not (boundp 'minor-mode-map-alist)) (assoc 'howto-mode minor-mode-map-alist) (setq minor-mode-map-alist (cons (cons 'howto-mode howto-minor-keymap) minor-mode-map-alist))) (defun howto-mode (&optional arg) "howto minor mode." (interactive "P") (setq howto-mode (if (null arg) (not howto-mode) (> (prefix-numeric-value arg) 0))) ) (defun sgml-insert-element-without-attributes (name &optional after silent) "Reads element name from minibuffer and inserts start and end tags." (interactive (list (sgml-read-element-name "Element: ") sgml-leave-point-after-insert)) (let (newpos ; position to leave cursor at element ; inserted element (sgml-show-warnings nil)) (when (and name (not (equal name ""))) (when (sgml-break-brefore-stag-p name) (sgml-insert-break)) (sgml-insert-tag (sgml-start-tag-of name) 'silent) (if (and sgml-xml-p (sgml-check-empty name)) (forward-char -2) (forward-char -1)) (setq element (sgml-find-element-of (point))) ; (sgml-insert-attributes (funcall sgml-new-attribute-list-function ; element) ; (sgml-element-attlist element)) (if (and sgml-xml-p (sgml-check-empty name)) (forward-char 2) (forward-char 1)) (when (sgml-break-after-stag-p name) (sgml-insert-break)) (when (not (sgml-element-empty element)) (when (and sgml-auto-insert-required-elements (sgml-model-group-p sgml-current-state)) (let (tem) (while (and (setq tem (sgml-required-tokens sgml-current-state)) (null (cdr tem))) (setq tem (sgml-insert-element (car tem) t t)) (setq newpos (or newpos tem)) (sgml-parse-to-here)) (when tem ; more than one req elem (insert "\n") (when sgml-insert-missing-element-comment (insert (format "" tem)) (sgml-indent-line nil element))))) (setq newpos (or newpos (point))) (when sgml-insert-end-tag-on-new-line (insert "\n")) (sgml-insert-tag (sgml-end-tag-of name) 'silent) (unless after (goto-char newpos)) (unless silent (sgml-show-context))) newpos))) (defun howto-insert-entities () (if howto-insert-index (insert "\n"))) (defun howto-new-document () "Interactively begin a new HowTo document." (interactive) (let (title author-firstname author-surname author-email pubdate abstract) (setq sgml-auto-insert-required-elements nil) (setq title (read-from-minibuffer "Title: ")) (setq author-firstname (read-from-minibuffer "Author firstname: ")) (setq author-surname (read-from-minibuffer "Author surname: ")) (setq author-email (read-from-minibuffer "Author email: ")) (setq pubdate (read-from-minibuffer "Publication date: ")) (setq abstract (read-from-minibuffer "Abstract: ")) (insert "") (sgml-parse-prolog) (sgml-insert-element "article") (if howto-insert-comments (insert "\n\n")) (sgml-insert-element "artheader") (sgml-insert-element "title") (insert title) (sgml-up-element) (sgml-insert-element "author") (sgml-insert-element "firstname") (insert author-firstname) (sgml-up-element) (sgml-insert-element "surname") (insert author-surname) (sgml-up-element) (sgml-insert-element "affiliation") (sgml-insert-element "address") (sgml-insert-element "email") (insert author-email) (sgml-up-element) (sgml-up-element) (sgml-up-element) (sgml-up-element) (sgml-insert-element "pubdate") (insert pubdate) (sgml-up-element) (sgml-insert-element "abstract") (sgml-insert-element "para") (insert abstract) (sgml-up-element) (sgml-up-element) (sgml-up-element) (if howto-insert-comments (insert "\n\n")) (if howto-insert-index (let ((start (point))) (insert "\n\n&index;") (goto-char start))) )) (defun howto-insert-section () "Insert a section." (interactive) (let (level title id) (setq sgml-auto-insert-required-elements nil) (setq level "") (while (not (and (> (string-to-number level) 0) (< (string-to-number level) 5) (integerp (string-to-number level)))) (setq level (read-from-minibuffer "Level [1,2,3,4]: "))) (setq title (read-from-minibuffer "Title: ")) (if howto-section-with-id (setq id (read-from-minibuffer "ID: "))) (if (and howto-insert-comments (or (string= level "1") (string= level "2"))) (progn (insert "\n\n"))) (sgml-insert-element (concat "sect" level)) (if howto-section-with-id (sgml-insert-attribute "ID" id)) (sgml-insert-element "title") (insert title) (sgml-up-element) (sgml-insert-element "para") (if (and howto-insert-comments (or (string= level "1") (string= level "2"))) (let ((start (point))) (sgml-up-element) (sgml-up-element) (insert "\n\n") (goto-char start))))) (defun howto-insert-itemized-list () "Insert an itemized list." (interactive) (let (item) (setq sgml-auto-insert-required-elements nil) (sgml-insert-element "itemizedlist") (setq item " ") (while (> (length item) 0) (setq item (read-from-minibuffer "Item: ")) (if (> (length item) 0) (progn (sgml-insert-element "listitem") (sgml-insert-element "para") (insert item) (sgml-up-element) (sgml-up-element) ))) (sgml-up-element))) (defun howto-insert-numbered-list () "Insert a numbered list." (interactive) (let (item) (setq sgml-auto-insert-required-elements nil) (sgml-insert-element "orderedlist") (setq item " ") (while (> (length item) 0) (setq item (read-from-minibuffer "Item: ")) (if (> (length item) 0) (progn (sgml-insert-element "listitem") (sgml-insert-element "para") (insert item) (sgml-up-element) (sgml-up-element) ))) (sgml-up-element))) (defun howto-insert-figure () "Insert a figure." (interactive) (let (title jpg eps text) (setq sgml-auto-insert-required-elements nil) (setq title (read-from-minibuffer "Title: ")) (setq eps (read-from-minibuffer "EPS image: ")) (setq jpg (read-from-minibuffer "JPEG image: ")) (setq text (read-from-minibuffer "Text: ")) (setq caption (read-from-minibuffer "Caption: ")) (sgml-insert-element "figure") (sgml-insert-element "title") (insert title) (sgml-up-element) (sgml-insert-element "mediaobject") (sgml-insert-element "imageobject") (sgml-insert-element "imagedata") (sgml-insert-attribute "FILEREF" eps) (sgml-insert-attribute "FORMAT" "eps") (sgml-up-element) (sgml-insert-element "imageobject") (sgml-insert-element "imagedata") (sgml-insert-attribute "FILEREF" jpg) (sgml-insert-attribute "FORMAT" "jpg") (sgml-up-element) (sgml-insert-element "textobject") (sgml-insert-element "phrase") (insert text) (sgml-up-element) (sgml-up-element) (if (> (length caption) 0) (progn (sgml-insert-element "caption") (sgml-insert-element "para") (insert caption) (sgml-up-element) (sgml-up-element) )) (sgml-up-element) (sgml-up-element))) (defun howto-insert-email () "Insert an email address." (interactive) (let (email) (setq sgml-auto-insert-required-elements nil) (setq email (read-from-minibuffer "Email: ")) (sgml-insert-element "email") (insert email) (sgml-up-element))) (defun howto-insert-key-sequence () "Insert a combinaison or sequence of keys." (interactive) (let (key) (setq sgml-auto-insert-required-elements nil) (sgml-insert-element "keycombo") (setq key " ") (while (> (length key) 0) (setq key (read-from-minibuffer "Key: ")) (if (> (length key) 0) (progn (sgml-insert-element "keycap") (insert key) (sgml-up-element) ))) (sgml-up-element))) (defun howto-insert-quote () "Insert a quote text." (interactive) (let (author text) (setq sgml-auto-insert-required-elements nil) (setq author (read-from-minibuffer "Author: ")) (setq text (read-from-minibuffer "Text: ")) (sgml-insert-element "blockquote") (sgml-insert-element "attribution") (insert author) (sgml-up-element) (sgml-insert-element "para") (insert text) (sgml-up-element) (sgml-up-element))) (defun howto-insert-url () "Insert a URL." (interactive) (let (url name) (setq sgml-auto-insert-required-elements nil) (setq url (read-from-minibuffer "URL: ")) (setq name (read-from-minibuffer "Name: ")) (sgml-insert-element-without-attributes "ulink") (sgml-insert-attribute "URL" url) (insert (if (> (length name) 0) name url)) (sgml-up-element))) (defun howto-insert-footnote () "Insert a footnote." (interactive) (setq sgml-auto-insert-required-elements nil) (sgml-insert-element "footnote") (sgml-insert-element "para")) (defun howto-insert-indexterm () "Insert a term in the index." (interactive) (let ((primary "") (secondary "") (tertiary "") (see "") (seealso " ") (zone "")) (setq zone (read-from-minibuffer "Zone: ")) (setq primary (read-from-minibuffer "Primary: ")) (if (> (length primary) 0) (setq secondary (read-from-minibuffer "Secondary: "))) (if (> (length secondary) 0) (setq tertiary (read-from-minibuffer "Tertiary: "))) (if (> (length primary) 0) (setq see (read-from-minibuffer "See: "))) (sgml-insert-element "indexterm") (if (> (length zone) 0) (sgml-insert-attribute "ZONE" zone)) (sgml-insert-element "primary") (insert primary) (sgml-up-element) (if (> (length secondary) 0) (progn (sgml-insert-element "secondary") (insert secondary) (sgml-up-element))) (if (> (length tertiary) 0) (progn (sgml-insert-element "tertiary") (insert tertiary) (sgml-up-element))) (if (> (length see) 0) (progn (sgml-insert-element "see") (insert see) (sgml-up-element)) (progn (while (> (length seealso) 0) (setq seealso (read-from-minibuffer "See also: ")) (if (> (length seealso) 0) (progn (sgml-insert-element "seealso") (insert seealso) (sgml-up-element) ))))) (sgml-up-element))) (defun howto-insert-simple-table () "Insert a table." (interactive) (let (title columns lines n-columns n-lines header footer) (setq sgml-auto-insert-required-elements nil) (setq title (read-from-minibuffer "Title: ")) (setq columns "") (while (not (and (> (string-to-number columns) 0) (integerp (string-to-number columns)))) (setq columns (read-from-minibuffer "Columns: "))) (setq n-columns (string-to-number columns)) (setq lines "") (while (not (and (> (string-to-number lines) 0) (integerp (string-to-number lines)))) (setq lines (read-from-minibuffer "Lines: "))) (setq n-lines (string-to-number lines)) (sgml-insert-element "table") (sgml-insert-element "title") (insert title) (sgml-up-element) (sgml-insert-element-without-attributes "tgroup") (sgml-insert-attribute "COLS" columns) (setq header (read-from-minibuffer "Header 1: ")) (if (> (length header) 0) (progn (sgml-insert-element "thead") (sgml-insert-element "row") (sgml-insert-element "entry") (insert header) (sgml-up-element) (loop for i from 2 to n-columns do (progn (setq header (read-from-minibuffer (concat "Header " (number-to-string i) ": "))) (sgml-insert-element "entry") (insert header) (sgml-up-element))) (sgml-up-element) (sgml-up-element))) (setq footer (read-from-minibuffer "Footer 1: ")) (if (> (length footer) 0) (progn (sgml-insert-element "tfoot") (sgml-insert-element "row") (sgml-insert-element "entry") (insert footer) (sgml-up-element) (loop for i from 2 to n-columns do (progn (setq footer (read-from-minibuffer (concat "Footer " (number-to-string i) ": "))) (sgml-insert-element "entry") (insert footer) (sgml-up-element))) (sgml-up-element) (sgml-up-element))) (sgml-insert-element "tbody") (loop for l from 1 to n-lines do (progn (sgml-insert-element "row") (loop for c from 1 to n-columns do (progn (sgml-insert-element "entry") (sgml-up-element))) (sgml-up-element))) ))