-
Notifications
You must be signed in to change notification settings - Fork 10
/
howm-mode.el
1271 lines (1162 loc) · 49.4 KB
/
howm-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; howm-mode.el --- Wiki-like note-taking tool
;;; Copyright (C) 2002, 2003, 2004, 2005-2024
;;; HIRAOKA Kazuyuki <kakkokakko@gmail.com>
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 1, or (at your option)
;;; any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; The GNU General Public License is available by anonymouse ftp from
;;; prep.ai.mit.edu in pub/gnu/COPYING. Alternately, you can write to
;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
;;; USA.
;;;--------------------------------------------------------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Backward compatibility
;; (require 'howm-mode) in .emacs is obsolete. Use (require 'howm) instead.
;; This must be earlier than (require 'howm-common), because
;; howm-common needs cl, and (require 'cl) should be written in howm.el.
(when (not (featurep 'howm))
(message "Warning: Requiring howm-mode is obsolete. Require howm instead.")
;; (beep)
;; (sit-for 1)
(require 'howm))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Require
(provide 'howm-mode)
(require 'howm)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Customize
;;; --- level 1 ---
;; You can easily modify them.
(howm-defvar-risky howm-template
(concat howm-view-title-header " %title%cursor\n%date %file\n\n")
"Contents of new file. %xxx are replaced with specified items.
If it is a list, <n>-th one is used when you type C-u <n> M-x howm-create.
If it is a function, it is called to get template string with the argument <n>.")
(defvar howm-keyword-header "<<<"
"Header string for declaration of keyword (implicit link).")
(defvar howm-ref-header ">>>"
"Header string for explicit link.")
(defvar howm-lighter " howm"
"Mode line for howm-mode")
(defvar howm-inhibit-title-file-match t
"If non-nil, inhibit howm-list-title when search string matches file name")
(defvar howm-list-all-title nil) ;; obsolete [2003-11-30]
(defvar howm-list-recent-title nil) ;; obsolete [2003-11-30]
(defvar howm-default-key-table
'(
;; ("key" func list-mode-p global-p)
("r" howm-refresh)
("l" howm-list-recent t t)
("a" howm-list-all t t)
("g" howm-list-grep t t)
("s" howm-list-grep-fixed t t)
("m" howm-list-migemo t t)
("t" howm-list-todo t t)
("y" howm-list-schedule t t)
("b" howm-list-buffers t t)
("x" howm-list-mark-ring t t)
("o" howm-occur t t)
("c" howm-create t t)
("e" howm-remember t t)
("," howm-menu t t)
("." howm-find-today nil t)
(":" howm-find-yesterday nil t)
("A" howm-list-around)
("h" howm-history nil t)
("D" howm-dup)
("i" howm-insert-keyword nil t)
("d" howm-insert-date nil t)
("T" howm-insert-dtime nil t)
("K" howm-keyword-to-kill-ring t t)
("n" action-lock-goto-next-link)
("p" action-lock-goto-previous-link)
("Q" howm-kill-all t t)
(" " howm-toggle-buffer nil t)
("N" howm-next-memo)
("P" howm-previous-memo)
("H" howm-first-memo)
("L" howm-last-memo)
("C" howm-create-here nil t)
("I" howm-create-interactively nil t)
("w" howm-random-walk nil t)
("M" howm-open-named-file t t)
)
"List of (key function list-mode-p global-p).
`howm-prefix' + this key is real stroke.
If optional argument list-mode-p is non-nil,
same key is also available in view mode.
It is further registered globally if global-p is non-nil."
)
(howm-defvar-risky howm-migemo-client nil
"Command name of migemo-client.
Example of cmigemo:
(setq howm-migemo-client '((type . cmigemo) (command . \"cmigemo\")))
Example of migemo-client (obsolete):
(setq howm-migemo-client \"migemo-client\")
See also `howm-migemo-client-option`")
(howm-defvar-risky howm-migemo-client-option nil
"List of option for migemo-client.
Example of cmigemo:
(setq howm-migemo-client-option
'(\"-q\" \"-d\" \"/usr/share/cmigemo/utf-8/migemo-dict\"))
Example of migemo-client (obsolete):
(setq howm-migemo-client-option '(\"-H\" \"::1\")
See also `howm-migemo-client`")
;;; --- level 2 ---
;; Be careful to keep consistency.
(howm-defvar-risky howm-keyword/ref-regexp-format
"\\(%s\\)[ \t]*\\([^ \t\r\n].*\\)")
(howm-defvar-risky howm-keyword-format
(format "%s %%s" howm-keyword-header)
"Format for declaration of keyword. See `format'.")
(howm-defvar-risky howm-keyword-regexp
(format howm-keyword/ref-regexp-format (regexp-quote howm-keyword-header)))
(howm-defvar-risky howm-keyword-regexp-hilit-pos 1)
(howm-defvar-risky howm-keyword-regexp-pos 2)
(howm-defvar-risky howm-ref-regexp
(format howm-keyword/ref-regexp-format (regexp-quote howm-ref-header))
"Regexp for explicit link.")
(howm-defvar-risky howm-ref-regexp-hilit-pos 0
"Position of search string in `howm-ref-regexp'")
(howm-defvar-risky howm-ref-regexp-pos 2
"Position of search string in `howm-ref-regexp'")
(howm-defvar-risky howm-wiki-regexp "\\[\\[\\([^]\r\n]+\\)\\]\\]"
"Regexp for explicit link.")
(howm-defvar-risky howm-wiki-regexp-hilit-pos 1
"Position of hilight in `howm-wiki-regexp'")
(howm-defvar-risky howm-wiki-regexp-pos 1
"Position of search string in `howm-wiki-regexp'")
(howm-defvar-risky howm-wiki-format "[[%s]]"
"Format for declaration of wiki word. See `format'.")
(howm-defvar-risky howm-template-rules
'(("%title" . howm-template-title)
("%date" . howm-template-date)
("%file" . howm-template-previous-file)
("%cursor" . howm-template-cursor))) ;; Cursor must be the last rule.
(defvar howm-template-date-format howm-dtime-format
"%date is replaced with `howm-template-date-format'
in `howm-template'. See `format-time-string'")
(defvar howm-template-file-format (concat howm-ref-header " %s")
"%file is replaced with `homw-template-file-format'
in `howm-template'. %s is replaced with name of last file. See `format'.")
;;; --- level 3 ---
;; As you like.
(defun howm-action-lock-general (command regexp pos
&optional hilit-pos
&rest options)
(list regexp
`(lambda (&optional dummy)
(let ((s (match-string-no-properties ,pos)))
;; (when howm-keyword-case-fold-search
;; (setq s (downcase s)))
(,command s ,@options)))
(or hilit-pos 0)
t))
(defun howm-action-lock-search (regexp
pos
&optional hilit-pos create-p open-unique-p)
(howm-action-lock-general 'howm-keyword-search
regexp pos hilit-pos create-p open-unique-p))
(defun howm-action-lock-related (regexp pos hilit-pos)
(howm-action-lock-general 'howm-list-related regexp pos hilit-pos))
(defun howm-action-lock-date-rule ()
(action-lock-general 'howm-action-lock-date howm-date-regexp 0 0))
(defun howm-action-lock-quote-keyword (keyword)
(let ((q (regexp-quote keyword)))
;; when a regexp is specified, leave unmatched keywords.
(if (and (stringp howm-check-word-break)
(not (string-match howm-check-word-break keyword)))
q
;; add word break checks
(concat "\\b" q "\\b"))))
(defun howm-action-lock-setup ()
(setq action-lock-case-fold-search howm-keyword-case-fold-search)
(action-lock-mode t)
(let* ((date-al (action-lock-date "{_}" howm-dtime-format)))
;; override the rule in action-lock.el
(action-lock-add-rules (list date-al) t))
(let* ((ks (howm-keyword-for-goto))
(r (mapconcat (if howm-check-word-break
#'howm-action-lock-quote-keyword
#'regexp-quote)
ks "\\|"))
;; The following optimization causes an error
;; "Variable binding depth exceeds max-specpdl-size".
;; (r (cond ((stringp howm-check-word-break)
;; (mapconcat #'howm-action-lock-quote-keyword ks "\\|"))
;; (t
;; (regexp-opt ks (and howm-check-word-break 'word)))))
(wiki (howm-action-lock-search howm-wiki-regexp
howm-wiki-regexp-pos
howm-wiki-regexp-hilit-pos
t))
(explicit (howm-action-lock-search howm-ref-regexp
howm-ref-regexp-pos
howm-ref-regexp-hilit-pos))
(implicit (howm-action-lock-search r 0))
(rev (howm-action-lock-related howm-keyword-regexp
howm-keyword-regexp-pos
howm-keyword-regexp-hilit-pos))
(date (howm-action-lock-date-rule))
(done (howm-action-lock-reminder-done-rule))
(all `(
,explicit
,rev
,@(if ks (list implicit) nil)
,wiki
,@(if (howm-menu-p) nil (list date done))
))
)
;; don't override the rule in action-lock.el
;; esp. http://xxx should call browser even if "<<< http" exists
(action-lock-add-rules all)))
(defun howm-file-name (&optional time)
(format-time-string howm-file-name-format
(or time (current-time))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Definitions
(define-minor-mode howm-mode
"Toggle Howm mode.
When the mode is enabled, underlines are drawn on texts which match
to titles of other files. Typing \\<action-lock-mode-map>\\[action-lock-magic-return] there,
you can jump to the corresponding file.
key binding
--- -------
\\[action-lock-magic-return] Follow link
\\<howm-mode-map>\\[howm-refresh] Refresh buffer
\\[howm-list-all] List all files
\\[howm-list-grep] Search (grep)
\\[howm-create] Create new file
\\[howm-dup] Duplicate current file
\\[howm-insert-keyword] Insert keyword
\\[howm-insert-date] Insert date
\\[howm-insert-dtime] Insert date with time
\\[howm-keyword-to-kill-ring] Copy current keyword to kill ring
\\[action-lock-goto-next-link] Go to next link
\\[action-lock-goto-previous-link] Go to previous link
\\[howm-next-memo] Go to next entry in current buffer
\\[howm-previous-memo] Go to previous entry in current buffer
\\[howm-first-memo] Go to first entry in current buffer
\\[howm-last-memo] Go to last entry in current buffer
\\[howm-create-here] Add new entry to current buffer
\\[howm-create-interactively] Create new file interactively (not recommended)
\\[howm-random-walk] Browse random entries automtically
"
:init-value nil ;; default = off
:lighter howm-lighter ;; mode-line
:keymap (mapcar (lambda (entry)
(let ((k (car entry))
(f (cadr entry)))
(cons (concat howm-prefix k) f)))
howm-default-key-table)
(if howm-mode
(howm-initialize-buffer)
(howm-restore-buffer)))
(defun howm-set-keymap ()
(mapc (lambda (entry)
(let* ((k (car entry))
(f (cadr entry))
(list-mode-p (cl-caddr entry))
(global-p (cl-cadddr entry))
(pk (concat howm-prefix k)))
(define-key howm-mode-map pk f)
(when list-mode-p
(mapc (lambda (m)
(define-key m k f)
(define-key m pk f))
(list howm-view-summary-mode-map
howm-view-contents-mode-map)))
(when global-p
(define-key global-map pk f))))
howm-default-key-table)
(define-key howm-mode-map "\C-x\C-s" 'howm-save-buffer))
(howm-set-keymap)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Main functions
(defun howm-refresh ()
(interactive)
(if (howm-menu-p)
(howm-menu-refresh)
(howm-initialize-buffer)))
(defun howm-initialize-buffer ()
(interactive)
(when (not howm-mode)
(error "Not howm-mode"))
(howm-message-time "init-buf"
(save-restriction
(widen)
(howm-set-configuration-for-major-mode major-mode)
(howm-action-lock-setup)
(howm-mode-add-font-lock)
(howm-reminder-add-font-lock)
(cheat-font-lock-fontify)
;; make-local-hook is obsolete for emacs >= 21.1.
(howm-funcall-if-defined (make-local-hook 'after-save-hook))
(add-hook 'after-save-hook 'howm-after-save t t))))
(defun howm-after-save ()
(when howm-mode
(howm-keyword-add-current-buffer)
(when howm-refresh-after-save
(howm-initialize-buffer))
(when (and howm-menu-refresh-after-save
(> howm-menu-expiry-hours 0))
(howm-menu-refresh-background))
(run-hooks 'howm-after-save-hook)))
(defun howm-restore-buffer ()
(action-lock-mode 0))
(defun howm-list-all ()
(interactive)
(howm-set-command 'howm-list-all)
(howm-normalize-show "" (howm-all-items))
;; for backward compatibility
(cond ((howm-list-title-p) t) ;; already done in howm-normalize-show
(howm-list-all-title (howm-list-title-internal))))
(defun howm-all-items ()
"Returns list of all items in the first search path."
(howm-folder-items (car (howm-search-path)) t))
(defun howm-list-recent (&optional days)
(interactive "P")
(howm-set-command 'howm-list-recent)
(let* ((d (or days howm-list-recent-days))
(now (current-time))
(from (howm-days-before now d))
(item-list (howm-folder-items howm-directory t)))
(howm-normalize-show "" (howm-filter-items-by-mtime item-list from now))
;; clean me [2003-11-30]
(cond ((howm-list-title-p) t) ;; already done in howm-normalize-show
(howm-list-recent-title (howm-list-title-internal))
((not days) (howm-view-summary-to-contents)))))
;; clean me: direct access to howm-view-* is undesirable.
(defvar howm-list-title-previous nil
"For internal use")
(make-variable-buffer-local 'howm-list-title-previous)
(defun howm-list-title-put-previous (&optional item-list)
(when howm-list-title-undo
(setq howm-list-title-previous (or item-list (howm-view-item-list)))))
(defun howm-list-title-clear-previous ()
(setq howm-list-title-previous nil))
(defun howm-list-title-get-previous ()
(if howm-list-title-undo
(let ((prev howm-list-title-previous))
(setq howm-list-title-previous nil)
(howm-view-summary-rebuild prev))
(error "Undo is not enabled.")))
(defun howm-list-title-regexp ()
(or howm-list-title-regexp (howm-view-title-regexp-grep)))
(defalias 'howm-list-title 'howm-list-toggle-title) ;; backward compatibility
(defun howm-list-toggle-title (&optional undo)
(interactive "P")
(if (or undo howm-list-title-previous)
(howm-list-title-get-previous)
(howm-list-title-internal)))
(defun howm-list-title-internal ()
(let ((b (current-buffer)))
(howm-list-title-put-previous)
(howm-view-list-title (howm-list-title-regexp))
;; (howm-view-filter-by-contents (howm-list-title-regexp))
(let ((c (current-buffer)))
(when (not (eq b c))
(set-buffer b)
(howm-view-kill-buffer)
(switch-to-buffer c)
(howm-view-summary-check t)))))
(defun howm-list-title-p ()
(let ((a (howm-get-value howm-list-title)))
(cond ((null a) nil) ;; I know this is redundant.
((listp a) (member (howm-command) a))
(t a))))
(defun howm-days-after (ti days &optional hours)
(let* ((ne (howm-decode-time ti))
(hour-pos 2)
(day-pos 3)
(nh (nth hour-pos ne))
(nd (nth day-pos ne)))
(setf (nth hour-pos ne) (+ nh (or hours 0)))
(setf (nth day-pos ne) (+ nd days))
(apply #'encode-time ne)))
(defun howm-days-before (ti days)
(howm-days-after ti (- days)))
(defun howm-list-grep (&optional completion-p)
(interactive "P")
(howm-set-command 'howm-list-grep)
(howm-list-grep-general completion-p))
(defun howm-list-grep-fixed ()
(interactive)
(howm-set-command 'howm-list-grep-fixed)
(howm-list-grep-general t))
(defun howm-list-grep-general (&optional completion-p)
(let* ((action (lambda (pattern) (howm-search pattern completion-p)))
(regexp (howm-iigrep completion-p action)))
(when completion-p ;; Goto link works only for fixed string at now.
(howm-write-history regexp))
(funcall action regexp)))
(defun howm-search (regexp fixed-p &optional emacs-regexp filter bufname)
(if (string= regexp "")
(howm-list-all)
(howm-message-time "search"
(let* ((trio (howm-call-view-search-internal regexp fixed-p emacs-regexp))
(kw (car trio))
(name (or bufname (cadr trio)))
(items (cl-caddr trio)))
(when filter
(setq items (funcall filter items)))
(howm-normalize-show name items (or emacs-regexp regexp) nil nil kw)
(howm-record-view-window-configuration)))))
(defun howm-iigrep (completion-p action)
(howm-with-iigrep (howm-iigrep-command-for-pattern completion-p)
howm-iigrep-show-what action
(if completion-p
(howm-completing-read-keyword)
(read-from-minibuffer "Search all (grep): "))))
(defmacro howm-with-iigrep (command-for-pattern show-what action &rest body)
(declare (indent 3))
`(let ((*iigrep-post-sentinel* (howm-iigrep-post-sentinel ,action))
(howm-view-summary-name "*howmS(preview)*")
(howm-view-contents-name "*howmC(preview)*")
(howm-history-limit 0)
(*howm-show-item-filename* nil)
(howm-message-time nil))
(unwind-protect
(iigrep-with-grep ,command-for-pattern ,show-what
,@body)
(mapc (lambda (b) (and (get-buffer b) (kill-buffer b)))
(list howm-view-summary-name howm-view-contents-name)))))
(defmacro howm-iigrep-command-for-pattern (&optional fixed-p converter)
;; use macro due to dynamic binding. Sigh...
`(and howm-view-use-grep
(lambda (str)
(let* ((pattern (funcall (or ,converter #'identity) str))
(trio (howm-real-grep-single-command
pattern (list howm-directory) ,fixed-p))
(com (car trio))
(args (cl-second trio))
(fs (cl-third trio)))
(append (list com) (cons "-I" args) fs)))))
(defmacro howm-iigrep-post-sentinel (action)
;; use macro due to dynamic binding. Sigh...
`(lambda (hits pattern)
(when (<= hits howm-iigrep-preview-items)
(save-selected-window
(funcall ,action pattern)))))
(defvar *howm-view-window-configuration* nil
"For internal use")
(defun howm-view-window-configuration ()
*howm-view-window-configuration*)
(defun howm-set-view-window-configuration (conf)
(setq *howm-view-window-configuration* conf))
(defun howm-record-view-window-configuration ()
(howm-set-view-window-configuration (current-window-configuration)))
(defun howm-restore-view-window-configuration ()
(set-window-configuration (howm-view-window-configuration)))
(defun howm-return-to-list ()
(interactive)
(howm-restore-view-window-configuration))
(defun howm-call-view-search-internal (regexp fixed-p &optional emacs-regexp)
(let ((hilit (if emacs-regexp
`((,emacs-regexp . howm-view-hilit-face))
nil)))
(howm-view-search-folder-internal regexp (howm-search-path-folder)
nil nil fixed-p hilit)))
(defun howm-list-migemo (&optional completion-p)
(interactive "P")
(howm-set-command 'howm-list-migemo)
(if completion-p
(howm-list-grep t)
(howm-list-migemo-action (howm-iigrep-migemo))))
(defun howm-list-migemo-action (roma)
(let* ((e-reg (howm-migemo-get-pattern roma "emacs"))
(g-reg (if howm-view-use-grep
(howm-migemo-get-pattern roma "egrep")
e-reg)))
(if (and e-reg g-reg)
(howm-search g-reg nil e-reg nil roma)
(message "No response from migemo-client."))))
(defun howm-iigrep-migemo ()
(let* ((converter (lambda (yomi) (howm-migemo-get-pattern yomi "egrep")))
(command-for-pattern (howm-iigrep-command-for-pattern nil converter))
(show-what (if (eq howm-iigrep-migemo-show-what 'inherit)
howm-iigrep-show-what
howm-iigrep-migemo-show-what)))
(howm-with-iigrep command-for-pattern show-what
#'howm-list-migemo-action
(read-from-minibuffer "Search all (migemo): "))))
(defun howm-migemo-get-pattern (roma type)
(when (and (null howm-migemo-client) (not howm-view-use-grep))
(require 'migemo))
(cl-labels ((ref (key) (cdr (assoc key howm-migemo-client))))
(cond ((and (featurep 'migemo) (string= type "emacs"))
(howm-funcall-if-defined (migemo-get-pattern roma)))
((or (null howm-migemo-client) (stringp howm-migemo-client))
(car (howm-call-process (or howm-migemo-client "migemo-client")
`(,@howm-migemo-client-option "-t" ,type ,roma)
0)))
((eq (ref 'type) 'cmigemo)
(car (howm-call-process (ref 'command)
`(,@howm-migemo-client-option
,@(and (string= type "emacs") '("-e"))
"-w" ,roma))))
(t (error "Invalid howm-migemo-client: %s" howm-migemo-client)))))
(defun howm-normalize-oldp ()
howm-list-normalizer)
;; ;; generate conv in howm-normalizer-pair
;; (let ((methods '("random" "name" "numerical-name" "date" "reverse-date"
;; "summary" "reminder" "mtime" "reverse")))
;; (mapcar (lambda (m)
;; (let ((command
;; (howm-get-symbol nil "howm-view-sort-by-" m))
;; (internal
;; (howm-get-symbol nil "howm-sort-items-by-" m)))
;; (cons command internal)))
;; methods))
(defun howm-normalizer-pair ()
(let* ((old howm-list-normalizer)
(new howm-normalizer)
(conv '((howm-view-sort-by-random . howm-sort-items-by-random)
(howm-view-sort-by-name . howm-sort-items-by-name)
(howm-view-sort-by-numerical-name
. howm-sort-items-by-numerical-name)
(howm-view-sort-by-date . howm-sort-items-by-date)
(howm-view-sort-by-reverse-date
. howm-sort-items-by-reverse-date)
(howm-view-sort-by-summary . howm-sort-items-by-summary)
(howm-view-sort-by-reminder . howm-sort-items-by-reminder)
(howm-view-sort-by-mtime . howm-sort-items-by-mtime)
(howm-view-sort-by-reverse . howm-sort-items-by-reverse)))
(p (assoc old conv))
(q (assoc new conv)))
(when q
(message "Warning: %s is wrong for howm-normalizer. Use %s." (car q) (cdr q))
(setq new (cdr q)))
(cond ((null old) (cons old new))
(p (cons nil (cdr p)))
(t (cons old #'identity)))))
(defmacro howm-with-normalizer (&rest body)
(declare (indent 0))
(let ((g (cl-gensym)))
`(progn
(when (howm-normalize-oldp)
(message
"Warning: howm-list-normalizer is obsolete. Use howm-normalizer."))
(let* ((,g (howm-normalizer-pair))
(howm-list-normalizer (car ,g))
(howm-normalizer (cdr ,g)))
,@body))))
(defun howm-normalize-show (name item-list
&optional keyword comefrom-regexp no-list-title
fl-keywords)
;; comefrom-regexp and no-list-title are never used now. [2009-07-23]
(howm-with-normalizer
(if (howm-normalize-oldp)
;; for backward compatibility.
(progn
(howm-view-summary name item-list fl-keywords)
(howm-list-normalize-old keyword comefrom-regexp no-list-title))
(let* ((r (howm-normalize item-list keyword
comefrom-regexp no-list-title)))
(howm-call-view-summary name (cdr r) fl-keywords)
(car r)))))
(defun howm-call-view-summary (name item-list-pair fl-keywords)
(let ((orig (car item-list-pair))
(entitled (cdr item-list-pair)))
(howm-view-summary name (or entitled orig) fl-keywords)
;; side effect
(if entitled
(howm-list-title-put-previous orig)
(howm-list-title-clear-previous))))
(defun howm-normalize (item-list
&optional keyword comefrom-regexp no-list-title)
;; no-list-title is never used now. [2009-07-23]
"Sort ITEM-LIST in the standard order."
(let ((matched nil)
(entitled-item-list nil))
(setq item-list (funcall howm-normalizer item-list))
(when keyword
(let ((key-reg (or comefrom-regexp
(howm-make-keyword-regexp1 keyword)))
(word-reg (format "\\<%s\\>"
(if (stringp keyword)
(regexp-quote keyword)
(regexp-opt keyword t))))
(wiki-reg (regexp-quote (howm-make-wiki-string keyword)))
(file-reg (and
(stringp keyword)
(format "^%s$"
(regexp-quote (expand-file-name keyword)))))
(case-fold-search howm-keyword-case-fold-search))
(cl-labels ((check (tag flag reg &optional tag-when-multi-hits)
(when flag
(let ((r (howm-normalize-check item-list tag reg
tag-when-multi-hits)))
(setq matched (append (car r) matched))
(setq item-list (cdr r))))))
;; not efficient. should I do them at once?
(check 'word howm-list-prefer-word word-reg)
(check 'wiki howm-list-prefer-wiki wiki-reg)
(check 'related-keyword t howm-keyword-regexp)
(check 'keyword t key-reg 'keyword-multi-hits)
(check 'file file-reg file-reg))))
(when (and (howm-list-title-p)
(not no-list-title)
(not (and (member 'file matched)
howm-inhibit-title-file-match)))
(setq entitled-item-list
(howm-entitle-items (howm-list-title-regexp) item-list)))
(cons matched (cons item-list entitled-item-list))))
(defun howm-normalize-check (item-list tag reg tag-when-multi-hits)
(let* ((r (if (eq tag 'file)
(howm-view-lift-by-path-internal item-list reg)
(howm-view-lift-by-summary-internal item-list reg)))
(m (car r))
(item-list (cdr r))
(matched (cond ((and tag-when-multi-hits (eq m 'multi))
(list tag-when-multi-hits tag))
(m (list tag))
(t nil))))
(cons matched item-list)))
(defun howm-list-normalize-old (&optional keyword comefrom-regexp no-list-title)
"Sort displayed items in the standard order.
This function is obsolete. Use `howm-normalize' insteadly.
--- Sorry, below documentation is incomplete. ---
When KEYWORD is given, matched items are placed on the top.
KEYWORD can be a string or a list of strings.
"
(prog1
(howm-view-in-background
(howm-list-normalize-subr keyword comefrom-regexp no-list-title))
(howm-view-summary)))
(defun howm-list-normalize-subr (keyword comefrom-regexp no-list-title)
"Obsolete. Do not use this any more."
(let ((matched nil))
(funcall howm-list-normalizer)
(when keyword
(let ((key-reg (or comefrom-regexp
(howm-make-keyword-regexp1 keyword)))
(word-reg (format "\\<%s\\>"
(if (stringp keyword)
(regexp-quote keyword)
(regexp-opt keyword t))))
(wiki-reg (regexp-quote (howm-make-wiki-string keyword)))
(file-reg (and
(stringp keyword)
(format "^%s$"
(regexp-quote (expand-file-name keyword)))))
(case-fold-search howm-keyword-case-fold-search))
;; clean me.
(let ((check (lambda (tag flag reg &optional tag-when-multi-hits)
(when flag
(let ((m (if (eq tag 'file)
(howm-view-lift-by-name nil reg t)
(howm-view-lift-by-summary nil reg))))
(when m
(setq matched (cons tag matched)))
(when (and tag-when-multi-hits (eq m 'multi))
(setq matched
(cons tag-when-multi-hits matched))))))))
(funcall check 'word howm-list-prefer-word word-reg)
(funcall check 'wiki howm-list-prefer-wiki wiki-reg)
(funcall check 'related-keyword t howm-keyword-regexp)
(funcall check 'keyword t key-reg 'keyword-multi-hits)
(funcall check 'file file-reg file-reg))))
(when (and (howm-list-title-p)
(not no-list-title)
(not (and (member 'file matched)
howm-inhibit-title-file-match)))
(howm-list-title-internal))
matched))
(defun howm-make-keyword-string (keyword)
(format howm-keyword-format keyword))
(defun howm-make-wiki-string (keyword)
(format howm-wiki-format keyword))
;; clean me
(defvar howm-keyword-regexp-format "%s$"
"Format to make entire-match regexp from keyword string.
Default is \"%s$\" because we want to make regexp \"<<< foo$\"
from keyword string \"<<< foo\",
so that we can accept \"<<< foo\" and reject \"<<< foobar\".
We need entire-match in order to
(1) place \"<<< foo\" on the top when \"foo\" is searched, and
(2) judge existence of \"<<< foo\" when [[foo]] is hit.")
(defun howm-make-keyword-regexp1 (keyword)
(howm-make-keyword-regexp-general keyword #'howm-make-keyword-regexp1-sub))
(defun howm-make-keyword-regexp2 (keyword)
(howm-make-keyword-regexp-general keyword #'howm-make-keyword-regexp2-sub))
(defun howm-make-keyword-regexp1-sub (keyword)
(format howm-keyword-regexp-format
(regexp-quote (howm-make-keyword-string keyword))))
(defun howm-make-keyword-regexp2-sub (keyword)
(format howm-keyword-regexp-format
(howm-make-keyword-string (regexp-quote keyword))))
(defun howm-make-keyword-regexp-general (keyword regexp-generator)
(cond ((stringp keyword)
(funcall regexp-generator keyword))
((listp keyword)
(mapconcat (lambda (s)
(concat "\\("
(funcall regexp-generator s)
"\\)"))
keyword
"\\|"))
(t (error "Wrong type: %s" keyword))))
(defun howm-list-related (str)
(howm-set-command 'howm-list-related)
(let* ((keys (mapcar (lambda (k)
(if howm-keyword-case-fold-search
(downcase k)
k))
(howm-subkeyword str)))
(filter `(lambda (items)
(howm-filter-items-by-summary items ,(regexp-opt keys)))))
;; Note that regexp-opt returns a regexp for emacs (not for grep).
(howm-search (howm-make-keyword-string ".*") nil nil filter)))
(defun howm-subkeyword (str)
(with-temp-buffer
(insert str)
(howm-keyword-for-goto)))
(defun howm-list-around ()
(interactive)
(howm-set-command 'howm-list-around)
(let ((f (buffer-file-name))
(item-list (howm-view-sort-by-reverse-date-internal
(howm-all-items))))
(let ((howm-normalizer #'identity))
(howm-normalize-show "" item-list))
(let ((pos (cl-position-if (lambda (item)
(string= (howm-item-name item) f))
(howm-view-item-list))))
(goto-char (point-min))
(when pos
(forward-line pos)))
(howm-view-summary-check t)))
(defun howm-history ()
(interactive)
(unless (file-exists-p howm-history-file)
(error "No history."))
;; disable expansion of %schedule etc.
(let ((howm-menu-display-rules nil)) ;; dirty
(howm-menu-open howm-history-file)))
;; (defvar howm-history-exclude
;; (let ((strings '("[0-9][0-9][0-9][0-9]" "^[*=] [^ ]")))
;; `("| %.*%$"
;; ,(mapconcat 'regexp-quote strings "\\|"))))
;; (defun howm-history ()
;; (interactive)
;; (howm-menu-open howm-history-file)
;; (howm-edit-read-only-buffer
;; (mapc #'flush-lines
;; howm-history-exclude)))
(defvar *howm-command* nil
"For internal use")
(defun howm-set-command (com)
(setq *howm-command* com))
(defun howm-command ()
*howm-command*)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Create
(defun howm-create (&optional which-template here)
(interactive "p")
(let* ((t-c (howm-create-default-title-content))
(title (car t-c))
(content (cdr t-c)))
(howm-create-file-with-title title which-template nil here content)))
(howm-dont-warn-free-variable transient-mark-mode)
(howm-dont-warn-free-variable mark-active)
(defun howm-create-default-title-content ()
(let* ((p (point))
(m (or (mark t) -777))
(beg (min p m))
(end (max p m))
(search-str (howm-view-name)))
(let* ((transient-mark-p (and (boundp 'transient-mark-mode)
transient-mark-mode))
(mark-active-p (and (boundp 'mark-active) mark-active))
(active-p (if transient-mark-p
mark-active-p
t))
(strictly-active-p (and transient-mark-p mark-active-p))
(title-p (let* ((b (line-beginning-position))
(e (line-end-position)))
(and active-p
(< 0 beg) (<= b beg) (<= end e) (not (= beg end)))))
(content-p (and strictly-active-p
howm-content-from-region))
(search-p (and howm-title-from-search
(stringp search-str)))
(s (cond ((or title-p content-p) (buffer-substring-no-properties beg
end))
(search-p search-str))))
(cond ((null s) (cons "" ""))
((eq content-p t) (cons "" s))
((or title-p search-p) (cons s ""))
(content-p (cons "" s))
(t (cons "" ""))))))
(defun howm-create-here (&optional which-template)
(interactive "p")
(howm-create which-template t))
(defun howm-create-file-with-title (title &optional
which-template not-use-file here content)
(let ((b (current-buffer)))
(when (not here)
(howm-create-file))
(cond ((howm-buffer-empty-p) nil)
((and here howm-create-here-just) (beginning-of-line))
(t (howm-create-newline)))
(let ((p (point))
(insert-f (lambda (switch)
(howm-insert-template (if switch title "")
b which-template (not switch))))
(use-file (not not-use-file)))
;; second candidate which appears when undo is called
(let ((end (funcall insert-f not-use-file)))
(save-excursion
(goto-char end)
(insert (or content "")))
(undo-boundary)
(delete-region p end))
(funcall insert-f use-file))
(howm-create-finish)))
(defun howm-create-finish ()
(howm-set-mode)
(run-hooks 'howm-create-hook))
(defun howm-create-newline ()
(widen)
(if howm-prepend
(howm-create-newline-prepend)
(howm-create-newline-append)))
(defun howm-create-newline-prepend ()
(goto-char (point-min)))
(defun howm-create-newline-append ()
(goto-char (point-max))
(delete-blank-lines)
(when (not (= (line-beginning-position) (point))) ;; not empty line
(insert "\n"))
(insert "\n"))
(defun howm-insert-template (title &optional
previous-buffer which-template not-use-file)
(let* ((beg (point))
(f (buffer-file-name previous-buffer))
(af (and f (howm-abbreviate-file-name f))))
(insert (howm-template-string which-template previous-buffer))
(let* ((date (format-time-string howm-template-date-format))
(use-file (not not-use-file))
(file (cond ((not use-file) "")
((null f) "")
((string= f (buffer-file-name)) "")
(t (format howm-template-file-format af)))))
(let ((arg `((title . ,title) (date . ,date) (file . ,file)))
(end (point-marker)))
(howm-replace howm-template-rules arg beg end)
end))))
(defvar howm-template-receive-buffer t
"Non nil if howm-template should receive previous-buffer
when howm-template is a function.
Set this option to nil if backward compatibility with howm-1.2.4 or earlier
is necessary.")
(defun howm-template-string (which-template previous-buffer)
;; which-template should be 1, 2, 3, ...
(setq which-template (or which-template 1))
(cond ((stringp howm-template) howm-template)
((functionp howm-template) (let ((args (if howm-template-receive-buffer
(list which-template
previous-buffer)
(list which-template))))
(apply howm-template args)))
((listp howm-template) (nth (- which-template 1) howm-template))))
(defun howm-replace (rules arg &optional beg end)
(mapc (lambda (pair)
(let ((spell (car pair))
(disp-f (cdr pair)))
(goto-char (or beg (point-min)))
(while (re-search-forward spell end t)
(delete-region (match-beginning 0) (match-end 0))
(funcall disp-f arg))))
rules))
(defun howm-template-title (arg)
(insert (cdr (assoc 'title arg))))
(defun howm-template-date (arg)
(insert (cdr (assoc 'date arg))))
(defun howm-template-previous-file (arg)
(insert (cdr (assoc 'file arg))))
(defun howm-template-cursor (arg)) ;; do nothing
(defun howm-dup ()
(interactive)
(let* ((r (howm-view-paragraph-region))
(s (buffer-substring-no-properties (car r) (cadr r))))
(howm-create-file)
(howm-set-mode)
(insert "\n" s)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Keyword
(defun howm-completing-read-keyword ()
(message "Scanning...")
(let* ((kl (howm-keyword-list))
(table (mapcar #'list kl))
(completion-ignore-case howm-keyword-case-fold-search))
(completing-read "Keyword: " table)))
(defun howm-insert-keyword ()
(interactive)
(insert (howm-completing-read-keyword)))
(defun howm-keyword-to-kill-ring (&optional filename-p)
(interactive "P")
(let ((title (howm-title-at-current-point filename-p)))
(if title