实现像format-time-string这样的功能的最简单方法是什么


12

format-time-string 函数接受一个字符串,并%用一些特定的文本替换该字符串中的一组特殊构造(以开头的字符)。

我想在自己的函数中实现类似的功能:

  • 我在字符和符号之间有一个清单,例如:'((?n . name) (?r . reputation))
  • 该函数应采用类似的字符串"My name is %n, and my rep is %r"
  • 它应将%n和替换%r为变量name和的值reputation,然后返回结果。
  • 它应该%%像一样处理format-time-string(用代替%)。

实现此功能的最简单方法是什么?
是否有一个库或函数可以简化此过程?%%正确处理很重要。

Answers:


19

要使用的默认功能是format-specformat-spec-make

(let* ((name "Malabarba")
       (reputation "good")
       (fs (format-spec-make ?n name ?r reputation)))
  (format-spec "My name is %n, with a %r reputation.  %% is kept." fs))

1
很好,没想到Emacs中已经存在这样的东西。尤其有用的是,其中包括对无效格式代码的基本错误处理。
wasamasa 2015年

7

由于elisp具有处理字符串的原始工具,因此最好将格式字符串放入缓冲区并对其进行迭代:

(defvar malabarba-alist '((?n . "Malabarba") (?r . 7488) (?% . "%")))

(defun malabarba-format (string)
  (with-temp-buffer
    (insert string)
    (goto-char 1)
    (while (search-forward "%" nil t)
      (let ((s (cdr (assoc (char-after) malabarba-alist))))
        (cond
          (s (delete-char -1) (delete-char 1) (insert (format "%s" (eval s))))
          (t (unless (eobp) (forward-char))))))
    (buffer-string)))

细微的细微差别- %在字符串的末尾有一个孤行时,这不会中断,因为在缓冲区的末尾会char-after返回nil


是的,这种方法,一次遍历字符串,也被@AlanShutko使用,更有意义。我已删除答案。
提请

3

这是我用于伪造日历的格式时间字符串的代码:

(defun mystcal-format-time (format-string time)
  "Format time
%Y is the year.
%m is the numeric month.
%B is the full name of the month.
%d is the day of the month, zero-padded, %e is blank-padded.
%u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.
%A is the locale's full name of the day of week.
%H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H
 only blank-padded, %l is like %I blank-padded.
%p is the locale's equivalent of either AM or PM.
%M is the minute.
%S is the second."
  (let* (output
         (start 0)
         (decoded-time (if (listp time)
                           time
                         (mystcal-decode-time time)))
         (turn (nth 0 decoded-time))
         (minute (nth 1 decoded-time))
         (hour (nth 2 decoded-time))
         (day (nth 3 decoded-time))
         (month (mystcal-month decoded-time))
         (year (nth 5 decoded-time))
         (weekday (mystcal-weekday-of-day day))
         (hour12 (mod hour 12)))
    (save-match-data
      (while (string-match "%" format-string start)
        (let ((index (match-beginning 0)))
          ;; First copy non-format text
          (setq output (concat output (substring format-string start 
                                                 index)))
          ;; Process format codes here
          (let (fmted)
            (setq output (concat output 
                                 (case (aref format-string (1+ index))
                                   (?Y (number-to-string year))
                                   (?m (number-to-string month))
                                   (?B (mystcal-month-name month))
                                   (?d (format "%02d" day))
                                   (?e (format "%2d" day))
                                   (?u (number-to-string (if (zerop weekday)
                                                             7
                                                           weekday)))
                                   (?w (number-to-string weekday))
                                   (?A (mystcal-weekday-name 
                                        (mystcal-weekday-of-day day)))
                                   (?H (format "%02d" hour))
                                   (?k (format "%2d" hour))
                                   (?I (format "%02d" (if (zerop hour12) 12 hour12)))
                                   (?l (format "%2d" (if (zerop hour12) 12 hour12)))
                                   (?p (if (< hour 12)
                                           "AM" "PM"))
                                   (?M (format "%02d" minute))
                                   (?S "00")))))
          (setq start (+ 2 index)))))
    (setq output (concat output (substring format-string start)))
    output))
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.