emacs-devel
[Top][All Lists]
Advanced

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

Re: Proper English Title Capitalization


From: Barry Fishman
Subject: Re: Proper English Title Capitalization
Date: Tue, 26 May 2015 11:38:45 -0400
User-agent: Gnus/5.130014 (Ma Gnus v0.14) Emacs/25.0.50 (gnu/linux)

On 2015-05-25 12:47:51 +0200, Karl Voit wrote:
> * Karl Voit <address@hidden> wrote:
>>
>> My Elisp knowledge is sufficient to modify your code so that it
>> should get the job done:
>
> I have to post a corrected version of the code which also handles
> the edge case "a a a a a" (a do-not-capitalize word as second word)
> properly:
>
> ,----
> | (defun my-title-capitalization (beg end)
> |   (interactive "r")
> |   (save-excursion
> |     (let (
> |       (do-not-capitalize '("a" "ago" "an" "and" "as" "at" "but" "by" "for"
> |                    "from" "in" "into" "it" "next" "nor" "of" "off"
> |                    "on" "onto" "or" "over" "past" "so" "the" "till"
> |                    "to" "up" "yet" ))
> |       )
> |       ;; go to begin of first word:
> |       (goto-char beg)
> |       (forward-word)
> |       (backward-word)
> |       ;; capitalize first word in any case:
> |       (capitalize-word 1)
> |       (forward-word)
> |       (backward-word)
> |       (while (< (point) end)
> |     ;; capitalize each word in between except it is list member:
> |     (if (member (thing-at-point 'word t) do-not-capitalize)
> |         (forward-word)
> |       (capitalize-word 1) )
> |     (forward-word)
> |     (backward-word) )
> |       ;; capitalize last word in any case:
> |       (backward-word)
> |       (capitalize-word 1)
> |       )
> |     ))
> `----

If you are looking for more alternative coding:

--8<---------------cut here---------------start------------->8---
;; This is probably subject for debate and tuning.
(defvar own-stop-words '("a" "an" "the" "at" "by" "for" "in"
                         "of" "on" "to" "up" "and" "as" "but"
                         "it" "or" "nor" "n" "t" "es" "s")
  "Words that are not capitalized in titles.")

(defun own-title-region (beg end)
  "Capitalize region as a title,"
  (interactive "r")
  (save-excursion
    (goto-char beg)
    (capitalize-word 1)
    (while (< (point) end)
      (skip-syntax-forward "^w" end)
      (let ((word (thing-at-point 'word t)))
        (if (stringp word)
            (if (member (downcase word) own-stop-words)
                (downcase-word 1)
              (capitalize-word 1)))))
    (backward-word 1)
    (if (and (>= (point) beg)
             (not (member (or (thing-at-point 'word t) "s")
                          '("n" "t" "es" "s"))))
        (capitalize-word 1))))
--8<---------------cut here---------------end--------------->8---

--
Barry Fishman




reply via email to

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