import calendar
import datetime

import qmk

class CalendarCommand(qmk.Command):
    '''Display a calendar for a given month.  With no arguments, the
    current year and month are used.  If one argument is given, it is
    used as the month with the current year unless the month is invalid
    in which case the current month is used with the argument as the
    year.  If two arguments are given, they are assumed to be the year
    and month, respectively, unless the month is invalid in which case
    the arguments are used as month and year, respectively.'''
    def __init__(self):
        self._name = 'cal'
        self._help = self.__doc__

    def action(self, arg):
        now = datetime.datetime.now()
        year, month = now.year, now.month

        if arg is not None:
            args = arg.split()
            if len(args) == 2:
                try:
                    y, m = int(args[0]), int(args[1])
                    if m < 1 or m > 12:
                        y, m = m, y
                    year, month = y, m
                except:
                    pass
            elif len(args) == 1:
                try:
                    y = now.year
                    m = int(args[0])
                    if m < 1 or m > 12:
                        y = m
                        m = now.month
                    year, month = y, m
                except:
                    pass

        cal = calendar.TextCalendar(firstweekday=calendar.MONDAY)
        mark = ' '
        if year == now.year and month == now.month:
            mark = ':'
        cm = '%s\n%s\n' % (cal.formatmonthname(year, month, 20, True),
            cal.formatweekheader(2))
        i = 0
        for d in cal.itermonthdays(year, month):
            if 0 == d:
                f = '  '
            else:
                f = '%2d' % d
            if 6 == i % 7:
                f += '\n'
            elif (now.day - 1) == d or now.day == d:
                f += mark
            else:
                f += ' '
            cm += f
            i += 1

        qmk.Message()(cm)

def commands(): return [ CalendarCommand() ]

@@ -1481,121 +1481,18 @@
     
-mark = ' '%0A        if year == now.year and month == now.month:%0A            mark = ':'%0A        cm = '%25s%5Cn%25s%5Cn' %25 (
+lines = %5B 
 cal.
@@ -1529,17 +1529,18 @@
 0, True)
-,
+ %5D
 %0A       
@@ -1540,20 +1540,29 @@
         
-    
+lines.append(
 cal.form
@@ -1578,16 +1578,17 @@
 der(2))%0A
+%0A
         
@@ -1593,16 +1593,103 @@
   i = 0%0A
+        ln = ''%0A        mark = '*' if year == now.year and month == now.month else ' '%0A
         
@@ -1768,21 +1768,24 @@
         
-f 
+ln +
 = '
+ 
   '%0A    
@@ -1818,101 +1818,43 @@
     
-f 
+ln +
 = '%252d
+%25s
 ' %25 
-d%0A            if 6 == i %25 7:%0A                f += '%5Cn'%0A            el
+(d, ' ' 
 if 
-(
 now.day 
 - 1)
@@ -1853,78 +1853,84 @@
 day 
-- 1) == d or now.day == d:%0A                f += mark%0A            else:
+!= d else mark)%0A%0A            if 6 == i %25 7:%0A                lines.append(ln)
 %0A   
@@ -1946,36 +1946,16 @@
     
-f +
+ln 
 = '
- 
 '%0A
-            cm += f
 %0A   
@@ -1997,10 +1997,24 @@
 e()(
-cm
+'%5Cn'.join(lines)
 )%0A%0Ad

Fix bug in cal command that would add extraneous marks when 1 == now.day.