home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ruby164.zip / rbemx164.zip / ruby / share / doc / rdtool-0.6.10 / utils / rd-mode.el next >
Lisp/Scheme  |  2001-04-19  |  12KB  |  380 lines

  1. ;;;
  2. ;;; rd-mode.el
  3. ;;; Major mode for RD editing
  4. ;;; 
  5. ;;; NOTE: experimental.
  6.  
  7. ;; Copyright (C) 1999 Koji Arai, Toshiro Kuwabara.
  8.  
  9. ;; Author: Koji Arai, Toshiro Kuwabara
  10. ;; Created: Sat Nov 27 00:08:12 1999
  11.  
  12. ;; This file is not part of GNU Emacs, but the same permissions apply.
  13.  
  14. ;; GNU Emacs is free software; you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation; either version 2, or (at your option)
  17. ;; any later version.
  18.  
  19. ;; GNU Emacs is distributed in the hope that it will be useful,
  20. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. ;; GNU General Public License for more details.
  23.  
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  26. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  27. ;; Boston, MA 02111-1307, USA.
  28.  
  29. ;; Settings:
  30. ;;
  31. ;; add below lines in your ~/.emacs
  32. ;;
  33. ;; (global-font-lock-mode 1 t)
  34. ;; (autoload 'rd-mode "rd-mode" "major mode for ruby document formatter RD" t)
  35. ;; (add-to-list 'auto-mode-alist '("\\.rd$" . rd-mode))
  36.  
  37. (require 'derived)
  38.  
  39. (defvar rd-use-prompt-when-insertion-p nil
  40.   "Whether to use prompt when inserting inline.")
  41.  
  42. (defvar rd-selective-display-ellipses t
  43.   "*Displays ellipses in RD-mode if non-nil")
  44.  
  45. (define-derived-mode rd-mode text-mode "RD"
  46.   "Major mode for RD editing.
  47. \\{rd-mode-map}"
  48.   (make-local-variable 'paragraph-separate)
  49.   (setq paragraph-separate "=+\\|\\++\\|[ \t\n\^L]*$")
  50.   (make-local-variable 'paragraph-start)
  51.   (setq paragraph-start "=+\\|\\++\\|[ \t\n\^L]")
  52.   (make-local-variable 'require-final-newline)  
  53.   (setq require-final-newline t)
  54.   (make-local-variable 'font-lock-defaults)
  55.   (setq font-lock-defaults '((rd-font-lock-keywords) t nil))
  56.   (make-local-variable 'font-lock-keywords)
  57.   (setq font-lock-keywords rd-font-lock-keywords)
  58.   (make-local-variable 'outline-regexp)
  59.   (setq outline-regexp "^\\(=+\\)")
  60.   (outline-minor-mode t)
  61.   (add-hook (make-local-variable 'write-contents-hooks) 'rd-strip-cr-on-top)
  62.   (add-hook (make-local-variable 'after-save-hook) 'rd-rehide-endline)
  63.   (rd-hide-other-block-all)
  64.   (rd-setup-keys)
  65.   (setq indent-tabs-mode nil)
  66. )
  67.  
  68. (defvar rd-font-lock-keywords
  69.   (list
  70.    '("^= .*$"
  71.      0 font-lock-keyword-face)
  72.    '("^== .*$"
  73.      0 font-lock-type-face)
  74.    '("^=== .*$"
  75.       0 font-lock-variable-name-face)
  76.    '("^=====* .*$"
  77.      0 font-lock-comment-face)
  78.    '("((\\*[^*]*\\*+\\([^)*][^%]*\\*+\\)*))"    ; ((* ... *))
  79.      0 font-lock-function-name-face)
  80.    '("((%[^%]*%+\\([^)%][^%]*%+\\)*))"      ; ((% ... %))
  81.      0 font-lock-function-name-face)
  82.    '("((|[^|]*|+\\([^)|][^|]*|+\\)*))"      ; ((| ... |))
  83.      0 font-lock-function-name-face)
  84.    '("(('[^']*'+\\([^)'][^']*'+\\)*))"      ; ((' ... '))
  85.      0 font-lock-function-name-face)
  86.    '("((:[^:]*:+\\([^):][^:]*:+\\)*))"      ; ((: ... :))
  87.      0 font-lock-function-name-face)
  88.    '("((-[^-]*-+\\([^)-][^-]*-+\\)*))"      ; ((- ... -))
  89.      0 font-lock-function-name-face)
  90.    '("((<[^>]*>+\\([^)>][^>]*>+\\)*))"      ; ((< ... >))
  91.      0 font-lock-function-name-face)
  92.    '("(({[^}]*}+\\([^)}][^}]*}+\\)*))"      ; (({ ... }))
  93.      0 font-lock-function-name-face)
  94.    '("^:.*$"
  95.      0 font-lock-reference-face)
  96.    ))
  97.  
  98. (defun rd-setup-keys ()
  99.   (interactive)
  100.   (define-key rd-mode-map "\t" 'rd-indent-line)
  101.   (define-key rd-mode-map "\C-j" 'rd-newline-and-indent)
  102.   (define-key rd-mode-map "\C-cv" 'rd-cite-region)
  103.   (define-key rd-mode-map "\C-cie" 'rd-insert-emphasis)
  104.   (define-key rd-mode-map "\C-cic" 'rd-insert-code)
  105.   (define-key rd-mode-map "\C-civ" 'rd-insert-var)
  106.   (define-key rd-mode-map "\C-cik" 'rd-insert-keyboard)
  107.   (define-key rd-mode-map "\C-cii" 'rd-insert-index)
  108.   (define-key rd-mode-map "\C-cir" 'rd-insert-ref)
  109.   (define-key rd-mode-map "\C-ciu" 'rd-insert-reftourl)
  110.   (define-key rd-mode-map "\C-cif" 'rd-insert-footnote)
  111.   (define-key rd-mode-map "\C-cib" 'rd-insert-verb)
  112.   (define-key rd-mode-map "\C-cy" 'rd-yank-as-url)
  113.   (define-key rd-mode-map "\C-c\C-y" 'rd-yank-pop-as-url)
  114.   (define-key rd-mode-map "\C-c\C-u" 'rd-insert-url))
  115.  
  116. (defun rd-strip-cr-on-top ()
  117.   (save-excursion
  118.     (widen)
  119.     (goto-char (point-min))
  120.     (let ((mod (buffer-modified-p)))
  121.       (while (re-search-forward "^\r=end\\>" nil t)
  122.     (beginning-of-line)
  123.     (delete-char 1)
  124.     (forward-line))
  125.       (set-buffer-modified-p mod)))
  126.   nil)
  127.  
  128. (defun rd-rehide-endline ()
  129.   (save-excursion
  130.     (widen)
  131.     (goto-char (point-min))
  132.     (let ((mod (buffer-modified-p)))
  133.       (while (re-search-forward "^=end\\>.*\r" nil t)
  134.     (beginning-of-line)
  135.     (insert "\r")
  136.     (forward-line))
  137.       (set-buffer-modified-p mod))))
  138.  
  139. (defun rd-hide-other-block ()
  140.   "Hides following lines not in RD format."
  141.   (interactive)
  142.   (let (end (mod (buffer-modified-p)))
  143.     (save-excursion
  144.       (widen)
  145.       (and (setq end (re-search-forward "^=begin\\>" nil t))
  146.        (re-search-backward "^=end\\>" nil t))
  147.     (insert "\r")
  148.     (while (search-forward "\n" end t)
  149.       (replace-match "\r" t t)))
  150.     (set-buffer-modified-p mod))
  151.   (setq selective-display t
  152.     selective-display-ellipses rd-selective-display-ellipses))
  153.  
  154. (defun rd-hide-other-block-all ()
  155.   "Hides all lines not in RD format."
  156.   (interactive)
  157.   (let (beg end (mod (buffer-modified-p)))
  158.     (save-excursion
  159.       (widen)
  160.       (goto-char (point-min))
  161.       (while (and (re-search-forward "^=end\\>" nil t)
  162.           (setq beg (progn (beginning-of-line) (point)))
  163.           (setq end (re-search-forward "^=begin\\>" nil t)))
  164.     (goto-char beg)
  165.     (insert "\r")
  166.     (while (search-forward "\n" end t)
  167.       (replace-match "\r" t t))))
  168.     (set-buffer-modified-p mod))
  169.   (setq selective-display t
  170.     selective-display-ellipses rd-selective-display-ellipses))
  171.  
  172. (defun rd-show-other-block ()
  173.   "Shows lines not in RD format before current point."
  174.   (interactive)
  175.   (if selective-display
  176.       (save-excursion
  177.     (let (end (mod (buffer-modified-p)))
  178.       (widen)
  179.       (if (re-search-forward "^\r=end\\>" nil t)
  180.           (progn
  181.         (end-of-line)
  182.         (setq end (point))
  183.         (beginning-of-line)
  184.         (delete-char 1)
  185.         (while (search-forward "\r" end t)
  186.           (replace-match "\n" t t))))
  187.       (set-buffer-modified-p mod)))))
  188.  
  189. (defun rd-show-other-block-all ()
  190.   "Shows all lines not in RD format."
  191.   (interactive)
  192.   (if selective-display
  193.       (save-excursion
  194.     (let (end (mod (buffer-modified-p)))
  195.       (widen)
  196.       (goto-char (point-min))
  197.       (while (re-search-forward "^\r=end\\>" nil t)
  198.         (end-of-line)
  199.         (setq end (point))
  200.         (beginning-of-line)
  201.         (delete-char 1)
  202.         (while (search-forward "\r" end t)
  203.           (replace-match "\n" t t)))
  204.       (set-buffer-modified-p mod))))
  205.   (setq selective-display nil selective-display-ellipses t))
  206.  
  207. (defun rd-show-label-list ()
  208.   "Show RD Label list through temporary buffer."
  209.   (interactive)
  210.   (occur "^\\(=+\\s-\\|\\+\\|\\s-*:\\|\\s-*---\\)"))
  211.  
  212. (defun rd-insert-inline (beg end str)
  213.   "Insert Inline Inline (general)."
  214.   (if str
  215.       (insert (concat beg str end))
  216.   (progn
  217.     (if rd-use-prompt-when-insertion-p
  218.         (rd-insert-inline beg end (read-string (concat beg " elm " end ": ")))
  219.       (insert beg end)
  220.       (backward-char (length end))))))
  221.  
  222. (defun rd-insert-emphasis (&optional str)
  223.   "Insert Inline Emphasis."
  224.   (interactive "*") (rd-insert-inline "((*" "*))" str))
  225.  
  226. (defun rd-insert-code (&optional str)
  227.   "Insert Inline Code."
  228.   (interactive "*") (rd-insert-inline "(({" "}))" str))
  229.  
  230. (defun rd-insert-var (&optional str)
  231.   "Insert Inline Var."
  232.   (interactive "*") (rd-insert-inline "((|" "|))" str))
  233.  
  234. (defun rd-insert-keyboard (&optional str)
  235.   "Insert Inline Keyboard."
  236.   (interactive "*") (rd-insert-inline "((%" "%))" str))
  237.  
  238. (defun rd-insert-index (&optional str)
  239.   "Insert Inline Index."
  240.   (interactive "*") (rd-insert-inline "((:" ":))" str))
  241.  
  242. (defun rd-insert-ref (&optional str)
  243.   "Insert Inline Reference."
  244.   (interactive "*") (rd-insert-inline "((<" ">))" str))
  245.  
  246. (defun rd-insert-reftourl (&optional str)
  247.   "Insert Inline RefToURL."
  248.   (interactive "*") (rd-insert-inline "((<URL:" ">))" str))
  249.  
  250. (defun rd-insert-footnote (&optional str)
  251.   "Insert Inline Footnote."
  252.   (interactive "*") (rd-insert-inline "((-" "-))" str))
  253.  
  254. (defun rd-insert-verb (&optional str)
  255.   "Insert Inline Verb."
  256.   (interactive "*") (rd-insert-inline "(('" "'))" str))
  257.  
  258. (defun rd-yank-as-url (&optional arg)
  259.   "Yank as Inline RefToURL."
  260.   (interactive "*P")
  261.   (yank arg)
  262.   (setq this-command 'yank)
  263.   (let ((yanked-str
  264.      (concat "((<URL:" (buffer-substring (point) (mark)) ">))")))
  265.     (delete-region (point) (mark))
  266.     (if (listp arg)
  267.     (insert yanked-str)
  268.       (insert-before-markers yanked-str))))
  269.  
  270. (defun rd-yank-pop-as-url (arg)
  271.   "Yank pop as Inline RefToURL."
  272.   (interactive "*p")
  273.   (if (not (eq last-command 'yank))
  274.       (progn
  275.     (insert (symbol-name last-command))
  276.       (error "Previous command is not yank-like.")))
  277.   (setq this-command 'yank)
  278.   (yank-pop arg)
  279.   (let ((yanked-str
  280.      (concat "((<URL:" (buffer-substring (point) (mark)) ">))")))
  281.     (delete-region (point) (mark))
  282.     (insert yanked-str)))
  283.  
  284. (defun rd-newline-and-indent ()
  285.   "Newline and indent as deep as prev line."
  286.   (interactive "*")
  287.   (newline)
  288.   (rd-indent-line))
  289.  
  290. (defun rd-indent-line ()
  291.   "Indent line as deep as prev line."
  292.   (interactive "*")
  293.   (let ((prev-indent (progn
  294.                (forward-line -1)
  295.                (rd-current-indentation))))
  296.     (forward-line 1)
  297.     (rd-indent-to prev-indent)
  298.     (back-to-indentation)))
  299.  
  300. (defun rd-line-list-p ()
  301.   "Whether the line is list or not."
  302.   (save-excursion
  303.     (beginning-of-line)
  304.     (looking-at " *\\*\\|---")))
  305.  
  306. (defun rd-indent-to (num)
  307.   (let (beg)
  308.     (save-excursion
  309.       (beginning-of-line)
  310.       (setq beg (point))
  311.       (back-to-indentation)
  312.       (delete-region beg (point))
  313.       (indent-to num))))
  314.  
  315. (defun rd-current-indentation ()
  316.   ""
  317.   (save-excursion
  318.     (beginning-of-line)
  319.     (looking-at "--- +\\| *\\(\\* +\\)?")
  320.     (length (buffer-substring (match-beginning 0)(match-end 0)))))
  321.   
  322. (defun rd-cite-region (beg end)
  323.   "Make region into Verbatim."
  324.   (interactive "r*")
  325.   (let (listp prev-indent indent)
  326.     (save-excursion
  327.       (goto-char beg)
  328.       (forward-line -1)
  329.       (setq listp (rd-line-list-p)
  330.             prev-indent (rd-current-indentation))
  331.       (forward-line 1)
  332.       (setq indent (rd-current-indentation))
  333.       (if (and listp
  334.                (not (= (- indent prev-indent) 2)))
  335.           (rd-indent-region beg end (- (+ prev-indent 2) indent))
  336.         (cond ((= prev-indent indent)
  337.                (rd-indent-region beg end 2))
  338.               ((> prev-indent indent)
  339.                (rd-indent-region beg end prev-indent))
  340.               (t
  341.                (goto-char end)))))))
  342.   
  343. (defun rd-indent-region (beg end &optional indent)
  344.   "Make the indent of region deeper by INDENT."
  345.   (interactive "r*")
  346.   (setq indent (or indent 2))
  347.   (save-excursion
  348.     (goto-char beg)
  349.     (while (< (point) end)
  350.       (setq end (+ end indent))
  351.       (insert-char ?  indent)
  352.       (forward-line 1))))
  353.  
  354. (defun rd-yank-as-verbatim (&optional arg)
  355.   (interactive "P")
  356.   (let ((beg (point))
  357.         (end (progn
  358.                (yank)
  359.               (point)))
  360.         )
  361.     (rd-cite-region beg end)
  362.     (if arg (goto-char beg))))
  363.  
  364. (defun rd-insert-buffer-as-verbatim (buf)
  365.   (interactive "bInsert buffer (verb): ")
  366.   (insert-buffer buf)
  367.   (rd-cite-region (point)(mark)))
  368.  
  369.  
  370. (defun rd-insert-url (url label)
  371.   ""
  372.   (interactive "sURL: \nsLabel: ")
  373.   (if (string= label "")
  374.       (rd-insert-reftourl url)
  375.     (rd-insert-ref (concat label "|URL:" url))))
  376.  
  377. (provide 'rd-mode)
  378. ;;; rd-mode.el ends here
  379.  
  380.