简体   繁体   English

将字符串转换为首字母大写-Emacs Lisp

[英]Convert string to title case - Emacs Lisp

I am looking for an elisp function that accepts a string and returns the same in title case (ie, all words capitalized, except for "a", "an", "on", "the", etc.). 我正在寻找一个elisp函数,该函数接受一个字符串并以标题大小写返回相同的字符串(即,所有单词都大写,除了“ a”,“ an”,“ on”,“ the”等)。

I found this script , which requires a marked region. 我找到了这个脚本 ,它需要一个标记区域。

Only, I need a function that accepts a string variable, so I can use it with replace-regex . 只是,我需要一个接受字符串变量的函数,因此可以将其与replace-regex I would love to see a version of the above script that can accept either or... 我希望看到上述脚本的版本可以接受或...

Something like this? 像这样吗

(progn
  (defun title-case (input) ""
         (let* (
                (words (split-string input))
                (first (pop words))
                (last (car(last words)))
                (do-not-capitalize '("the" "of" "from" "and" "yet"))) ; etc
           (concat (capitalize first)
                   " "
                   (mapconcat (lambda (w)
                                (if (not(member (downcase w) do-not-capitalize))
                                    (capitalize w)(downcase w)))
                              (butlast words) " ")
                   " " (capitalize last))))
  (title-case "the presentation of this HEADING OF my own from my keyboard and yet\n"))

I'd say that the script you linked to does a good job at title casing. 我想说,您链接的脚本在标题框方面做得很好。 You can use it as-is. 您可以按原样使用它。

That leaves us with two more questions: 这给我们留下了两个问题:

  • How can we make it accept a string? 我们如何使其接受字符串?
  • How can we write a function which accepts both a string or a (marked) region? 我们如何编写一个既可以接受字符串也可以接受(标记)区域的函数?

Working with strings in Emacs is idiomatically done in temporary buffers which are not displayed. 通常在未显示的临时缓冲区中使用Emacs中的字符串进行处理。 You could write a wrapper like this: 您可以这样编写包装器:

(defun title-capitalization-string (s)
  (with-temp-buffer
    (erase-buffer)
    (insert s)
    (title-capitalization (point-min)
                          (point-max))
    (buffer-substring-no-properties (point-min)
                                    (point-max))))

Now, for a function which magically does what you mean, consider something like this: 现在,对于一个神奇地执行您想要的功能的函数,请考虑以下内容:

(defun title-capitalization-dwim (&optional arg)
  (interactive)
  (cond
   (arg
    (title-capitalization-string arg))
   ((use-region-p)
    (title-capitalization-string
     (buffer-substring-no-properties (region-beginning)
                                     (region-end))))
   (t
    (title-capitalization-string
     (buffer-substring-no-properties (point-at-bol)
                                     (point-at-eol))))))

It accepts an optional argument, or an active region or falls back to the text on the current line. 它接受可选参数或活动区域,或退回到当前行上的文本。 Note that this function is not really useful when used interactively, because it doesn't show any effects. 请注意,此功能在交互使用时并没有真正有用,因为它不会显示任何效果。 Hat tip also to https://www.emacswiki.org/emacs/titlecase.el 帽子提示也可访问https://www.emacswiki.org/emacs/titlecase.el

License 执照

I put all this code under the Apache License 2.0 and the GPL 2.0 (or later at your option) in addition to the site's default license. 除了站点的默认许可证之外,我还将所有这些代码置于Apache License 2.0和GPL 2.0(或更高版本)下。

Use Mx 使用Mx

upcase-initials-region is an interactive built-in function in 'C source code'. upcase-initials-region是'C源代码'中的一个交互式内置函数。

( upcase-initials-region BEG END ) upcase-initials-region BEG END

Upcase the initial of each word in the region. 大写该区域中每个单词的首字母。 This means that each word's first character is converted to either title case or upper case, and the rest are left unchanged. 这意味着每个单词的第一个字符都将转换为标题大写或大写,其余部分则保持不变。 In programs, give two arguments, the starting and ending character positions to operate on. 在程序中,提供两个参数,即要操作的开始和结束字符位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM