Skip to content

Commit

Permalink
Add commands for moving current chord forward and back in text.
Browse files Browse the repository at this point in the history
  • Loading branch information
hading committed Mar 16, 2014
1 parent c19565c commit 8cf2c13
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ All of the keyboard commands use the Ctrl-c prefix.
list of chords already in the document.
* Ctrl-c c : Copy the current chord
* Ctrl-c x : Copy the next chord
* Ctrl-Meta-n : Move current chord forward PREFIX chars.
* Ctrl-Meta-p : Move current chord backward PREFIX chars
* Ctrl-c h : Insert a chordpro comment
* Ctrl-c h : Insert a chordpro chorus
* Ctrl-c t : Insert a chordpro title
Expand Down
46 changes: 42 additions & 4 deletions chordpro-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Special commands:
(define-key chordpro-mode-map "\C-cs" 'chordpro-insert-subtitle)
(define-key chordpro-mode-map "\C-cl" 'chordpro-choose-insert-chord)
(define-key chordpro-mode-map "\C-cr" 'chordpro-choose-replace-current-chord)
(define-key chordpro-mode-map "\C-\M-n" 'chordpro-current-chord-forward)
(define-key chordpro-mode-map "\C-\M-p" 'chordpro-current-chord-backward)
(define-key chordpro-mode-map [C-down-mouse-1] 'mouse-set-point)
(define-key chordpro-mode-map [C-mouse-1] 'chordpro-kill-current-chord)
(define-key chordpro-mode-map [C-down-mouse-2] 'mouse-set-point)
Expand Down Expand Up @@ -120,19 +122,19 @@ already in the document."
(defun chordpro-kill-current-chord ()
"Kill the chord surrounding the point, if there is one."
(interactive)
(operate-on-current-chord 'kill-region))
(chordpro-operate-on-current-chord 'kill-region))

(defun chordpro-delete-current-chord ()
"Delete the chord surrounding the point, if there is one."
(interactive)
(operate-on-current-chord 'delete-region))
(chordpro-operate-on-current-chord 'delete-region))

(defun chordpro-copy-current-chord ()
"Copy the chord surrounding the point, if there is one."
(interactive)
(operate-on-current-chord 'copy-region-as-kill))
(chordpro-operate-on-current-chord 'copy-region-as-kill))

(defun operate-on-current-chord (function)
(defun chordpro-operate-on-current-chord (function)
"Call a two argument function on the current chord, if it exists, with
the start and end of the chord."
(let ((current-position (point-marker)))
Expand All @@ -147,6 +149,42 @@ the start and end of the chord."
(< current-position end))
(funcall function start end))))))))))

(defun chordpro-current-chord-forward (n)
"Move the current chord forward n characters."
(interactive "*p")
(let ((current-position (point-marker))
(chord-offset (chordpro-position-in-current-chord)))
(set-marker-insertion-type current-position t)
(chordpro-operate-on-current-chord
(lambda (start end)
(kill-region start end)
(forward-char n)
(yank)))
;;I have to assume there's a better way to do this, but this works
;;Get back in the chord and then move to the offset
(if (> n 0)
(goto-char (+ current-position n 1))
(goto-char (- current-position (+ n 4))))
(chordpro-move-to-position-in-current-chord chord-offset)))

(defun chordpro-current-chord-backward (n)
"Move the current chord backward n characters."
(interactive "*p")
(chordpro-current-chord-forward (- n)))

(defun chordpro-move-to-position-in-current-chord (n)
"Move to the nth character in the current chord."
(search-backward "[")
(forward-char n))

(defun chordpro-position-in-current-chord ()
"Determine the offset inside the current chord."
(interactive)
(let ((current-position (point)))
(save-excursion
(search-backward "[")
(- current-position (point)))))

(defun chordpro-insert-single-directive (text)
(insert "{" text ": }\n")
(search-backward "}"))
Expand Down

0 comments on commit 8cf2c13

Please sign in to comment.