update 'macs
This commit is contained in:
309
.emacs.d/lisp/dockerfile-mode.el
Normal file
309
.emacs.d/lisp/dockerfile-mode.el
Normal file
@@ -0,0 +1,309 @@
|
||||
;;; dockerfile-mode.el --- Major mode for editing Docker's Dockerfiles -*- lexical-binding: t -*-
|
||||
|
||||
;; Copyright (c) 2013 Spotify AB
|
||||
;; Package-Requires: ((emacs "24"))
|
||||
;; Homepage: https://github.com/spotify/dockerfile-mode
|
||||
;; URL: https://github.com/spotify/dockerfile-mode
|
||||
;; Version: 1.9
|
||||
;; Keywords: docker languages processes tools
|
||||
;;
|
||||
;; Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
;; use this file except in compliance with the License. You may obtain a copy of
|
||||
;; the License at
|
||||
;;
|
||||
;; http://www.apache.org/licenses/LICENSE-2.0
|
||||
;;
|
||||
;; Unless required by applicable law or agreed to in writing, software
|
||||
;; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
;; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
;; License for the specific language governing permissions and limitations under
|
||||
;; the License.
|
||||
|
||||
;;; Commentary:
|
||||
|
||||
;; Provides a major mode `dockerfile-mode' for use with the standard
|
||||
;; `Dockerfile' file format. Additional convenience functions allow
|
||||
;; images to be built easily.
|
||||
|
||||
;;; Code:
|
||||
|
||||
(require 'sh-script)
|
||||
(require 'rx)
|
||||
|
||||
|
||||
(declare-function cygwin-convert-file-name-to-windows "cygw32.c" (file &optional absolute-p))
|
||||
|
||||
(defgroup dockerfile nil
|
||||
"Dockerfile editing commands for Emacs."
|
||||
:link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
|
||||
:prefix "dockerfile-"
|
||||
:group 'languages)
|
||||
|
||||
(defcustom dockerfile-mode-command "docker"
|
||||
"Which binary to use to build images."
|
||||
:group 'dockerfile
|
||||
:type 'string)
|
||||
|
||||
(defcustom dockerfile-use-sudo nil
|
||||
"Runs docker builder command with sudo."
|
||||
:type 'boolean
|
||||
:group 'dockerfile)
|
||||
|
||||
(defcustom dockerfile-build-force-rm nil
|
||||
"Runs docker builder command with --force-rm switch."
|
||||
:type 'boolean
|
||||
:group 'dockerfile)
|
||||
|
||||
(defcustom dockerfile-build-pull nil
|
||||
"Runs docker builder command with --pull switch."
|
||||
:type 'boolean
|
||||
:group 'dockerfile)
|
||||
|
||||
(defcustom dockerfile-build-args nil
|
||||
"List of --build-arg to pass to docker build.
|
||||
|
||||
Each element of the list will be passed as a separate
|
||||
--build-arg to the docker build command."
|
||||
:type '(repeat string)
|
||||
:group 'dockerfile)
|
||||
|
||||
(defcustom dockerfile-build-progress "auto"
|
||||
"Type of --progress output (auto, plain, tty) of docker build."
|
||||
:group 'dockerfile
|
||||
:type 'string)
|
||||
|
||||
(defcustom dockerfile-build-extra-options nil
|
||||
"Extra command-line options to send to docker build.
|
||||
|
||||
Use this variable to add custom command-line switches not covered by
|
||||
existing dockerfile-build-* variables.
|
||||
|
||||
Example:
|
||||
(setq-default dockerfile-build-extra-options \"--network host\")"
|
||||
:group 'dockerfile
|
||||
:type 'string)
|
||||
|
||||
(defcustom dockerfile-use-buildkit nil
|
||||
"Use Docker buildkit for building images?
|
||||
|
||||
This is the new buildsystem for docker, and in time it will replace the old one
|
||||
but for now it has to be explicitly enabled to work.
|
||||
It is supported from docker 18.09"
|
||||
:type 'boolean)
|
||||
|
||||
(defcustom dockerfile-enable-auto-indent t
|
||||
"Toggles the auto indentation functionality."
|
||||
:type 'boolean)
|
||||
|
||||
(defcustom dockerfile-indent-offset (or standard-indent 2)
|
||||
"Dockerfile number of columns for margin-changing functions to indent."
|
||||
:type 'integer
|
||||
:safe #'integerp
|
||||
:group 'dockerfile)
|
||||
|
||||
(defface dockerfile-image-name
|
||||
'((t (:inherit (font-lock-type-face bold))))
|
||||
"Face to highlight the base image name after FROM instruction.")
|
||||
|
||||
(defface dockerfile-image-alias
|
||||
'((t (:inherit (font-lock-constant-face bold))))
|
||||
"Face to highlight the base image alias inf FROM ... AS <alias> construct.")
|
||||
|
||||
(defconst dockerfile--from-regex
|
||||
(rx line-start (* blank) "from" (+ blank)
|
||||
(? "--platform=" (+ (not (any blank "\n"))) (+ blank))
|
||||
(group (+ (not (any blank "\n"))))
|
||||
(* blank)
|
||||
(? "as" (+ blank) (group (+ (not (any blank "\n")))))
|
||||
(* blank)
|
||||
(? "#" (* nonl))
|
||||
line-end))
|
||||
|
||||
(defvar dockerfile-font-lock-keywords
|
||||
`(,(cons (rx (or line-start "onbuild ")
|
||||
(group (or "from" "maintainer" "run" "cmd" "expose" "env" "arg"
|
||||
"add" "copy" "entrypoint" "volume" "user" "workdir" "onbuild"
|
||||
"label" "stopsignal" "shell" "healthcheck"))
|
||||
word-boundary)
|
||||
'font-lock-keyword-face)
|
||||
(,dockerfile--from-regex
|
||||
(1 'dockerfile-image-name)
|
||||
(2 'dockerfile-image-alias nil t))
|
||||
,@(sh-font-lock-keywords)
|
||||
,@(sh-font-lock-keywords-2)
|
||||
,@(sh-font-lock-keywords-1))
|
||||
"Default `font-lock-keywords' for `dockerfile mode'.")
|
||||
|
||||
(defvar dockerfile-mode-map
|
||||
(let ((map (make-sparse-keymap))
|
||||
(menu-map (make-sparse-keymap)))
|
||||
(define-key map "\C-c\C-b" #'dockerfile-build-buffer)
|
||||
(define-key map "\C-c\M-b" #'dockerfile-build-no-cache-buffer)
|
||||
(define-key map "\C-c\C-c" #'comment-region)
|
||||
(define-key map [menu-bar dockerfile-mode] (cons "Dockerfile" menu-map))
|
||||
(define-key menu-map [dfc]
|
||||
'(menu-item "Comment Region" comment-region
|
||||
:help "Comment Region"))
|
||||
(define-key-after menu-map [dfb]
|
||||
'(menu-item "Build" dockerfile-build-buffer
|
||||
:help "Send the Dockerfile to docker build"))
|
||||
(define-key-after menu-map [dfbnc]
|
||||
'(menu-item "Build without cache" dockerfile-build-no-cache-buffer
|
||||
:help "Send the Dockerfile to docker build without cache"))
|
||||
map))
|
||||
|
||||
(defvar dockerfile-mode-syntax-table
|
||||
(let ((table (make-syntax-table)))
|
||||
(modify-syntax-entry ?# "<" table)
|
||||
(modify-syntax-entry ?\n ">" table)
|
||||
(modify-syntax-entry ?' "\"" table)
|
||||
(modify-syntax-entry ?= "." table)
|
||||
table)
|
||||
"Syntax table for `dockerfile-mode'.")
|
||||
|
||||
(define-abbrev-table 'dockerfile-mode-abbrev-table nil
|
||||
"Abbrev table used while in `dockerfile-mode'.")
|
||||
|
||||
(unless dockerfile-mode-abbrev-table
|
||||
(define-abbrev-table 'dockerfile-mode-abbrev-table ()))
|
||||
|
||||
(defun dockerfile-indent-line-function ()
|
||||
"Indent lines in a Dockerfile.
|
||||
|
||||
Lines beginning with a keyword are ignored, and any others are
|
||||
indented by one `dockerfile-indent-offset'. Functionality toggled
|
||||
by `dockerfile-enable-auto-indent'."
|
||||
(when dockerfile-enable-auto-indent
|
||||
(unless (member (get-text-property (line-beginning-position) 'face)
|
||||
'(font-lock-comment-delimiter-face font-lock-keyword-face))
|
||||
(save-excursion
|
||||
(beginning-of-line)
|
||||
(unless (looking-at-p "\\s-*$") ; Ignore empty lines.
|
||||
(indent-line-to dockerfile-indent-offset))))))
|
||||
|
||||
(defun dockerfile-build-arg-string ()
|
||||
"Create a --build-arg string for each element in `dockerfile-build-args'."
|
||||
(mapconcat (lambda (arg) (concat "--build-arg=" (replace-regexp-in-string "\\\\=" "=" (shell-quote-argument arg))))
|
||||
dockerfile-build-args " "))
|
||||
|
||||
(defun dockerfile-standard-filename (file)
|
||||
"Convert the FILE name to OS standard.
|
||||
If in Cygwin environment, uses Cygwin specific function to convert the
|
||||
file name. Otherwise, uses Emacs' standard conversion function."
|
||||
(if (fboundp 'cygwin-convert-file-name-to-windows)
|
||||
(replace-regexp-in-string
|
||||
(rx "\\") "\\\\" (cygwin-convert-file-name-to-windows file) t t)
|
||||
(convert-standard-filename file)))
|
||||
|
||||
(defun dockerfile-tag-string (image-name)
|
||||
"Return a --tag shell-quoted IMAGE-NAME string.
|
||||
|
||||
Returns an empty string if IMAGE-NAME is blank."
|
||||
(if (string= image-name "") "" (format "--tag %s " (shell-quote-argument image-name))))
|
||||
|
||||
(define-obsolete-variable-alias 'docker-image-name 'dockerfile-image-name "2017-10-22")
|
||||
|
||||
(defvar dockerfile-image-name nil
|
||||
"Name of the dockerfile currently being used.
|
||||
This can be set in file or directory-local variables.")
|
||||
|
||||
(defvar dockerfile-image-name-history nil
|
||||
"History of image names read by `dockerfile-read-image-name'.")
|
||||
|
||||
(defun dockerfile-read-image-name ()
|
||||
"Read a docker image name."
|
||||
(read-string "Image name: " dockerfile-image-name 'dockerfile-image-name-history))
|
||||
|
||||
|
||||
;;;###autoload
|
||||
(defun dockerfile-build-buffer (image-name &optional no-cache)
|
||||
"Build an image called IMAGE-NAME based upon the buffer.
|
||||
|
||||
If the prefix arg NO-CACHE is set, don't cache the image.
|
||||
|
||||
The shell command used to build the image is:
|
||||
|
||||
sudo docker build \\
|
||||
--no-cache \\
|
||||
--force-rm \\
|
||||
--pull \\
|
||||
--tag IMAGE-NAME \\
|
||||
--build-args args \\
|
||||
--progress type \\
|
||||
-f filename \\
|
||||
directory"
|
||||
|
||||
(interactive (list (dockerfile-read-image-name) prefix-arg))
|
||||
(save-buffer)
|
||||
(compilation-start
|
||||
(format
|
||||
"%s%s%s build %s %s %s %s %s --progress %s %s -f %s %s"
|
||||
(if dockerfile-use-buildkit "DOCKER_BUILDKIT=1 " "")
|
||||
(if dockerfile-use-sudo "sudo " "")
|
||||
dockerfile-mode-command
|
||||
(if no-cache "--no-cache" "")
|
||||
(if dockerfile-build-force-rm "--force-rm " "")
|
||||
(if dockerfile-build-pull "--pull " "")
|
||||
(dockerfile-tag-string image-name)
|
||||
(dockerfile-build-arg-string)
|
||||
dockerfile-build-progress
|
||||
(or dockerfile-build-extra-options "")
|
||||
(shell-quote-argument (dockerfile-standard-filename
|
||||
(or (file-remote-p (buffer-file-name) 'localname)
|
||||
(buffer-file-name))))
|
||||
(shell-quote-argument (dockerfile-standard-filename
|
||||
(or (file-remote-p default-directory 'localname)
|
||||
default-directory))))
|
||||
nil
|
||||
(lambda (_) (format "*docker-build-output: %s *" image-name))))
|
||||
|
||||
;;;###autoload
|
||||
(defun dockerfile-build-no-cache-buffer (image-name)
|
||||
"Build an image called IMAGE-NAME based upon the buffer without cache."
|
||||
(interactive (list (dockerfile-read-image-name)))
|
||||
(dockerfile-build-buffer image-name t))
|
||||
|
||||
(defun dockerfile--imenu-function ()
|
||||
"Find the previous headline from point.
|
||||
|
||||
Search for a FROM instruction. If an alias is used this is
|
||||
returned, otherwise the base image name is used."
|
||||
(when (re-search-backward dockerfile--from-regex nil t)
|
||||
(let ((data (match-data)))
|
||||
(when (match-string 2)
|
||||
;; we drop the first match group because
|
||||
;; imenu-generic-expression can only use one offset, so we
|
||||
;; normalize to `1'.
|
||||
(set-match-data (list (nth 0 data) (nth 1 data) (nth 4 data) (nth 5 data))))
|
||||
t)))
|
||||
|
||||
;;;###autoload
|
||||
(define-derived-mode dockerfile-mode prog-mode "Dockerfile"
|
||||
"A major mode to edit Dockerfiles.
|
||||
\\{dockerfile-mode-map}"
|
||||
(set-syntax-table dockerfile-mode-syntax-table)
|
||||
(set (make-local-variable 'imenu-generic-expression)
|
||||
`(("Stage" dockerfile--imenu-function 1)))
|
||||
(set (make-local-variable 'require-final-newline) mode-require-final-newline)
|
||||
(set (make-local-variable 'comment-start) "#")
|
||||
(set (make-local-variable 'comment-end) "")
|
||||
(set (make-local-variable 'comment-start-skip) "#+ *")
|
||||
(set (make-local-variable 'parse-sexp-ignore-comments) t)
|
||||
(set (make-local-variable 'font-lock-defaults)
|
||||
'(dockerfile-font-lock-keywords nil t))
|
||||
(setq local-abbrev-table dockerfile-mode-abbrev-table)
|
||||
(set (make-local-variable 'indent-line-function) #'dockerfile-indent-line-function))
|
||||
|
||||
;;;###autoload
|
||||
(add-to-list 'auto-mode-alist
|
||||
(cons (concat "[/\\]"
|
||||
"\\(?:Containerfile\\|Dockerfile\\)"
|
||||
"\\(?:\\.[^/\\]*\\)?\\'")
|
||||
'dockerfile-mode))
|
||||
|
||||
;;;###autoload
|
||||
(add-to-list 'auto-mode-alist '("\\.dockerfile\\'" . dockerfile-mode))
|
||||
|
||||
(provide 'dockerfile-mode)
|
||||
|
||||
;;; dockerfile-mode.el ends here
|
||||
@@ -1,165 +0,0 @@
|
||||
;;; queue.el --- Queue data structure -*- lexical-binding: t; -*-
|
||||
|
||||
;; Copyright (C) 1991-1995, 2008-2009, 2012, 2017 Free Software Foundation, Inc
|
||||
|
||||
;; Author: Inge Wallin <inge@lysator.liu.se>
|
||||
;; Toby Cubitt <toby-predictive@dr-qubit.org>
|
||||
;; Maintainer: Toby Cubitt <toby-predictive@dr-qubit.org>
|
||||
;; Version: 0.2
|
||||
;; Keywords: extensions, data structures, queue
|
||||
;; URL: http://www.dr-qubit.org/emacs.php
|
||||
;; Repository: http://www.dr-qubit.org/git/predictive.git
|
||||
|
||||
;; This file is part of Emacs.
|
||||
;;
|
||||
;; GNU Emacs 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 3 of the License, or (at your option)
|
||||
;; any later version.
|
||||
;;
|
||||
;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
;;; Commentary:
|
||||
;;
|
||||
;; These queues can be used both as a first-in last-out (FILO) and as a
|
||||
;; first-in first-out (FIFO) stack, i.e. elements can be added to the front or
|
||||
;; back of the queue, and can be removed from the front. (This type of data
|
||||
;; structure is sometimes called an "output-restricted deque".)
|
||||
;;
|
||||
;; You create a queue using `make-queue', add an element to the end of the
|
||||
;; queue using `queue-enqueue', and push an element onto the front of the
|
||||
;; queue using `queue-prepend'. To remove the first element from a queue, use
|
||||
;; `queue-dequeue'. A number of other queue convenience functions are also
|
||||
;; provided, all starting with the prefix `queue-'. Functions with prefix
|
||||
;; `queue--' are for internal use only, and should never be used outside this
|
||||
;; package.
|
||||
|
||||
|
||||
;;; Code:
|
||||
|
||||
(eval-when-compile (require 'cl))
|
||||
|
||||
(defmacro queue--when-generators (then)
|
||||
"Evaluate THEN if `generator' library is available."
|
||||
(declare (debug t))
|
||||
(if (require 'generator nil 'noerror) then))
|
||||
|
||||
|
||||
(defstruct (queue
|
||||
;; A tagged list is the pre-defstruct representation.
|
||||
;; (:type list)
|
||||
:named
|
||||
(:constructor nil)
|
||||
(:constructor queue-create ())
|
||||
(:copier nil))
|
||||
head tail)
|
||||
|
||||
|
||||
;;;###autoload
|
||||
(defalias 'make-queue 'queue-create
|
||||
"Create an empty queue data structure.")
|
||||
|
||||
|
||||
(defun queue-enqueue (queue element)
|
||||
"Append an ELEMENT to the end of the QUEUE."
|
||||
(if (queue-head queue)
|
||||
(setcdr (queue-tail queue)
|
||||
(setf (queue-tail queue) (cons element nil)))
|
||||
(setf (queue-head queue)
|
||||
(setf (queue-tail queue) (cons element nil)))))
|
||||
|
||||
(defalias 'queue-append 'queue-enqueue)
|
||||
|
||||
|
||||
(defun queue-prepend (queue element)
|
||||
"Prepend an ELEMENT to the front of the QUEUE."
|
||||
(if (queue-head queue)
|
||||
(push element (queue-head queue))
|
||||
(setf (queue-head queue)
|
||||
(setf (queue-tail queue) (cons element nil)))))
|
||||
|
||||
|
||||
(defun queue-dequeue (queue)
|
||||
"Remove the first element of QUEUE and return it.
|
||||
Returns nil if the queue is empty."
|
||||
(unless (cdr (queue-head queue)) (setf (queue-tail queue) nil))
|
||||
(pop (queue-head queue)))
|
||||
|
||||
|
||||
(defun queue-empty (queue)
|
||||
"Return t if QUEUE is empty, otherwise return nil."
|
||||
(null (queue-head queue)))
|
||||
|
||||
|
||||
(defun queue-first (queue)
|
||||
"Return the first element of QUEUE or nil if it is empty,
|
||||
without removing it from the QUEUE."
|
||||
(car (queue-head queue)))
|
||||
|
||||
|
||||
(defun queue-nth (queue n)
|
||||
"Return the nth element of a queue, without removing it.
|
||||
If the length of the queue is less than N, return nil. The first
|
||||
element in the queue has index 0."
|
||||
(nth n (queue-head queue)))
|
||||
|
||||
|
||||
(defun queue-last (queue)
|
||||
"Return the last element of QUEUE, without removing it.
|
||||
Returns nil if the QUEUE is empty."
|
||||
(car (queue-tail queue)))
|
||||
|
||||
|
||||
(defun queue-all (queue)
|
||||
"Return a list of all elements of QUEUE or nil if it is empty.
|
||||
The oldest element in the queue is the first in the list."
|
||||
(queue-head queue))
|
||||
|
||||
|
||||
(defun queue-copy (queue)
|
||||
"Return a copy of QUEUE.
|
||||
The new queue contains the elements of QUEUE in the same
|
||||
order. The elements themselves are *not* copied."
|
||||
(let ((q (queue-create))
|
||||
(list (queue-head queue)))
|
||||
(when (queue-head queue)
|
||||
(setf (queue-head q) (cons (car (queue-head queue)) nil)
|
||||
(queue-tail q) (queue-head q))
|
||||
(while (setq list (cdr list))
|
||||
(setf (queue-tail q)
|
||||
(setcdr (queue-tail q) (cons (car list) nil)))))
|
||||
q))
|
||||
|
||||
|
||||
(defun queue-length (queue)
|
||||
"Return the number of elements in QUEUE."
|
||||
(length (queue-head queue)))
|
||||
|
||||
|
||||
(defun queue-clear (queue)
|
||||
"Remove all elements from QUEUE."
|
||||
(setf (queue-head queue) nil
|
||||
(queue-tail queue) nil))
|
||||
|
||||
|
||||
(queue--when-generators
|
||||
(iter-defun queue-iter (queue)
|
||||
"Return a queue iterator object.
|
||||
|
||||
Calling `iter-next' on this object will retrieve the next element
|
||||
from the queue. The queue itself is not modified."
|
||||
(let ((list (queue-head queue)))
|
||||
(while list (iter-yield (pop list))))))
|
||||
|
||||
|
||||
(provide 'queue)
|
||||
|
||||
|
||||
;;; queue.el ends here
|
||||
File diff suppressed because it is too large
Load Diff
1484
.emacs.d/lisp/vundo.el
Normal file
1484
.emacs.d/lisp/vundo.el
Normal file
File diff suppressed because it is too large
Load Diff
491
.emacs.d/lisp/yaml-mode.el
Normal file
491
.emacs.d/lisp/yaml-mode.el
Normal file
@@ -0,0 +1,491 @@
|
||||
;;; yaml-mode.el --- Major mode for editing YAML files -*- lexical-binding: t -*-
|
||||
|
||||
;; Copyright (C) 2010-2014 Yoshiki Kurihara
|
||||
|
||||
;; Author: Yoshiki Kurihara <clouder@gmail.com>
|
||||
;; Marshall T. Vandegrift <llasram@gmail.com>
|
||||
;; Maintainer: Vasilij Schneidermann <mail@vasilij.de>
|
||||
;; URL: https://github.com/yoshiki/yaml-mode
|
||||
;; Package-Requires: ((emacs "24.1"))
|
||||
;; Keywords: data yaml
|
||||
;; Version: 0.0.16
|
||||
|
||||
;; This file is not part of Emacs
|
||||
|
||||
;; 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 3 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, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
;;; Commentary:
|
||||
|
||||
;; This is a major mode for editing files in the YAML data
|
||||
;; serialization format. It was initially developed by Yoshiki
|
||||
;; Kurihara and many features were added by Marshall Vandegrift. As
|
||||
;; YAML and Python share the fact that indentation determines
|
||||
;; structure, this mode provides indentation and indentation command
|
||||
;; behavior very similar to that of python-mode.
|
||||
|
||||
;;; Installation:
|
||||
|
||||
;; To install, just drop this file into a directory in your
|
||||
;; `load-path' and (optionally) byte-compile it. To automatically
|
||||
;; handle files ending in '.yml', add something like:
|
||||
;;
|
||||
;; (require 'yaml-mode)
|
||||
;; (add-to-list 'auto-mode-alist '("\\.yml\\'" . yaml-mode))
|
||||
;;
|
||||
;; to your .emacs file.
|
||||
;;
|
||||
;; Unlike python-mode, this mode follows the Emacs convention of not
|
||||
;; binding the ENTER key to `newline-and-indent'. To get this
|
||||
;; behavior, add the key definition to `yaml-mode-hook':
|
||||
;;
|
||||
;; (add-hook 'yaml-mode-hook
|
||||
;; '(lambda ()
|
||||
;; (define-key yaml-mode-map "\C-m" 'newline-and-indent)))
|
||||
|
||||
;;; Known Bugs:
|
||||
|
||||
;; YAML is easy to write but complex to parse, and this mode doesn't
|
||||
;; even really try. Indentation and highlighting will break on
|
||||
;; abnormally complicated structures.
|
||||
|
||||
;;; Code:
|
||||
|
||||
|
||||
;; User definable variables
|
||||
|
||||
;;;###autoload
|
||||
(defgroup yaml nil
|
||||
"Support for the YAML serialization format"
|
||||
:group 'languages
|
||||
:prefix "yaml-")
|
||||
|
||||
(defcustom yaml-mode-hook nil
|
||||
"*Hook run by `yaml-mode'."
|
||||
:type 'hook
|
||||
:group 'yaml)
|
||||
|
||||
(defcustom yaml-indent-offset 2
|
||||
"*Amount of offset per level of indentation."
|
||||
:type 'integer
|
||||
:safe 'natnump
|
||||
:group 'yaml)
|
||||
|
||||
(defcustom yaml-backspace-function 'backward-delete-char-untabify
|
||||
"*Function called by `yaml-electric-backspace' when deleting backwards.
|
||||
It will receive one argument, the numeric prefix value."
|
||||
:type 'function
|
||||
:group 'yaml)
|
||||
|
||||
(defcustom yaml-block-literal-search-lines 100
|
||||
"*Maximum number of lines to search for start of block literals."
|
||||
:type 'integer
|
||||
:group 'yaml)
|
||||
|
||||
(defcustom yaml-block-literal-electric-alist
|
||||
'((?| . "") (?> . "-"))
|
||||
"*Characters for which to provide electric behavior.
|
||||
The association list key should be a key code and the associated value
|
||||
should be a string containing additional characters to insert when
|
||||
that key is pressed to begin a block literal."
|
||||
:type 'alist
|
||||
:group 'yaml)
|
||||
|
||||
(defface yaml-tab-face
|
||||
'((((class color)) (:background "red" :foreground "red" :bold t))
|
||||
(t (:inverse-video t)))
|
||||
"Face to use for highlighting tabs in YAML files."
|
||||
:group 'faces
|
||||
:group 'yaml)
|
||||
|
||||
(defcustom yaml-imenu-generic-expression
|
||||
'((nil "^\\(:?[a-zA-Z_-]+\\):" 1))
|
||||
"The imenu regex to parse an outline of the yaml file."
|
||||
:type 'string
|
||||
:group 'yaml)
|
||||
|
||||
|
||||
;; Constants
|
||||
|
||||
(defconst yaml-mode-version "0.0.15" "Version of `yaml-mode'.")
|
||||
|
||||
(defconst yaml-blank-line-re "^ *$"
|
||||
"Regexp matching a line containing only (valid) whitespace.")
|
||||
|
||||
(defconst yaml-directive-re "^\\(?:--- \\)? *%\\(\\w+\\)"
|
||||
"Regexp matching a line containing a YAML directive.")
|
||||
|
||||
(defconst yaml-document-delimiter-re "^\\(?:---\\|[.][.][.]\\)"
|
||||
"Rexexp matching a YAML document delimiter line.")
|
||||
|
||||
(defconst yaml-node-anchor-alias-re "[&*][a-zA-Z0-9_-]+"
|
||||
"Regexp matching a YAML node anchor or alias.")
|
||||
|
||||
(defconst yaml-tag-re "!!?[^ \n]+"
|
||||
"Rexexp matching a YAML tag.")
|
||||
|
||||
(defconst yaml-bare-scalar-re
|
||||
"\\(?:[^-:,#!\n{\\[ ]\\|[^#!\n{\\[ ]\\S-\\)[^#\n]*?"
|
||||
"Rexexp matching a YAML bare scalar.")
|
||||
|
||||
(defconst yaml-hash-key-re
|
||||
(concat "\\(?:^\\(?:--- \\)?\\|{\\|\\(?:[-,] +\\)+\\) *"
|
||||
"\\(?:" yaml-tag-re " +\\)?"
|
||||
"\\(" yaml-bare-scalar-re "\\) *:"
|
||||
"\\(?: +\\|$\\)")
|
||||
"Regexp matching a single YAML hash key.")
|
||||
|
||||
(defconst yaml-scalar-context-re
|
||||
(concat "\\(?:^\\(?:--- \\)?\\|{\\|\\(?: *[-,] +\\)+\\) *"
|
||||
"\\(?:" yaml-bare-scalar-re " *: \\)?")
|
||||
"Regexp indicating the beginning of a scalar context.")
|
||||
|
||||
(defconst yaml-nested-map-re
|
||||
(concat "[^#\n]*: *\\(?:&.*\\|{ *\\|" yaml-tag-re " *\\)?$")
|
||||
"Regexp matching a line beginning a YAML nested structure.")
|
||||
|
||||
(defconst yaml-block-literal-base-re " *[>|][-+0-9]* *\\(?:\n\\|\\'\\)"
|
||||
"Regexp matching the substring start of a block literal.")
|
||||
|
||||
(defconst yaml-block-literal-re
|
||||
(concat yaml-scalar-context-re
|
||||
"\\(?:" yaml-tag-re "\\)?"
|
||||
yaml-block-literal-base-re)
|
||||
"Regexp matching a line beginning a YAML block literal.")
|
||||
|
||||
(defconst yaml-nested-sequence-re
|
||||
(concat "^\\(?:\\(?: *- +\\)+\\|\\(:? *-$\\)\\)"
|
||||
"\\(?:" yaml-bare-scalar-re " *:\\(?: +.*\\)?\\)?$")
|
||||
"Regexp matching a line containing one or more nested YAML sequences.")
|
||||
|
||||
(defconst yaml-constant-scalars-re
|
||||
(concat "\\(?:^\\|\\(?::\\|-\\|,\\|{\\|\\[\\) +\\) *"
|
||||
(regexp-opt
|
||||
'("~" "null" "Null" "NULL"
|
||||
".nan" ".NaN" ".NAN"
|
||||
".inf" ".Inf" ".INF"
|
||||
"-.inf" "-.Inf" "-.INF"
|
||||
"y" "Y" "yes" "Yes" "YES" "n" "N" "no" "No" "NO"
|
||||
"true" "True" "TRUE" "false" "False" "FALSE"
|
||||
"on" "On" "ON" "off" "Off" "OFF") t)
|
||||
"\\_>")
|
||||
"Regexp matching certain scalar constants in scalar context.")
|
||||
|
||||
|
||||
;; Mode setup
|
||||
|
||||
(defvar yaml-mode-map
|
||||
(let ((map (make-sparse-keymap)))
|
||||
(define-key map "|" 'yaml-electric-bar-and-angle)
|
||||
(define-key map ">" 'yaml-electric-bar-and-angle)
|
||||
(define-key map "-" 'yaml-electric-dash-and-dot)
|
||||
(define-key map "." 'yaml-electric-dash-and-dot)
|
||||
(define-key map (kbd "DEL") 'yaml-electric-backspace)
|
||||
map)
|
||||
"Keymap used in `yaml-mode' buffers.")
|
||||
|
||||
(defvar yaml-mode-syntax-table
|
||||
(let ((syntax-table (make-syntax-table)))
|
||||
(modify-syntax-entry ?\' "\"" syntax-table)
|
||||
(modify-syntax-entry ?\" "\"" syntax-table)
|
||||
(modify-syntax-entry ?# "<" syntax-table)
|
||||
(modify-syntax-entry ?\n ">" syntax-table)
|
||||
(modify-syntax-entry ?\\ "\\" syntax-table)
|
||||
(modify-syntax-entry ?- "_" syntax-table)
|
||||
(modify-syntax-entry ?_ "_" syntax-table)
|
||||
(modify-syntax-entry ?& "." syntax-table)
|
||||
(modify-syntax-entry ?* "." syntax-table)
|
||||
(modify-syntax-entry ?\( "." syntax-table)
|
||||
(modify-syntax-entry ?\) "." syntax-table)
|
||||
(modify-syntax-entry ?\{ "(}" syntax-table)
|
||||
(modify-syntax-entry ?\} "){" syntax-table)
|
||||
(modify-syntax-entry ?\[ "(]" syntax-table)
|
||||
(modify-syntax-entry ?\] ")[" syntax-table)
|
||||
syntax-table)
|
||||
"Syntax table in use in `yaml-mode' buffers.")
|
||||
|
||||
;;;###autoload
|
||||
(define-derived-mode yaml-mode text-mode "YAML"
|
||||
"Simple mode to edit YAML.
|
||||
|
||||
\\{yaml-mode-map}"
|
||||
:syntax-table yaml-mode-syntax-table
|
||||
(set (make-local-variable 'comment-start) "# ")
|
||||
(set (make-local-variable 'comment-start-skip) "#+ *")
|
||||
(set (make-local-variable 'comment-end) "")
|
||||
(set (make-local-variable 'indent-line-function) 'yaml-indent-line)
|
||||
(set (make-local-variable 'indent-tabs-mode) nil)
|
||||
(set (make-local-variable 'fill-paragraph-function) 'yaml-fill-paragraph)
|
||||
(set (make-local-variable 'page-delimiter) "^---\\([ \t].*\\)*\n")
|
||||
|
||||
(set (make-local-variable 'syntax-propertize-function)
|
||||
'yaml-mode-syntax-propertize-function)
|
||||
(setq font-lock-defaults '(yaml-font-lock-keywords)))
|
||||
|
||||
|
||||
;; Font-lock support
|
||||
|
||||
(defvar yaml-font-lock-keywords
|
||||
`((yaml-font-lock-block-literals 0 font-lock-string-face)
|
||||
(,yaml-constant-scalars-re . (1 font-lock-constant-face))
|
||||
(,yaml-tag-re . (0 font-lock-type-face))
|
||||
(,yaml-node-anchor-alias-re . (0 font-lock-function-name-face))
|
||||
(,yaml-hash-key-re . (1 font-lock-variable-name-face))
|
||||
(,yaml-document-delimiter-re . (0 font-lock-comment-face))
|
||||
(,yaml-directive-re . (1 font-lock-builtin-face))
|
||||
("^[\t]+" 0 'yaml-tab-face t))
|
||||
"Additional expressions to highlight in YAML mode.")
|
||||
|
||||
(defun yaml-mode-syntax-propertize-function (beg end)
|
||||
"Override buffer's syntax table for special syntactic constructs."
|
||||
;; Unhighlight foo#bar tokens between BEG and END.
|
||||
(save-excursion
|
||||
(goto-char beg)
|
||||
(while (search-forward "#" end t)
|
||||
(save-excursion
|
||||
(forward-char -1)
|
||||
;; both ^# and [ \t]# are comments
|
||||
(when (and (not (bolp))
|
||||
(not (memq (preceding-char) '(?\s ?\t))))
|
||||
(put-text-property (point) (1+ (point))
|
||||
'syntax-table (string-to-syntax "_"))))))
|
||||
|
||||
(save-excursion
|
||||
(goto-char beg)
|
||||
(while (and
|
||||
(> end (point))
|
||||
(re-search-forward "['\"]" end t))
|
||||
(when (get-text-property (point) 'yaml-block-literal)
|
||||
(put-text-property (1- (point)) (point)
|
||||
'syntax-table (string-to-syntax "w")))
|
||||
(let* ((pt (point))
|
||||
(sps (save-excursion (syntax-ppss (1- pt)))))
|
||||
(when (not (nth 8 sps))
|
||||
(cond
|
||||
((and (char-equal ?' (char-before (1- pt)))
|
||||
(char-equal ?' (char-before pt)))
|
||||
(put-text-property (- pt 2) pt
|
||||
'syntax-table (string-to-syntax "w"))
|
||||
;; Workaround for https://debbugs.gnu.org/41195.
|
||||
(let ((syntax-propertize--done syntax-propertize--done))
|
||||
;; Carefully invalidate the last cached ppss.
|
||||
(syntax-ppss-flush-cache (- pt 2))))
|
||||
;; If quote is detected as a syntactic string start but appeared
|
||||
;; after a non-whitespace character, then mark it as syntactic word.
|
||||
((and (char-before (1- pt))
|
||||
(char-equal ?w (char-syntax (char-before (1- pt)))))
|
||||
(put-text-property (1- pt) pt
|
||||
'syntax-table (string-to-syntax "w")))
|
||||
(t
|
||||
;; We're right after a quote that opens a string literal.
|
||||
;; Skip over it (big speedup for long JSON strings).
|
||||
(goto-char (1- pt))
|
||||
(condition-case nil
|
||||
(forward-sexp)
|
||||
(scan-error
|
||||
(goto-char end))))))))))
|
||||
|
||||
(defun yaml-font-lock-block-literals (bound)
|
||||
"Find lines within block literals.
|
||||
Find the next line of the first (if any) block literal after point and
|
||||
prior to BOUND. Returns the beginning and end of the block literal
|
||||
line in the match data, as consumed by `font-lock-keywords' matcher
|
||||
functions. The function begins by searching backwards to determine
|
||||
whether or not the current line is within a block literal. This could
|
||||
be time-consuming in large buffers, so the number of lines searched is
|
||||
artificially limited to the value of
|
||||
`yaml-block-literal-search-lines'."
|
||||
(if (eolp) (goto-char (1+ (point))))
|
||||
(unless (or (eobp) (>= (point) bound))
|
||||
(let ((begin (point))
|
||||
(end (min (1+ (line-end-position)) bound)))
|
||||
(goto-char (line-beginning-position))
|
||||
(while (and (looking-at yaml-blank-line-re)
|
||||
(not (bobp)))
|
||||
(forward-line -1))
|
||||
(let ((nlines yaml-block-literal-search-lines)
|
||||
(min-level (current-indentation)))
|
||||
(forward-line -1)
|
||||
(while (and (/= nlines 0)
|
||||
(/= min-level 0)
|
||||
(not (looking-at yaml-block-literal-re))
|
||||
(not (bobp)))
|
||||
(setq nlines (1- nlines))
|
||||
(unless (looking-at yaml-blank-line-re)
|
||||
(setq min-level (min min-level (current-indentation))))
|
||||
(forward-line -1))
|
||||
(when (looking-at-p " *- ")
|
||||
(setq min-level (- min-level 2)))
|
||||
(cond
|
||||
((and (< (current-indentation) min-level)
|
||||
(looking-at yaml-block-literal-re))
|
||||
(goto-char end)
|
||||
(put-text-property begin end 'yaml-block-literal t)
|
||||
(set-match-data (list begin end))
|
||||
t)
|
||||
((progn
|
||||
(goto-char begin)
|
||||
(re-search-forward (concat yaml-block-literal-re
|
||||
" *\\(.*\\)\n")
|
||||
bound t))
|
||||
(let ((range (nthcdr 2 (match-data))))
|
||||
(put-text-property (car range) (cadr range) 'yaml-block-literal t)
|
||||
(set-match-data range))
|
||||
t))))))
|
||||
|
||||
|
||||
;; Indentation and electric keys
|
||||
|
||||
(defun yaml-compute-indentation ()
|
||||
"Calculate the maximum sensible indentation for the current line."
|
||||
(save-excursion
|
||||
(beginning-of-line)
|
||||
(if (looking-at yaml-document-delimiter-re) 0
|
||||
(forward-line -1)
|
||||
(while (and (looking-at yaml-blank-line-re)
|
||||
(> (point) (point-min)))
|
||||
(forward-line -1))
|
||||
(+ (current-indentation)
|
||||
(if (looking-at yaml-nested-map-re) yaml-indent-offset 0)
|
||||
(if (looking-at yaml-nested-sequence-re) yaml-indent-offset 0)
|
||||
(if (looking-at yaml-block-literal-re) yaml-indent-offset 0)))))
|
||||
|
||||
(defun yaml-indent-line ()
|
||||
"Indent the current line.
|
||||
The first time this command is used, the line will be indented to the
|
||||
maximum sensible indentation. Each immediately subsequent usage will
|
||||
back-dent the line by `yaml-indent-offset' spaces. On reaching column
|
||||
0, it will cycle back to the maximum sensible indentation."
|
||||
(interactive "*")
|
||||
(let ((ci (current-indentation))
|
||||
(need (yaml-compute-indentation)))
|
||||
(save-excursion
|
||||
(if (and (equal last-command this-command) (/= ci 0))
|
||||
(indent-line-to (* (/ (- ci 1) yaml-indent-offset) yaml-indent-offset))
|
||||
(indent-line-to need)))
|
||||
(if (< (current-column) (current-indentation))
|
||||
(forward-to-indentation 0))))
|
||||
|
||||
(defun yaml-electric-backspace (arg)
|
||||
"Delete characters or back-dent the current line.
|
||||
If invoked following only whitespace on a line, will back-dent to the
|
||||
immediately previous multiple of `yaml-indent-offset' spaces."
|
||||
(interactive "*p")
|
||||
(if (or (/= (current-indentation) (current-column)) (bolp))
|
||||
(funcall yaml-backspace-function arg)
|
||||
(let ((ci (current-column)))
|
||||
(beginning-of-line)
|
||||
(delete-horizontal-space)
|
||||
(indent-to (* (/ (- ci (* arg yaml-indent-offset))
|
||||
yaml-indent-offset)
|
||||
yaml-indent-offset)))))
|
||||
|
||||
(defun yaml-electric-bar-and-angle (arg)
|
||||
"Insert the bound key and possibly begin a block literal.
|
||||
Inserts the bound key. If inserting the bound key causes the current
|
||||
line to match the initial line of a block literal, then inserts the
|
||||
matching string from `yaml-block-literal-electric-alist', a newline,
|
||||
and indents appropriately."
|
||||
(interactive "*P")
|
||||
(self-insert-command (prefix-numeric-value arg))
|
||||
(let ((extra-chars
|
||||
(assoc last-command-event
|
||||
yaml-block-literal-electric-alist)))
|
||||
(cond
|
||||
((and extra-chars (not arg) (eolp)
|
||||
(save-excursion
|
||||
(beginning-of-line)
|
||||
(looking-at yaml-block-literal-re)))
|
||||
(insert (cdr extra-chars))
|
||||
(newline-and-indent)))))
|
||||
|
||||
(defun yaml-electric-dash-and-dot (arg)
|
||||
"Insert the bound key and possibly de-dent line.
|
||||
Inserts the bound key. If inserting the bound key causes the current
|
||||
line to match a document delimiter, de-dent the line to the left
|
||||
margin."
|
||||
(interactive "*P")
|
||||
(self-insert-command (prefix-numeric-value arg))
|
||||
(save-excursion
|
||||
(beginning-of-line)
|
||||
(when (and (not arg) (looking-at yaml-document-delimiter-re))
|
||||
(delete-horizontal-space))))
|
||||
|
||||
(defun yaml-narrow-to-block-literal ()
|
||||
"Narrow the buffer to block literal if the point is in it,
|
||||
otherwise do nothing."
|
||||
(interactive)
|
||||
(save-excursion
|
||||
(goto-char (line-beginning-position))
|
||||
(while (and (looking-at-p yaml-blank-line-re) (not (bobp)))
|
||||
(forward-line -1))
|
||||
(let ((nlines yaml-block-literal-search-lines)
|
||||
(min-level (current-indentation))
|
||||
beg)
|
||||
(forward-line -1)
|
||||
(while (and (/= nlines 0)
|
||||
(/= min-level 0)
|
||||
(not (looking-at-p yaml-block-literal-re))
|
||||
(not (bobp)))
|
||||
(setq nlines (1- nlines))
|
||||
(unless (looking-at-p yaml-blank-line-re)
|
||||
(setq min-level (min min-level (current-indentation))))
|
||||
(forward-line -1))
|
||||
(when (and (< (current-indentation) min-level)
|
||||
(looking-at-p yaml-block-literal-re))
|
||||
(setq min-level (current-indentation))
|
||||
(forward-line)
|
||||
(setq beg (point))
|
||||
(while (and (not (eobp))
|
||||
(or (looking-at-p yaml-blank-line-re)
|
||||
(> (current-indentation) min-level)))
|
||||
(forward-line))
|
||||
(narrow-to-region beg (point))))))
|
||||
|
||||
(defun yaml-fill-paragraph (&optional justify region)
|
||||
"Fill paragraph.
|
||||
Outside of comments, this behaves as `fill-paragraph' except that
|
||||
filling does not cross boundaries of block literals. Inside comments,
|
||||
this will do usual adaptive fill behaviors."
|
||||
(interactive "*P")
|
||||
(save-restriction
|
||||
(yaml-narrow-to-block-literal)
|
||||
(let ((fill-paragraph-function nil))
|
||||
(or (fill-comment-paragraph justify)
|
||||
(fill-paragraph justify region)))))
|
||||
|
||||
(defun yaml-set-imenu-generic-expression ()
|
||||
(make-local-variable 'imenu-generic-expression)
|
||||
(make-local-variable 'imenu-create-index-function)
|
||||
(setq imenu-create-index-function 'imenu-default-create-index-function)
|
||||
(setq imenu-generic-expression yaml-imenu-generic-expression))
|
||||
|
||||
(add-hook 'yaml-mode-hook 'yaml-set-imenu-generic-expression)
|
||||
|
||||
|
||||
(defun yaml-mode-version ()
|
||||
"Display version of `yaml-mode'."
|
||||
(interactive)
|
||||
(message "yaml-mode %s" yaml-mode-version)
|
||||
yaml-mode-version)
|
||||
|
||||
;;;###autoload
|
||||
(add-to-list 'auto-mode-alist '("\\.\\(e?ya?\\|ra\\)ml\\'" . yaml-mode))
|
||||
|
||||
;;;###autoload
|
||||
(add-to-list 'magic-mode-alist
|
||||
'("^%YAML\\s-+[0-9]+\\.[0-9]+\\(\\s-+#\\|\\s-*$\\)" . yaml-mode))
|
||||
|
||||
(provide 'yaml-mode)
|
||||
|
||||
;;; yaml-mode.el ends here
|
||||
Reference in New Issue
Block a user