* escaping now works inside lisp expressions

* new keybindings
* minor fixes
* more doc
This commit is contained in:
capitaomorte 2009-07-25 10:50:40 +00:00
parent 2f21c06622
commit b90926499a

View File

@ -70,10 +70,10 @@
;; ;;
;; M-x yas/visit-snippet-file ;; M-x yas/visit-snippet-file
;; ;;
;; Prompts you for possible snippet expasions like ;; Prompts you for possible snippet expansions like
;; `yas/insert-snippet', but instead of expanding it, takes ;; `yas/insert-snippet', but instead of expanding it, takes
;; you directly to the snippet definition's file, if it ;; you directly to the snippet definition's file, if it
;; exits. ;; exists.
;; ;;
;; M-x yas/load-snippet-buffer ;; M-x yas/load-snippet-buffer
;; ;;
@ -430,6 +430,8 @@ Here's an example:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Minor mode stuff ;; Minor mode stuff
;; ;;
;; TODO: XXX: This is somehow needed in Carbon Emacs for MacOSX
(defvar last-buffer-undo-list nil)
(defvar yas/minor-mode-map (make-sparse-keymap) (defvar yas/minor-mode-map (make-sparse-keymap)
"The keymap used when function `yas/minor-mode' is active.") "The keymap used when function `yas/minor-mode' is active.")
@ -445,7 +447,9 @@ Here's an example:
(vector (first ent) (second ent) t)) (vector (first ent) (second ent) t))
(list (list "--") (list (list "--")
(list "Expand trigger" 'yas/expand (read-kbd-macro yas/trigger-key)) (list "Expand trigger" 'yas/expand (read-kbd-macro yas/trigger-key))
(list "Insert at point" 'yas/insert-snippet "\C-c&\C-s") (list "Insert at point..." 'yas/insert-snippet "\C-c&\C-s")
(list "Visit snippet file..." 'yas/visit-snippet-file "\C-c&\C-v")
(list "Find snippets..." 'yas/find-snippets "\C-c&\C-f")
(list "About" 'yas/about) (list "About" 'yas/about)
(list "Reload-all-snippets" 'yas/reload-all) (list "Reload-all-snippets" 'yas/reload-all)
(list "Load snippets..." 'yas/load-directory)))))) (list "Load snippets..." 'yas/load-directory))))))
@ -1024,6 +1028,8 @@ Here's the default value for all the parameters:
(insert ";;; yasnippet-bundle.el --- " (insert ";;; yasnippet-bundle.el --- "
"Yet another snippet extension (Auto compiled bundle)\n") "Yet another snippet extension (Auto compiled bundle)\n")
(insert-file-contents yasnippet) (insert-file-contents yasnippet)
(goto-char (point-max))
(insert "\n")
(when dropdown (when dropdown
(insert-file-contents dropdown)) (insert-file-contents dropdown))
(goto-char (point-max)) (goto-char (point-max))
@ -1375,40 +1381,54 @@ major mode."
(snippet-mode))))))) (snippet-mode)))))))
(defun yas/compute-major-mode-and-parent (file) (defun yas/compute-major-mode-and-parent (file &optional prompt-if-failed)
(let* ((file-dir (directory-file-name (file-name-directory file))) (let* ((file-dir (and file
(major-mode-name (file-name-nondirectory file-dir)) (directory-file-name (file-name-directory file))))
(parent-file-dir (directory-file-name (file-name-directory file-dir))) (major-mode-name (and file-dir
(parent-mode-name (file-name-nondirectory parent-file-dir)) (file-name-nondirectory file-dir)))
(major-mode-sym (intern major-mode-name)) (parent-file-dir (and file-dir
(parent-mode-sym (intern parent-mode-name))) (directory-file-name (file-name-directory file-dir))))
(when (fboundp major-mode-sym) (parent-mode-name (and parent-file-dir
(cons major-mode-sym (file-name-nondirectory parent-file-dir)))
(when (fboundp parent-mode-sym) (major-mode-sym (or (and major-mode-name
parent-mode-sym))))) (intern major-mode-name))
(when prompt-if-failed
(read-from-minibuffer "[yas] Cannot auto-detect major mode! Enter a major mode: "))))
(parent-mode-sym (and parent-mode-name
(intern parent-mode-name))))
(if (fboundp major-mode-sym)
(cons major-mode-sym
(when (fboundp parent-mode-sym)
parent-mode-sym)))))
(defun yas/load-snippet-buffer (&optional kill) (defun yas/load-snippet-buffer (&optional kill)
"Parse and load current buffer's snippet definition." "Parse and load current buffer's snippet definition.
With optional prefix argument KILL quit the window and buffer."
(interactive "P") (interactive "P")
(if buffer-file-name (if buffer-file-name
(let ((major-mode-and-parent (yas/compute-major-mode-and-parent buffer-file-name))) (let ((major-mode-and-parent (yas/compute-major-mode-and-parent buffer-file-name)))
(when major-mode-and-parent (if major-mode-and-parent
(yas/define-snippets (car major-mode-and-parent) (let* ((parsed (yas/parse-template buffer-file-name))
(list (yas/parse-template buffer-file-name)) (name (and parsed
(cdr major-mode-and-parent))) (third parsed))))
(when (and (buffer-modified-p) (when name
(y-or-n-p "Save snippet? ")) (yas/define-snippets (car major-mode-and-parent)
(save-buffer)) (list parsed)
(quit-window)) (cdr major-mode-and-parent))
(when (and (buffer-modified-p)
(y-or-n-p "Save snippet? "))
(save-buffer))
(if kill
(quit-window kill)
(message "[yas] Snippet \"%s\" loaded for %s." name (car major-mode-and-parent)))))
(message "[yas] Cannot load snippet for unknown major mode")))
(message "Save the buffer as a file first!"))) (message "Save the buffer as a file first!")))
(defun yas/tryout-snippet (&optional debug) (defun yas/tryout-snippet (&optional debug)
"Test current buffers's snippet template in other buffer." "Test current buffers's snippet template in other buffer."
(interactive "P") (interactive "P")
(let* ((major-mode-and-parent (or (and buffer-file-name (let* ((major-mode-and-parent (yas/compute-major-mode-and-parent buffer-file-name))
(yas/compute-major-mode-and-parent buffer-file-name))
(cons (intern (read-from-minibuffer "Cannot auto-detect major mode! Enter a major mode: "))
nil)))
(parsed (and major-mode-and-parent (parsed (and major-mode-and-parent
(fboundp (car major-mode-and-parent)) (fboundp (car major-mode-and-parent))
(yas/parse-template (symbol-name (car major-mode-and-parent))))) (yas/parse-template (symbol-name (car major-mode-and-parent)))))
@ -1480,17 +1500,16 @@ Otherwise throw exception."
;;; Snippet expansion and field management ;;; Snippet expansion and field management
(defvar yas/active-field-overlay nil (defvar yas/active-field-overlay nil
"Overlays the currently active field") "Overlays the currently active field.")
(defvar yas/field-protection-overlays nil (defvar yas/field-protection-overlays nil
"Two overlays protect the current actipve field ") "Two overlays protect the current active field ")
(defvar yas/deleted-text nil (defvar yas/deleted-text nil
"The text deleted in the last snippet expansion.") "The text deleted in the last snippet expansion.")
(defvar yas/selected-text nil (defvar yas/selected-text nil
"The selected region deleted before the last snippet "The selected region deleted on the last snippet expansion.")
expansion.")
(defvar yas/start-column nil (defvar yas/start-column nil
"The column where the snippet expansion started.") "The column where the snippet expansion started.")
@ -1548,11 +1567,15 @@ for this field, apply it. Otherwise, returned nil."
(yas/eval-string transform))))) (yas/eval-string transform)))))
transformed)) transformed))
(defsubst yas/replace-all (from to) (defsubst yas/replace-all (from to &optional text)
"Replace all occurance from FROM to TO." "Replace all occurance from FROM to TO.
With optional string TEXT do it in that string."
(goto-char (point-min)) (goto-char (point-min))
(while (search-forward from nil t) (if text
(replace-match to t t))) (replace-regexp-in-string from to text t t)
(while (search-forward from nil t)
(replace-match to t t text))))
(defun yas/snippet-find-field (snippet number) (defun yas/snippet-find-field (snippet number)
(find-if #'(lambda (field) (find-if #'(lambda (field)
@ -2326,12 +2349,19 @@ Meant to be called in a narrowed buffer, does various passes"
(yas/escape-string escaped))) (yas/escape-string escaped)))
(or escaped yas/escaped-characters))) (or escaped yas/escaped-characters)))
(defun yas/restore-escapes () (defun yas/restore-escapes (&optional text)
"Restore all escaped characters from their numeric ASCII value." "Restore all escaped characters from their numeric ASCII value.
(mapc #'(lambda (escaped)
(yas/replace-all (yas/escape-string escaped) With optional string TEXT do it in string instead"
(char-to-string escaped))) (let ((changed-text text)
yas/escaped-characters)) (text-provided-p text))
(mapc #'(lambda (escaped)
(setq changed-text
(yas/replace-all (yas/escape-string escaped)
(char-to-string escaped)
(when text-provided-p changed-text))))
yas/escaped-characters)
changed-text))
(defun yas/replace-backquotes () (defun yas/replace-backquotes ()
"Replace all the \"`(lisp-expression)`\"-style expression "Replace all the \"`(lisp-expression)`\"-style expression
@ -2339,7 +2369,7 @@ Meant to be called in a narrowed buffer, does various passes"
(while (re-search-forward yas/backquote-lisp-expression-regexp nil t) (while (re-search-forward yas/backquote-lisp-expression-regexp nil t)
(let ((transformed (yas/eval-string (match-string 1)))) (let ((transformed (yas/eval-string (match-string 1))))
(goto-char (match-end 0)) (goto-char (match-end 0))
(insert transformed) (when transformed (insert transformed))
(delete-region (match-beginning 0) (match-end 0))))) (delete-region (match-beginning 0) (match-end 0)))))
(defun yas/scan-sexps (from count) (defun yas/scan-sexps (from count)
@ -2393,7 +2423,7 @@ When multiple expressions are found, only the last one counts."
(let* ((real-match-end-1 (yas/scan-sexps (match-beginning 1) 1))) (let* ((real-match-end-1 (yas/scan-sexps (match-beginning 1) 1)))
(when real-match-end-1 (when real-match-end-1
(let ((lisp-expression-string (buffer-substring-no-properties (match-beginning 1) real-match-end-1))) (let ((lisp-expression-string (buffer-substring-no-properties (match-beginning 1) real-match-end-1)))
(setf (yas/field-transform parent-field) lisp-expression-string)) (setf (yas/field-transform parent-field) (yas/restore-escapes lisp-expression-string)))
(delete-region (match-beginning 0) real-match-end-1))))))) (delete-region (match-beginning 0) real-match-end-1)))))))
(defun yas/transform-mirror-parse-create (snippet) (defun yas/transform-mirror-parse-create (snippet)
@ -2408,8 +2438,8 @@ When multiple expressions are found, only the last one counts."
field) field)
(push (yas/make-mirror (yas/make-marker (match-beginning 0)) (push (yas/make-mirror (yas/make-marker (match-beginning 0))
(yas/make-marker (match-beginning 0)) (yas/make-marker (match-beginning 0))
(buffer-substring-no-properties (match-beginning 2) (yas/restore-escapes (buffer-substring-no-properties (match-beginning 2)
(1- real-match-end-0))) (1- real-match-end-0))))
(yas/field-mirrors field)) (yas/field-mirrors field))
(delete-region (match-beginning 0) real-match-end-0))))) (delete-region (match-beginning 0) real-match-end-0)))))