[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Preliminary: tool for poetry writing
Preliminary: tool for poetry writing
Wed, 11 Feb 2015 19:59:45 -1000
Gnus/5.130012 (Ma Gnus v0.12) Emacs/24.3 (gnu/linux)
This is a quick set of hacks. I've gotten interested in sonnet
writing. Here is a hack of linum to show not line numbers but syllables
per line, and also an interface to the 'rhyme' package to get possible
rhymes for a word. The syllable counter is taken from one of the emacs
auxiliary packages --- the one that does a readability index. So there's
not a lot original here.
I was inspired by the Windoze program "Verse Perfect." This isn't as
good but it isn't so bad, either. At least it's a start. Comments
;; begin poetry.el
;; Install the 'rhyme' package. There could be dependency
;; issues for the binary. The source is here as of 2015-02-10:
;; Building it may require some header packages. You know what
;; to do.
(defun poetry-syllables-in-line ()
"Count syllables in current line and return"
(poetry-count-syllables (line-beginning-position) (line-end-position)))
;; The core of this is borrowed from a readability index program
;; found in some emacs utilities.
(defun poetry-count-syllables (begin end)
"Count the syllables per line in a range"
(let ((letter-regexp "[A-Za-z]")
(let ((count 0))
(while (< (point) end)
(while (and (< (point) end)
(not (looking-at letter-regexp)))
(let ((state (if (looking-at vowel-regexp) 2 1)))
(when (= state 2)
(setq count (1+ count)))
(while (looking-at letter-regexp)
(if (and (= state 1)
(not (looking-at e-end-regexp)))
(setq state 2
count (1+ count))
(if (and (= state 2)
(not (looking-at vowel-regexp)))
(setq state 1)))
(defun poetry-find-rhyme (word)
"Simple interface to rhyme package"
(interactive "sWord: ")
(replace-regexp-in-string "\r?\n$" " "
(shell-command-to-string (concat "rhyme " word))))
(replace-regexp-in-string "Finding perfect rhymes for " "" poetry-rhyme))
;; Not enough display space?
(defun poetry-rhyme-word ()
"Rhymes for word at point"
(poetry-find-rhyme (thing-at-point 'word))
;; end poetry.el
;; begin poemnum.el
;;; poemnum.el --- Display line syllable counts to the left of buffers
;; Based on linum.el with just a few changes.
;; Requires poetry.el.
;; Copyright (C) 2007, 2008 Markus Triska
;; Additions/changes by Bob Newell are public domain.
;; Author: Markus Triska <address@hidden>
;; Keywords: convenience
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation; either version 3, or (at your
;; option) any later version.
;; 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. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;; Display line numbers for the current buffer. Copy poemnum.el to
;; your load-path and add to your .emacs:
;; (require 'poemnum)
;; (require 'poetry)
;; Then toggle display of syllable counts with M-x poemnum-mode. To
;; enable in all buffers, use M-x global-poemnum-mode, but why would
(defconst poemnum-version "0.001")
(defvar poemnum-overlays nil "Overlays used in this buffer.")
(defvar poemnum-available nil "Overlays available for reuse.")
(defvar poemnum-before-numbering-hook nil
"Functions run in each buffer before line numbering starts.")
(defgroup poemnum nil
"Show line numbers to the left of buffers"
(defcustom poemnum-format 'dynamic
"Format used to display line numbers. Either a format string
like \"%7d\", 'dynamic to adapt the width as needed, or a
function that is called with a line number as its argument and
should evaluate to a string to be shown on that line. See also
'((t :inherit (shadow default)))
"Face for displaying line numbers in the display margin."
(defcustom poemnum-eager t
"Whether line numbers should be updated after each command.
The conservative setting `nil' might miss some buffer changes,
and you have to scroll or press C-l to update the numbers."
(defcustom poemnum-delay nil
"Delay updates to give Emacs a chance for other changes."
"Toggle display of line numbers in the left marginal area."
:lighter "" ; for desktop.el
(add-hook 'post-command-hook (if poemnum-delay
'poemnum-update-current) nil t)
(add-hook 'after-change-functions 'poemnum-after-change nil t))
(add-hook 'window-scroll-functions 'poemnum-after-scroll nil t)
;; mistake in Emacs: window-size-change-functions cannot be local
(add-hook 'window-size-change-functions 'poemnum-after-size)
(add-hook 'change-major-mode-hook 'poemnum-delete-overlays nil t)
'poemnum-after-config nil t)
(remove-hook 'post-command-hook 'poemnum-update-current t)
(remove-hook 'post-command-hook 'poemnum-schedule t)
(remove-hook 'window-size-change-functions 'poemnum-after-size)
(remove-hook 'window-scroll-functions 'poemnum-after-scroll t)
(remove-hook 'after-change-functions 'poemnum-after-change t)
(remove-hook 'window-configuration-change-hook 'poemnum-after-config t)
(remove-hook 'change-major-mode-hook 'poemnum-delete-overlays t)
(define-globalized-minor-mode global-poemnum-mode poemnum-mode poemnum-on)
(defun poemnum-on ()
(defun poemnum-delete-overlays ()
"Delete all overlays displaying line numbers for this buffer."
(mapc #'delete-overlay poemnum-overlays)
(setq poemnum-overlays nil)
(dolist (w (get-buffer-window-list (current-buffer) nil t))
(set-window-margins w 0)))
(defun poemnum-update-current ()
"Update line numbers for the current buffer."
(defun poemnum-update (buffer)
"Update line numbers for all windows displaying BUFFER."
(setq poemnum-available poemnum-overlays)
(setq poemnum-overlays nil)
(get-buffer-window-list buffer nil 'visible)))
(mapc #'delete-overlay poemnum-available)
(setq poemnum-available nil))))
(defun poemnum-update-window (win)
"Update line numbers for the portion visible in window WIN."
(goto-char (window-start win))
; (let ((line (line-number-at-pos))
(let ((line (poetry-syllables-in-line))
(limit (window-end win t))
(fmt (cond ((stringp poemnum-format) poemnum-format)
((eq poemnum-format 'dynamic)
(let ((w (length (number-to-string
(count-lines (point-min) (point-max))))))
;; This dynamic formatting could be undone, I think, but it does no harm.
(concat "%" (number-to-string w) "d")))))
;; Create an overlay (or reuse an existing one) for each
;; line visible in this window, if necessary.
(while (and (not (eobp)) (<= (point) limit))
(let* ((str (if fmt
(propertize (format fmt line) 'face 'poemnum)
(funcall poemnum-format line)))
(visited (catch 'visited
(dolist (o (overlays-in (point) (point)))
(when (string= (overlay-get o 'poemnum-str) str)
(unless (memq o poemnum-overlays)
(push o poemnum-overlays))
(setq poemnum-available (delete o poemnum-available))
(throw 'visited t))))))
(setq width (max width (length str)))
(let ((ov (if (null poemnum-available)
(make-overlay (point) (point))
(move-overlay (pop poemnum-available) (point) (point)))))
(push ov poemnum-overlays)
(overlay-put ov 'before-string
(propertize " " 'display `((margin left-margin) ,str)))
(overlay-put ov 'poemnum-str str))))
(setq line (poetry-syllables-in-line)))
(set-window-margins win width)))
(defun poemnum-after-change (beg end len)
;; update overlays on deletions, and after newlines are inserted
(when (or (= beg end)
(= end (point-max))
;; TODO: use string-match-p with CVS or new release
(string-match "\n" (buffer-substring-no-properties beg end)))
(defun poemnum-after-scroll (win start)
(poemnum-update (window-buffer win)))
(defun poemnum-after-size (frame)
(defun poemnum-schedule ()
;; schedule an update; the delay gives Emacs a chance for display changes
(run-with-idle-timer 0 nil #'poemnum-update-current))
(defun poemnum-after-config ()
(walk-windows (lambda (w) (poemnum-update (window-buffer w))) nil 'visible))
;;; end poemnum.el
* Sent via Ma Gnus 0.12-Emacs 24.3-Linux Mint 17 *
- Preliminary: tool for poetry writing,
Bob Newell <=