(defvar nxml-where-max-elements 6)
(require 'cl)
+(require 'nxml-mode)
(defun nxml-where ()
"Display the hierarchy of XML elements the point is on as a path."
(and (eq major-mode 'nxml-mode)
- (let (path-to-id path-rest)
- (save-excursion
- (save-restriction
- (widen)
- (while (and (not (bobp))
- (condition-case nil (progn (nxml-backward-up-element) t) (error nil)))
- (multiple-value-bind
- (has-id step)
- (loop with has-id = nil
- with step = (xmltok-start-tag-local-name)
- for att in xmltok-attributes
- if (string= (xmltok-attribute-local-name att) "id")
- return (values t (concat "\"" (xmltok-attribute-value att) "\""))
- else if (string= (xmltok-attribute-local-name att) "name")
- do (setq has-id t step (concat "\"" (xmltok-attribute-value att) "\""))
- finally return (values has-id step ))
- (if (or path-to-id has-id)
- (setq path-to-id (cons step path-to-id))
- (setq path-rest (cons step path-rest)))))))
- (let ((path-to-id-len (length path-to-id))
- (path-rest-len (length path-rest)))
- (if (> path-to-id-len nxml-where-elements-to-id)
- (progn
- (setq path-to-id (nthcdr (- path-to-id-len nxml-where-elements-to-id -1) path-to-id))
- (setq path-to-id (cons "..." path-to-id))
- (setq path-to-id-len nxml-where-elements-to-id))
- (setq path-to-id (cons "" path-to-id)))
- (when (> (+ path-to-id-len path-rest-len) nxml-where-max-elements)
- (setq path-rest (nbutlast path-rest (- path-rest-len (- nxml-where-max-elements path-to-id-len) -1)))
- (setq path-rest (nconc path-rest (list "...")))))
- (mapconcat 'identity (nconc path-to-id path-rest) "/"))))
-
+ (let (path-to-id path-rest)
+ (save-excursion
+ (save-restriction
+ (widen)
+ (while (and (not (bobp))
+ (condition-case nil (progn (nxml-backward-up-element) t)
+ (error nil)))
+ (multiple-value-bind
+ (has-id step)
+ (loop with has-id = nil
+ with step = (xmltok-start-tag-local-name)
+ for att in xmltok-attributes
+ if (string= (xmltok-attribute-local-name att) "id")
+ return (values t (concat "\""
+ (xmltok-attribute-value att)
+ "\""))
+ else if (string= (xmltok-attribute-local-name att) "name")
+ do (setq has-id t
+ step (concat "\""
+ (xmltok-attribute-value att)
+ "\""))
+ finally return (values has-id step ))
+ (if (or path-to-id has-id)
+ (setq path-to-id (cons step path-to-id))
+ (setq path-rest (cons step path-rest)))))))
+ (let ((path-to-id-len (length path-to-id))
+ (path-rest-len (length path-rest)))
+ (if (> path-to-id-len nxml-where-elements-to-id)
+ (progn
+ (setq path-to-id (nthcdr (- path-to-id-len
+ nxml-where-elements-to-id
+ -1) path-to-id))
+ (setq path-to-id (cons "..." path-to-id))
+ (setq path-to-id-len nxml-where-elements-to-id))
+ (setq path-to-id (cons "" path-to-id)))
+ (when (> (+ path-to-id-len path-rest-len) nxml-where-max-elements)
+ (setq path-rest (nbutlast path-rest (- path-rest-len
+ (- nxml-where-max-elements
+ path-to-id-len)
+ -1)))
+ (setq path-rest (nconc path-rest (list "...")))))
+ (mapconcat 'identity (nconc path-to-id path-rest) "/"))))
+
(require 'which-func)
+(which-func-mode)
+
+(delete (assoc 'which-func-mode mode-line-format) mode-line-format)
+(setq which-func-header-line-format
+ '(which-func-mode
+ ("" which-func-format
+ )))
+(defadvice which-func-ff-hook (after header-line activate)
+ (when which-func-mode
+ (delete (assoc 'which-func-mode mode-line-format) mode-line-format)
+ (setq header-line-format which-func-header-line-format)))
(add-to-list 'which-func-functions 'nxml-where)
(add-to-list 'which-func-modes 'nxml-mode)
(require 'hideshow)
-(add-to-list 'hs-special-modes-alist '(nxml-mode ("\\(<[^/>]*>\\)$" 1)
- "</[^/>]*>$"))
+(add-to-list 'hs-special-modes-alist '(nxml-mode ("\\(<[^/>]*>\\)$" 1) "</[^/>]*>$"))
+
(defun nxml-enable-hs ()
(setq nxml-sexp-element-flag t)
(hs-minor-mode 1))
(defun hs-nxml-enter ()
(interactive)
(when (hs-already-hidden-p)
- (hs-show-block)
- (hs-hide-level 1)
- (nxml-forward-element)
- (nxml-backward-element)))
+ (hs-show-block)
+ (hs-hide-level 1)
+ (nxml-forward-element)
+ (nxml-backward-element)))
(defun hs-nxml-leave ()
(interactive)
(define-key nxml-mode-map (kbd "\C-c\C-c") 'recompile)
(defconst nxml-docbook-common-elements
- '(("section" . ("para" "itemizedlist" "variablelist" "section" "bridgehead" "task" "procedure" "title"))
- ("para" . ("emphasis" "code" "replaceable"))
+ '(("section" . ("para" "itemizedlist" "variablelist" "section" "bridgehead" "task" "procedure"
+ "title"))
+ ("para" . ("emphasis" "code" "replaceable" "literal"))
("emphasis" . ("code"))
("itemizedlist" . ("listitem"))
("orderedlist" . ("listitem"))
("variablelist" . ("varlistentry"))
("varlistentry" . ("term" "listitem"))
- ("term" . ("emphasis" "code"))
+ ("term" . ("emphasis" "literal" "replaceable" "option"))
("listitem" . ("para" "itemizedlist"))
("task" . ("tasksummary" "procedure" "title"))
- ("tasksummary" . ("para"))
+ ("tasksummary" . ("para" "itemizedlist" "variablelist"))
("procedure" . ("step"))
("step" . ("para" "procedure"))
("mathphrase" . ("replaceable" "superscript" "subscript"))
(save-excursion
(nxml-backward-up-element)
(let ((tag (xmltok-start-tag-qname)))
- (list (save-excursion
- (skip-chars-forward "^>")
- (forward-char 1)
+ (list (save-excursion
+ (skip-chars-forward "^>")
+ (forward-char 1)
(point))
(progn
(nxml-forward-balanced-item)
(let ((start (set-marker (make-marker) (or start (point))))
(end (set-marker (make-marker) (or end (point)))))
(when kill-tag
- (delete-region (save-excursion (goto-char start) (skip-chars-backward "^<") (1- (point))) start)
- (delete-region end (save-excursion (goto-char end) (skip-chars-forward "^>") (1+ (point)))))
+ (delete-region (save-excursion
+ (goto-char start) (skip-chars-backward "^<") (1- (point))) start)
+ (delete-region end (save-excursion
+ (goto-char end) (skip-chars-forward "^>") (1+ (point)))))
(save-excursion
(goto-char start)
(let* ((token-end (nxml-token-before))
(context (xmltok-start-tag-qname))
(common-elements (cdr (assoc context nxml-docbook-common-elements)))
(valid-elements (or valid
- (let ((lt-pos (point)))
- (rng-set-state-after lt-pos)
- (loop for (ns . name) in (rng-match-possible-start-tag-names)
+ (let ((lt-pos (point)))
+ (rng-set-state-after lt-pos)
+ (loop for (ns . name) in (rng-match-possible-start-tag-names)
if (not (member name elements)) collect name into elements
finally return elements))))
(elements (loop for element in common-elements
finally return 0))))
0))
(element (and elements (nth index elements))))
- (message "!elements %s %s %s" common-elements valid-elements elements)
(when (not elements)
(error "No common elements for %s" context))
(if element
(defun nxml-open-line ()
(interactive)
- (open-line 1)
+ (if (region-active-p)
+ (let ((start (region-beginning))
+ (end (region-end))
+ chars)
+ (save-excursion
+ (goto-char end)
+ (newline-and-indent)
+ (goto-char start)
+ (setq chars (- (- (point) (progn (newline-and-indent) (point)))))
+ (indent-region (+ start chars) (+ end chars))))
+ (open-line 1)
+ (save-excursion
+ (forward-line 1)
+ (indent-according-to-mode))
+ (newline-and-indent)))
+
+(defun nxml-split-element ()
+ (interactive)
+ (let (element block-p)
+ (save-excursion
+ (nxml-backward-up-element)
+ (setq element (xmltok-start-tag-qname)
+ block-p (looking-back "^\s-*" (save-excursion (beginning-of-line) (point)))))
+ (delete-horizontal-space)
+ (insert "</" element ">")
+ (fill-paragraph)
+ (insert "\n")
+ (newline-and-indent)
+ (insert "<" element ">")
+ (fill-paragraph)))
+
+(defun my-nxml-reformat-region (begin end)
+ (interactive "r")
(save-excursion
- (forward-line 1)
- (indent-according-to-mode))
- (newline-and-indent))
+ (nxml-mode)
+ (goto-char begin)
+ (while (search-forward-regexp "\>[ \\t]*\<" nil t)
+ (backward-char) (insert "\n"))
+ (indent-region begin end nil)))
(define-key nxml-mode-map (kbd "M-o") 'nxml-open-line)
+(define-key nxml-mode-map (kbd "S-<return>") 'nxml-split-element)