dotemacs/contrapunctus/cp-lily.el
2016-08-04 19:59:56 +05:30

131 lines
3.6 KiB
EmacsLisp

;;;; Hacks for Lilypond work
(with-eval-after-load 'lilypond-mode
(add-hook 'LilyPond-mode-hook 'subword-mode)
(add-hook 'LilyPond-mode-hook 'god-local-mode)
(cp-set-keys
:bindings
`((,(kbd "C-c C-i") 'LilyPond-info)
(,(kbd "M-]") 'cp-toggle-selective-display)))
(cp-set-keys
:keymap LilyPond-mode-map
:bindings
`((,(kbd "M-p") cp-backward-def)
(,(kbd "M-n") cp-forward-def)
(,(kbd "M-P") cp-upper-level)
(,(kbd "M-N") cp-lower-level)
(,(kbd "C-c C-w") cp-ly-wrap-para))))
(defun cp-toggle-selective-display ()
(interactive)
(set-selective-display (if selective-display nil 1)))
(defun cp-backward-def ()
(interactive)
(re-search-backward "^[\\a-zA-Z]")
(beginning-of-line))
(defun cp-forward-def ()
(interactive)
(forward-char)
(re-search-forward "^[\\a-zA-Z]")
(beginning-of-line))
;; (defun cp-backward-def ()
;; (interactive)
;; (re-search-backward "\(^\\\\?[a-zA-Z]\|^ *\\[a-zA-Z]\)")
;; (beginning-of-line))
;; (defun cp-forward-def ()
;; (interactive)
;; (forward-char)
;; (re-search-forward "\(^\\\\?[a-zA-Z]\|^ *\\[a-zA-Z]\)")
;; (beginning-of-line))
(defun cp-upper-level ()
(interactive)
(re-search-backward "{"))
(defun cp-lower-level ()
(interactive)
(if (equal (string (char-after)) "{")
(forward-char))
(if (not (re-search-forward "{"))
(message "At deepest level."))
(backward-char))
;; (defun cp-lilypond-enclose-<< ()
;; (interactive)
;; (if (equal (string (char-after)) "\\")
;; (progn (insert "<< ")
;; (search-forward "{")
;; (backward-char)
;; (forward-sexp))))
;; if at a \new ... block - enclose expression
;; otherwise, enclose current position and after the first bar check
;; found
;; if region is active, enclose beginning and end
;; (defun cp-lilypond-enclose-<< ()
;; (interactive)
;; (if (equal (thing-at-point 'sexp)
;; "\\new")
;; (progn ;; (insert "<< ")
;; (newline-and-indent)
;; (search-forward "{")
;; (backward-char)
;; (forward-list)
;; ;; (forward-sexp))
;; )
;; ;; (let ((point1 (point)))
;; ;; (next-line)
;; ;; (goto-char point1))
;; ))
;; (define-key LilyPond-mode-map (kbd "<<")
;; 'cp-lilypond-enclose-<<)
(defadvice LilyPond-save-buffer
(after lysb activate)
;; (compile "make")
(cd (locate-dominating-file (buffer-file-name) "main.ly"))
(compile (car compile-history)))
;; TODO - refactor into one COND, with one case per operation.
;; operate on region as well.
(defun cp-ly-wrap-para (arg)
"Wrap current paragraph with -
\\relative c { ... } with no args,
\\repeat { ... } with universal argument,
and only braces - { ... } - with null argument.
Numeric arg wraps that many paragraphs."
(interactive "P")
(let ((point-a (point)))
(beginning-of-line)
(or (looking-at "[[:blank:]]*$")
(backward-paragraph))
(newline-and-indent)
(insert (pcase arg
(`(,x) "\\repeat {")
(0 "{")
;; nil
(_ "\\relative c {")))
(let ((point-b (point)))
(forward-paragraph (pcase arg
(`(,x) 1)
(_ (if (and arg (<= arg 0))
1 arg))))
(indent-region point-b (point))
(insert "}")
(indent-for-tab-command)
(newline)
;; FIXME
(goto-char (pcase arg
(0 point-a)
(_ (- point-b 2)))))))
;; TODO - cp-ly-new-var, bind to M-RET.
;; Exits current variable body, if in any, and inserts "| = \relative
;; c {\n\n \n}", where | is the cursor