1 ;;; howm-mode.el --- Wiki-like note-taking tool
2 ;;; Copyright (C) 2002, 2003, 2004, 2005-2020
3 ;;; HIRAOKA Kazuyuki <khi@users.osdn.me>
5 ;;; This program is free software; you can redistribute it and/or modify
6 ;;; it under the terms of the GNU General Public License as published by
7 ;;; the Free Software Foundation; either version 1, or (at your option)
10 ;;; This program is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;; GNU General Public License for more details.
15 ;;; The GNU General Public License is available by anonymouse ftp from
16 ;;; prep.ai.mit.edu in pub/gnu/COPYING. Alternately, you can write to
17 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
19 ;;;--------------------------------------------------------------------
21 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22 ;; Backward compatibility
24 ;; (require 'howm-mode) in .emacs is obsolete. Use (require 'howm) instead.
26 ;; This must be earlier than (require 'howm-common), because
27 ;; howm-common needs cl, and (require 'cl) should be written in howm.el.
28 (when (not (featurep 'howm))
29 (message "Warning: Requiring howm-mode is obsolete. Require howm instead.")
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 ;; You can easily modify them.
47 (howm-defvar-risky howm-template
48 (concat howm-view-title-header " %title%cursor\n%date %file\n\n")
49 "Contents of new file. %xxx are replaced with specified items.
50 If it is a list, <n>-th one is used when you type C-u <n> M-x howm-create.
51 If it is a function, it is called to get template string with the argument <n>.")
52 (defvar howm-keyword-header "<<<"
53 "Header string for declaration of keyword (implicit link).")
54 (defvar howm-ref-header ">>>"
55 "Header string for explicit link.")
56 (defvar howm-lighter " howm"
57 "Mode line for howm-mode")
59 (defvar howm-inhibit-title-file-match t
60 "If non-nil, inhibit howm-list-title when search string matches file name")
61 (defvar howm-list-all-title nil) ;; obsolete [2003-11-30]
62 (defvar howm-list-recent-title nil) ;; obsolete [2003-11-30]
64 (defvar howm-default-key-table
66 ;; ("key" func list-mode-p global-p)
68 ("l" howm-list-recent t t)
69 ("a" howm-list-all t t)
70 ("g" howm-list-grep t t)
71 ("s" howm-list-grep-fixed t t)
72 ("m" howm-list-migemo t t)
73 ("t" howm-list-todo t t)
74 ("y" howm-list-schedule t t)
75 ("b" howm-list-buffers t t)
76 ("x" howm-list-mark-ring t t)
79 ("e" howm-remember t t)
81 ("." howm-find-today nil t)
82 (":" howm-find-yesterday nil t)
83 ("A" howm-list-around)
84 ("h" howm-history nil t)
86 ("i" howm-insert-keyword nil t)
87 ("d" howm-insert-date nil t)
88 ("T" howm-insert-dtime nil t)
89 ("K" howm-keyword-to-kill-ring t t)
90 ("n" action-lock-goto-next-link)
91 ("p" action-lock-goto-previous-link)
92 ("Q" howm-kill-all t t)
93 (" " howm-toggle-buffer nil t)
95 ("P" howm-previous-memo)
98 ("C" howm-create-here nil t)
99 ("I" howm-create-interactively nil t)
100 ("w" howm-random-walk nil t)
101 ("M" howm-open-named-file t t)
103 "List of (key function list-mode-p global-p).
104 `howm-prefix' + this key is real stroke.
105 If optional argument list-mode-p is non-nil,
106 same key is also available in view mode.
107 It is further registered globally if global-p is non-nil."
110 (howm-defvar-risky howm-migemo-client nil
111 "Command name of migemo-client.
113 (setq howm-migemo-client '((type . cmigemo) (command . \"cmigemo\")))
114 Example of migemo-client (obsolete):
115 (setq howm-migemo-client \"migemo-client\")
116 See also `howm-migemo-client-option`")
117 (howm-defvar-risky howm-migemo-client-option nil
118 "List of option for migemo-client.
120 (setq howm-migemo-client-option
121 '(\"-q\" \"-d\" \"/usr/share/cmigemo/utf-8/migemo-dict\"))
122 Example of migemo-client (obsolete):
123 (setq howm-migemo-client-option '(\"-H\" \"::1\")
124 See also `howm-migemo-client`")
128 ;; Be careful to keep consistency.
130 (howm-defvar-risky howm-keyword/ref-regexp-format
131 "\\(%s\\)[ \t]*\\([^ \t\r\n].*\\)")
132 (howm-defvar-risky howm-keyword-format
133 (format "%s %%s" howm-keyword-header)
134 "Format for declaration of keyword. See `format'.")
135 (howm-defvar-risky howm-keyword-regexp
136 (format howm-keyword/ref-regexp-format (regexp-quote howm-keyword-header)))
137 (howm-defvar-risky howm-keyword-regexp-hilit-pos 1)
138 (howm-defvar-risky howm-keyword-regexp-pos 2)
139 (howm-defvar-risky howm-ref-regexp
140 (format howm-keyword/ref-regexp-format (regexp-quote howm-ref-header))
141 "Regexp for explicit link.")
142 (howm-defvar-risky howm-ref-regexp-hilit-pos 0
143 "Position of search string in `howm-ref-regexp'")
144 (howm-defvar-risky howm-ref-regexp-pos 2
145 "Position of search string in `howm-ref-regexp'")
146 (howm-defvar-risky howm-wiki-regexp "\\[\\[\\([^]\r\n]+\\)\\]\\]"
147 "Regexp for explicit link.")
148 (howm-defvar-risky howm-wiki-regexp-hilit-pos 1
149 "Position of hilight in `howm-wiki-regexp'")
150 (howm-defvar-risky howm-wiki-regexp-pos 1
151 "Position of search string in `howm-wiki-regexp'")
152 (howm-defvar-risky howm-wiki-format "[[%s]]"
153 "Format for declaration of wiki word. See `format'.")
155 (howm-defvar-risky howm-template-rules
156 '(("%title" . howm-template-title)
157 ("%date" . howm-template-date)
158 ("%file" . howm-template-previous-file)
159 ("%cursor" . howm-template-cursor))) ;; Cursor must be the last rule.
160 (defvar howm-template-date-format howm-dtime-format
161 "%date is replaced with `howm-template-date-format'
162 in `howm-template'. See `format-time-string'")
163 (defvar howm-template-file-format (concat howm-ref-header " %s")
164 "%file is replaced with `homw-template-file-format'
165 in `howm-template'. %s is replaced with name of last file. See `format'.")
171 (defun howm-action-lock-general (command regexp pos
175 `(lambda (&optional dummy)
176 (let ((s (match-string-no-properties ,pos)))
177 ;; (when howm-keyword-case-fold-search
178 ;; (setq s (downcase s)))
179 (,command s ,@options)))
183 (defun howm-action-lock-search (regexp
185 &optional hilit-pos create-p open-unique-p)
186 (howm-action-lock-general 'howm-keyword-search
187 regexp pos hilit-pos create-p open-unique-p))
188 (defun howm-action-lock-related (regexp pos hilit-pos)
189 (howm-action-lock-general 'howm-list-related regexp pos hilit-pos))
191 (defun howm-action-lock-date-rule ()
192 (action-lock-general 'howm-action-lock-date howm-date-regexp 0 0))
194 (defun howm-action-lock-quote-keyword (keyword)
195 (let ((q (regexp-quote keyword)))
196 ;; when a regexp is specified, leave unmatched keywords.
197 (if (and (stringp howm-check-word-break)
198 (not (string-match howm-check-word-break keyword)))
200 ;; add word break checks
201 (concat "\\b" q "\\b"))))
203 (defun howm-action-lock-setup ()
204 (setq action-lock-case-fold-search howm-keyword-case-fold-search)
206 (let* ((date-al (action-lock-date "{_}" howm-dtime-format)))
207 ;; override the rule in action-lock.el
208 (action-lock-add-rules (list date-al) t))
209 (let* ((ks (howm-keyword-for-goto))
210 (r (mapconcat (if howm-check-word-break
211 #'howm-action-lock-quote-keyword
214 ;; The following optimization causes an error
215 ;; "Variable binding depth exceeds max-specpdl-size".
216 ;; (r (cond ((stringp howm-check-word-break)
217 ;; (mapconcat #'howm-action-lock-quote-keyword ks "\\|"))
219 ;; (regexp-opt ks (and howm-check-word-break 'word)))))
220 (wiki (howm-action-lock-search howm-wiki-regexp
222 howm-wiki-regexp-hilit-pos
224 (explicit (howm-action-lock-search howm-ref-regexp
226 howm-ref-regexp-hilit-pos))
227 (implicit (howm-action-lock-search r 0))
228 (rev (howm-action-lock-related howm-keyword-regexp
229 howm-keyword-regexp-pos
230 howm-keyword-regexp-hilit-pos))
231 (date (howm-action-lock-date-rule))
232 (done (howm-action-lock-reminder-done-rule))
236 ,@(if ks (list implicit) nil)
238 ,@(if (howm-menu-p) nil (list date done))
241 ;; don't override the rule in action-lock.el
242 ;; esp. http://xxx should call browser even if "<<< http" exists
243 (action-lock-add-rules all)))
245 (defun howm-file-name (&optional time)
246 (format-time-string howm-file-name-format
247 (or time (current-time))))
249 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
252 (easy-mmode-define-minor-mode howm-mode
253 "With no argument, this command toggles the mode.
254 Non-null prefix argument turns on the mode.
255 Null prefix argument turns off the mode.
257 When the mode is enabled, underlines are drawn on texts which match
258 to titles of other files. Typing \\[action-lock-magic-return] there,
259 you can jump to the corresponding file.
263 \\[action-lock-magic-return] Follow link
264 \\[howm-refresh] Refresh buffer
265 \\[howm-list-all] List all files
266 \\[howm-list-grep] Search (grep)
267 \\[howm-create] Create new file
268 \\[howm-dup] Duplicate current file
269 \\[howm-insert-keyword] Insert keyword
270 \\[howm-insert-date] Insert date
271 \\[howm-insert-dtime] Insert date with time
272 \\[howm-keyword-to-kill-ring] Copy current keyword to kill ring
273 \\[action-lock-goto-next-link] Go to next link
274 \\[action-lock-goto-previous-link] Go to previous link
275 \\[howm-next-memo] Go to next entry in current buffer
276 \\[howm-previous-memo] Go to previous entry in current buffer
277 \\[howm-first-memo] Go to first entry in current buffer
278 \\[howm-last-memo] Go to last entry in current buffer
279 \\[howm-create-here] Add new entry to current buffer
280 \\[howm-create-interactively] Create new file interactively (not recommended)
281 \\[howm-random-walk] Browse random entries automtically
284 howm-lighter ;; mode-line
285 (mapcar (lambda (entry)
286 (let ((k (car entry))
288 (cons (concat howm-prefix k) f)))
289 howm-default-key-table)
292 ;; emacs20's easy-mmode-define-minor-mode can't have body. sigh...
293 (add-hook 'howm-mode-on-hook 'howm-initialize-buffer)
294 (add-hook 'howm-mode-off-hook 'howm-restore-buffer)
296 (defun howm-set-keymap ()
297 (mapc (lambda (entry)
298 (let* ((k (car entry))
300 (list-mode-p (cl-caddr entry))
301 (global-p (cl-cadddr entry))
302 (pk (concat howm-prefix k)))
303 (define-key howm-mode-map pk f)
308 (list howm-view-summary-mode-map
309 howm-view-contents-mode-map)))
311 (define-key global-map pk f))))
312 howm-default-key-table)
313 (define-key howm-mode-map "\C-x\C-s" 'howm-save-buffer))
316 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
319 (defun howm-refresh ()
323 (howm-initialize-buffer)))
325 (defun howm-initialize-buffer ()
327 (when (not howm-mode)
328 (error "Not howm-mode"))
329 (howm-message-time "init-buf"
332 (howm-set-configuration-for-major-mode major-mode)
333 (howm-action-lock-setup)
334 (howm-mode-add-font-lock)
335 (howm-reminder-add-font-lock)
336 (cheat-font-lock-fontify)
337 ;; make-local-hook is obsolete for emacs >= 21.1.
338 (howm-funcall-if-defined (make-local-hook 'after-save-hook))
339 (add-hook 'after-save-hook 'howm-after-save t t))))
341 (defun howm-after-save ()
343 (howm-keyword-add-current-buffer)
344 (when howm-refresh-after-save
345 (howm-initialize-buffer))
346 (when (and howm-menu-refresh-after-save
347 (> howm-menu-expiry-hours 0))
348 (howm-menu-refresh-background))
349 (run-hooks 'howm-after-save-hook)))
351 (defun howm-restore-buffer ()
352 (action-lock-mode 0))
354 (defun howm-list-all ()
356 (howm-set-command 'howm-list-all)
357 (howm-normalize-show "" (howm-all-items))
358 ;; for backward compatibility
359 (cond ((howm-list-title-p) t) ;; already done in howm-normalize-show
360 (howm-list-all-title (howm-list-title-internal))))
362 (defun howm-all-items ()
363 "Returns list of all items in the first search path."
364 (howm-folder-items (car (howm-search-path)) t))
366 (defun howm-list-recent (&optional days)
368 (howm-set-command 'howm-list-recent)
369 (let* ((d (or days howm-list-recent-days))
371 (from (howm-days-before now d))
372 (item-list (howm-folder-items howm-directory t)))
373 (howm-normalize-show "" (howm-filter-items-by-mtime item-list from now))
374 ;; clean me [2003-11-30]
375 (cond ((howm-list-title-p) t) ;; already done in howm-normalize-show
376 (howm-list-recent-title (howm-list-title-internal))
377 ((not days) (howm-view-summary-to-contents)))))
379 ;; clean me: direct access to howm-view-* is undesirable.
381 (defvar howm-list-title-previous nil
383 (make-variable-buffer-local 'howm-list-title-previous)
384 (defun howm-list-title-put-previous (&optional item-list)
385 (when howm-list-title-undo
386 (setq howm-list-title-previous (or item-list (howm-view-item-list)))))
387 (defun howm-list-title-clear-previous ()
388 (setq howm-list-title-previous nil))
389 (defun howm-list-title-get-previous ()
390 (if howm-list-title-undo
391 (let ((prev howm-list-title-previous))
392 (setq howm-list-title-previous nil)
393 (howm-view-summary-rebuild prev))
394 (error "Undo is not enabled.")))
395 (defun howm-list-title-regexp ()
396 (or howm-list-title-regexp (howm-view-title-regexp-grep)))
397 (defalias 'howm-list-title 'howm-list-toggle-title) ;; backward compatibility
398 (defun howm-list-toggle-title (&optional undo)
400 (if (or undo howm-list-title-previous)
401 (howm-list-title-get-previous)
402 (howm-list-title-internal)))
403 (defun howm-list-title-internal ()
404 (let ((b (current-buffer)))
405 (howm-list-title-put-previous)
406 (howm-view-list-title (howm-list-title-regexp))
407 ;; (howm-view-filter-by-contents (howm-list-title-regexp))
408 (let ((c (current-buffer)))
411 (howm-view-kill-buffer)
413 (howm-view-summary-check t)))))
415 (defun howm-list-title-p ()
416 (let ((a (howm-get-value howm-list-title)))
417 (cond ((null a) nil) ;; I know this is redundant.
418 ((listp a) (member (howm-command) a))
421 (defun howm-days-after (ti days &optional hours)
422 (let* ((ne (howm-decode-time ti))
425 (nh (nth hour-pos ne))
426 (nd (nth day-pos ne)))
427 (setf (nth hour-pos ne) (+ nh (or hours 0)))
428 (setf (nth day-pos ne) (+ nd days))
429 (apply #'encode-time ne)))
431 (defun howm-days-before (ti days)
432 (howm-days-after ti (- days)))
434 (defun howm-list-grep (&optional completion-p)
436 (howm-set-command 'howm-list-grep)
437 (howm-list-grep-general completion-p))
439 (defun howm-list-grep-fixed ()
441 (howm-set-command 'howm-list-grep-fixed)
442 (howm-list-grep-general t))
444 (defun howm-list-grep-general (&optional completion-p)
445 (let* ((regexp (if completion-p
446 (howm-completing-read-keyword)
447 (read-from-minibuffer "Search all (grep): "))))
448 (when completion-p ;; Goto link works only for fixed string at now.
449 (howm-write-history regexp))
450 (howm-search regexp completion-p)))
452 (defun howm-search (regexp fixed-p &optional emacs-regexp filter)
453 (if (string= regexp "")
455 (howm-message-time "search"
456 (let* ((trio (howm-call-view-search-internal regexp fixed-p emacs-regexp))
459 (items (cl-caddr trio)))
461 (setq items (funcall filter items)))
462 (howm-normalize-show name items (or emacs-regexp regexp) nil nil kw)
463 (howm-record-view-window-configuration)))))
465 (defvar *howm-view-window-configuration* nil
467 (defun howm-view-window-configuration ()
468 *howm-view-window-configuration*)
469 (defun howm-set-view-window-configuration (conf)
470 (setq *howm-view-window-configuration* conf))
471 (defun howm-record-view-window-configuration ()
472 (howm-set-view-window-configuration (current-window-configuration)))
473 (defun howm-restore-view-window-configuration ()
474 (set-window-configuration (howm-view-window-configuration)))
475 (defun howm-return-to-list ()
477 (howm-restore-view-window-configuration))
479 (defun howm-call-view-search-internal (regexp fixed-p &optional emacs-regexp)
480 (let ((hilit (if emacs-regexp
481 `((,emacs-regexp . howm-view-hilit-face))
483 (howm-view-search-folder-internal regexp (howm-search-path-folder)
484 nil nil fixed-p hilit)))
486 (defun howm-list-migemo (&optional completion-p)
488 (howm-set-command 'howm-list-migemo)
491 (let* ((roma (read-from-minibuffer "Search all (migemo): "))
492 (e-reg (howm-migemo-get-pattern roma "emacs"))
493 (g-reg (if howm-view-use-grep
494 (howm-migemo-get-pattern roma "egrep")
496 (if (and e-reg g-reg)
497 (howm-search g-reg nil e-reg)
498 (message "No response from migemo-client.")))))
500 (defun howm-migemo-get-pattern (roma type)
501 (when (and (null howm-migemo-client) (not howm-view-use-grep))
503 (cl-labels ((ref (key) (cdr (assoc key howm-migemo-client))))
504 (cond ((and (featurep 'migemo) (string= type "emacs"))
505 (howm-funcall-if-defined (migemo-get-pattern roma)))
506 ((or (null howm-migemo-client) (stringp howm-migemo-client))
507 (car (howm-call-process (or howm-migemo-client "migemo-client")
508 `(,@howm-migemo-client-option "-t" ,type ,roma)
510 ((eq (ref 'type) 'cmigemo)
511 (car (howm-call-process (ref 'command)
512 `(,@howm-migemo-client-option
513 ,@(and (string= type "emacs") '("-e"))
515 (t (error "Invalid howm-migemo-client: %s" howm-migemo-client)))))
517 (defun howm-normalize-oldp ()
518 howm-list-normalizer)
520 ;; ;; generate conv in howm-normalizer-pair
521 ;; (let ((methods '("random" "name" "numerical-name" "date" "reverse-date"
522 ;; "summary" "reminder" "mtime" "reverse")))
523 ;; (mapcar (lambda (m)
525 ;; (howm-get-symbol nil "howm-view-sort-by-" m))
527 ;; (howm-get-symbol nil "howm-sort-items-by-" m)))
528 ;; (cons command internal)))
531 (defun howm-normalizer-pair ()
532 (let* ((old howm-list-normalizer)
533 (new howm-normalizer)
534 (conv '((howm-view-sort-by-random . howm-sort-items-by-random)
535 (howm-view-sort-by-name . howm-sort-items-by-name)
536 (howm-view-sort-by-numerical-name
537 . howm-sort-items-by-numerical-name)
538 (howm-view-sort-by-date . howm-sort-items-by-date)
539 (howm-view-sort-by-reverse-date
540 . howm-sort-items-by-reverse-date)
541 (howm-view-sort-by-summary . howm-sort-items-by-summary)
542 (howm-view-sort-by-reminder . howm-sort-items-by-reminder)
543 (howm-view-sort-by-mtime . howm-sort-items-by-mtime)
544 (howm-view-sort-by-reverse . howm-sort-items-by-reverse)))
546 (q (assoc new conv)))
548 (message "Warning: %s is wrong for howm-normalizer. Use %s." (car q) (cdr q))
550 (cond ((null old) (cons old new))
551 (p (cons nil (cdr p)))
552 (t (cons old #'identity)))))
554 (defmacro howm-with-normalizer (&rest body)
556 (let ((g (cl-gensym)))
558 (when (howm-normalize-oldp)
560 "Warning: howm-list-normalizer is obsolete. Use howm-normalizer."))
561 (let* ((,g (howm-normalizer-pair))
562 (howm-list-normalizer (car ,g))
563 (howm-normalizer (cdr ,g)))
566 (defun howm-normalize-show (name item-list
567 &optional keyword comefrom-regexp no-list-title
569 ;; comefrom-regexp and no-list-title are never used now. [2009-07-23]
570 (howm-with-normalizer
571 (if (howm-normalize-oldp)
572 ;; for backward compatibility.
574 (howm-view-summary name item-list fl-keywords)
575 (howm-list-normalize-old keyword comefrom-regexp no-list-title))
576 (let* ((r (howm-normalize item-list keyword
577 comefrom-regexp no-list-title)))
578 (howm-call-view-summary name (cdr r) fl-keywords)
581 (defun howm-call-view-summary (name item-list-pair fl-keywords)
582 (let ((orig (car item-list-pair))
583 (entitled (cdr item-list-pair)))
584 (howm-view-summary name (or entitled orig) fl-keywords)
587 (howm-list-title-put-previous orig)
588 (howm-list-title-clear-previous))))
590 (defun howm-normalize (item-list
591 &optional keyword comefrom-regexp no-list-title)
592 ;; no-list-title is never used now. [2009-07-23]
593 "Sort ITEM-LIST in the standard order."
595 (entitled-item-list nil))
596 (setq item-list (funcall howm-normalizer item-list))
598 (let ((key-reg (or comefrom-regexp
599 (howm-make-keyword-regexp1 keyword)))
600 (word-reg (format "\\<%s\\>"
601 (if (stringp keyword)
602 (regexp-quote keyword)
603 (regexp-opt keyword t))))
604 (wiki-reg (regexp-quote (howm-make-wiki-string keyword)))
608 (regexp-quote (expand-file-name keyword)))))
609 (case-fold-search howm-keyword-case-fold-search))
610 (cl-labels ((check (tag flag reg &optional tag-when-multi-hits)
612 (let ((r (howm-normalize-check item-list tag reg
613 tag-when-multi-hits)))
614 (setq matched (append (car r) matched))
615 (setq item-list (cdr r))))))
616 ;; not efficient. should I do them at once?
617 (check 'word howm-list-prefer-word word-reg)
618 (check 'wiki howm-list-prefer-wiki wiki-reg)
619 (check 'related-keyword t howm-keyword-regexp)
620 (check 'keyword t key-reg 'keyword-multi-hits)
621 (check 'file file-reg file-reg))))
622 (when (and (howm-list-title-p)
624 (not (and (member 'file matched)
625 howm-inhibit-title-file-match)))
626 (setq entitled-item-list
627 (howm-entitle-items (howm-list-title-regexp) item-list)))
628 (cons matched (cons item-list entitled-item-list))))
630 (defun howm-normalize-check (item-list tag reg tag-when-multi-hits)
631 (let* ((r (if (eq tag 'file)
632 (howm-view-lift-by-path-internal item-list reg)
633 (howm-view-lift-by-summary-internal item-list reg)))
636 (matched (cond ((and tag-when-multi-hits (eq m 'multi))
637 (list tag-when-multi-hits tag))
640 (cons matched item-list)))
642 (defun howm-list-normalize-old (&optional keyword comefrom-regexp no-list-title)
643 "Sort displayed items in the standard order.
644 This function is obsolete. Use `howm-normalize' insteadly.
645 --- Sorry, below documentation is incomplete. ---
646 When KEYWORD is given, matched items are placed on the top.
647 KEYWORD can be a string or a list of strings.
650 (howm-view-in-background
651 (howm-list-normalize-subr keyword comefrom-regexp no-list-title))
652 (howm-view-summary)))
654 (defun howm-list-normalize-subr (keyword comefrom-regexp no-list-title)
655 "Obsolete. Do not use this any more."
657 (funcall howm-list-normalizer)
659 (let ((key-reg (or comefrom-regexp
660 (howm-make-keyword-regexp1 keyword)))
661 (word-reg (format "\\<%s\\>"
662 (if (stringp keyword)
663 (regexp-quote keyword)
664 (regexp-opt keyword t))))
665 (wiki-reg (regexp-quote (howm-make-wiki-string keyword)))
669 (regexp-quote (expand-file-name keyword)))))
670 (case-fold-search howm-keyword-case-fold-search))
672 (let ((check (lambda (tag flag reg &optional tag-when-multi-hits)
674 (let ((m (if (eq tag 'file)
675 (howm-view-lift-by-name nil reg t)
676 (howm-view-lift-by-summary nil reg))))
678 (setq matched (cons tag matched)))
679 (when (and tag-when-multi-hits (eq m 'multi))
681 (cons tag-when-multi-hits matched))))))))
682 (funcall check 'word howm-list-prefer-word word-reg)
683 (funcall check 'wiki howm-list-prefer-wiki wiki-reg)
684 (funcall check 'related-keyword t howm-keyword-regexp)
685 (funcall check 'keyword t key-reg 'keyword-multi-hits)
686 (funcall check 'file file-reg file-reg))))
687 (when (and (howm-list-title-p)
689 (not (and (member 'file matched)
690 howm-inhibit-title-file-match)))
691 (howm-list-title-internal))
694 (defun howm-make-keyword-string (keyword)
695 (format howm-keyword-format keyword))
696 (defun howm-make-wiki-string (keyword)
697 (format howm-wiki-format keyword))
700 (defvar howm-keyword-regexp-format "%s$"
701 "Format to make entire-match regexp from keyword string.
702 Default is \"%s$\" because we want to make regexp \"<<< foo$\"
703 from keyword string \"<<< foo\",
704 so that we can accept \"<<< foo\" and reject \"<<< foobar\".
705 We need entire-match in order to
706 (1) place \"<<< foo\" on the top when \"foo\" is searched, and
707 (2) judge existence of \"<<< foo\" when [[foo]] is hit.")
708 (defun howm-make-keyword-regexp1 (keyword)
709 (howm-make-keyword-regexp-general keyword #'howm-make-keyword-regexp1-sub))
710 (defun howm-make-keyword-regexp2 (keyword)
711 (howm-make-keyword-regexp-general keyword #'howm-make-keyword-regexp2-sub))
712 (defun howm-make-keyword-regexp1-sub (keyword)
713 (format howm-keyword-regexp-format
714 (regexp-quote (howm-make-keyword-string keyword))))
715 (defun howm-make-keyword-regexp2-sub (keyword)
716 (format howm-keyword-regexp-format
717 (howm-make-keyword-string (regexp-quote keyword))))
718 (defun howm-make-keyword-regexp-general (keyword regexp-generator)
719 (cond ((stringp keyword)
720 (funcall regexp-generator keyword))
722 (mapconcat (lambda (s)
724 (funcall regexp-generator s)
728 (t (error "Wrong type: %s" keyword))))
730 (defun howm-list-related (str)
731 (howm-set-command 'howm-list-related)
732 (let* ((keys (mapcar (lambda (k)
733 (if howm-keyword-case-fold-search
736 (howm-subkeyword str)))
737 (filter `(lambda (items)
738 (howm-filter-items-by-summary items ,(regexp-opt keys)))))
739 ;; Note that regexp-opt returns a regexp for emacs (not for grep).
740 (howm-search (howm-make-keyword-string ".*") nil nil filter)))
742 (defun howm-subkeyword (str)
745 (howm-keyword-for-goto)))
747 (defun howm-list-around ()
749 (howm-set-command 'howm-list-around)
750 (let ((f (buffer-file-name))
751 (item-list (howm-view-sort-by-reverse-date-internal
753 (let ((howm-normalizer #'identity))
754 (howm-normalize-show "" item-list))
755 (let ((pos (cl-position-if (lambda (item)
756 (string= (howm-item-name item) f))
757 (howm-view-item-list))))
758 (goto-char (point-min))
761 (howm-view-summary-check t)))
763 (defun howm-history ()
765 (unless (file-exists-p howm-history-file)
766 (error "No history."))
767 ;; disable expansion of %schedule etc.
768 (let ((howm-menu-display-rules nil)) ;; dirty
769 (howm-menu-open howm-history-file)))
771 ;; (defvar howm-history-exclude
772 ;; (let ((strings '("[0-9][0-9][0-9][0-9]" "^[*=] [^ ]")))
774 ;; ,(mapconcat 'regexp-quote strings "\\|"))))
775 ;; (defun howm-history ()
777 ;; (howm-menu-open howm-history-file)
778 ;; (howm-edit-read-only-buffer
779 ;; (mapc #'flush-lines
780 ;; howm-history-exclude)))
782 (defvar *howm-command* nil
784 (defun howm-set-command (com)
785 (setq *howm-command* com))
786 (defun howm-command ()
789 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
792 (defun howm-create (&optional which-template here)
794 (let* ((t-c (howm-create-default-title-content))
797 (howm-create-file-with-title title which-template nil here content)))
799 (howm-dont-warn-free-variable transient-mark-mode)
800 (howm-dont-warn-free-variable mark-active)
801 (defun howm-create-default-title-content ()
803 (m (or (mark t) -777))
806 (search-str (howm-view-name)))
807 (let* ((transient-mark-p (and (boundp 'transient-mark-mode)
808 transient-mark-mode))
809 (mark-active-p (and (boundp 'mark-active) mark-active))
810 (active-p (if transient-mark-p
813 (strictly-active-p (and transient-mark-p mark-active-p))
814 (title-p (let* ((b (line-beginning-position))
815 (e (line-end-position)))
817 (< 0 beg) (<= b beg) (<= end e) (not (= beg end)))))
818 (content-p (and strictly-active-p
819 howm-content-from-region))
820 (search-p (and howm-title-from-search
821 (stringp search-str)))
822 (s (cond ((or title-p content-p) (buffer-substring-no-properties beg
824 (search-p search-str))))
825 (cond ((null s) (cons "" ""))
826 ((eq content-p t) (cons "" s))
827 ((or title-p search-p) (cons s ""))
828 (content-p (cons "" s))
831 (defun howm-create-here (&optional which-template)
833 (howm-create which-template t))
835 (defun howm-create-file-with-title (title &optional
836 which-template not-use-file here content)
837 (let ((b (current-buffer)))
840 (cond ((howm-buffer-empty-p) nil)
841 ((and here howm-create-here-just) (beginning-of-line))
842 (t (howm-create-newline)))
844 (insert-f (lambda (switch)
845 (howm-insert-template (if switch title "")
846 b which-template (not switch))))
847 (use-file (not not-use-file)))
848 ;; second candidate which appears when undo is called
849 (let ((end (funcall insert-f not-use-file)))
852 (insert (or content "")))
854 (delete-region p end))
855 (funcall insert-f use-file))
856 (howm-create-finish)))
858 (defun howm-create-finish ()
860 (run-hooks 'howm-create-hook))
862 (defun howm-create-newline ()
865 (howm-create-newline-prepend)
866 (howm-create-newline-append)))
867 (defun howm-create-newline-prepend ()
868 (goto-char (point-min)))
869 (defun howm-create-newline-append ()
870 (goto-char (point-max))
872 (when (not (= (line-beginning-position) (point))) ;; not empty line
876 (defun howm-insert-template (title &optional
877 previous-buffer which-template not-use-file)
879 (f (buffer-file-name previous-buffer))
880 (af (and f (howm-abbreviate-file-name f))))
881 (insert (howm-template-string which-template previous-buffer))
882 (let* ((date (format-time-string howm-template-date-format))
883 (use-file (not not-use-file))
884 (file (cond ((not use-file) "")
886 ((string= f (buffer-file-name)) "")
887 (t (format howm-template-file-format af)))))
888 (let ((arg `((title . ,title) (date . ,date) (file . ,file)))
889 (end (point-marker)))
890 (howm-replace howm-template-rules arg beg end)
893 (defvar howm-template-receive-buffer t
894 "Non nil if howm-template should receive previous-buffer
895 when howm-template is a function.
896 Set this option to nil if backward compatibility with howm-1.2.4 or earlier
899 (defun howm-template-string (which-template previous-buffer)
900 ;; which-template should be 1, 2, 3, ...
901 (setq which-template (or which-template 1))
902 (cond ((stringp howm-template) howm-template)
903 ((functionp howm-template) (let ((args (if howm-template-receive-buffer
906 (list which-template))))
907 (apply howm-template args)))
908 ((listp howm-template) (nth (- which-template 1) howm-template))))
910 (defun howm-replace (rules arg &optional beg end)
912 (let ((spell (car pair))
914 (goto-char (or beg (point-min)))
915 (while (re-search-forward spell end t)
916 (delete-region (match-beginning 0) (match-end 0))
917 (funcall disp-f arg))))
920 (defun howm-template-title (arg)
921 (insert (cdr (assoc 'title arg))))
922 (defun howm-template-date (arg)
923 (insert (cdr (assoc 'date arg))))
924 (defun howm-template-previous-file (arg)
925 (insert (cdr (assoc 'file arg))))
926 (defun howm-template-cursor (arg)) ;; do nothing
930 (let* ((r (howm-view-paragraph-region))
931 (s (buffer-substring-no-properties (car r) (cadr r))))
936 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
939 (defun howm-completing-read-keyword ()
940 (message "Scanning...")
941 (let* ((kl (howm-keyword-list))
942 (table (mapcar #'list kl))
943 (completion-ignore-case howm-keyword-case-fold-search))
944 (completing-read "Keyword: " table)))
946 (defun howm-insert-keyword ()
948 (insert (howm-completing-read-keyword)))
950 (defun howm-keyword-to-kill-ring (&optional filename-p)
952 (let ((title (howm-title-at-current-point filename-p)))
954 (howm-string-to-kill-ring title)
955 (error "No keyword."))))
957 (defun howm-title-at-current-point (&optional filename-p
958 title-regexp title-regexp-pos)
959 (let ((reg (or title-regexp howm-view-title-regexp))
960 (pos (or title-regexp-pos howm-view-title-regexp-pos)))
963 (cond ((and (not filename-p)
964 (re-search-backward reg nil t))
965 (match-string-no-properties pos))
967 (howm-abbreviate-file-name (buffer-file-name)))
970 (defun howm-string-to-kill-ring (str)
977 (defun howm-keyword-for-comefrom ()
979 (goto-char (point-min))
980 (let ((keyword-list nil))
981 (while (re-search-forward howm-keyword-regexp nil t)
983 (cons (match-string-no-properties howm-keyword-regexp-pos)
985 (reverse keyword-list))))
987 (defun howm-keyword-list ()
988 (let ((sep (format "[\n%s]" (or howm-keyword-list-alias-sep ""))))
989 (with-current-buffer (howm-keyword-buffer)
991 (split-string (buffer-substring (point-min) (point-max)) sep)))))
993 (defun howm-keyword-add (keyword-list)
994 (interactive "sKeyword: ")
995 (setq keyword-list (if (stringp keyword-list)
998 (with-current-buffer (howm-keyword-buffer)
1000 (goto-char (point-max))
1002 (when (howm-keyword-new-p k)
1005 (when (buffer-file-name)
1006 (howm-basic-save-buffer)))))
1008 (defun howm-keyword-new-p (str)
1010 (let ((r (format "^%s$" (regexp-quote str)))
1011 (case-fold-search howm-keyword-case-fold-search))
1012 (goto-char (point-min))
1013 (not (re-search-forward r nil t)))))
1015 (defun howm-support-aliases-p ()
1016 howm-keyword-list-alias-sep)
1017 (defun howm-aliases ()
1018 (if (howm-support-aliases-p)
1021 (defun howm-read-aliases ()
1022 (with-current-buffer (howm-keyword-buffer)
1025 (goto-char (point-min))
1026 (while (search-forward howm-keyword-list-alias-sep nil t)
1027 (let* ((line (buffer-substring-no-properties (line-beginning-position)
1028 (line-end-position)))
1029 (keys (split-string line howm-keyword-list-alias-sep))
1030 (ks (if howm-keyword-case-fold-search
1031 (mapcar #'downcase keys)
1033 (setq ans (cons ks ans))
1037 (defun howm-expand-aliases-recursively (keyword aliases)
1038 (let ((keys (list keyword))
1040 (cl-labels ((expand (keys)
1041 (sort (cl-remove-duplicates
1042 (cl-mapcan (lambda (k)
1044 (lambda (a) (if (member k a)
1048 keys) :test #'string=)
1050 (while (not (equal prev keys))
1052 (setq keys (expand keys))))
1054 (cl-assert (equal (howm-expand-aliases-recursively "a"
1055 '(("d" "e" "f") ("a" "b" "c")))
1057 (cl-assert (equal (howm-expand-aliases-recursively "a"
1058 '(("d" "e" "b") ("a" "b" "c")))
1059 '("a" "b" "c" "d" "e")))
1061 (defun howm-keyword-aliases (keyword)
1062 "List of strings which are equivalent to KEYWORD.
1063 KEYWORD itself is always at the head of the returneded list.
1065 ;; Return the original keyword (not downcased) for backward compatibility.
1066 ;; I'm not sure whether this behavior is really needed.
1067 (let* ((key (if howm-keyword-case-fold-search
1070 (aliases (howm-aliases))
1071 (equiv (if howm-keyword-aliases-recursive
1072 (howm-expand-aliases-recursively key aliases)
1073 (cl-remove-duplicates
1075 (cl-remove-if-not (lambda (a) (member key a))
1079 (cons keyword (remove key equiv)))))
1081 (defun howm-keyword-search (keyword &optional create-p open-unique-p)
1082 (howm-message-time "key-search"
1083 (howm-set-command 'howm-keyword-search)
1084 (howm-with-normalizer
1085 (howm-keyword-search-subr keyword create-p open-unique-p))))
1087 (defun howm-keyword-search-subr (keyword create-p open-unique-p)
1088 (let* ((aliases (if (howm-support-aliases-p)
1089 (howm-keyword-aliases keyword)
1091 (menu-p (howm-menu-keyword-p keyword))
1092 (comefrom-regexp (if menu-p ;; clean me
1094 (howm-make-keyword-regexp2 aliases)))
1095 (trio (let ((howm-search-other-dir (if menu-p ;; clean me
1097 howm-search-other-dir))
1098 (*howm-view-force-case-fold-search*
1099 howm-keyword-case-fold-search)) ;; dirty!
1100 (howm-call-view-search-internal aliases t)))
1101 ;; code for <http://pc8.2ch.net/test/read.cgi/unix/1077881095/823>.
1102 ;; but this change is canceled; I'll try more fundamental fix. [2005-11-04]
1103 ;; (if open-unique-p
1104 ;; (let ((r (concat "^" (regexp-quote keyword) "$")))
1105 ;; (howm-call-view-search r nil))
1106 ;; (howm-call-view-search aliases t))))
1109 (items (cl-caddr trio))
1111 (found (if items t nil)) ;; want to forget items as soon as possible
1113 (let* ((howm-keyword-format
1114 (if menu-p ;; clean me
1115 (default-value 'howm-keyword-format)
1116 howm-keyword-format))
1117 (r (howm-normalize items aliases
1119 (setq items-pair (cdr r))
1121 (keyword-matched (member 'keyword matched))
1122 (keyword-matched-multi (member 'keyword-multi-hits matched))
1123 (file-matched (member 'file matched))
1124 (title (howm-make-keyword-string keyword)))
1125 ;; main processing (clean me!) [2003-12-01]
1128 ((and menu-p keyword-matched)
1129 (howm-keyword-search-open-menu keyword (car items-pair)
1130 keyword-matched-multi))
1132 ((and create-p (not keyword-matched))
1133 (howm-keyword-search-create title))
1134 ;; open if unique match
1135 ((and open-unique-p (howm-single-element-p items))
1136 (howm-keyword-search-open-unique items))
1138 (howm-call-view-summary name items-pair kw)
1139 (when (howm-normalize-oldp)
1140 ;; sorry for redundancy & inefficiency
1141 (howm-list-normalize-old aliases comefrom-regexp t))))
1144 (howm-write-history keyword))
1145 ;; return information
1146 `((menu-p . ,menu-p)
1148 (matched . ,matched)
1149 (keyword-matched . ,keyword-matched)
1150 (create-p . ,create-p))
1153 (defun howm-keyword-search-open-menu (keyword item-list multi-hits-p)
1154 "Open KEYWORD as menu."
1155 ;; dirty. peeking howm-view.el
1156 (let* ((item (car item-list))
1157 (fname (howm-view-item-filename item))
1158 (place (howm-view-item-place item)))
1159 (let ((howm-search-other-dir nil))
1160 (howm-menu-open fname place (howm-menu-name keyword))))
1162 (message "Warning: found two or more %s." keyword)))
1164 (defun howm-keyword-search-create (title)
1165 "create new memo <<< TITLE."
1166 (howm-create-file-with-title title)
1167 (message "New keyword."))
1169 (defun howm-keyword-search-open-unique (items)
1170 "Open unique match."
1171 (howm-view-open-item (car items)))
1173 ;; (defvar *howm-keyword-buffer* nil) ;; for internal use
1174 (defun howm-keyword-for-goto (&optional keyword-list)
1176 (let ((case-fold-search howm-keyword-case-fold-search))
1177 (sort (cl-mapcan (lambda (k)
1178 (goto-char (point-min))
1179 ;; when howm-check-word-break is non-nil,
1180 ;; checking word breaks is desired for efficiency.
1181 ;; it is not implemented yet.
1182 (if (search-forward k nil 'noerr)
1185 (or keyword-list (howm-keyword-list)))
1187 (> (length x) (length y)))))))
1189 (defun howm-keyword-add-current-buffer ()
1191 (goto-char (point-min))
1192 (let ((m (current-message))
1194 (while (re-search-forward howm-keyword-regexp nil t)
1195 (let ((key-str (if howm-keyword-list-alias-sep
1196 (mapconcat #'identity
1198 howm-keyword-list-alias-sep)
1199 (match-string-no-properties howm-keyword-regexp-pos))))
1200 (setq keyword-list (cons key-str keyword-list))))
1201 (howm-keyword-add keyword-list)
1203 (defun howm-keyword-add-items (items)
1204 (let ((files (mapcar #'howm-view-item-filename items)))
1208 (insert-file-contents f)
1209 (howm-set-configuration-for-file-name f)
1210 (howm-keyword-add-current-buffer))
1213 (defun howm-keyword-read ()
1215 (beg (line-beginning-position)))
1217 (skip-chars-backward " ")
1218 (while (re-search-backward howm-keyword-regexp beg t)
1219 (setq ks (cons (match-string-no-properties howm-keyword-regexp-pos) ks))
1220 (skip-chars-backward " "))
1224 ;;; howm-mode.el ends here