Merge topic 'emacs-mode-updates'
5593f28f cmake-mode.el: Derive cmake-mode from prog-mode 41d6044b cmake-mode.el: Add font-lock for keywords and variables 7987d985 cmake-mode.el: Rename function to be consistent 63de609e cmake-mode.el: Use `rx' for regexps 9d5082b2 cmake-mode.el: Minor clean-up
This commit is contained in:
commit
dce1b2991c
@ -43,7 +43,14 @@ set the path with these commands:
|
|||||||
(setenv \"PATH\" (concat (getenv \"PATH\") \":/usr/local/cmake/bin\"))"
|
(setenv \"PATH\" (concat (getenv \"PATH\") \":/usr/local/cmake/bin\"))"
|
||||||
:type 'file
|
:type 'file
|
||||||
:group 'cmake)
|
:group 'cmake)
|
||||||
;;
|
|
||||||
|
;; Keywords
|
||||||
|
(defconst cmake-keywords-block-open '("IF" "MACRO" "FOREACH" "ELSE" "ELSEIF" "WHILE" "FUNCTION"))
|
||||||
|
(defconst cmake-keywords-block-close '("ENDIF" "ENDFOREACH" "ENDMACRO" "ELSE" "ELSEIF" "ENDWHILE" "ENDFUNCTION"))
|
||||||
|
(defconst cmake-keywords
|
||||||
|
(let ((kwds (append cmake-keywords-block-open cmake-keywords-block-close nil)))
|
||||||
|
(delete-dups kwds)))
|
||||||
|
|
||||||
;; Regular expressions used by line indentation function.
|
;; Regular expressions used by line indentation function.
|
||||||
;;
|
;;
|
||||||
(defconst cmake-regex-blank "^[ \t]*$")
|
(defconst cmake-regex-blank "^[ \t]*$")
|
||||||
@ -51,40 +58,39 @@ set the path with these commands:
|
|||||||
(defconst cmake-regex-paren-left "(")
|
(defconst cmake-regex-paren-left "(")
|
||||||
(defconst cmake-regex-paren-right ")")
|
(defconst cmake-regex-paren-right ")")
|
||||||
(defconst cmake-regex-argument-quoted
|
(defconst cmake-regex-argument-quoted
|
||||||
"\"\\([^\"\\\\]\\|\\\\\\(.\\|\n\\)\\)*\"")
|
(rx ?\" (* (or (not (any ?\" ?\\)) (and ?\\ anything))) ?\"))
|
||||||
(defconst cmake-regex-argument-unquoted
|
(defconst cmake-regex-argument-unquoted
|
||||||
"\\([^ \t\r\n()#\"\\\\]\\|\\\\.\\)\\([^ \t\r\n()#\\\\]\\|\\\\.\\)*")
|
(rx (or (not (any space "()#\"\\\n")) (and ?\\ nonl))
|
||||||
(defconst cmake-regex-token (concat "\\(" cmake-regex-comment
|
(* (or (not (any space "()#\\\n")) (and ?\\ nonl)))))
|
||||||
"\\|" cmake-regex-paren-left
|
(defconst cmake-regex-token
|
||||||
"\\|" cmake-regex-paren-right
|
(rx-to-string `(group (or (regexp ,cmake-regex-comment)
|
||||||
"\\|" cmake-regex-argument-unquoted
|
?( ?)
|
||||||
"\\|" cmake-regex-argument-quoted
|
(regexp ,cmake-regex-argument-unquoted)
|
||||||
"\\)"))
|
(regexp ,cmake-regex-argument-quoted)))))
|
||||||
(defconst cmake-regex-indented (concat "^\\("
|
(defconst cmake-regex-indented
|
||||||
cmake-regex-token
|
(rx-to-string `(and bol (* (group (or (regexp ,cmake-regex-token) (any space ?\n)))))))
|
||||||
"\\|" "[ \t\r\n]"
|
|
||||||
"\\)*"))
|
|
||||||
(defconst cmake-regex-block-open
|
(defconst cmake-regex-block-open
|
||||||
"^\\(if\\|macro\\|foreach\\|else\\|elseif\\|while\\|function\\)$")
|
(rx-to-string `(and bow (or ,@(append cmake-keywords-block-open
|
||||||
|
(mapcar 'downcase cmake-keywords-block-open))) eow)))
|
||||||
(defconst cmake-regex-block-close
|
(defconst cmake-regex-block-close
|
||||||
"^[ \t]*\\(endif\\|endforeach\\|endmacro\\|else\\|elseif\\|endwhile\\|endfunction\\)[ \t]*(")
|
(rx-to-string `(and bow (or ,@(append cmake-keywords-block-close
|
||||||
|
(mapcar 'downcase cmake-keywords-block-close))) eow)))
|
||||||
|
(defconst cmake-regex-close
|
||||||
|
(rx-to-string `(and bol (* space) (regexp ,cmake-regex-block-close)
|
||||||
|
(* space) (regexp ,cmake-regex-paren-left))))
|
||||||
|
|
||||||
;------------------------------------------------------------------------------
|
;------------------------------------------------------------------------------
|
||||||
|
|
||||||
;;
|
;; Line indentation helper functions
|
||||||
;; Helper functions for line indentation function.
|
|
||||||
;;
|
|
||||||
(defun cmake-line-starts-inside-string ()
|
(defun cmake-line-starts-inside-string ()
|
||||||
"Determine whether the beginning of the current line is in a string."
|
"Determine whether the beginning of the current line is in a string."
|
||||||
(if (save-excursion
|
(save-excursion
|
||||||
(beginning-of-line)
|
(beginning-of-line)
|
||||||
(let ((parse-end (point)))
|
(let ((parse-end (point)))
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(nth 3 (parse-partial-sexp (point) parse-end))
|
(nth 3 (parse-partial-sexp (point) parse-end))
|
||||||
)
|
)
|
||||||
)
|
|
||||||
t
|
|
||||||
nil
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -111,57 +117,40 @@ set the path with these commands:
|
|||||||
;; Line indentation function.
|
;; Line indentation function.
|
||||||
;;
|
;;
|
||||||
(defun cmake-indent ()
|
(defun cmake-indent ()
|
||||||
"Indent current line as CMAKE code."
|
"Indent current line as CMake code."
|
||||||
(interactive)
|
(interactive)
|
||||||
(if (cmake-line-starts-inside-string)
|
(unless (cmake-line-starts-inside-string)
|
||||||
()
|
|
||||||
(if (bobp)
|
(if (bobp)
|
||||||
(cmake-indent-line-to 0)
|
(cmake-indent-line-to 0)
|
||||||
(let (cur-indent)
|
(let (cur-indent)
|
||||||
|
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(beginning-of-line)
|
(beginning-of-line)
|
||||||
|
|
||||||
(let ((point-start (point))
|
(let ((point-start (point))
|
||||||
(case-fold-search t) ;; case-insensitive
|
(case-fold-search t) ;; case-insensitive
|
||||||
token)
|
token)
|
||||||
|
|
||||||
; Search back for the last indented line.
|
; Search back for the last indented line.
|
||||||
(cmake-find-last-indented-line)
|
(cmake-find-last-indented-line)
|
||||||
|
|
||||||
; Start with the indentation on this line.
|
; Start with the indentation on this line.
|
||||||
(setq cur-indent (current-indentation))
|
(setq cur-indent (current-indentation))
|
||||||
|
|
||||||
; Search forward counting tokens that adjust indentation.
|
; Search forward counting tokens that adjust indentation.
|
||||||
(while (re-search-forward cmake-regex-token point-start t)
|
(while (re-search-forward cmake-regex-token point-start t)
|
||||||
(setq token (match-string 0))
|
(setq token (match-string 0))
|
||||||
(if (string-match (concat "^" cmake-regex-paren-left "$") token)
|
(when (or (string-match (concat "^" cmake-regex-paren-left "$") token)
|
||||||
(setq cur-indent (+ cur-indent cmake-tab-width))
|
(and (string-match cmake-regex-block-open token)
|
||||||
)
|
(looking-at (concat "[ \t]*" cmake-regex-paren-left))))
|
||||||
(if (string-match (concat "^" cmake-regex-paren-right "$") token)
|
(setq cur-indent (+ cur-indent cmake-tab-width)))
|
||||||
(setq cur-indent (- cur-indent cmake-tab-width))
|
(when (string-match (concat "^" cmake-regex-paren-right "$") token)
|
||||||
)
|
(setq cur-indent (- cur-indent cmake-tab-width)))
|
||||||
(if (and
|
|
||||||
(string-match cmake-regex-block-open token)
|
|
||||||
(looking-at (concat "[ \t]*" cmake-regex-paren-left))
|
|
||||||
)
|
|
||||||
(setq cur-indent (+ cur-indent cmake-tab-width))
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
(goto-char point-start)
|
(goto-char point-start)
|
||||||
|
;; If next token closes the block, decrease indentation
|
||||||
; If this is the end of a block, decrease indentation.
|
(when (looking-at cmake-regex-close)
|
||||||
(if (looking-at cmake-regex-block-close)
|
(setq cur-indent (- cur-indent cmake-tab-width))
|
||||||
(setq cur-indent (- cur-indent cmake-tab-width))
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
; Indent this line by the amount selected.
|
; Indent this line by the amount selected.
|
||||||
(if (< cur-indent 0)
|
(cmake-indent-line-to (max cur-indent 0))
|
||||||
(cmake-indent-line-to 0)
|
|
||||||
(cmake-indent-line-to cur-indent)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -183,17 +172,19 @@ the indentation. Otherwise it retains the same position on the line"
|
|||||||
;;
|
;;
|
||||||
;; Helper functions for buffer
|
;; Helper functions for buffer
|
||||||
;;
|
;;
|
||||||
(defun unscreamify-cmake-buffer ()
|
(defun cmake-unscreamify-buffer ()
|
||||||
"Convert all CMake commands to lowercase in buffer."
|
"Convert all CMake commands to lowercase in buffer."
|
||||||
(interactive)
|
(interactive)
|
||||||
(goto-char (point-min))
|
(save-excursion
|
||||||
(while (re-search-forward "^\\([ \t]*\\)\\(\\w+\\)\\([ \t]*(\\)" nil t)
|
(goto-char (point-min))
|
||||||
(replace-match
|
(while (re-search-forward "^\\([ \t]*\\)\\(\\w+\\)\\([ \t]*(\\)" nil t)
|
||||||
(concat
|
(replace-match
|
||||||
(match-string 1)
|
(concat
|
||||||
(downcase (match-string 2))
|
(match-string 1)
|
||||||
(match-string 3))
|
(downcase (match-string 2))
|
||||||
t))
|
(match-string 3))
|
||||||
|
t))
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
;------------------------------------------------------------------------------
|
;------------------------------------------------------------------------------
|
||||||
@ -202,18 +193,32 @@ the indentation. Otherwise it retains the same position on the line"
|
|||||||
;; Keyword highlighting regex-to-face map.
|
;; Keyword highlighting regex-to-face map.
|
||||||
;;
|
;;
|
||||||
(defconst cmake-font-lock-keywords
|
(defconst cmake-font-lock-keywords
|
||||||
(list '("^[ \t]*\\([[:word:]_]+\\)[ \t]*(" 1 font-lock-function-name-face))
|
`((,(rx-to-string `(and symbol-start
|
||||||
"Highlighting expressions for CMAKE mode."
|
(or ,@cmake-keywords
|
||||||
)
|
,@(mapcar #'downcase cmake-keywords))
|
||||||
|
symbol-end))
|
||||||
|
. font-lock-keyword-face)
|
||||||
|
(,(rx symbol-start (group (+ (or word (syntax symbol)))) ?\()
|
||||||
|
1 font-lock-function-name-face)
|
||||||
|
("\\${?\\([[:alpha:]_][[:alnum:]_]*\\|[0-9]+\\|[$*_]\\)"
|
||||||
|
1 font-lock-variable-name-face t)
|
||||||
|
)
|
||||||
|
"Highlighting expressions for CMake mode.")
|
||||||
|
|
||||||
;------------------------------------------------------------------------------
|
;------------------------------------------------------------------------------
|
||||||
|
|
||||||
;;
|
;; Syntax table for this mode.
|
||||||
;; Syntax table for this mode. Initialize to nil so that it is
|
(defvar cmake-mode-syntax-table nil
|
||||||
;; regenerated when the cmake-mode function is called.
|
"Syntax table for CMake mode.")
|
||||||
;;
|
(or cmake-mode-syntax-table
|
||||||
(defvar cmake-mode-syntax-table nil "Syntax table for cmake-mode.")
|
(setq cmake-mode-syntax-table
|
||||||
(setq cmake-mode-syntax-table nil)
|
(let ((table (make-syntax-table)))
|
||||||
|
(modify-syntax-entry ?\( "()" table)
|
||||||
|
(modify-syntax-entry ?\) ")(" table)
|
||||||
|
(modify-syntax-entry ?# "<" table)
|
||||||
|
(modify-syntax-entry ?\n ">" table)
|
||||||
|
(modify-syntax-entry ?$ "'" table)
|
||||||
|
table)))
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; User hook entry point.
|
;; User hook entry point.
|
||||||
@ -227,41 +232,23 @@ the indentation. Otherwise it retains the same position on the line"
|
|||||||
|
|
||||||
;------------------------------------------------------------------------------
|
;------------------------------------------------------------------------------
|
||||||
|
|
||||||
;;
|
;; For compatibility with Emacs < 24
|
||||||
;; CMake mode startup function.
|
(defalias 'cmake--parent-mode
|
||||||
|
(if (fboundp 'prog-mode) 'prog-mode 'fundamental-mode))
|
||||||
|
|
||||||
|
;;------------------------------------------------------------------------------
|
||||||
|
;; Mode definition.
|
||||||
;;
|
;;
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun cmake-mode ()
|
(define-derived-mode cmake-mode cmake--parent-mode "CMake"
|
||||||
"Major mode for editing CMake listfiles."
|
"Major mode for editing CMake source files."
|
||||||
(interactive)
|
|
||||||
(kill-all-local-variables)
|
|
||||||
(setq major-mode 'cmake-mode)
|
|
||||||
(setq mode-name "CMAKE")
|
|
||||||
|
|
||||||
; Create the syntax table
|
|
||||||
(setq cmake-mode-syntax-table (make-syntax-table))
|
|
||||||
(set-syntax-table cmake-mode-syntax-table)
|
|
||||||
(modify-syntax-entry ?\( "()" cmake-mode-syntax-table)
|
|
||||||
(modify-syntax-entry ?\) ")(" cmake-mode-syntax-table)
|
|
||||||
(modify-syntax-entry ?# "<" cmake-mode-syntax-table)
|
|
||||||
(modify-syntax-entry ?\n ">" cmake-mode-syntax-table)
|
|
||||||
|
|
||||||
; Setup font-lock mode.
|
; Setup font-lock mode.
|
||||||
(make-local-variable 'font-lock-defaults)
|
(set (make-local-variable 'font-lock-defaults) '(cmake-font-lock-keywords))
|
||||||
(setq font-lock-defaults '(cmake-font-lock-keywords))
|
|
||||||
|
|
||||||
; Setup indentation function.
|
; Setup indentation function.
|
||||||
(make-local-variable 'indent-line-function)
|
(set (make-local-variable 'indent-line-function) 'cmake-indent)
|
||||||
(setq indent-line-function 'cmake-indent)
|
|
||||||
|
|
||||||
; Setup comment syntax.
|
; Setup comment syntax.
|
||||||
(make-local-variable 'comment-start)
|
(set (make-local-variable 'comment-start) "#"))
|
||||||
(setq comment-start "#")
|
|
||||||
|
|
||||||
; Run user hooks.
|
|
||||||
(if (boundp 'prog-mode-hook)
|
|
||||||
(run-hooks 'prog-mode-hook 'cmake-mode-hook)
|
|
||||||
(run-hooks 'cmake-mode-hook)))
|
|
||||||
|
|
||||||
; Help mode starts here
|
; Help mode starts here
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user