;; ;; Hide specific "sections" ;; 2011.11.19. Toshi Nagata ;; (defun LilyPond-hide-sections (str) (interactive "sSections to hide (like 1,2,3 or 1-3): ") (let (sections-to-hide last-section next-section last-pos next-pos has-brace) ;; Get a list of sections to hide (setq sections-to-hide (apply 'append (mapcar (lambda (s) (if (string-match "^\\([0-9]+\\)-\\([0-9]+\\)$" s) (let ((n1 (string-to-number (match-string 1 s))) (n2 (string-to-number (match-string 2 s)))) (if (<= n1 n2) (let ((ls nil)) (while (<= n1 n2) (setq ls (cons n2 ls)) (setq n2 (- n2 1))) ls) nil)) (list (string-to-number s)))) (split-string str ",")))) (save-excursion ;; Unhide all sections ;; }?%{ [n] ... % [m] %} -> % [n] ... % [m] (goto-char (point-min)) (while (re-search-forward "^\\(}?\\)%{ \\[[0-9]+\\]" nil t) (setq last-pos (match-beginning 0)) (setq has-brace (equal (match-string 1) "}")) (cond ((re-search-forward "^% \\[[0-9]+\\] %}" nil t) (delete-char -3) (beginning-of-line) (setq next-pos (- (point) 1)) (goto-char last-pos) (cond (has-brace (delete-char 1) (setq next-pos (- next-pos 1)))) (forward-char 1) (delete-char 1) (end-of-line) (forward-char 1) (uncomment-region (point) next-pos)))) ;; Hide specified sections (goto-char (point-min)) (setq last-section nil) (while (re-search-forward "^% \\[\\([0-9]+\\)\\]" nil t) (setq next-section (string-to-number (match-string 1))) (cond ((member next-section sections-to-hide) ;; This section is to be hidden (cond ((or (not last-section) (>= last-section next-section)) ;; The beginning section - record as last-section (setq last-pos (match-beginning 0)) (setq last-section next-section)) (t ;; Otherwise, just wait until the end section comes nil))) (t ;; This section is not to be hidden (cond ((and last-section (< last-section next-section)) ;; Hide last-section up to here (insert " %}") (beginning-of-line) (setq next-pos (+ (point) 1)) (goto-char last-pos) (forward-char 1) (insert "{") (end-of-line) (cond ((eq (preceding-char) ?\{) ;; % [n] .... {\n -> }%{ [n] .... {\n (beginning-of-line) (insert "}") (end-of-line) (setq next-pos (+ next-pos 1)))) (setq last-pos (+ (point) 1)) (comment-region last-pos next-pos) (end-of-line))) (setq last-section nil)))))))