File: latte.el

package info (click to toggle)
latte 2.1-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,064 kB
  • ctags: 1,223
  • sloc: sh: 7,506; cpp: 5,831; ansic: 662; perl: 628; makefile: 350; yacc: 196; lisp: 114
file content (185 lines) | stat: -rw-r--r-- 7,132 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
;;; latte.el --- Major mode for editing Latte source.

;; Copyright 1998,199 Zanshin Inc.           <http://www.zanshin.com/>

;; The contents of this file are subject to the Zanshin Public License Version
;; 1.0 (the "License"); you may not use this file except in compliance with the
;; License.  You should have received a copy of the License with Latte; see
;; the file COPYING.  You may also obtain a copy of the License at
;; <http://www.zanshin.com/ZPL.html>.
;; 
;; Software distributed under the License is distributed on an "AS IS" basis,
;; WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
;; for the specific language governing rights and limitations under the
;; License.
;; 
;; The Original Code is Latte.
;; 
;; The Initial Developer of the Original Code is Zanshin, Inc.

;;; Author:

;; Bob Glickstein <bobg@zanshin.com> <http://www.zanshin.com/~bobg>

;;; Plug:

;; Check out my book, "Writing GNU Emacs Extensions," from O'Reilly
;; and Associates.  <http://www.ora.com/catalog/gnuext/>

;;; Code:

(require 'derived)

(defvar latte-mode-map nil
  "*Keymap for Latte mode.")
(if latte-mode-map
    nil
  (setq latte-mode-map (copy-keymap text-mode-map))
  (substitute-key-definition 'backward-delete-char
                             'backward-delete-char-untabify
                             latte-mode-map)
  (substitute-key-definition 'delete-backward-char
                             'backward-delete-char-untabify
                             latte-mode-map)
  (define-key latte-mode-map "\t" 'indent-for-tab-command)
  )

(defvar latte-mode-syntax-table nil
  "*Syntax table for Latte mode.")
(if latte-mode-syntax-table
    nil
  (setq latte-mode-syntax-table (copy-syntax-table text-mode-syntax-table))
  (modify-syntax-entry ?\\ "\\" latte-mode-syntax-table))

(defvar latte-font-lock-keywords
  '(("\\\\;.*" . font-lock-comment-face)
    ("\\\\\\\\" . font-lock-keyword-face)
    ("\\\\/" . font-lock-keyword-face)
    ("\\\\['`]" . font-lock-keyword-face)
    ("\\\\,@?" . font-lock-keyword-face)
    ;; The following regex was created with
    ;; (sregexq "\\\""
    ;;          (0+ (or (not-char "\\")
    ;;                  (sequence "\\" (not-char "\""))))
    ;;          "\\\"")
    ;; See <http://www.zanshin.com/~bobg/sregex.html>
    ("\\\\\"\\([^\\\\]\\|\\\\[^\"]\\)*\\\\\"" 0 font-lock-string-face t)
    ("{\\(\\\\\\(def\\|if\\|lambda\\)\\)\\>" 1 font-lock-keyword-face)
    ("{\\(\\\\[A-Za-z_][A-Za-z_0-9?!+-]*\\)" 1 font-lock-function-name-face)
    ("\\\\[&=]?[A-Za-z_][A-Za-z_0-9?!+-]*" . font-lock-variable-name-face))
  "*Value of font-lock-keywords for Latte mode.")

(define-derived-mode latte-mode text-mode "Latte"
  "Major mode for editing Latte source."
  (make-local-variable 'comment-column)
  (make-local-variable 'comment-start-skip)
  (make-local-variable 'comment-start)
  (make-local-variable 'comment-end)
  (make-local-variable 'font-lock-defaults)
  (make-local-variable 'indent-line-function)
  (make-local-variable 'imenu-generic-expression)
  (setq comment-column 40
        comment-start-skip "\\\\;\\s-*"
        comment-start "\\; "
        comment-end ""                  ;xxx also set comment-indent-function?
        font-lock-defaults '(latte-font-lock-keywords t)
        indent-line-function 'latte-indent-line
        )
  (setq imenu-generic-expression
        '((nil "^{\\\\def\\s-*\\\\\\([A-Za-z_][A-Za-z_0-9?!+-]*\\)" 1))))

(defconst latte-keyword-regex
  (concat "\\\\\\("
          (mapconcat 'symbol-name
                     '(def if let while)
                     "\\|")
          "\\)\\>"))

(defun latte-indent-line ()
  (save-match-data
    (let* ((start (point))

           ;; the position of the first non-whitespace on the line
           (bol (progn (back-to-indentation)
                       (point)))

           ;; the position of the innermost open-brace enclosing bol
           (open (condition-case nil
                     (let ((looping t))
                       (while looping
                         (backward-up-list 1)
                         (setq looping
                               (not (= (following-char) ?{))))
                       (point))
                   (error nil)))

           ;; the column of that open-brace
           (open-column (and open (current-column)))

           ;; whether that open-brace is followed by one of a few
           ;; keywords with special indentation rules
           (keyword (and open
                         (progn
                           (forward-char 1)
                           (and (looking-at latte-keyword-regex)
                                (match-string 1)))))

           ;; the position of the first non-whitespace on the previous
           ;; nonblank line
           (prev-bol (progn
                       (goto-char bol)
                       (and (zerop (forward-line -1))
                            (progn
                              (end-of-line)
                              (skip-chars-backward " \t\n\r\f")
                              (and (not (memq (preceding-char)
                                              '(0 ?\  ?\t ?\n ?\r ?\f)))
                                   (progn
                                     (back-to-indentation)
                                     (point)))))))

           ;; the indentation of the previous nonblank line relative
           ;; to start
           (prev-indent (and prev-bol
                             (current-indentation)))

           ;; the position of the open-brace enclosing the beginning
           ;; of the previous line
           (prev-open (and prev-bol
                           (progn
                             (condition-case nil
                                 (let ((looping t))
                                   (while looping
                                     (backward-up-list 1)
                                     (setq looping
                                           (not (= (following-char) ?{))))
                                   (point))
                               (error nil)))))

           ;; the result
           (column (cond (keyword (+ open-column 2))

                         ;; if current line and previous line are
                         ;; contained in the same level of braces, use
                         ;; previous line's indentation
                         ((and prev-open
                               (equal open prev-open))
                          prev-indent)

                         ;; if current line is enclosed in braces, use
                         ;; the brace's column + 1
                         (open (1+ open-column))

                         ;; Use previous line's indentation
                         (prev-indent prev-indent)

                         ;; No indentation
                         (t 0))))
      (goto-char bol)
      (if (and (= start bol)
               (>= (current-indentation) column))
          ;; repeated presses of TAB should indent the line further
          (indent-relative)
        (indent-line-to column)))))

;;; latte.el ends here