~ubuntu-branches/ubuntu/jaunty/python-docutils/jaunty

« back to all changes in this revision

Viewing changes to tools/editors/emacs/restructuredtext.el

  • Committer: Bazaar Package Importer
  • Author(s): martin f. krafft
  • Date: 2006-07-10 11:45:05 UTC
  • mfrom: (2.1.4 edgy)
  • Revision ID: james.westby@ubuntu.com-20060710114505-otkhqcslevewxmz5
Tags: 0.4-3
Added build dependency on python-central (closes: #377580).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
;; Authors: David Goodger <goodger@python.org>;
2
 
;;          Martin Blais
3
 
;; Date: $Date: 2003/05/23 13:48:22 $
4
 
;; Copyright: This module has been placed in the public domain.
5
 
;;
6
 
;; Support code for editing reStructuredText with Emacs indented-text mode.
7
 
;; The goal is to create an integrated reStructuredText editing mode.
8
 
;;
9
 
;; Updates
10
 
;; -------
11
 
;;
12
 
;; 2003-02-25 (blais): updated repeat-last-character function and added
13
 
;;                     a few routines for navigating between titles.
14
 
 
15
 
(defun replace-lines (fromchar tochar)
16
 
  ;; by David Goodger
17
 
  "Replace flush-left lines, consisting of multiple FROMCHAR characters,
18
 
with equal-length lines of TOCHAR."
19
 
  (interactive "\
20
 
cSearch for flush-left lines of char:
21
 
cand replace with char: ")
22
 
  (save-excursion
23
 
    (let* ((fromstr (string fromchar))
24
 
           (searchre (concat "^" (regexp-quote fromstr) "+ *$"))
25
 
           (found 0))
26
 
      (condition-case err
27
 
          (while t
28
 
            (search-forward-regexp searchre)
29
 
            (setq found (1+ found))
30
 
            (search-backward fromstr)  ;; point will be *before* last char
31
 
            (setq p (1+ (point)))
32
 
            (beginning-of-line)
33
 
            (setq l (- p (point)))
34
 
            (kill-line)
35
 
            (insert-char tochar l))
36
 
        (search-failed
37
 
         (message (format "%d lines replaced." found)))))))
38
 
 
39
 
(defun repeat-last-character ()
40
 
  ;; by Martin Blais
41
 
  "Fills the current line up to the length of the preceding line (if not empty),
42
 
using the last character on the current line.  If the preceding line is empty,
43
 
or if a prefix argument is provided, fill up to the fill-column.
44
 
 
45
 
If the current line is longer than the desired length, shave the characters off
46
 
the current line to fit the desired length.
47
 
 
48
 
As an added convenience, if the command is repeated immediately, the alternative
49
 
behaviour is performed."
50
 
 
51
 
;; TODO
52
 
;; ----
53
 
;; It would be useful if only these characters were repeated:
54
 
;; =-`:.'"~^_*+#<>!$%&(),/;?@[\]{|}
55
 
;; Especially, empty lines shouldn't be repeated.
56
 
 
57
 
  (interactive)
58
 
  (let* ((curcol (current-column))
59
 
         (curline (+ (count-lines (point-min) (point)) (if (eq curcol 0) 1 0)))
60
 
         (lbp (line-beginning-position 0))
61
 
         (prevcol (if (= curline 1)
62
 
                      fill-column
63
 
                    (save-excursion
64
 
                      (forward-line -1)
65
 
                      (end-of-line)
66
 
                      (skip-chars-backward " \t" lbp)
67
 
                      (let ((cc (current-column)))
68
 
                        (if (= cc 0) fill-column cc)))))
69
 
         (rightmost-column
70
 
          (cond (current-prefix-arg fill-column)
71
 
                ((equal last-command 'repeat-last-character)
72
 
                 (if (= curcol fill-column) prevcol fill-column))
73
 
                (t (save-excursion
74
 
                     (if (= prevcol 0) fill-column prevcol))) )) )
75
 
    (end-of-line)
76
 
    (if (> (current-column) rightmost-column)
77
 
        ;; shave characters off the end
78
 
        (delete-region (- (point)
79
 
                          (- (current-column) rightmost-column))
80
 
                       (point))
81
 
      ;; fill with last characters
82
 
      (insert-char (preceding-char)
83
 
                   (- rightmost-column (current-column)))) ))
84
 
 
85
 
(defun reST-title-char-p (c)
86
 
  ;; by Martin Blais
87
 
  "Returns true if the given character is a valid title char."
88
 
  (and (string-match "[-=`:\\.'\"~^_*+#<>!$%&(),/;?@\\\|]" 
89
 
                     (char-to-string c)) t))
90
 
 
91
 
(defun reST-forward-title ()
92
 
  ;; by Martin Blais
93
 
  "Skip to the next restructured text section title."
94
 
  (interactive)
95
 
  (let* ( (newpoint
96
 
           (save-excursion
97
 
             (forward-char) ;; in case we're right on a title
98
 
             (while
99
 
               (not
100
 
                (and (re-search-forward "^[A-Za-z0-9].*[ \t]*$" nil t)
101
 
                     (reST-title-char-p (char-after (+ (point) 1)))
102
 
                     (looking-at (format "\n%c\\{%d,\\}[ \t]*$" 
103
 
                                         (char-after (+ (point) 1)) 
104
 
                                         (current-column))))))
105
 
             (beginning-of-line)
106
 
             (point))) )
107
 
    (if newpoint (goto-char newpoint)) ))
108
 
 
109
 
(defun reST-backward-title ()
110
 
  ;; by Martin Blais
111
 
  "Skip to the previous restructured text section title."
112
 
  (interactive)
113
 
  (let* ( (newpoint
114
 
           (save-excursion
115
 
             ;;(forward-char) ;; in case we're right on a title
116
 
             (while
117
 
               (not
118
 
                (and (or (backward-char) t)
119
 
                     (re-search-backward "^[A-Za-z0-9].*[ \t]*$" nil t)
120
 
                     (or (end-of-line) t)
121
 
                     (reST-title-char-p (char-after (+ (point) 1)))
122
 
                     (looking-at (format "\n%c\\{%d,\\}[ \t]*$" 
123
 
                                         (char-after (+ (point) 1)) 
124
 
                                         (current-column))))))
125
 
             (beginning-of-line)
126
 
             (point))) )
127
 
    (if newpoint (goto-char newpoint)) ))
128
 
 
129
 
(defun join-paragraph ()
130
 
  ;; by David Goodger
131
 
  "Join lines in current paragraph into one line, removing end-of-lines."
132
 
  (interactive)
133
 
  (save-excursion
134
 
    (backward-paragraph 1)
135
 
    (forward-char 1)
136
 
    (let ((start (point)))      ; remember where we are
137
 
      (forward-paragraph 1)     ; go to the end of the paragraph
138
 
      (beginning-of-line 0)     ; go to the beginning of the previous line
139
 
      (while (< start (point))  ; as long as we haven't passed where we started
140
 
        (delete-indentation)    ; join this line to the line before
141
 
        (beginning-of-line))))) ; and go back to the beginning of the line
142
 
 
143
 
(defun force-fill-paragraph ()
144
 
  ;; by David Goodger
145
 
  "Fill paragraph at point, first joining the paragraph's lines into one.
146
 
This is useful for filling list item paragraphs."
147
 
  (interactive)
148
 
  (join-paragraph)
149
 
  (fill-paragraph nil))