gnu-emacs-sources
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Preliminary: tool for poetry writing


From: Bob Newell
Subject: Preliminary: tool for poetry writing
Date: Wed, 11 Feb 2015 19:59:45 -1000
User-agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/24.3 (gnu/linux)

Aloha kakou,

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
welcome.


===
;; begin poetry.el
;; Install the 'rhyme' package. There could be dependency
;; issues for the binary. The source is here as of 2015-02-10:
;;  https://launchpad.net/ubuntu/+source/rhyme/0.9-5
;; Building it may require some header packages. You know what
;; to do.

 (defvar poetry-count)
 (defun poetry-syllables-in-line ()
 "Count syllables in current line and return"
  (interactive)
  (setq poetry-count
    (poetry-count-syllables (line-beginning-position) (line-end-position)))
  poetry-count
)

;; 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"
 (interactive)
   (let ((letter-regexp "[A-Za-z]")
         (vowel-regexp "[AEIOUYaeiouy]")
         (e-end-regexp "[Ee]\\W"))
     (save-excursion
       (let ((count 0))
         (goto-char begin)
         (while (< (point) end)
           (while (and (< (point) end)
                       (not (looking-at letter-regexp)))
             (forward-char 1))
           (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)
                        (looking-at vowel-regexp)
                        (not (looking-at e-end-regexp)))
                   (setq state 2
                         count (1+ count))
                 (if (and (= state 2)
                          (not (looking-at vowel-regexp)))
                     (setq state 1)))
               (forward-char 1))))
         count)))
)

(defvar poetry-rhyme)
(defun poetry-find-rhyme (word)
"Simple interface to rhyme package"
(interactive "sWord: ")
(setq poetry-rhyme 
  (replace-regexp-in-string "\r?\n$" " "
    (shell-command-to-string (concat "rhyme " word))))
(setq poetry-rhyme 
    (replace-regexp-in-string "Finding perfect rhymes for " "" poetry-rhyme))
;; Not enough display space?
(message poetry-rhyme)
)

(defun poetry-rhyme-word ()
"Rhymes for word at point"
 (interactive)
 (poetry-find-rhyme (thing-at-point 'word))
)

(provide 'poetry)
;; 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.

;;; Commentary:

;; 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
;; you?

;;; Code:

(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.")

(mapc #'make-variable-buffer-local 
 '(poemnum-overlays poemnum-available))

(defgroup poemnum nil
  "Show line numbers to the left of buffers"
  :group 'convenience)

;;;###autoload
(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
`poemnum-before-numbering-hook'."
  :group 'poemnum
  :type 'sexp)

(defface poemnum
  '((t :inherit (shadow default)))
  "Face for displaying line numbers in the display margin."
  :group 'poemnum)

(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."
  :group 'poemnum
  :type 'boolean)

(defcustom poemnum-delay nil
  "Delay updates to give Emacs a chance for other changes."
  :group 'poemnum
  :type 'boolean)

;;;###autoload
(define-minor-mode poemnum-mode
  "Toggle display of line numbers in the left marginal area."
  :lighter ""                           ; for desktop.el
  (if poemnum-mode
      (progn
        (if poemnum-eager
            (add-hook 'post-command-hook (if poemnum-delay
                                             'poemnum-schedule
                                           '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)
        (add-hook 'window-configuration-change-hook
                  'poemnum-after-config nil t)
        (poemnum-update-current))
    (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)
    (poemnum-delete-overlays)))

;;;###autoload
(define-globalized-minor-mode global-poemnum-mode poemnum-mode poemnum-on)

(defun poemnum-on ()
  (unless (minibufferp)
    (poemnum-mode 1)))

(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."
  (poemnum-update (current-buffer)))

(defun poemnum-update (buffer)
  "Update line numbers for all windows displaying BUFFER."
  (with-current-buffer buffer
    (when poemnum-mode
      (setq poemnum-available poemnum-overlays)
      (setq poemnum-overlays nil)
      (save-excursion
        (mapc #'poemnum-update-window
              (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")))))
        (width 0))
    (run-hooks 'poemnum-before-numbering-hook)
    ;; 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)))
        (unless visited
          (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))))
      (forward-line)
      (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)))
    (poemnum-update-current)))

(defun poemnum-after-scroll (win start)
  (poemnum-update (window-buffer win)))

(defun poemnum-after-size (frame)
  (poemnum-after-config))

(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))

(provide 'poemnum)
;;; end poemnum.el
===

-- 
Bob Newell
Honolulu, Hawai`i
* Sent via Ma Gnus 0.12-Emacs 24.3-Linux Mint 17 *


reply via email to

[Prev in Thread] Current Thread [Next in Thread]