branch: elpa/lua-mode commit ab2d44c0ab35811894e2f40fa49617098a958f1e Author: juergen <juergen> Commit: juergen <juergen>
Multiple changes by Reuben Thomas: Reinstate Version: header for Emacs Lisp Package Archive, and remove lua-version defconst instead (so as only to have the version in one place). Remove install.rst, folding the one comment not already in lua-mode.el, about hs-minor-mode, into lua-mode.el. This reduces the "distribution" to a single file, making it simpler, especially for the ELisp Package Archive (ELPA). Remove folding-mode comments. Remove XEmacs tests, as specific feature tests are already used. Add syntax colouring for variable declarations (closes patch #1842). Add syntax colouring for long strings (closes FR #2068). Remove replace-in-string definition, and call replace-regexp-in-string directly, since we only call it once. Use "FIXME" consistently throughout lua-mode.el --- lua-mode.el | 246 +++++++----------------------------------------------------- 1 file changed, 27 insertions(+), 219 deletions(-) diff --git a/lua-mode.el b/lua-mode.el index d088503..92d5513 100755 --- a/lua-mode.el +++ b/lua-mode.el @@ -12,6 +12,7 @@ ;; Paul Du Bois <pld-...@gelatinous.com> and ;; Aaron Smith <aaron-...@gelatinous.com>. ;; URL: http://lua-mode.luaforge.net/ +;; Version: 20100404 ;; This file is NOT part of Emacs. ;; ;; This program is free software; you can redistribute it and/or @@ -29,9 +30,6 @@ ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, ;; MA 02110-1301, USA. -(defconst lua-version "20100318" - "Lua Mode version number.") - ;; Keywords: languages, processes, tools @@ -40,7 +38,7 @@ ;; Thanks to Tobias Polzin <polzin<at>gmx.de> for function indenting ;; patch: Indent "(" like "{" -;; Thanks to Fabien <fleutot<at>gmail.com> for imenu patches. +;; Thanks to Fabien <fleutot<at>gmail.com> for imenu patches. ;; Special Thanks to Simon Marshall <sim...@mail.esrin.esa.it> for ;; font-lock patches. @@ -48,20 +46,15 @@ ;; Additional font-lock highlighting and indentation tweaks by ;; Adam D. Moss <a...@gimp.org> <aspi...@icculus.org> -;; This file was written with emacs using Jamie Lokier's folding mode -;; That's what the funny ;;{{{ ;;}}} marks are there for - -;;{{{ INSTALLATION: +;; INSTALLATION: ;; To install, just drop this file into a directory on your load-path (and ;; byte-compile it). To set up Emacs to automatically edit files ending in -;; ".lua" using lua-mode add the following to your ~/.emacs file (GNU -;; Emacs) or ~/.xemacs/init.el file (XEmacs): +;; ".lua" using lua-mode add the following to your init file: ;; (setq auto-mode-alist (cons '("\\.lua$" . lua-mode) auto-mode-alist)) ;; (autoload 'lua-mode "lua-mode" "Lua editing mode." t) -;;}}} -;;{{{ Usage +;; Usage ;; Lua-mode supports c-mode style formatting and sending of ;; lines/regions/files to a Lua interpreter. An interpreter (see @@ -71,8 +64,9 @@ ;; interactive shell. See the documentation for `comint.el' for ;; details. -;;}}} -;;{{{ Key-bindings +;; Lua-mode works with Hide Show minor mode (see ``hs-minor-mode``). + +;; Key-bindings ;; To see all the keybindings for Lua mode, look at `lua-setup-keymap' ;; or start `lua-mode' and type `\C-h m'. @@ -88,17 +82,10 @@ ;; (define-key lua-mode-map <your-key> <function>) ;; for all the functions you need. -;;}}} ;;; Code: -(defconst lua-using-xemacs (string-match "XEmacs" emacs-version) - "Nil unless using XEmacs).") - -;; We need that ! (require 'comint) -;;{{{ variables - ;; Local variables (defgroup lua nil "Major mode for editing lua code." @@ -182,7 +169,7 @@ traceback location." (defvar lua-mode-menu (make-sparse-keymap "Lua") "Keymap for lua-mode's menu.") -(defvar lua-xemacs-menu +(defvar lua-emacs-menu '(["Restart With Whole File" lua-restart-with-whole-file t] ["Kill Process" lua-kill-process t] ["Hide Process Buffer" lua-hide-process-buffer t] @@ -197,7 +184,7 @@ traceback location." ["Send Proc" lua-send-proc t] ["Send Buffer" lua-send-buffer t] ["Search Documentation" lua-search-documentation t]) - "XEmacs menu for Lua mode.") + "Emacs menu for Lua mode.") (defvar lua-font-lock-keywords (eval-when-compile @@ -208,7 +195,6 @@ traceback location." '("\\(local[ \t]+\\(\\sw+\\)[ \t]*=\\)" (2 font-lock-variable-name-face)) - ;; ;; Function name declarations. '("^[ \t]*\\<\\(\\(local[ \t]+\\)?function\\)\\>[ \t]+\\(\\(\\sw:\\|\\sw\\.\\|\\sw_\\|\\sw\\)+\\)" (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t)) @@ -217,16 +203,14 @@ traceback location." '("\\(\\(\\sw:\\|\\sw\\.\\|\\sw_\\|\\sw\\)+\\)[ \t]*=[ \t]*\\(function\\)\\>" (1 font-lock-function-name-face nil t) (3 font-lock-keyword-face)) - ; Highlight multi-line comment blocks; since font-lock-mode doesn't - ; claim to handle the highlighting of multi-line expressions elegantly - ; this works best with lazy-lock-mode if your Emacs supports it, e.g. - ; try (setq font-lock-support-mode 'lazy-lock-mode) in your ~/.emacs - - ;; Multi-line comment blocks. - `("\\(?:^\\|[^-]\\)\\(--\\[\\(=*\\)\\[\\(?:.\\|\n\\)*?\\]\\2\\]\\)" + ;; Long comment blocks. + '("\\(?:^\\|[^-]\\)\\(--\\[\\(=*\\)\\[\\(?:.\\|\n\\)*?\\]\\2\\]\\)" (1 font-lock-comment-face t)) - ;; + ;; Long strings. + '("\\(?:^\\|[^[-]\\)\\(\\[\\(=*\\)\\[\\(?:.\\|\n\\)*?\\]\\2\\]\\)" + (1 font-lock-string-face t)) + ;; Keywords. (concat "\\<" (regexp-opt '("and" "break" "do" "else" "elseif" "end" "false" @@ -258,9 +242,6 @@ traceback location." (defconst lua-indent-whitespace " \t" "Character set that constitutes whitespace for indentation in lua.") -;;}}} -;;{{{ lua-make-temp-file - (eval-and-compile (defalias 'lua-make-temp-file (if (fboundp 'make-temp-file) @@ -272,17 +253,6 @@ traceback location." (temp-directory) temporary-file-directory)))))) -;;}}} -;;{{{ replace-in-string - -(eval-and-compile - (if (not (fboundp 'replace-in-string)) ;GNU emacs doesn't have it - (defun replace-in-string (string regexp newtext &optional literal) - (replace-regexp-in-string regexp newtext string nil literal)))) - -;;}}} -;;{{{ lua-mode - ;;;###autoload (defun lua-mode () "Major mode for editing Lua code. @@ -329,17 +299,16 @@ The following keys are bound: ;; _ needs to be part of a word, or the regular expressions will ;; incorrectly regognize end_ to be matched by "\\<end\\>"! (modify-syntax-entry ?_ "w") - (if (and lua-using-xemacs - (featurep 'menubar) + (if (and (featurep 'menubar) current-menubar (not (assoc "Lua" current-menubar))) (progn (set-buffer-menubar (copy-sequence current-menubar)) - (add-menu nil "Lua" lua-xemacs-menu))) - ;; Append Lua menu to popup menu for XEmacs. - (if (and lua-using-xemacs (boundp 'mode-popup-menu)) + (add-menu nil "Lua" lua-emacs-menu))) + ;; Append Lua menu to popup menu for Emacs. + (if (boundp 'mode-popup-menu) (setq mode-popup-menu - (cons (concat mode-name " Mode Commands") lua-xemacs-menu))) + (cons (concat mode-name " Mode Commands") lua-emacs-menu))) ;; hideshow setup (unless (assq 'lua-mode hs-special-modes-alist) @@ -353,9 +322,6 @@ The following keys are bound: ;;;###autoload (add-to-list 'auto-mode-alist '("\\.lua$" . lua-mode)) -;;}}} -;;{{{ lua-setup-keymap - (defun lua-setup-keymap () "Set up keymap for Lua mode. If the variable `lua-prefix-key' is nil, the bindings go directly @@ -381,9 +347,6 @@ to `lua-mode-map', otherwise they are prefixed with `lua-prefix-key'." (define-key lua-mode-map lua-prefix-key map)) )) -;;}}} -;;{{{ lua-electric-match - (defun lua-electric-match (arg) "Insert character and adjust indentation." (interactive "P") @@ -392,9 +355,7 @@ to `lua-mode-map', otherwise they are prefixed with `lua-prefix-key'." (lua-indent-line)) (blink-matching-open)) -;;}}} - -;;{{{ private functions +;; private functions (defun lua-syntax-status () "Returns the syntactic status of the character after the point." (parse-partial-sexp (save-excursion (beginning-of-line) (point)) @@ -414,9 +375,6 @@ to `lua-mode-map', otherwise they are prefixed with `lua-prefix-key'." (let ((parse-result (lua-syntax-status))) (or (elt parse-result 3) (elt parse-result 4)))) -;;}}} -;;{{{ lua-indent-line - (defun lua-indent-line () "Indent current line for Lua mode. Return the amount the indentation changed by." @@ -439,9 +397,6 @@ Return the amount the indentation changed by." shift-amt indent)) -;;}}} -;;{{{ lua-find-regexp - (defun lua-find-regexp (direction regexp &optional limit ignore-p) "Searches for a regular expression in the direction specified. Direction is one of 'forward and 'backward. @@ -459,17 +414,11 @@ ignored, nil otherwise." (if (not (funcall ignore-func)) (throw 'found (point))))))) -;;}}} -;;{{{ lua-backwards-to-block-begin-or-end - (defun lua-backwards-to-block-begin-or-end () "Move backwards to nearest block begin or end. Returns nil if not successful." (interactive) (lua-find-regexp 'backward lua-block-regexp)) -;;}}} -;;{{{ var. constants - (defconst lua-block-regexp (eval-when-compile ;; This is the code we used to generate the regexp: @@ -523,9 +472,6 @@ ignored, nil otherwise." ) -;;}}} -;;{{{ lua-find-matching-token-word - (defun lua-find-matching-token-word (token search-start) (let* ((token-info (assoc token lua-block-token-alist)) (match (car (cdr token-info))) @@ -554,9 +500,6 @@ ignored, nil otherwise." (lua-find-matching-token-word found-token nil))) (throw 'found nil))))))) -;;}}} -;;{{{ lua-goto-matching-block-token - (defun lua-goto-matching-block-token (&optional search-start parse-start) "Find block begion/end token matching the one at the point. This function moves the point to the token that matches the one @@ -570,28 +513,6 @@ the matching token if successful, nil otherwise." (and position (goto-char position)))))) - -;; The following may be useful to speed up the search in the future. -; (let ((token-type (char-syntax (string-to-char token-to-match))) -; matching-pos) -; (cond ((eq token-type ?\() -; (setq matching-pos (scan-sexps (point) 1 (current-buffer) t)) -; (when matching-pos (goto-char matching-pos))) - -; ((eq token-type ?\)) -; ;; need to move one char forward, because scan-sexps -; ;; expects the point to be one past the closing parenthesis -; (forward-char 1) -; (setq matching-pos (scan-sexps (point) -1 (current-buffer) t)) -; (when matching-pos (goto-char matching-pos))) - -; (t -; (lua-goto-matching-token-word token-to-match search-start))))))) - - -;;}}} -;;{{{ lua-goto-matching-block - (defun lua-goto-matching-block (&optional noreport) "Go to the keyword balancing the one under the point. If the point is on a keyword/brace that starts a block, go to the @@ -606,9 +527,6 @@ matching keyword that ends the block, and vice versa." (error "Not on a block control keyword or brace.") position))) -;;}}} -;;{{{ lua-goto-nonblank-previous-line - (defun lua-goto-nonblank-previous-line () "Puts the point at the first previous line that is not blank. Returns the point, or nil if it reached the beginning of the buffer" @@ -620,9 +538,6 @@ Returns the point, or nil if it reached the beginning of the buffer" (beginning-of-line) (if (not (looking-at "\\s *\\(--.*\\)?$")) (throw 'found (point)))))) -;;}}} -;;{{{ lua-goto-nonblank-next-line - (defun lua-goto-nonblank-next-line () "Puts the point at the first next line that is not blank. Returns the point, or nil if it reached the end of the buffer" @@ -638,9 +553,6 @@ Returns the point, or nil if it reached the end of the buffer" (defconst lua-operator-class "-+*/^.=<>~")) -;;}}} -;;{{{ var. constants - (defconst lua-cont-eol-regexp (eval-when-compile ;; expression used to generate the regexp @@ -671,9 +583,6 @@ Returns the point, or nil if it reached the end of the buffer" )) -;;}}} -;;{{{ lua-last-token-continues-p - (defun lua-last-token-continues-p () "Returns true if the last token on this line is a continuation token." (let (line-begin @@ -691,9 +600,6 @@ Returns the point, or nil if it reached the end of the buffer" (goto-char line-end) (re-search-backward lua-cont-eol-regexp line-begin t)))) -;;}}} -;;{{{ lua-first-token-continues-p - (defun lua-first-token-continues-p () "Returns true if the first token on this line is a continuation token." (let (line-end) @@ -703,9 +609,6 @@ Returns the point, or nil if it reached the end of the buffer" (beginning-of-line) (re-search-forward lua-cont-bol-regexp line-end t)))) -;;}}} -;;{{{ lua-is-continuing-statement-p - (defun lua-is-continuing-statement-p (&optional parse-start) "Return non-nil if the line continues a statement. More specifically, return the point in the line that is continued. @@ -725,9 +628,6 @@ The criteria for a continuing statement are: ;; check last token of previous nonblank line (lua-last-token-continues-p))))))) -;;}}} -;;{{{ lua-make-indentation-info-pair - (defun lua-make-indentation-info-pair () "This is a helper function to lua-calculate-indentation-info. Don't use standalone." @@ -772,9 +672,6 @@ use standalone." (- lua-indent-level)))))) -;;}}} -;;{{{ lua-calculate-indentation-info - (defun lua-calculate-indentation-info (&optional parse-start parse-end) "For each block token on the line, computes how it affects the indentation. The effect of each token can be either a shift relative to the current @@ -797,9 +694,6 @@ and relative each, and the shift/column to indent to." (cons (lua-make-indentation-info-pair) indentation-info))))) indentation-info)) -;;}}} -;;{{{ lua-accumulate-indentation-info - (defun lua-accumulate-indentation-info (info) "Accumulates the indentation information previously calculated by lua-calculate-indentation-info. Returns either the relative indentation @@ -815,9 +709,6 @@ shift, or the absolute column to indent to." info-list) (cons type accu))) -;;}}} -;;{{{ lua-calculate-indentation-block-modifier - (defun lua-calculate-indentation-block-modifier (&optional parse-start parse-end) "Return amount by which this line modifies the indentation. @@ -847,8 +738,6 @@ one." (+ (lua-calculate-indentation-left-shift) (cdr indentation-info) (if (lua-is-continuing-statement-p) (- lua-indent-level) 0))))) -;;}}} -;;{{{ constants (defconst lua-left-shift-regexp-1 (concat "\\(" @@ -861,7 +750,6 @@ one." (regexp-opt '("end") t) "\\>\\)")) - (defconst lua-left-shift-regexp ;; This is the code we used to generate the regexp: ;; ("else", "elseif", "until" followed by whitespace, or "end"/closing @@ -885,9 +773,6 @@ one." (+ lua-left-shift-pos-2 (regexp-opt-depth lua-left-shift-regexp-2))) -;;}}} -;;{{{ lua-calculate-indentation-left-shift - (defun lua-calculate-indentation-left-shift (&optional parse-start) "Return amount, by which this line should be shifted left. Look for an uninterrupted sequence of block-closing tokens that starts @@ -916,9 +801,6 @@ to the left by the amount specified in lua-indent-level." (forward-char (length (match-string 0)))))) indentation-modifier))) -;;}}} -;;{{{ lua-calculate-indentation - (defun lua-calculate-indentation (&optional parse-start) "Return appropriate indentation for current line as Lua code. In usual case returns an integer: the column to indent to." @@ -950,9 +832,6 @@ In usual case returns an integer: the column to indent to." (forward-line -1) (+ (current-indentation) shift-amt)))))) -;;}}} -;;{{{ lua-beginning-of-proc - (defun lua-beginning-of-proc (&optional arg) "Move backward to the beginning of a lua proc (or similar). With argument, do it that many times. Negative arg -N @@ -981,9 +860,6 @@ Returns t unless search stops due to beginning or end of buffer." arg 0))) ret)) -;;}}} -;;{{{ lua-end-of-proc - (defun lua-end-of-proc (&optional arg) "Move forward to next end of lua proc (or similar). With argument, do it that many times. Negative argument -N means move @@ -1017,9 +893,6 @@ This function just searches for a `end' at the beginning of a line." (end-of-line)) ret)) -;;}}} -;;{{{ lua-start-process - (defun lua-start-process (name &optional program startfile &rest switches) "Start a lua process named NAME, running PROGRAM." (or switches @@ -1033,34 +906,22 @@ This function just searches for a `end' at the beginning of a line." (accept-process-output (get-buffer-process (current-buffer))) (goto-char (point-max))))) -;;}}} -;;{{{ lua-kill-process - (defun lua-kill-process () "Kill lua subprocess and its buffer." (interactive) (if lua-process-buffer (kill-buffer lua-process-buffer))) -;;}}} -;;{{{ lua-set-lua-region-start - (defun lua-set-lua-region-start (&optional arg) "Set start of region for use with `lua-send-lua-region'." (interactive) (set-marker lua-region-start (or arg (point)))) -;;}}} -;;{{{ lua-set-lua-region-end - (defun lua-set-lua-region-end (&optional arg) "Set end of region for use with `lua-send-lua-region'." (interactive) (set-marker lua-region-end (or arg (point)))) -;;}}} -;;{{{ lua-send-current-line - (defun lua-send-current-line () "Send current line to lua subprocess, found in `lua-process'. If `lua-process' is nil or dead, start a new process first." @@ -1069,9 +930,6 @@ If `lua-process' is nil or dead, start a new process first." (end (save-excursion (end-of-line) (point)))) (lua-send-region start end))) -;;}}} -;;{{{ lua-send-region - (defun lua-send-region (start end) "Send region to lua subprocess." (interactive "r") @@ -1095,7 +953,7 @@ If `lua-process' is nil or dead, start a new process first." (setq last-prompt (point-max)) (comint-simple-send (get-buffer-process (current-buffer)) (format "dofile(\"%s\")" - (replace-in-string tempfile "\\\\" "\\\\\\\\" ))) + (replace-regexp-in-string "\\\\" "\\\\\\\\" tempfile))) ;; wait for prompt (while (not prompt-found) (accept-process-output (get-buffer-process (current-buffer))) @@ -1107,9 +965,6 @@ If `lua-process' is nil or dead, start a new process first." (if lua-always-show (display-buffer lua-process-buffer))))) -;;}}} -;;{{{ lua-postprocess-output-buffer - (defun lua-postprocess-output-buffer (buf start &optional lua-stdin-line-offset) "Highlight tracebacks found in buf. If an traceback occurred return t, otherwise return nil. BUF must exist." @@ -1124,14 +979,11 @@ t, otherwise return nil. BUF must exist." line (string-to-number (match-string 2))))) (when (and lua-jump-on-traceback line) (beep) - ;; TODO: highlight + ;; FIXME: highlight (lua-jump-to-traceback file line lua-stdin-line-offset) (setq err-p t)) err-p)) -;;}}} -;;{{{ lua-jump-to-tracebackw - (defun lua-jump-to-traceback (file line lua-stdin-line-offset) "Jump to the Lua code in FILE at LINE." ;; sanity check: temporary-file-directory @@ -1145,13 +997,10 @@ t, otherwise return nil. BUF must exist." ;; Force Lua mode (if (not (eq major-mode 'lua-mode)) (lua-mode)) - ;; TODO fix offset when executing region + ;; FIXME: fix offset when executing region (goto-line line) (message "Jumping to error in file %s on line %d" file line)))) -;;}}} -;;{{{ lua-prompt-line - (defun lua-prompt-line () (save-excursion (save-match-data @@ -1159,9 +1008,6 @@ t, otherwise return nil. BUF must exist." (if (looking-at comint-prompt-regexp) (match-end 0))))) -;;{{{ lua-send-lua-region -;;}}} - (defun lua-send-lua-region () "Send preset lua region to lua subprocess." (interactive) @@ -1176,9 +1022,6 @@ t, otherwise return nil. BUF must exist." (if lua-always-show (display-buffer lua-process-buffer))) -;;}}} -;;{{{ lua-send-proc - (defun lua-send-proc () "Send proc around point to lua subprocess." (interactive) @@ -1196,18 +1039,12 @@ t, otherwise return nil. BUF must exist." (if lua-always-show (display-buffer lua-process-buffer)))) -;;}}} -;;{{{ lua-send-buffer - -; This needs work... -Bret +; FIXME: This needs work... -Bret (defun lua-send-buffer () "Send whole buffer to lua subprocess." (interactive) (lua-send-region (point-min) (point-max))) -;;}}} -;;{{{ lua-restart-with-whole-file - (defun lua-restart-with-whole-file () "Restart lua subprocess and send whole file as input." (interactive) @@ -1215,33 +1052,21 @@ t, otherwise return nil. BUF must exist." (lua-start-process lua-default-application) (lua-send-buffer)) -;;}}} -;;{{{ lua-show-process-buffer - (defun lua-show-process-buffer () "Make sure `lua-process-buffer' is being displayed." (interactive) (display-buffer lua-process-buffer)) -;;}}} -;;{{{ lua-hide-process-buffer - (defun lua-hide-process-buffer () "Delete all windows that display `lua-process-buffer'." (interactive) (delete-windows-on lua-process-buffer)) -;;}}} -;;{{{ lua-search-documentation - (defun lua-search-documentation () "Search Lua documentation for the word at the point." (interactive) (browse-url (concat lua-search-url-prefix (current-word t)))) -;;}}} -;;{{{ lua-calculate-state - (defun lua-calculate-state (arg prevstate) ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If ;; arg is nil or zero, toggle the state. If arg is negative, turn @@ -1251,9 +1076,6 @@ t, otherwise return nil. BUF must exist." (not prevstate) (> arg 0))) -;;}}} -;;{{{ lua-toggle-electric-state - (defun lua-toggle-electric-state (&optional arg) "Toggle the electric indentation feature. Optional numeric ARG, if supplied, turns on electric indentation when @@ -1262,9 +1084,6 @@ left out." (interactive "P") (setq lua-electric-flag (lua-calculate-state arg lua-electric-flag))) -;;}}} -;;{{{ lua-forward-sexp - (defun lua-forward-sexp (&optional count) "Forward to block end" (interactive "p") @@ -1288,8 +1107,7 @@ left out." (setq count (1- count)))))) -;;}}} -;;{{{ menu bar +;; menu bar (define-key lua-mode-menu [restart-with-whole-file] '("Restart With Whole File" . lua-restart-with-whole-file)) @@ -1324,17 +1142,7 @@ left out." (define-key lua-mode-menu [search-documentation] '("Search Documentation" . lua-search-documentation)) -;;}}} - (provide 'lua-mode) -;;{{{ Emacs local variables - -;; Local Variables: -;; folded-file: t -;; End: - -;;}}} - ;;; lua-mode.el ends here