;;; latex-sections.el --- section manipulations in LaTeX ;; Copyright (C) 2005 Matthieu MOY ;; Author: Matthieu MOY ;; Keywords: tex, outlines ;; This file is free software; you can redistribute it and/or modify ;; it with no restriction. ;; This file 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. ;;; Commentary: ;; ;;; Code: (require 'latex) (defun LaTeX-section-list-level (level &optional list) "List of sections with level lower or equal to LEVEL (a string). LIST is `LaTeX-section-list' by default, and must have the same format." (let ((list (or list LaTeX-section-list))) (if (string= level (caar list)) list (let ((rest (cdr list))) (if rest (LaTeX-section-list-level level rest) (error "\"%s\" is not a section name." level)))))) (defun LaTeX-outline-regexp-level (&optional anywhere level) "Return regexp for LaTeX sections with level lower than LEVEL. If optional argument ANYWHERE is not nil, do not require that the header is at the start of a line." (concat (if anywhere "" "^") "[ \t]*" (regexp-quote TeX-esc) "\\(appendix\\|documentstyle\\|documentclass\\|" (regexp-opt (mapcar 'car (LaTeX-section-list-level level))) "\\)\\b" (if TeX-outline-extra "\\|" "") (mapconcat 'car TeX-outline-extra "\\|") "\\|" TeX-header-end "\\|" TeX-trailer-start)) (defun LaTeX-section-list-level-higher (level &optional list) "List of sections with level higher or equal to LEVEL (a string). LIST is `LaTeX-section-list' by default, and must have the same format." (let ((list (or list LaTeX-section-list))) (if (string= level (caar list)) (list (car list)) (let ((rest (cdr list))) (if rest (cons (car list) (LaTeX-section-list-level-higher level rest)) (error "\"%s\" is not a section name." level)))))) (defun LaTeX-outline-regexp-level-higher (&optional anywhere level) "Return regexp for LaTeX sections with level higher than LEVEL. If optional argument ANYWHERE is not nil, do not require that the header is at the start of a line." (concat (if anywhere "" "^") "[ \t]*" (regexp-quote TeX-esc) "\\(appendix\\|documentstyle\\|documentclass\\|" (regexp-opt (mapcar 'car (LaTeX-section-list-level-higher level))) "\\)\\b" (if TeX-outline-extra "\\|" "") (mapconcat 'car TeX-outline-extra "\\|") "\\|" TeX-header-end "\\|" TeX-trailer-start)) (defun LaTeX-outline-next-level (level) "Return the string corresponding to the level below LEVEL." (let ((list LaTeX-section-list)) (while (and list (not (string= (caar list) level))) (setq list (cdr list))) (caadr list))) (defun LaTeX-outline-prev-level (level) "Return the string corresponding to the level above LEVEL. Return nil if LEVEL is the top level." (if (string= level (caar LaTeX-section-list)) nil (let ((list (cdr LaTeX-section-list)) (prev LaTeX-section-list)) (while (and list (not (string= (caar list) level))) (setq prev list) (setq list (cdr list))) (caar prev)))) (defun LaTeX-outline-find-section (point) "Find begining and end of current section. Return a pair (begin end) of points." (save-excursion (goto-char point) (end-of-line) (re-search-backward (concat "\\(" (LaTeX-outline-regexp) "\\|\\`\\)")) (let* ((begin (point)) (level (substring (match-string 1) 1))) (forward-line 1) (or (re-search-forward (LaTeX-outline-regexp-level-higher t level) nil t) (goto-char (point-max))) (forward-line -1) (end-of-line) (list begin (point))))) ;;;###autoload (defun LaTeX-outline-select-section (point) "Select entire section (and subsections) in a LaTeX document." (interactive "d") (let* ((pair (LaTeX-outline-find-section point)) (begin (car pair)) (end (cadr pair))) (goto-char begin) (set-mark end) (TeX-activate-region))) (defun LaTeX-outline-move (begin end nxt-function rev-function) (save-excursion (let* ((list (funcall rev-function LaTeX-section-list))) (save-restriction (narrow-to-region begin end) (while list (let* ((level (caar list)) (nxt-level (funcall nxt-function level))) (when nxt-level (goto-char (point-min)) (while (search-forward (concat "\\" level) nil t) (let ((begin (match-beginning 0)) (end (match-end 0))) (delete-region begin end) (goto-char begin) (insert "\\" nxt-level)))) (setq list (cdr list)))))))) ;;;###autoload (defun LaTeX-outline-move-right (begin end) (interactive "r") (LaTeX-outline-move begin end 'LaTeX-outline-next-level 'reverse)) ;;;###autoload (defun LaTeX-outline-move-left (begin end) (interactive "r") (LaTeX-outline-move begin end 'LaTeX-outline-prev-level 'identity)) ;;;###autoload (defun LaTeX-outline-move-current-right (point) (interactive "d") (let* ((pair (LaTeX-outline-find-section point)) (begin (car pair)) (end (cadr pair))) (LaTeX-outline-move-right begin end))) ;;;###autoload (defun LaTeX-outline-move-current-left (point) (interactive "d") (let* ((pair (LaTeX-outline-find-section point)) (begin (car pair)) (end (cadr pair))) (LaTeX-outline-move-left begin end))) (provide 'latex-sections) ;;; latex-sections.el ends here ; arch-tag: Matthieu MOY, Mon Apr 25 13:24:03 2005 (latex-sections.el)