lse-mode.el 17.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
;;;; -*- mode:emacs-lisp;coding:utf-8 -*-
;;;;****************************************************************************
;;;;FILE:               lse-mode.el
;;;;LANGUAGE:           emacs lisp
;;;;SYSTEM:             POSIX
;;;;USER-INTERFACE:     NONE
;;;;DESCRIPTION
;;;;    
;;;;    L.S.E. editing support package
;;;;    
;;;;    A major mode for editing L.S.E. code.  It provides convenient
;;;;    abbrevs for L.S.E. keywords, knows about the standard layout
;;;;    rules, and supports a native compile command.
;;;;
;;;;    http://www.emacswiki.org/cgi-bin/wiki.pl?SampleMode
;;;;
;;;;AUTHORS
;;;;    <PJB> Pascal Bourguignon <pjb@informatimago.com>
;;;;          Michael Schmidt <michael@pbinfo.UUCP>
;;;;          Tom Perrine <Perrin@LOGICON.ARPA>
;;;;          Mick Jordan
;;;;          Peter Robinson
;;;;MODIFICATIONS
24
;;;;    2014-06-25 <PJB> Improved lse-renumber-region (update ALLER EN etc).
25
;;;;    2005-08-21 <PJB> Added header & license.
26
;;;;    2003-01-31 <PJB> Created, from the modula-2 mode.
27 28 29 30
;;;;BUGS
;;;;LEGAL
;;;;    GPL
;;;;    
31
;;;;    Copyright Pascal Bourguignon 2005 - 2014
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
;;;;    
;;;;    This program 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 of the License, or (at your option) any later version.
;;;;    
;;;;    This program 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.
;;;;    
;;;;    You should have received a copy of the GNU General Public
;;;;    License along with this program; if not, write to the Free
;;;;    Software Foundation, Inc., 59 Temple Place, Suite 330,
;;;;    Boston, MA 02111-1307 USA
;;;;****************************************************************************
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

