问题
I'm trying to write a function that will let me choose (interactively) the month and year for org-agenda-month-view
-- which uses a format of 201312
(without double-quotes) for December 2013 . I would like to use either words (December
) or numbers (12
) for the month, but need some assistance (please) to decode the name of the month into an integer -- something like (if (not (integer-p month)) (setq month (decode-name-of-month month)))
.
Also, any pointers on how to consolidate my two read-string
let
bindings into just one action would be greatly appreciated -- i.e., be able to manually input January 2014
or 01 2014
; instead of first inputting January
and then inputting 2014
in two separate actions.
(let* (
(year (read-string "Insert the year: "))
(month (read-string "Insert the month: ")))
(org-agenda nil "1")
(org-agenda-month-view (read (format "%s%02s" year month))))
EDIT (December 11, 2013): First working draft based upon the helpful answers by @Drew and @Sean Perry. Added variable org-agenda-start-day
, which permits (org-agenda nil "1")
to display [at the outset] the correct month based upon the user input -- the function org-agenda-month-view
is no longer needed. Updated variable lawlist-parse-time-months
to fix a typo and include additional possibilities -- e.g., 01, 02, etc.
(defvar lawlist-parse-time-months '(
("1" . "01")
("01" . "01")
("jan" . "01")
("january" . "01")
("2" . "02")
("02" . "02")
("feb" . "02")
("february". "02")
("3" . "03")
("03" . "03")
("mar" . "03")
("march" . "03")
("4" . "04")
("04" . "04")
("apr" . "04")
("april" . "04")
("5" . "05")
("05" . "05")
("may" . "05")
("6" . "06")
("06" . "06")
("jun" . "06")
("june" . "06")
("7" . "07")
("07" . "07")
("jul" . "07")
("july" . "07")
("8" . "08")
("08" . "08")
("aug" . "08")
("august" . "08")
("9" . "09")
("09" . "09")
("sep" . "09")
("september" . "09")
("10" . "10")
("oct" . "10")
("october" . "10")
("11" . "11")
("nov" . "11")
("november" . "11")
("12" . "12")
("dec" . "12")
("december" . "12")))
(defun encode-name-of-month (month_name)
(cdr (assoc (downcase month_name) lawlist-parse-time-months)) )
(defun decode-name-of-month (month)
(car (rassoc month lawlist-parse-time-months)))
(defun foo (month year)
(interactive
(let* ((mon+yr (split-string
(read-string "Month and year: " nil nil
(format-time-string "%B %Y"(current-time)))
nil 'TRIM))
(mon (car mon+yr))
(yr (cadr mon+yr))
(m2 (condition-case nil (format "%d" mon) (error nil))))
(list (or m2 mon) yr)))
(setq org-agenda-start-day (concat year "-" (encode-name-of-month month) "-" "01"))
(org-agenda nil "1") )
回答1:
(require 'parse-time) ;; in standard lib
(defun encode-name-of-month (month_name)
(cdr (assoc (downcase month_name) parse-time-months))
)
(defun decode-name-of-month (month)
(car (rassoc month parse-time-months))
)
That should get you started.
回答2:
(defun foo (month year)
(interactive
(let* ((mon+yr (split-string
(read-string "Month and year: " nil nil
(format-time-string "%B %Y"(current-time)))
nil 'TRIM))
(mon (car mon+yr))
(yr (cadr mon+yr))
(m2 (condition-case nil (format "%d" mon) (error nil))))
(list (or m2 mon) yr)))
(message "Month: %s, Year: %s" month year))
M-x foo 12 2013 ==> Month: 12, Year: 2013
M-x foo December 2013 ==> Month: December, Year: 2013
M-x foo Dec 2013 ==> Month: Dec, Year: 2013
M-x foo Nebraska 200000 ==> Month: Nebraska, Year: 200000
But you get the idea. (Check for valid month names and month & year numbers.)
来源:https://stackoverflow.com/questions/20504673/how-to-decode-name-of-month-into-an-integer