-
Notifications
You must be signed in to change notification settings - Fork 5
/
howm-view.el
1630 lines (1462 loc) · 66.7 KB
/
howm-view.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-view.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.
;;--------------------------------------------------------------------
(provide 'howm-view)
(require 'howm)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; variables
;; customize
(defvar howm-view-summary-sep "|")
(defvar howm-view-summary-format
(let* ((path (format-time-string howm-file-name-format))
(width (length (file-name-nondirectory path))))
(concat "%-" (format "%s" (1+ width)) "s" howm-view-summary-sep " ")))
(defvar howm-view-header-format
"\n==========================>>> %s\n"
"Format string of header for howm-view-contents.
%s is replaced with file name. See `format'.")
(defvar howm-view-header-regexp "^==========================>>> .*$")
(defvar howm-view-open-recenter howm-view-search-recenter)
(defvar howm-view-title-header "=")
;; howm-view-title-regexp is assumed to have a form "^xxxxxxx$"
(defvar howm-view-title-regexp (format "^%s\\( +\\(.*\\)\\|\\)$"
(regexp-quote howm-view-title-header)))
(defvar howm-view-title-regexp-pos 2)
(defvar howm-view-title-regexp-grep (format "^%s +"
(regexp-quote howm-view-title-header)))
(defun howm-view-title-regexp-grep ()
(if howm-view-use-grep
howm-view-title-regexp-grep
howm-view-title-regexp))
(howm-defvar-risky howm-view-sort-methods
'(("random" . howm-view-sort-by-random)
("name" . howm-view-sort-by-name)
("name-match" . howm-view-lift-by-name)
("numerical-name" . howm-view-sort-by-numerical-name)
("summary" . howm-view-sort-by-summary)
("summary-match" . howm-view-lift-by-summary)
("summary-match-string" . howm-view-lift-by-summary-substring)
; ("atime" . howm-view-sort-by-atime) ;; nonsense
; ("ctime" . howm-view-sort-by-ctime) ;; needless
("mtime" . howm-view-sort-by-mtime)
("date" . howm-view-sort-by-reverse-date)
("reminder" . howm-view-sort-by-reminder)
("reverse" . howm-view-sort-reverse)))
(howm-defvar-risky howm-view-filter-methods
'(("name" . howm-view-filter-by-name)
("summary" . howm-view-filter-by-summary)
("mtime" . howm-view-filter-by-mtime)
; ("ctime" . howm-view-filter-by-ctime) ;; needless
("date" . howm-view-filter-by-date)
("reminder" . howm-view-filter-by-reminder)
("contents" . howm-view-filter-by-contents)
("Region" . howm-view-filter-by-region)
("Around" . howm-view-filter-by-around)
("uniq" . howm-view-filter-uniq)
))
;; referred only when howm-view-use-grep is nil
(defvar howm-view-watch-modified-buffer t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; item
(defun howm-view-item-basename (item &optional nonempty)
(let* ((f (howm-item-name item))
(b (file-name-nondirectory f)))
(if (and (string= b "") nonempty)
f
b)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; riffle
(defalias 'riffle-home:howm 'howm-view-item-home)
(defalias 'riffle-summary-item:howm 'howm-view-summary-item)
(defalias 'riffle-contents-item:howm 'howm-view-contents-item)
(defalias 'riffle-summary-set-mode:howm 'howm-view-summary-mode)
(defalias 'riffle-contents-set-mode:howm 'howm-view-contents-mode)
(defun riffle-summary-name-format:howm ()
howm-view-summary-name)
(defun riffle-contents-name-format:howm ()
howm-view-contents-name)
(defvar *howm-show-item-filename* t)
(defun riffle-post-update:howm (item)
(when *howm-show-item-filename*
(howm-message-nolog "View: %s" (howm-view-item-filename item))))
;;; aliases
;; Only howm-view.el should call riffle-xxx.
;; Define alias if it is used in howm-xxx besides howm-view.el.
(defalias 'howm-view-name #'riffle-name)
(defalias 'howm-view-item-list #'riffle-item-list)
(defalias 'howm-view-line-number #'riffle-line-number)
(defalias 'howm-view-summary-check #'riffle-summary-check)
(defalias 'howm-view-persistent-p #'riffle-persistent-p)
(defalias 'howm-view-kill-buffer #'riffle-kill-buffer)
(defalias 'howm-view-set-place #'riffle-set-place)
(defalias 'howm-view-get-place #'riffle-get-place)
(defalias 'howm-view-summary-current-item #'riffle-summary-current-item)
(defalias 'howm-view-contents-current-item #'riffle-contents-current-item)
(defalias 'howm-view-summary-to-contents #'riffle-summary-to-contents)
(defalias 'howm-view-restore-window-configuration #'riffle-restore-window-configuration)
;; for howmoney.el
;; https://howm.osdn.jp/cgi-bin/hiki/hiki.cgi?howmoney
(defun howm-view-get-buffer (name-format &optional name new)
(let ((riffle-type ':howm)) ;; cheat
(riffle-get-buffer name-format name new)))
(defun howm-view-summary-buffer (&optional new)
(let ((riffle-type ':howm)) ;; cheat
(riffle-summary-buffer new)))
(defalias 'howm-view-summary-show 'riffle-summary-show)
(defalias 'howm-view-set-item-list 'riffle-set-item-list)
;; for howmz
;; http://noir.s7.xrea.com/archives/000136.html
;; http://noir.s7.xrea.com/pub/zaurus/howmz.el
(defalias 'howm-view-sort-items 'howm-sort)
;;; variables
(defvar howm-view-font-lock-silent t
"Inhibit font-lock-verbose if non-nil.")
(howm-defvar-risky howm-view-summary-font-lock-keywords
`((,(concat "\\(^[^ \t\r\n].*?\\)" (regexp-quote howm-view-summary-sep))
1 howm-view-name-face)
("^ +" . howm-view-empty-face)))
(howm-defvar-risky howm-view-contents-font-lock-keywords nil)
(howm-defvar-risky *howm-view-font-lock-keywords* nil
"For internal use. Don't set this variable.
This is a shameful global variable and should be clearned in future.")
(howm-defvar-risky howm-view-font-lock-keywords nil
"For internal use.")
(defvar howm-view-font-lock-first-time t
"For internal use.")
(make-variable-buffer-local 'howm-view-font-lock-keywords)
(make-variable-buffer-local 'howm-view-font-lock-first-time)
(defvar howm-view-mode-line-text ""
"For internal use.")
(make-variable-buffer-local 'howm-view-mode-line-text)
;;; modes
(riffle-define-derived-mode howm-view-summary-mode riffle-summary-mode "HowmS"
"memo viewer (summary mode)
key binding
--- -------\\<howm-view-summary-mode-map>
\\[howm-view-summary-open] Open file
\\[next-line] Next item
\\[previous-line] Previous item
\\[riffle-pop-or-scroll-other-window] Pop and scroll contents
\\[scroll-other-window-down] Scroll contents
\\[riffle-scroll-other-window] Scroll contents one line
\\[riffle-scroll-other-window-down] Scroll contents one line
\\[riffle-summary-to-contents] Concatenate all contents
\\[howm-view-summary-next-section] Next file (Skip items in the same file)
\\[howm-view-summary-previous-section] Previous file (Skip items in the same file)
\\[howm-view-filter-uniq] Remove duplication of same file
\\[howm-view-summary-shell-command] Execute command in inferior shell
\\[delete-other-windows] Delete contents window
\\[riffle-pop-window] Pop contents window
\\[riffle-toggle-window] Toggle contents window
\\[howm-list-toggle-title] Show/Hide Title
\\[howm-view-filter] Filter (by date, contents, etc.)
\\[howm-view-filter-by-contents] Search (= filter by contents)
\\[howm-view-sort] Sort (by date, summary line, etc.)
\\[howm-view-sort-reverse] Reverse order
\\[howm-view-dired] Invoke Dired-X
\\[describe-mode] This help
\\[howm-view-kill-buffer] Quit
"
(howm-view-summary-mode-body))
(defun howm-view-summary-mode-body ()
(make-local-variable 'font-lock-keywords)
(cheat-font-lock-mode howm-view-font-lock-silent)
(when howm-view-font-lock-first-time
(setq howm-view-font-lock-first-time nil)
(cheat-font-lock-merge-keywords howm-user-font-lock-keywords
howm-view-summary-font-lock-keywords
;; dirty! Clean dependency between files.
(howm-reminder-today-font-lock-keywords)))
(when *howm-view-font-lock-keywords*
(setq howm-view-font-lock-keywords *howm-view-font-lock-keywords*))
(when howm-view-font-lock-keywords
(cheat-font-lock-merge-keywords howm-view-font-lock-keywords
howm-user-font-lock-keywords
howm-view-summary-font-lock-keywords))
;; font-lock-set-defaults removes these local variables after 2008-02-24
(set (make-local-variable 'font-lock-keywords-only) t)
(set (make-local-variable 'font-lock-keywords-case-fold-search) t)
(setq howm-view-mode-line-text "")
(setq mode-line-format
(cons '(:eval howm-view-mode-line-text)
(default-value 'mode-line-format)))
;; (setq font-lock-keywords-case-fold-search
;; howm-view-grep-ignore-case-option)
(cheat-font-lock-fontify)
)
(riffle-define-derived-mode howm-view-contents-mode riffle-contents-mode "HowmC"
"memo viewer (contents mode)
key binding
--- -------\\<howm-view-contents-mode-map>
\\[howm-view-contents-open] Open file
\\[next-line] Next line
\\[previous-line] Previous line
\\[scroll-up] Scroll up
\\[scroll-down] Scroll down
\\[riffle-scroll-up] Scroll one line up
\\[riffle-scroll-down] Scroll one line down
\\[riffle-contents-to-summary] Summary
\\[riffle-contents-goto-next-item] Next item
\\[riffle-contents-goto-previous-item] Previous item
\\[howm-view-filter] Filter (by date, contents, etc.)
\\[howm-view-filter-by-contents] Search (= filter by contents)
\\[howm-view-sort] Sort
\\[howm-view-sort-reverse] Reverse order
\\[howm-view-dired] Invoke Dired-X
\\[describe-mode] This help
\\[howm-view-kill-buffer] Quit
"
; (kill-all-local-variables)
(make-local-variable 'font-lock-keywords)
(cheat-font-lock-mode howm-view-font-lock-silent)
(let ((ck `((,howm-view-header-regexp (0 howm-view-hilit-face))))
(sk (or (howm-view-font-lock-keywords)
*howm-view-font-lock-keywords*)))
;; ;; extremely dirty!! [2003/10/06 21:08]
;; (sk (or (with-current-buffer (riffle-summary-buffer)
;; font-lock-keywords)
;; *howm-view-font-lock-keywords*)))
(cheat-font-lock-merge-keywords sk ck
howm-user-font-lock-keywords
howm-view-contents-font-lock-keywords)
;; font-lock-set-defaults removes these local variables after 2008-02-24
(set (make-local-variable 'font-lock-keywords-only) t)
(set (make-local-variable 'font-lock-keywords-case-fold-search)
howm-view-grep-ignore-case-option)
(cheat-font-lock-fontify)
))
(defun howm-view-font-lock-keywords ()
(with-current-buffer (riffle-summary-buffer)
howm-view-font-lock-keywords))
;;; keymaps
;; (defvar howm-view-summary-mode-map nil)
;; (defvar howm-view-contents-mode-map nil)
(defun howm-view-define-common-key (keymap)
(let ((m keymap))
;; (define-key m "?" 'howm-view-help)
(define-key m "f" 'howm-view-filter)
(define-key m "G" 'howm-view-filter-by-contents)
(define-key m "S" 'howm-view-sort)
(define-key m "R" 'howm-view-sort-reverse)
(define-key m "q" 'howm-view-kill-buffer)
(define-key m "X" 'howm-view-dired)
))
(let ((m howm-view-summary-mode-map))
(define-key m "\C-m" 'howm-view-summary-open)
(define-key m "\C-j" 'howm-view-summary-open)
(define-key m "u" 'howm-view-filter-uniq)
(define-key m "!" 'howm-view-summary-shell-command)
(define-key m "T" 'howm-list-toggle-title) ;; defined in other file. dirty!
;; (define-key m howm-reminder-quick-check-key 'howm-reminder-quick-check)
;; (define-key m ";" 'howm-view-invoke-action-lock)
(define-key m "\C-i" 'howm-view-summary-next-section)
(define-key m "\M-\C-i" 'howm-view-summary-previous-section)
(define-key m [tab] 'howm-view-summary-next-section)
(define-key m [(meta tab)] 'howm-view-summary-previous-section)
(howm-view-define-common-key m))
(let ((m howm-view-contents-mode-map))
(define-key m "\C-m" 'howm-view-contents-open)
(define-key m "\C-j" 'howm-view-contents-open)
(howm-view-define-common-key m))
;;; summary
(defun howm-view-summary (&optional name item-list fl-keywords)
(let* ((*howm-view-font-lock-keywords* fl-keywords) ;; ok? [2008-07-11]
(r (riffle-summary name item-list ':howm
(howm-view-in-background-p))))
(if (null r)
(message "No match")
(howm-view-expire-uniq)
;; We want to entry font-lock keywords even when background-p.
(when *howm-view-font-lock-keywords*
(setq howm-view-font-lock-keywords *howm-view-font-lock-keywords*)))
r))
;; (defun howm-view-summary (&optional name item-list)
;; (let ((*howm-view-font-lock-keywords* t))
;; (riffle-summary name item-list ':howm)))
(defun howm-view-summary-open (&optional reverse-delete-p)
(interactive "P")
(when (not (and howm-view-summary-keep-cursor
(get-buffer-window (riffle-contents-buffer))))
(riffle-summary-check t))
(let* ((p (riffle-persistent-p howm-view-summary-persistent))
(persistent (if reverse-delete-p
(not p)
p)))
(howm-record-view-window-configuration)
(howm-view-summary-open-sub (not persistent))))
(defun howm-view-summary-open-sub (&optional kill)
(interactive "P")
(let ((b (riffle-contents-buffer))
(looking-at-str (buffer-substring-no-properties (point)
(line-end-position))))
(riffle-pop-to-buffer b howm-view-summary-window-size)
(let ((howm-view-open-hook nil)) ;; Don't execute it in contents-open.
(howm-view-contents-open-sub kill))
(end-of-line)
(or (search-backward looking-at-str (line-beginning-position) t)
(beginning-of-line))
(run-hooks 'howm-view-open-hook)))
(defvar howm-view-summary-item-previous-name nil
"for internal use")
(defun howm-view-summary-item (item)
;; Clean me. This depends on implementation of `riffle-summary-show'
;; severely.
(when (eq (point) (point-min))
(setq howm-view-summary-item-previous-name ""))
(let* ((f (howm-item-name item))
(name (if (and howm-view-summary-omit-same-name
(string= f howm-view-summary-item-previous-name))
""
(progn
(setq howm-view-summary-item-previous-name f)
(howm-view-item-basename item t))))
(h (format howm-view-summary-format name)))
(concat h (howm-view-item-summary item))))
(defun howm-view-summary-next-section (&optional n)
(interactive "P")
(setq n (or n 1))
(let ((i (abs n))
(step (if (>= n 0) 1 -1)))
(while (and (> i 0)
(howm-view-summary-next-section-sub step))
(setq i (1- i)))))
(defun howm-view-summary-previous-section (&optional n)
(interactive "P")
(setq n (or n 1))
(howm-view-summary-next-section (- n)))
(defun howm-view-summary-next-section-sub (step)
;; inefficient. so what?
(let* ((f (lambda ()
(howm-view-item-filename (riffle-summary-current-item))))
;; (riffle-controller 'section (riffle-summary-current-item))))
(cont-p (lambda ()
(save-excursion
(let ((a (funcall f)))
(forward-line -1)
(string= a (funcall f)))))))
(while (and (= (forward-line step) 0)
(funcall cont-p))
;; no body
)))
;;; contents
(defun howm-view-contents-open (&optional reverse-delete-p)
(interactive "P")
(let* ((p (riffle-persistent-p howm-view-contents-persistent))
(persistent (if reverse-delete-p
(not p)
p)))
(howm-record-view-window-configuration)
(howm-view-contents-open-sub (not persistent))))
(defvar *howm-view-item-privilege* nil) ;; dirty
(defun howm-view-contents-open-sub (&optional kill)
(let* ((item (riffle-contents-current-item))
(page (howm-item-page item))
(offset (howm-view-item-offset item))
(pos (- (point) offset))
(viewer (howm-view-external-viewer page)))
(when kill
(riffle-kill-buffer))
(when (howm-view-item-privilege item)
(riffle-restore-window-configuration)) ;; force without mode check
(setq *howm-view-item-privilege* (howm-view-item-privilege item)) ;; dirty
(run-hooks 'howm-view-before-open-hook)
(if viewer
(howm-view-call-external-viewer viewer page)
(howm-view-open-item item
(lambda ()
(when (or (< pos (point-min)) (<= (point-max) pos))
(widen))
(goto-char pos))
t))
(run-hooks 'howm-view-open-hook)))
(defun howm-view-open-item (item &optional position-setter merely)
(howm-page-open (howm-item-page item))
(howm-view-set-mark-command)
(if position-setter
(funcall position-setter)
(howm-view-set-place (howm-item-place item)))
(recenter howm-view-open-recenter)
(when (not merely)
(howm-view-open-postprocess)))
(defun howm-view-open-postprocess ()
(run-hooks 'howm-view-open-hook))
(defvar howm-view-previous-section-page nil "For internal use")
(defvar howm-view-previous-section-beg nil "For internal use")
(defvar howm-view-previous-section-end nil "For internal use")
(defun howm-view-contents-item (item)
(when (howm-buffer-empty-p)
(setq howm-view-previous-section-page ""
howm-view-previous-section-beg nil
howm-view-previous-section-end nil))
(let* ((page (howm-item-page item))
(place (howm-view-item-place item))
(peq (howm-page= page howm-view-previous-section-page)) ;; dirty!
(done-p (if place
(and peq
(<= howm-view-previous-section-beg place)
(<= place howm-view-previous-section-end))
peq)))
(if done-p
""
(let* ((header (if (null (cdr (howm-view-item-list))) ;; dirty!
""
(format howm-view-header-format
(howm-page-abbreviate-name page))))
(header-length (howm-view-string-point-count header))
(viewer (howm-view-external-viewer page)))
(concat header
(howm-view-contents-item-sub item page place header viewer
(+ (point) header-length)))))))
(defvar howm-view-string-point-count-strict nil)
(defun howm-view-string-point-count (str)
"Count points of string STR.
Namely, it is the difference between start position and end position
of STR if STR is inserted to a buffer.
It looks to be simply equal to (length STR) on emacs-21.1.1.
But I'm not sure for multi-byte characters on other versions of emacsen."
(if howm-view-string-point-count-strict
(with-temp-buffer
(insert str)
(- (point) (point-min)))
;; I assume (length (buffer-substring-no-properties START END))
;; is equal to (abs (- START END))). Is it correct?
;; (cf.) snap://Info-mode/elisp#Positions
(length str)))
(defun howm-view-contents-item-sub (item page place header viewer c)
(with-temp-buffer
(let (b e h)
(if viewer
(howm-view-contents-indicator viewer page)
(howm-page-insert page))
(if place
(progn
(riffle-set-place place)
(setq h (point))
(let ((r (howm-view-contents-region page)))
(setq b (car r)
e (cadr r))))
(setq b (point-min)
e (point-max)
h b))
(howm-view-item-set-offset item (- c b))
(howm-view-item-set-home item (+ c (- b) h))
(setq howm-view-previous-section-page page ;; dirty!
howm-view-previous-section-beg (riffle-get-place b)
howm-view-previous-section-end (riffle-get-place e))
(buffer-substring-no-properties b e))))
(defvar howm-view-preview-narrow t)
(defun howm-view-contents-region (filename)
(when filename
(howm-page-set-configuration filename))
(if (or howm-view-preview-narrow
(not (riffle-preview-p)))
(howm-view-paragraph-region)
(list (point-min) (point-max))))
(defun howm-view-contents-indicator (viewer fname)
(insert (howm-viewer-indicator viewer fname)))
(defun howm-view-paragraph-region (&optional include-following-blank-p)
(let ((b (save-excursion
(end-of-line)
(re-search-backward howm-view-title-regexp
nil 'to-limit)
(line-beginning-position)))
(e (save-excursion
(end-of-line)
(let ((found (re-search-forward howm-view-title-regexp
nil 'to-limit)))
(if include-following-blank-p
(if found (match-beginning 0) (point-max))
(progn
(if found
(forward-line -1)
(goto-char (point-max)))
; (end-of-line)
(while (and (looking-at "^$")
(= (forward-line -1) 0)) ;; successful
nil) ;; dummy
(end-of-line)
(point)))))))
(list b e)))
(defun howm-view-set-mark-command ()
(set-mark-command nil)
(howm-deactivate-mark))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; misc.
(defun howm-view-file-list (&optional item-list)
(howm-cl-remove-duplicates* (mapcar #'howm-view-item-filename
(or item-list (howm-view-item-list)))
:test #'howm-page=))
(defun howm-view-mtime (file)
(howm-view-time-to-string (howm-page-mtime file)))
;; (defun howm-view-xtime (file x)
;; (let* ((a (file-attributes file))
;; (n (cdr (assoc x '((a . 4) (m . 5) (c . 6)))))
;; (ti (nth n a)))
;; (howm-view-time-to-string ti)))
(defun howm-view-time-to-string (ti)
(format-time-string "%Y%m%d-%H%M%S" ti))
(defun howm-view-string> (a b)
(string< b a))
(defun howm-view-string<= (a b)
(not (string< b a)))
(defun howm-view-string< (a b)
(string< a b))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; dir
(defun howm-view-directory (dir &optional recursive-p)
(howm-view-summary "" (howm-folder-items dir recursive-p)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; filter
(defun howm-view-filter (&optional remove-p)
(interactive "P")
(let* ((table howm-view-filter-methods)
(command (completing-read (if remove-p
"(Reject) filter by: "
"filter by: ")
table nil t)))
(call-interactively (cdr (assoc command table)))))
(defalias 'howm-view-filter-uniq #'howm-view-toggle-uniq)
(defvar howm-view-uniq-previous nil)
(make-variable-buffer-local 'howm-view-uniq-previous)
(defun howm-view-toggle-uniq ()
(interactive)
(if howm-view-uniq-previous
(howm-view-summary-rebuild howm-view-uniq-previous)
(let ((prev (howm-view-item-list)))
(howm-view-filter-doit #'howm-filter-items-uniq)
;; need to set howm-view-uniq-previous AFTER rebuilding of
;; the summary buffer because howm-view-expire-uniq is called in it.
(setq howm-view-uniq-previous prev))))
(defun howm-view-expire-uniq ()
(setq howm-view-uniq-previous nil))
(defun howm-view-filter-by-name (&optional remove-p regexp)
(interactive "P")
(howm-view-filter-by-name/summary #'howm-filter-items-by-name
regexp remove-p))
(defun howm-view-filter-by-summary (&optional remove-p regexp)
(interactive "P")
(howm-view-filter-by-name/summary #'howm-filter-items-by-summary
regexp remove-p))
(defun howm-view-filter-by-name/summary (filter regexp remove-p)
(let* ((r (or regexp (howm-view-filter-read-from-minibuffer "Regexp: "
remove-p)))
(f `(lambda (item-list rmv-p)
(funcall #',filter item-list ,r rmv-p))))
(howm-view-filter-doit f remove-p)))
(defun howm-view-filter-by-date (&optional remove-p)
(interactive "P")
(howm-view-filter-by-time-range #'howm-filter-items-by-date
remove-p))
(defun howm-view-filter-by-reminder (&optional remove-p)
(interactive "P")
(howm-view-filter-by-time-range #'howm-filter-items-by-reminder remove-p))
(defun howm-view-filter-by-mtime (&optional remove-p range)
(interactive "P")
(howm-view-filter-by-time-range #'howm-filter-items-by-mtime remove-p range))
(defun howm-view-filter-by-time-range (filter &optional remove-p range)
(let* ((r (or range (howm-view-ask-time-range remove-p)))
(from (car r))
(to (cadr r))
(f `(lambda (item-list rmv-p)
(funcall #',filter item-list ',from ',to rmv-p))))
(howm-view-filter-doit f remove-p)))
(defun howm-view-filter-by-region (beg end)
(interactive "r")
(let ((r (mapcar #'howm-view-line-number (list beg end))))
(howm-view-filter-by-line-range (car r) (cadr r))))
(defvar howm-view-filter-by-around-default 10)
(defun howm-view-filter-by-around (&optional distance)
(interactive "P")
(let* ((d (or distance howm-view-filter-by-around-default))
(c (howm-view-line-number)))
(howm-view-filter-by-line-range (- c d) (+ c d))))
(defun howm-view-filter-by-line-range (beg end)
(let ((f `(lambda (item-list remove-p)
(when remove-p
(error "Not supported."))
;; beg and end are counted as 1,2,3,...
(cl-subseq item-list
(max (1- ,beg) 0)
;; end is included.
(min ,end (length item-list))))))
(howm-view-filter-doit f)))
(defun howm-view-filter-by-contents (&optional remove-p regexp)
(interactive "P")
(let ((r (or regexp (howm-view-filter-read-from-minibuffer
"Search in result (grep): "
remove-p))))
(if remove-p
(howm-view-remove-by-contents r)
(howm-view-search-in-result r))))
(howm-if-ver1dot3 nil
(defcustom howm-view-search-in-result-correctly t
"*Non nil if search-in-result should be aware of paragraph."
:type 'boolean
:group 'howm-search))
(defun howm-view-search-in-result (regexp)
;; (interactive "sSearch in result (grep): ")
(let* ((orig (howm-view-name))
(name (if (string= orig "")
regexp
(format "%s&%s" orig regexp)))
(orig-item-list (howm-view-item-list))
(folder (howm-make-folder-from-items orig-item-list)))
(howm-write-history regexp)
(howm-view-search-folder regexp folder name)
(when howm-view-search-in-result-correctly
(howm-view-summary-rebuild (howm-item-list-filter (howm-view-item-list)
orig-item-list)))))
(defun howm-view-remove-by-contents (regexp)
;; (interactive "s(Reject) Search in result (grep): ")
(let ((howm-v-r-b-c-regexp regexp))
(howm-view-sort/filter-doit
(lambda (item-list switch)
(howm-filter-items-by-contents item-list howm-v-r-b-c-regexp t)))))
(defun howm-view-sort/filter-doit (proc &optional switch)
(let ((kw font-lock-keywords))
(prog1
;; return item-list for
;; https://howm.osdn.jp/cgi-bin/hiki/hiki.cgi?HidePrivateReminder
(howm-view-summary-rebuild (funcall proc (howm-view-item-list) switch))
(setq font-lock-keywords kw))))
(defalias 'howm-view-filter-doit 'howm-view-sort/filter-doit)
;; For backward compatibility with howmoney. Don't use this.
(defun howm-view-filter-general (pred)
(howm-view-filter-doit (lambda (item-list dummy)
(cl-remove-if-not pred item-list))))
;; (defun howm-view-filter-general (pred &optional remove-p with-index)
;; (let* ((item-list (howm-view-item-list))
;; (s (if with-index
;; (howm-map-with-index #'list item-list)
;; item-list))
;; (r (if remove-p
;; (cl-remove-if pred s)
;; (cl-remove-if-not pred s)))
;; (filtered (if with-index
;; (mapcar #'car r)
;; r)))
;; (howm-view-summary-rebuild filtered)))
(defmacro howm-filter-items (pred lis &optional remove-p)
`(if ,remove-p
(cl-remove-if ,pred ,lis)
(cl-remove-if-not ,pred ,lis)))
(defun howm-filter-items-uniq (item-list &optional remove-p)
(when remove-p
(error "Not supported."))
(let* ((howm-view-filter-uniq-prev (if howm-view-search-in-result-correctly
(cons "" nil)
""))
(pred (if howm-view-search-in-result-correctly
(lambda (item)
(let ((page (howm-item-page item))
(place (howm-item-place item))
(range (howm-item-range item))
(p-page (car howm-view-filter-uniq-prev))
(p-range (cdr howm-view-filter-uniq-prev)))
(prog1
(not (and (howm-page= page p-page)
(and place p-range
(<= (car p-range) place)
(<= place (cadr p-range)))))
(setq howm-view-filter-uniq-prev (cons page range)))))
;; old code
(lambda (item)
(let ((f (howm-view-item-filename item)))
(prog1
(not (howm-page= f howm-view-filter-uniq-prev))
(setq howm-view-filter-uniq-prev f)))))))
(cl-remove-if-not pred item-list)))
(defun howm-filter-items-by-name (item-list regexp &optional remove-p)
(howm-filter-items-by-name/summary #'howm-view-item-basename
item-list regexp remove-p))
(defun howm-filter-items-by-summary (item-list regexp &optional remove-p)
(howm-filter-items-by-name/summary #'howm-view-item-summary
item-list regexp remove-p))
(defun howm-filter-items-by-name/summary (accessor item-list regexp remove-p)
(howm-filter-items (lambda (item)
(string-match regexp (funcall accessor item)))
item-list remove-p))
(defun howm-filter-items-by-date (item-list from to &optional remove-p)
(let* ((form (howm-view-file-name-format))
(fts (mapcar (lambda (x)
(file-name-nondirectory (format-time-string form x)))
(list from to)))
(fs (car fts))
(ts (cadr fts)))
(howm-filter-items (lambda (item)
(let ((cs (howm-view-item-basename item)))
(and (howm-view-string<= fs cs)
(howm-view-string< cs ts))))
item-list remove-p)))
(defun howm-filter-items-by-reminder (item-list from to &optional remove-p)
(let* ((from-str (format-time-string howm-date-format from))
(to-str (format-time-string howm-date-format to))
(reg (howm-reminder-regexp howm-reminder-types)))
(howm-filter-items
(lambda (item)
(let ((s (howm-view-item-summary item)))
(and (string-match reg s)
(let* ((x (match-string-no-properties 0 s)) ;; [2004-02-07]@
(d (and (string-match howm-date-regexp x)
(match-string-no-properties 0 x)))) ;; [2004-02-07]
(and (howm-view-string<= from-str d)
(howm-view-string< d to-str))))))
item-list remove-p)))
(defun howm-filter-items-by-mtime (item-list from to &optional remove-p)
(let ((fs (howm-view-time-to-string from))
(ts (howm-view-time-to-string to)))
(howm-filter-items
(lambda (item)
(let ((cs (howm-view-mtime (howm-view-item-filename item))))
(and (howm-view-string<= fs cs)
(howm-view-string< cs ts))))
item-list remove-p)))
(defun howm-filter-items-by-contents (item-list regexp &optional remove-p)
(let* ((match (howm-view-search-folder-items-fi regexp item-list)))
(if howm-view-search-in-result-correctly
(howm-item-list-filter item-list match remove-p)
;; old behavior
(let ((match-names (howm-cl-remove-duplicates*
(mapcar #'howm-item-name match))))
(howm-filter-items (lambda (item)
(member (howm-item-name item) match-names))
item-list remove-p)))))
(defun howm-view-file-name-format ()
howm-file-name-format) ;; defined in howm-common.el
(defun howm-view-ask-time-range (&optional remove-p)
(let* ((now (current-time))
(from (howm-view-ask-time "From" now t remove-p))
(to (howm-view-ask-time "To" from nil remove-p)))
(list from to)))
(defvar howm-view-min-year 1950)
(defvar howm-view-max-year 2030)
(defun howm-view-ask-time (prompt default &optional from-p remove-p)
(let* ((z (decode-time default))
(yd (nth 5 z))
(md (nth 4 z))
(dd (nth 3 z)))
(let (y0 m0 d0 hour0 min0 sec0)
(if from-p
(setq y0 howm-view-min-year m0 1 d0 1
hour0 0 min0 0 sec0 0)
(setq y0 howm-view-max-year m0 12 d0 'last-day-of-month
hour0 24 min0 0 sec0 0))
(let ((y (howm-ask-time-sub prompt "year" yd remove-p)))
(if (null y)
(howm-view-encode-time sec0 min0 hour0 d0 m0 y0)
(let ((m (howm-ask-time-sub prompt "month" md remove-p)))
(if (null m)
(howm-view-encode-time sec0 min0 hour0 d0 m0 y)
(let ((d (or (howm-ask-time-sub prompt "date" dd remove-p) d0)))
(howm-view-encode-time sec0 min0 hour0 d m y)))))))))
(defun howm-ask-time-sub (prompt ymd default remove-p)
(let* ((message (format "%s %s (* = no limit) [%d]: " prompt ymd default))
(raw (howm-view-filter-read-from-minibuffer message remove-p))
(n (if (string= raw "")
default
(string-to-number raw))))
(if (= n 0)
nil
n)))
(defun howm-view-encode-time (sec min hour d m y)
(when (eq d 'last-day-of-month)
(setq m (+ m 1))
(setq d -1))
(encode-time sec min hour d m y))
(defun howm-view-filter-read-from-minibuffer (message &optional remove-p)
(read-from-minibuffer (if remove-p
(concat "(Reject) " message)
message)))
(defun howm-view-summary-rebuild (item-list &optional fl-keywords)
(howm-view-summary (howm-view-name) item-list fl-keywords))
(let* ((h (regexp-quote howm-view-title-header))
(t1 (format "Skip \"%s \"" howm-view-title-header))
(r1 (format "^\\(%s\\)? *$" h))
(t2 (format "Skip \"%s \" and \"[xxxx-xx-xx xx:xx]\""
howm-view-title-header))
(r2 (format "\\(%s\\)\\|\\(^\\[[-: 0-9]+\\]\\)" r1)))
(howm-if-ver1dot3 nil
(defcustom howm-view-title-skip-regexp r2
"*Regular expression for lines which should not be titles.
If the original title matches this regexp, the first non-matched line
is shown as title instead.
Nil disables this feature.
This feature does not work when `howm-view-search-in-result-correctly' is nil."
:type `(radio (const :tag "Off" nil)
(const :tag ,t1 ,r1)
(const :tag ,t2 ,r2)
regexp)
:group 'howm-title
:group 'howm-efficiency)))
(defcustom howm-view-list-title-type 1
"*Type of showing title in summary buffer.
Value 1 means \"show title instead of summary\".
Value 2 means \"show title before summary\".
You may want to set `howm-view-summary-format' to be \"\" if you never need
to see file names."
:type '(radio (const :tag "title instead of summary"
1)
(const :tag "title before summary"
2))
:group 'howm-experimental)
(defun howm-view-list-title (title-regexp)
(howm-view-summary-rebuild (howm-entitle-items
title-regexp (howm-view-item-list))))
(defun howm-entitle-items (title-regexp item-list)
(if (= howm-view-list-title-type 1)
(howm-entitle-items-style1 title-regexp item-list)
(howm-entitle-items-style2 title-regexp item-list)))
(defun howm-entitle-items-style1 (title-regexp item-list)
"Put title instead of summary."
(let ((items (howm-view-search-folder-items-fi title-regexp item-list)))
(if howm-view-search-in-result-correctly
(let* ((r (howm-item-list-filter items item-list 'with-rest))
(hit-items (car r))
(nohit-items (cdr r))
;; should I use (howm-classify #'howm-item-place nohit-items) ?
(noplace-nohit-items
(cl-remove-if #'howm-item-place nohit-items))
(rest-items
(howm-item-list-filter (cl-remove-if-not #'howm-item-place
nohit-items)
items t))
(all-items (append hit-items noplace-nohit-items rest-items)))
(when howm-view-title-skip-regexp
(mapc #'howm-view-change-title all-items))
all-items)
(let* ((pages (howm-cl-remove-duplicates* (mapcar #'howm-item-page
item-list)))
(hit-pages (mapcar #'howm-item-page items))
(nohit-pages (cl-remove-if
(lambda (p) (cl-member p hit-pages
:test #'howm-page=))
pages))
(nohit-items (mapcar #'howm-make-item nohit-pages))
(all-items (if (null nohit-items)
items
(append items nohit-items))))
all-items))))
(defvar howm-entitle-items-style2-max-length 20)
(defvar howm-entitle-items-style2-format "%-13s | %s") ;; for title and summary
(defvar howm-entitle-items-style2-title-line nil) ;; independent title line?
(defun howm-entitle-items-style2 (title-regexp item-list)
"Put title before summary."
;; fix me: howm-item-place is not set for howm-list-all
(let ((last-title ""))
(cl-mapcan
(lambda (item)
(let ((orig (howm-item-summary item))
(titles (howm-item-titles item)))
(cl-mapcan
(lambda (s)
(if (string= s last-title)
(setq s "")
(setq last-title s))
(when (> (length s) howm-entitle-items-style2-max-length)
(setq s (substring s 0 howm-entitle-items-style2-max-length)))
(mapcar (lambda (x)
(let ((i (howm-item-dup item)))
(howm-item-set-summary i x)
i))
(if (and howm-entitle-items-style2-title-line
(not (string= s "")))
(list (format howm-entitle-items-style2-format
s "")
(format howm-entitle-items-style2-format
"" orig))
(list (format howm-entitle-items-style2-format
s orig)))))
(or titles (list "")))))
item-list)))
;;; detect items in same paragraph (= entry = memo. sorry for inconsistent terminology)
(defun howm-item-with-temp-buffer (item proc)
(with-temp-buffer
(howm-page-insert (howm-item-page item))
(let* ((p (howm-item-place item))
(r (if (null p)
(list (point-min) (point-max))