(require 'cl)


(setq auto-mode-alist (append '(("\\.\\(lse\\|LSE\\)\\'" . lse-mode))
                              auto-mode-alist))


(defgroup lse nil
  "Major mode for editing L.S.E. code."
  :prefix "lse-"
  :group 'languages)


(defvar lse-mode-syntax-table nil
  "Syntax table in use in L.S.E. buffers.")

65
(unless lse-mode-syntax-table
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  (let ((table (make-syntax-table)))
    (modify-syntax-entry ?\\ "\\" table)
    (modify-syntax-entry ?\( "()" table)
    (modify-syntax-entry ?\) ")(" table)
    (modify-syntax-entry ?* "." table)
    (modify-syntax-entry ?& "_" table)
    (modify-syntax-entry ?+ "." table)
    (modify-syntax-entry ?- "." table)
    (modify-syntax-entry ?= "." table)
    (modify-syntax-entry ?< "." table)
    (modify-syntax-entry ?> "." table)
    (modify-syntax-entry ?\' "\"" table)
    (setq lse-mode-syntax-table table)) )


(defvar lse-mode-map nil
  "Keymap used in L.S.E. mode.")
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
;; (setf  lse-mode-map nil)

(unless lse-mode-map
  (let ((map (make-sparse-keymap)))
    (do ((lcc ?a (1+ lcc))
         (ucc ?A (1+ ucc)))
        ((< ?z lcc))
      (define-key map (make-string 1 lcc) (make-string 1 ucc)))
    ;; (define-key map "_"  'lse-assign)
    (define-key map "\n"  'lse-newline)
    (define-key map "\r"  'lse-newline)
    (define-key map "\C-c\C-g" 'lse-goto-line)
    (define-key map "\C-c\C-r" 'lse-test-result)
    ;; (define-key map "\C-c\C-z" 'suspend-emacs)
    (setq lse-mode-map map)))
98 99 100 101 102 103 104 105

;;(define-key  lse-mode-map "_"  'self-insert-command)


(setq lse-default-font-lock
      ;;"Font-lock for L.S.E. mode.")
      (append 
       '(
106 107 108 109 110
         ;; pretty chars:
         ("\\(_\\)" (1 (progn (compose-region (match-beginning 1) (match-end 1)
                                        ? 'decompose-region) nil)))
         ("\\(\\^\\)" (1 (progn (compose-region (match-beginning 1) (match-end 1)
                                        ? 'decompose-region) nil)))
111 112 113 114 115
         ;; comments:
         ("\\(^[0-9]+\\|; *\\)\\(\\*.*\\)"
          (2 font-lock-comment-face))
         ;; strings
         ("\\('[^'\n]*'\\)"
116 117 118 119 120 121 122 123 124 125 126 127
          (1 font-lock-string-face)))
       
       (let ((lse-keywords
              '("AFFICHER" "ALLER" "ALORS" "CHAINE" "CHARGER" "DEBUT"
                "EN" "ET" "EXECUTER" "FAIRE" "FIN" "GARER"
                "JUSQUA" "LIBERER" "LIRE" "LOCAL" "NON" "OU" "PAS"
                "PAUSE" "POUR" "PROCEDURE" "QUE" "RESULTAT" "RETOUR"  
                "SI" "SINON" "SUPPRIMER" "TABLEAU" "TANT" "TERMINER" ))
             (lse-builtins
              '("ENT" "RAC" "ABS" "EXP" "LGN" "SIN" "COS" "ATG" "ALE"
                "TEM" "ATT" "DIS" "ETL" "OUL" "OUX" "CCA" "CNB" "CNB"
                "DAT" "EQC" "EQN" "EQN" "GRL" "GRL" "LGR" "POS" "PTR"
128 129 130
                "PTR" "SCH" "SCH" "SKP" "SKP"
                ;; unix extensions:
                "ENV" "ARG"))
131
             (lse-types
132
              '("CHAINE" "TABLEAU" )))
133 134 135 136 137 138 139 140 141 142 143 144 145
         (list
          ;;
          ;; Keywords except those fontified elsewhere.
          (concat "\\<\\(" (regexp-opt lse-keywords) "\\)\\>")
          ;;
          ;; Builtins.
          (cons (concat "\\<\\(" (regexp-opt lse-builtins) "\\)\\>")
                'font-lock-builtin-face)
          ;;
          ;; Type names.
          (cons (concat "\\<\\(" (regexp-opt lse-types) "\\)\\>")
                'font-lock-type-face)
          ))   
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
       '(
         ;; fonction
         ("\\(&[A-Z][A-Z0-9]\\{0,4\\}\\)\\>"
          (1 font-lock-function-name-face))
         ;; variables
         ("\\<\\([A-Z][A-Z0-9]\\{0,4\\}\\)\\>"
          (1 font-lock-variable-name-face))
         ;; constantes
         ("\\([0-9]+\\.[0-9]+E[-+]?[0-9]+\\)"    (1 font-lock-constant-face))
         ("\\([0-9]+\\.E[-+]?[0-9]+\\)"          (1 font-lock-constant-face))
         ("\\([0-9]+E[-+]?[0-9]+\\)"             (1 font-lock-constant-face))
         ("\\([0-9]+\\.[0-9]*\\)"                (1 font-lock-constant-face))
         ("\\([0-9]+\\)"                         (1 font-lock-constant-face))
         ;; line numbers:
         ("^\\([0-9]+\\)"
          (1 font-lock-type-face)))))

163

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
(setq lse-font-lock lse-default-font-lock)

;;  "Default expressions to highlight in L.S.E. modes.")



;;;###autoload
(defun lse-mode ()
  "This is a mode intended to support program development in L.S.E..
\\<lse-mode-map>
"
  (interactive)
  (let ((modp (buffer-modified-p)))
    (kill-all-local-variables)
    (use-local-map lse-mode-map)
    (setq major-mode 'lse-mode)
    (setq mode-name "L.S.E.")
    (set-syntax-table lse-mode-syntax-table)
    ;;(make-local-variable 'paragraph-start)
    ;;(setq paragraph-start (concat "$\\|" page-delimiter))
    ;;(make-local-variable 'paragraph-separate)
    ;;(setq paragraph-separate paragraph-start)
    ;;(make-local-variable 'paragraph-ignore-fill-prefix)
    ;;(setq paragraph-ignore-fill-prefix t)
    ;; (make-local-variable 'indent-line-function)
    ;; (setq indent-line-function 'c-indent-line)
    (make-local-variable 'require-final-newline)
    (setq require-final-newline t)
    (make-local-variable 'comment-start)
    (setq comment-start "*")
    (make-local-variable 'comment-end)
    (setq comment-end "")
    ;;(make-local-variable 'comment-column)
    ;;(setq comment-column 41)
    (make-local-variable 'comment-start-skip)
    (setq comment-start-skip "\\*")
    ;;(make-local-variable 'comment-indent-function)
    ;;(setq comment-indent-function 'lse-comment-indent)
    ;;(make-local-variable 'parse-sexp-ignore-comments)
    ;;(setq parse-sexp-ignore-comments t)
    (make-local-variable 'font-lock-defaults)
    (setq font-lock-defaults '(lse-font-lock  t t nil nil ))
    (setf (buffer-modified-p (current-buffer)) modp))
  (run-hooks 'lse-mode-hook))


210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
(defun lse-string-trim (character-bag string-designator)
  "Common-Lisp: returns a substring of string, with all characters in \
character-bag stripped off the beginning and end.
"
  (unless (sequencep character-bag)
    (signal 'type-error  "Expected a sequence for `character-bag'."))
  (let* ((string (string* string-designator))
         (margin (format "[%s]*" (regexp-quote
                                  (if (stringp character-bag)
                                      character-bag
                                      (map 'string 'identity character-bag)))))
         (trimer (format "\\`%s\\(\\(.\\|\n\\)*?\\)%s\\'" margin margin)))
    (replace-regexp-in-string  trimer "\\1" string)))


225 226 227
(defun lse-newline ()
  "Insert newline and line number incremented with the same step as previously."
  (interactive)
Pascal J. Bourguignon's avatar
Pascal J. Bourguignon committed
228 229 230 231 232 233
  (cond
    ((looking-at " *\\([0-9]+\\)\\([ *]\\|$\\)")
     ;; Before a line number
     (let* ((linum  (1- (string-to-number (match-string 1))))
            (pt     (point))
            (before (buffer-substring (progn (beginning-of-line) (point)) pt)))
234
       (if (string= "" (lse-string-trim " " before))
Pascal J. Bourguignon's avatar
Pascal J. Bourguignon committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
           ;; nothing before
           (progn
             (delete-region (progn (beginning-of-line) (point)) pt)
             (insert (format "%d \n" linum)))
           ;; a line number in the middle of the line
           (progn
             (goto-char pt)
             (insert "\n")))))
    ((looking-at "\\( *\n *\\)\\([0-9]+\\)\\([ *]\\|$\\)")
     ;; At the end of a line, followed by a line with a number.
     (let* ((nl.start (match-beginning 1))
            (nl.end   (match-end       1))
            (folnum   (string-to-number (match-string 2)))
            (curnum   (progn (beginning-of-line)
                             (if (looking-at " *\\([0-9]+\\)\\([ *]\\|$\\)")
                                 (string-to-number (match-string 1))
                                 (- folnum 2))))
            (prenum   (if (< (point-min) (point))
                          (progn
254 255
                            (forward-line -1)
                            (beginning-of-line)
Pascal J. Bourguignon's avatar
Pascal J. Bourguignon committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
                            (if (looking-at " *\\([0-9]+\\)\\([ *]\\|$\\)")
                                (string-to-number (match-string 1))
                                0))
                          0))
            (increm   (max 1 (min (- folnum curnum) (- curnum folnum))))
            (newnum   (1+ curnum)))
       (goto-char nl.start)
       (delete-region nl.start nl.end)
       (insert (format "\n%d \n" newnum))
       (forward-char -1)))
    (t
     ;; Elsewhere
     (insert "\n")
     (let ((nlpt (point))
           (line (progn
                   (forward-line -1)
                   (beginning-of-line)
                   (if (looking-at " *[0-9]+")
                       (let ((curr (string-to-number (match-string 0)))
                             (curpos (point)))
                         (forward-line -1)
                         (beginning-of-line)
                         (cond
                           ((= curpos (point))
                            (1+ curr))
                           ((looking-at " *[0-9]+")
                            (let ((prev (string-to-number (match-string 0))))
                              (+ curr (abs (- curr prev)))))
                           (t
                            (+ 10 curr))))
                       (progn
                         10)))))
       (goto-char nlpt)
       (beginning-of-line)
       (insert (format "%d " line))
       (when (looking-at " +")
         (delete-region (match-beginning 0) (match-end 0)))))))
293 294 295



296 297 298 299 300 301 302 303 304 305 306
(defvar *lse-jumps-re* "\\<\\(\\(ALLER\\|RETOUR\\) +EN\\|FAIRE\\) +\\([0-9]+\\)[\n ;]")

(defun lse-jumps ()
  "Return an a-list mapping line number jumped to to point at which jump instruction is found (sorted by point) in the current buffer."
  (save-excursion
   (goto-char (point-min))
   (let ((linos '()))
    (while (re-search-forward *lse-jumps-re* nil t)
      (push (cons (parse-integer (match-string 3)) (match-beginning 3)) linos))
     linos)))

307

308 309 310 311
(defun lse-marker (point)
  (let ((marker (make-marker)))
    (set-marker marker point)
    marker))
312

313 314 315 316 317 318
(defun lse-free-markers (sexp)
  (cond
    ((markerp sexp) (set-marker sexp nil))
    ((atom sexp))
    (t (lse-free-markers (car sexp))
       (lse-free-markers (cdr sexp)))))
319 320


321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
(defun* lse-renumber-region (start end &optional (from 1) (step 1))
  "Renumber lines in the region.
If there are instructions ALLER EN, RETOUR EN, ou FAIRE in the buffer,
the following line number is updated according to the new number of
the changed lines."
  (interactive "r\nnFrom: \nnStep: ")
  (let ((jumps (mapcar (lambda (e)
                         (cons (car e)
                               (lse-marker (cdr e))))
                       (lse-jumps)))
        (linum from)
        (end (lse-marker end)))
    (unwind-protect
         (let ((renumbers '()))
           (goto-char start)
           (while (< (point) end)
                  (beginning-of-line)
                  (when (looking-at " *\\([0-9]+\\) *")
                    (push (cons (parse-integer (buffer-substring-no-properties
                                                (match-beginning 1) (match-end 1)))
                                linum)
                          renumbers)
                    (delete-region (match-beginning 0) (match-end 0)))
                  (if (looking-at "*")
                      (insert (format "%d" linum))
                      (insert (format "%d " linum)))
                  (incf linum step)
                  (forward-line 1))
           (while renumbers
                  (destructuring-bind (old . new) (pop renumbers)
                    (dolist (j jumps)
                      (when (= (car j) old)
                        (goto-char (cdr j))
                        (forward-sexp)
                        (delete-region (cdr j) (point))
                        (insert (format "%d" new)))))))
      (lse-free-markers end)
      (lse-free-markers jumps))))

360 361

;; (defparameter lse-assign-image  
362 363 364
;;   (find-image 
;;    (list
;;     (list :type 'xbm 
365
;;           :width 8
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
;;           :height 12
;;           :ascent 91
;;           :background nil
;;           :foreground nil
;;           :data
;;           (loop
;;              with lines = (mapcar 
;;                            (lambda (line)
;;                              (loop
;;                                 with vec = (make-bool-vector (length line) nil)
;;                                 for bit in (mapcar
;;                                             (lambda (char) (STRING/= char ".")) 
;;                                             (split-string line ""))
;;                                 for i from 0 below (length vec)
;;                                 do (setf (aref vec i) bit)
;;                                 finally (return vec)))
382
;;                            (split-string "........\n........\n........\n..**....\n.**.....\n*******.\n.**.....\n..**....\n........\n........\n........\n........\n"))
383 384 385 386
;;              with pic = (make-vector (length lines) nil)
;;              for line in lines
;;              for i from 0
;;              do (setf (aref pic i) line)
387
;;              finally (return pic)) )))
388
;; "A left arrow glyph.")
389 390


391 392 393 394 395 396 397 398 399 400 401
;; (defun lse-underline-to-assign ()
;;   (interactive)
;;   (save-excursion
;;     (save-restriction
;;       (widen)
;;       (goto-char (point-min))
;;       (while (search-forward "_" nil t)
;;         (delete-region (match-beginning 0) (match-end 0))
;;         (insert-image lse-assign-image "_")))))


402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
;; (defun lse-put-image (image pos &optional string area)
;;   "Put image IMAGE in front of POS in the current buffer.
;; IMAGE must be an image created with `create-image' or `defimage'.
;; IMAGE is displayed by putting an overlay into the current buffer with a
;; `before-string' STRING that has a `display' property whose value is the
;; image.  STRING is defaulted if you omit it.
;; POS may be an integer or marker.
;; AREA is where to display the image.  AREA nil or omitted means
;; display it in the text area, a value of `left-margin' means
;; display it in the left marginal area, a value of `right-margin'
;; means display it in the right marginal area."
;;   (unless string (setq string "x"))
;;   (let ((buffer (current-buffer)))
;;     (unless (eq (car-safe image) 'image)
;;       (error "Not an image: %s" image))
;;     (unless (or (null area) (memq area '(left-margin right-margin)))
;;       (error "Invalid area %s" area))
;;     (let (overlay
;;           (curpos (point))
;;           (prop (if (null area) image (list (list 'margin area) image))))
;;       (setq string (copy-seq string))
;;       (put-text-property 0 (length string) 'display prop string)
;;       (insert string)
;;       (setq overlay (make-overlay curpos (point) buffer))
;;       (overlay-put overlay 'put-image t)
;;       (overlay-put overlay 'evaporate t)
;;       (overlay-put overlay 'intangible nil)
;;       )))


;; (defun lse-assign ()
;;   "Insert an assign operator (the ASCII code of the underline) 
;; but adding an overlay on it with the `lse-assign-image' (a left arrow)."
;;   (interactive)
;;   (lse-put-image lse-assign-image (point) "_"))
437

438 439 440 441 442 443 444 445
(defun lse-line-numbers ()
  "Return an a-list mapping line numbers to point (sorted by point) in the current buffer."
  (save-excursion
   (goto-char (point-min))
   (let ((linos '()))
    (while (re-search-forward "^ *\\([0-9]+\\)[* ]" nil t)
      (push (cons (parse-integer (match-string 1)) (match-beginning 0)) linos))
     (nreverse linos))))
446

447 448 449 450 451 452 453 454
(defun lse-goto-line (lino)
  (interactive "nNuméro de ligne: ")
  (let* ((lines (sort (lse-line-numbers)
                     (lambda (a b) (< (car a) (car b)))))
        (line (find lino lines :key (function car) :test-not (function >))))
    (if line
        (goto-char (cdr line))
        (goto-char (point-max)))))
455

456

457

Pascal J. Bourguignon's avatar
Pascal J. Bourguignon committed
458
(defvar *lse-test-commit*)
Pascal J. Bourguignon's avatar
Pascal J. Bourguignon committed
459
(setq *lse-test-commit* "f6ec51ef")
460 461 462 463 464 465 466 467 468 469 470 471
(defun lse-test-result (success)
  (interactive "p")
  (lse-goto-line 255)
  (when (looking-at " *255[* ]")
    (kill-line))
  (insert (format "255*[TEST NASIUM-LSE %s %s]\n"
                  (if (plusp success) "REUSSI" "RATE")
                  *lse-test-commit*)))



(provide 'lse-mode)
472
;;;; THE END ;;;;
473

474