1 ;;; howm-mode.el --- Wiki-like note-taking tool
2 ;;; Copyright (C) 2002, 2003, 2004, 2005-2018
3 ;;; HIRAOKA Kazuyuki <khi@users.osdn.me>
4 ;;; $Id: howm-mode.el,v 1.318 2012-12-29 08:57:18 hira Exp $
6 ;;; This program is free software; you can redistribute it and/or modify
7 ;;; it under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 1, or (at your option)
11 ;;; This program is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; The GNU General Public License is available by anonymouse ftp from
17 ;;; prep.ai.mit.edu in pub/gnu/COPYING. Alternately, you can write to
18 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
20 ;;;--------------------------------------------------------------------
22 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
23 ;; Backward compatibility
25 ;; (require 'howm-mode) in .emacs is obsolete. Use (require 'howm) instead.
27 ;; This must be earlier than (require 'howm-common), because
28 ;; howm-common needs cl, and (require 'cl) should be written in howm.el.
29 (when (not (featurep 'howm-version))
30 (message "Warning: Requiring howm-mode is obsolete. Require howm instead.")
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46 ;; You can easily modify them.
48 (howm-defvar-risky howm-template
49 (concat howm-view-title-header " %title%cursor\n%date %file\n\n")
50 "Contents of new file. %xxx are replaced with specified items.
51 If it is a list, <n>-th one is used when you type C-u <n> M-x howm-create.
52 If it is a function, it is called to get template string with the argument <n>.")
53 (defvar howm-keyword-header "<<<"
54 "Header string for declaration of keyword (implicit link).")
55 (defvar howm-ref-header ">>>"
56 "Header string for explicit link.")
57 (defvar howm-lighter " howm"
58 "Mode line for howm-mode")
60 (defvar howm-inhibit-title-file-match t
61 "If non-nil, inhibit howm-list-title when search string matches file name")
62 (defvar howm-list-all-title nil) ;; obsolete [2003-11-30]
63 (defvar howm-list-recent-title nil) ;; obsolete [2003-11-30]
65 (defvar howm-default-key-table
67 ;; ("key" func list-mode-p global-p)
69 ("l" howm-list-recent t t)
70 ("a" howm-list-all t t)
71 ("g" howm-list-grep t t)
72 ("s" howm-list-grep-fixed t t)
73 ("m" howm-list-migemo t t)
74 ("t" howm-list-todo t t)
75 ("y" howm-list-schedule t t)
76 ("b" howm-list-buffers t t)
77 ("x" howm-list-mark-ring t t)
80 ("e" howm-remember t t)
82 ("." howm-find-today nil t)
83 (":" howm-find-yesterday nil t)
84 ("A" howm-list-around)
85 ("h" howm-history nil t)
87 ("i" howm-insert-keyword nil t)
88 ("d" howm-insert-date nil t)
89 ("T" howm-insert-dtime nil t)
90 ("K" howm-keyword-to-kill-ring t t)
91 ("n" action-lock-goto-next-link)
92 ("p" action-lock-goto-previous-link)
93 ("Q" howm-kill-all t t)
94 (" " howm-toggle-buffer nil t)
96 ("P" howm-previous-memo)
99 ("C" howm-create-here nil t)
100 ("I" howm-create-interactively nil t)
101 ("w" howm-random-walk nil t)
102 ("M" howm-open-named-file t t)
104 "List of (key function list-mode-p global-p).
105 `howm-prefix' + this key is real stroke.
106 If optional argument list-mode-p is non-nil,
107 same key is also available in view mode.
108 It is further registered globally if global-p is non-nil."
111 (howm-defvar-risky howm-migemo-client nil
112 "Command name of migemo-client.
113 Try (setq howm-migemo-client \"migemo-client\") for howm-migemo-*.")
114 (howm-defvar-risky howm-migemo-client-option nil
115 "List of option for migemo-client.
116 e.g. (\"-H\" \"::1\")")
120 ;; Be careful to keep consistency.
122 (howm-defvar-risky howm-keyword/ref-regexp-format
123 "\\(%s\\)[ \t]*\\([^ \t\r\n].*\\)")
124 (howm-defvar-risky howm-keyword-format
125 (format "%s %%s" howm-keyword-header)
126 "Format for declaration of keyword. See `format'.")
127 (howm-defvar-risky howm-keyword-regexp
128 (format howm-keyword/ref-regexp-format (regexp-quote howm-keyword-header)))
129 (howm-defvar-risky howm-keyword-regexp-hilit-pos 1)
130 (howm-defvar-risky howm-keyword-regexp-pos 2)
131 (howm-defvar-risky howm-ref-regexp
132 (format howm-keyword/ref-regexp-format (regexp-quote howm-ref-header))
133 "Regexp for explicit link.")
134 (howm-defvar-risky howm-ref-regexp-hilit-pos 0
135 "Position of search string in `howm-ref-regexp'")
136 (howm-defvar-risky howm-ref-regexp-pos 2
137 "Position of search string in `howm-ref-regexp'")
138 (howm-defvar-risky howm-wiki-regexp "\\[\\[\\([^]\r\n]+\\)\\]\\]"
139 "Regexp for explicit link.")
140 (howm-defvar-risky howm-wiki-regexp-hilit-pos 1
141 "Position of hilight in `howm-wiki-regexp'")
142 (howm-defvar-risky howm-wiki-regexp-pos 1
143 "Position of search string in `howm-wiki-regexp'")
144 (howm-defvar-risky howm-wiki-format "[[%s]]"
145 "Format for declaration of wiki word. See `format'.")
147 (howm-defvar-risky howm-template-rules
148 '(("%title" . howm-template-title)
149 ("%date" . howm-template-date)
150 ("%file" . howm-template-previous-file)
151 ("%cursor" . howm-template-cursor))) ;; Cursor must be the last rule.
152 (defvar howm-template-date-format howm-dtime-format
153 "%date is replaced with `howm-template-date-format'
154 in `howm-template'. See `format-time-string'")
155 (defvar howm-template-file-format (concat howm-ref-header " %s")
156 "%file is replaced with `homw-template-file-format'
157 in `howm-template'. %s is replaced with name of last file. See `format'.")
163 (defun howm-action-lock-general (command regexp pos
167 `(lambda (&optional dummy)
168 (let ((s (match-string-no-properties ,pos)))
169 ;; (when howm-keyword-case-fold-search
170 ;; (setq s (downcase s)))
171 (,command s ,@options)))
175 (defun howm-action-lock-search (regexp
177 &optional hilit-pos create-p open-unique-p)
178 (howm-action-lock-general 'howm-keyword-search
179 regexp pos hilit-pos create-p open-unique-p))
180 (defun howm-action-lock-related (regexp pos hilit-pos)
181 (howm-action-lock-general 'howm-list-related regexp pos hilit-pos))
183 (defun howm-action-lock-date-rule ()
184 (action-lock-general 'howm-action-lock-date howm-date-regexp 0 0))
186 (defun howm-action-lock-quote-keyword (keyword)
187 (let ((q (regexp-quote keyword)))
188 ;; when a regexp is specified, leave unmatched keywords.
189 (if (and (stringp howm-check-word-break)
190 (not (string-match howm-check-word-break keyword)))
192 ;; add word break checks
193 (concat "\\b" q "\\b"))))
195 (defun howm-action-lock-setup ()
196 (setq action-lock-case-fold-search howm-keyword-case-fold-search)
198 (let* ((date-al (action-lock-date "{_}" howm-dtime-format)))
199 ;; override the rule in action-lock.el
200 (action-lock-add-rules (list date-al) t))
201 (let* ((ks (howm-keyword-for-goto))
202 (r (mapconcat (if howm-check-word-break
203 #'howm-action-lock-quote-keyword
206 ;; The following optimization causes an error
207 ;; "Variable binding depth exceeds max-specpdl-size".
208 ;; (r (cond ((stringp howm-check-word-break)
209 ;; (mapconcat #'howm-action-lock-quote-keyword ks "\\|"))
211 ;; (regexp-opt ks (and howm-check-word-break 'word)))))
212 (wiki (howm-action-lock-search howm-wiki-regexp
214 howm-wiki-regexp-hilit-pos
216 (explicit (howm-action-lock-search howm-ref-regexp
218 howm-ref-regexp-hilit-pos))
219 (implicit (howm-action-lock-search r 0))
220 (rev (howm-action-lock-related howm-keyword-regexp
221 howm-keyword-regexp-pos
222 howm-keyword-regexp-hilit-pos))
223 (date (howm-action-lock-date-rule))
224 (done (howm-action-lock-reminder-done-rule))
228 ,@(if ks (list implicit) nil)
230 ,@(if (howm-menu-p) nil (list date done))
233 ;; don't override the rule in action-lock.el
234 ;; esp. http://xxx should call browser even if "<<< http" exists
235 (action-lock-add-rules all)))
237 (defun howm-file-name (&optional time)
238 (format-time-string howm-file-name-format
239 (or time (current-time))))
241 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
244 (easy-mmode-define-minor-mode howm-mode
245 "With no argument, this command toggles the mode.
246 Non-null prefix argument turns on the mode.
247 Null prefix argument turns off the mode.
249 When the mode is enabled, underlines are drawn on texts which match
250 to titles of other files. Typing \\[action-lock-magic-return] there,
251 you can jump to the corresponding file.
255 \\[action-lock-magic-return] Follow link
256 \\[howm-refresh] Refresh buffer
257 \\[howm-list-all] List all files
258 \\[howm-list-grep] Search (grep)
259 \\[howm-create] Create new file
260 \\[howm-dup] Duplicate current file
261 \\[howm-insert-keyword] Insert keyword
262 \\[howm-insert-date] Insert date
263 \\[howm-insert-dtime] Insert date with time
264 \\[howm-keyword-to-kill-ring] Copy current keyword to kill ring
265 \\[action-lock-goto-next-link] Go to next link
266 \\[action-lock-goto-previous-link] Go to previous link
267 \\[howm-next-memo] Go to next entry in current buffer
268 \\[howm-previous-memo] Go to previous entry in current buffer
269 \\[howm-first-memo] Go to first entry in current buffer
270 \\[howm-last-memo] Go to last entry in current buffer
271 \\[howm-create-here] Add new entry to current buffer
272 \\[howm-create-interactively] Create new file interactively (not recommended)
273 \\[howm-random-walk] Browse random entries automtically
276 howm-lighter ;; mode-line
277 (mapcar (lambda (entry)
278 (let ((k (car entry))
280 (cons (concat howm-prefix k) f)))
281 howm-default-key-table)
284 ;; emacs20's easy-mmode-define-minor-mode can't have body. sigh...
285 (add-hook 'howm-mode-on-hook 'howm-initialize-buffer)
286 (add-hook 'howm-mode-off-hook 'howm-restore-buffer)
288 (defun howm-set-keymap ()
289 (mapc (lambda (entry)
290 (let* ((k (car entry))
292 (list-mode-p (cl-caddr entry))
293 (global-p (cl-cadddr entry))
294 (pk (concat howm-prefix k)))
295 (define-key howm-mode-map pk f)
300 (list howm-view-summary-mode-map
301 howm-view-contents-mode-map)))
303 (define-key global-map pk f))))
304 howm-default-key-table)
305 (define-key howm-mode-map "\C-x\C-s" 'howm-save-buffer))
308 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
311 (defun howm-refresh ()
315 (howm-initialize-buffer)))
317 (defun howm-initialize-buffer ()
319 (when (not howm-mode)
320 (error "Not howm-mode"))
321 (howm-message-time "init-buf"
324 (howm-set-configuration-for-major-mode major-mode)
325 (howm-action-lock-setup)
326 (howm-mode-add-font-lock)
327 (howm-reminder-add-font-lock)
328 (cheat-font-lock-fontify)
329 ;; make-local-hook is obsolete for emacs >= 21.1.
330 (howm-funcall-if-defined (make-local-hook 'after-save-hook))
331 (add-hook 'after-save-hook 'howm-after-save t t))))
333 (defun howm-after-save ()
335 (howm-keyword-add-current-buffer)
336 (when howm-refresh-after-save
337 (howm-initialize-buffer))
338 (when (and howm-menu-refresh-after-save
339 (> howm-menu-expiry-hours 0))
340 (howm-menu-refresh-background))
341 (run-hooks 'howm-after-save-hook)))
343 (defun howm-restore-buffer ()
344 (action-lock-mode 0))
346 (defun howm-list-all ()
348 (howm-set-command 'howm-list-all)
349 (howm-normalize-show "" (howm-all-items))
350 ;; for backward compatibility
351 (cond ((howm-list-title-p) t) ;; already done in howm-normalize-show
352 (howm-list-all-title (howm-list-title-internal))))
354 (defun howm-all-items ()
355 "Returns list of all items in the first search path."
356 (howm-folder-items (car (howm-search-path)) t))
358 (defun howm-list-recent (&optional days)
360 (howm-set-command 'howm-list-recent)
361 (let* ((d (or days howm-list-recent-days))
363 (from (howm-days-before now d))
364 (item-list (howm-folder-items howm-directory t)))
365 (howm-normalize-show "" (howm-filter-items-by-mtime item-list from now))
366 ;; clean me [2003-11-30]
367 (cond ((howm-list-title-p) t) ;; already done in howm-normalize-show
368 (howm-list-recent-title (howm-list-title-internal))
369 ((not days) (howm-view-summary-to-contents)))))
371 ;; clean me: direct access to howm-view-* is undesirable.
373 (defvar howm-list-title-previous nil
375 (make-variable-buffer-local 'howm-list-title-previous)
376 (defun howm-list-title-put-previous (&optional item-list)
377 (when howm-list-title-undo
378 (setq howm-list-title-previous (or item-list (howm-view-item-list)))))
379 (defun howm-list-title-clear-previous ()
380 (setq howm-list-title-previous nil))
381 (defun howm-list-title-get-previous ()
382 (if howm-list-title-undo
383 (let ((prev howm-list-title-previous))
384 (setq howm-list-title-previous nil)
385 (howm-view-summary-rebuild prev))
386 (error "Undo is not enabled.")))
387 (defun howm-list-title-regexp ()
388 (or howm-list-title-regexp (howm-view-title-regexp-grep)))
389 (defalias 'howm-list-title 'howm-list-toggle-title) ;; backward compatibility
390 (defun howm-list-toggle-title (&optional undo)
392 (if (or undo howm-list-title-previous)
393 (howm-list-title-get-previous)
394 (howm-list-title-internal)))
395 (defun howm-list-title-internal ()
396 (let ((b (current-buffer)))
397 (howm-list-title-put-previous)
398 (howm-view-list-title (howm-list-title-regexp))
399 ;; (howm-view-filter-by-contents (howm-list-title-regexp))
400 (let ((c (current-buffer)))
403 (howm-view-kill-buffer)
405 (howm-view-summary-check t)))))
407 (defun howm-list-title-p ()
408 (let ((a (howm-get-value howm-list-title)))
409 (cond ((null a) nil) ;; I know this is redundant.
410 ((listp a) (member (howm-command) a))
413 (defun howm-days-after (ti days &optional hours)
414 (let* ((ne (howm-decode-time ti))
417 (nh (nth hour-pos ne))
418 (nd (nth day-pos ne)))
419 (setf (nth hour-pos ne) (+ nh (or hours 0)))
420 (setf (nth day-pos ne) (+ nd days))
421 (apply #'encode-time ne)))
423 (defun howm-days-before (ti days)
424 (howm-days-after ti (- days)))
426 (defun howm-list-grep (&optional completion-p)
428 (howm-set-command 'howm-list-grep)
429 (howm-list-grep-general completion-p))
431 (defun howm-list-grep-fixed ()
433 (howm-set-command 'howm-list-grep-fixed)
434 (howm-list-grep-general t))
436 (defun howm-list-grep-general (&optional completion-p)
437 (let* ((regexp (if completion-p
438 (howm-completing-read-keyword)
439 (read-from-minibuffer "Search all (grep): "))))
440 (when completion-p ;; Goto link works only for fixed string at now.
441 (howm-write-history regexp))
442 (howm-search regexp completion-p)))
444 (defun howm-search (regexp fixed-p &optional emacs-regexp filter)
445 (if (string= regexp "")
447 (howm-message-time "search"
448 (let* ((trio (howm-call-view-search-internal regexp fixed-p emacs-regexp))
451 (items (cl-caddr trio)))
453 (setq items (funcall filter items)))
454 (howm-normalize-show name items (or emacs-regexp regexp) nil nil kw)
455 (howm-record-view-window-configuration)))))
457 (defvar *howm-view-window-configuration* nil
459 (defun howm-view-window-configuration ()
460 *howm-view-window-configuration*)
461 (defun howm-set-view-window-configuration (conf)
462 (setq *howm-view-window-configuration* conf))
463 (defun howm-record-view-window-configuration ()
464 (howm-set-view-window-configuration (current-window-configuration)))
465 (defun howm-restore-view-window-configuration ()
466 (set-window-configuration (howm-view-window-configuration)))
467 (defun howm-return-to-list ()
469 (howm-restore-view-window-configuration))
471 (defun howm-call-view-search-internal (regexp fixed-p &optional emacs-regexp)
472 (let ((hilit (if emacs-regexp
473 `((,emacs-regexp . howm-view-hilit-face))
475 (howm-view-search-folder-internal regexp (howm-search-path-folder)
476 nil nil fixed-p hilit)))
478 (defun howm-list-migemo (&optional completion-p)
480 (howm-set-command 'howm-list-migemo)
483 (let* ((roma (read-from-minibuffer "Search all (migemo): "))
484 (e-reg (howm-migemo-get-pattern roma "emacs"))
485 (g-reg (if howm-view-use-grep
486 (howm-migemo-get-pattern roma "egrep")
488 (if (and e-reg g-reg)
489 (howm-search g-reg nil e-reg)
490 (message "No response from migemo-client.")))))
492 (defun howm-migemo-get-pattern (roma type)
493 (when (and (null howm-migemo-client) (not howm-view-use-grep))
495 (if (and (featurep 'migemo) (string= type "emacs"))
496 (howm-funcall-if-defined (migemo-get-pattern roma))
497 ;; (migemo-get-pattern roma)
498 (car (howm-call-process (or howm-migemo-client "migemo-client")
499 `(,@howm-migemo-client-option "-t" ,type ,roma)
502 ;; (defun howm-migemo-get-pattern (roma type)
503 ;; (when (and (null (howm-migemo-client)) (not howm-view-use-grep))
504 ;; (require 'migemo))
505 ;; (if (and (featurep 'migemo) (string= type "emacs"))
506 ;; (howm-funcall-if-defined (migemo-get-pattern roma))
507 ;; ;; (migemo-get-pattern roma)
508 ;; (car (howm-call-process (howm-migemo-client)
509 ;; `(,@(howm-migemo-client-option) "-t" ,type ,roma)
512 ;; (defun howm-migemo-client ()
513 ;; (if (stringp howm-migemo-client)
514 ;; howm-migemo-client
515 ;; (or (car howm-migemo-client) "migemo-client")))
517 ;; (defun howm-migemo-client-option ()
518 ;; (cdr-safe howm-migemo-client))
520 (defun howm-normalize-oldp ()
521 howm-list-normalizer)
523 ;; ;; generate conv in howm-normalizer-pair
524 ;; (let ((methods '("random" "name" "numerical-name" "date" "reverse-date"
525 ;; "summary" "reminder" "mtime" "reverse")))
526 ;; (mapcar (lambda (m)
528 ;; (howm-get-symbol nil "howm-view-sort-by-" m))
530 ;; (howm-get-symbol nil "howm-sort-items-by-" m)))
531 ;; (cons command internal)))
534 (defun howm-normalizer-pair ()
535 (let* ((old howm-list-normalizer)
536 (new howm-normalizer)
537 (conv '((howm-view-sort-by-random . howm-sort-items-by-random)
538 (howm-view-sort-by-name . howm-sort-items-by-name)
539 (howm-view-sort-by-numerical-name
540 . howm-sort-items-by-numerical-name)
541 (howm-view-sort-by-date . howm-sort-items-by-date)
542 (howm-view-sort-by-reverse-date
543 . howm-sort-items-by-reverse-date)
544 (howm-view-sort-by-summary . howm-sort-items-by-summary)
545 (howm-view-sort-by-reminder . howm-sort-items-by-reminder)
546 (howm-view-sort-by-mtime . howm-sort-items-by-mtime)
547 (howm-view-sort-by-reverse . howm-sort-items-by-reverse)))
549 (q (assoc new conv)))
551 (message "Warning: %s is wrong for howm-normalizer. Use %s." (car q) (cdr q))
553 (cond ((null old) (cons old new))
554 (p (cons nil (cdr p)))
555 (t (cons old #'identity)))))
557 (defmacro howm-with-normalizer (&rest body)
559 (let ((g (cl-gensym)))
561 (when (howm-normalize-oldp)
563 "Warning: howm-list-normalizer is obsolete. Use howm-normalizer."))
564 (let* ((,g (howm-normalizer-pair))
565 (howm-list-normalizer (car ,g))
566 (howm-normalizer (cdr ,g)))
569 (defun howm-normalize-show (name item-list
570 &optional keyword comefrom-regexp no-list-title
572 ;; comefrom-regexp and no-list-title are never used now. [2009-07-23]
573 (howm-with-normalizer
574 (if (howm-normalize-oldp)
575 ;; for backward compatibility.
577 (howm-view-summary name item-list fl-keywords)
578 (howm-list-normalize-old keyword comefrom-regexp no-list-title))
579 (let* ((r (howm-normalize item-list keyword
580 comefrom-regexp no-list-title)))
581 (howm-call-view-summary name (cdr r) fl-keywords)
584 (defun howm-call-view-summary (name item-list-pair fl-keywords)
585 (let ((orig (car item-list-pair))
586 (entitled (cdr item-list-pair)))
587 (howm-view-summary name (or entitled orig) fl-keywords)
590 (howm-list-title-put-previous orig)
591 (howm-list-title-clear-previous))))
593 (defun howm-normalize (item-list
594 &optional keyword comefrom-regexp no-list-title)
595 ;; no-list-title is never used now. [2009-07-23]
596 "Sort ITEM-LIST in the standard order."
598 (entitled-item-list nil))
599 (setq item-list (funcall howm-normalizer item-list))
601 (let ((key-reg (or comefrom-regexp
602 (howm-make-keyword-regexp1 keyword)))
603 (word-reg (format "\\<%s\\>"
604 (if (stringp keyword)
605 (regexp-quote keyword)
606 (regexp-opt keyword t))))
607 (wiki-reg (regexp-quote (howm-make-wiki-string keyword)))
611 (regexp-quote (expand-file-name keyword)))))
612 (case-fold-search howm-keyword-case-fold-search))
613 (cl-labels ((check (tag flag reg &optional tag-when-multi-hits)
615 (let ((r (howm-normalize-check item-list tag reg
616 tag-when-multi-hits)))
617 (setq matched (append (car r) matched))
618 (setq item-list (cdr r))))))
619 ;; not efficient. should I do them at once?
620 (check 'word howm-list-prefer-word word-reg)
621 (check 'wiki howm-list-prefer-wiki wiki-reg)
622 (check 'related-keyword t howm-keyword-regexp)
623 (check 'keyword t key-reg 'keyword-multi-hits)
624 (check 'file file-reg file-reg))))
625 (when (and (howm-list-title-p)
627 (not (and (member 'file matched)
628 howm-inhibit-title-file-match)))
629 (setq entitled-item-list
630 (howm-entitle-items (howm-list-title-regexp) item-list)))
631 (cons matched (cons item-list entitled-item-list))))
633 (defun howm-normalize-check (item-list tag reg tag-when-multi-hits)
634 (let* ((r (if (eq tag 'file)
635 (howm-view-lift-by-path-internal item-list reg)
636 (howm-view-lift-by-summary-internal item-list reg)))
639 (matched (cond ((and tag-when-multi-hits (eq m 'multi))
640 (list tag-when-multi-hits tag))
643 (cons matched item-list)))
645 (defun howm-list-normalize-old (&optional keyword comefrom-regexp no-list-title)
646 "Sort displayed items in the standard order.
647 This function is obsolete. Use `howm-normalize' insteadly.
648 --- Sorry, below documentation is incomplete. ---
649 When KEYWORD is given, matched items are placed on the top.
650 KEYWORD can be a string or a list of strings.
653 (howm-view-in-background
654 (howm-list-normalize-subr keyword comefrom-regexp no-list-title))
655 (howm-view-summary)))
657 (defun howm-list-normalize-subr (keyword comefrom-regexp no-list-title)
658 "Obsolete. Do not use this any more."
660 (funcall howm-list-normalizer)
662 (let ((key-reg (or comefrom-regexp
663 (howm-make-keyword-regexp1 keyword)))
664 (word-reg (format "\\<%s\\>"
665 (if (stringp keyword)
666 (regexp-quote keyword)
667 (regexp-opt keyword t))))
668 (wiki-reg (regexp-quote (howm-make-wiki-string keyword)))
672 (regexp-quote (expand-file-name keyword)))))
673 (case-fold-search howm-keyword-case-fold-search))
675 (let ((check (lambda (tag flag reg &optional tag-when-multi-hits)
677 (let ((m (if (eq tag 'file)
678 (howm-view-lift-by-name nil reg t)
679 (howm-view-lift-by-summary nil reg))))
681 (setq matched (cons tag matched)))
682 (when (and tag-when-multi-hits (eq m 'multi))
684 (cons tag-when-multi-hits matched))))))))
685 (funcall check 'word howm-list-prefer-word word-reg)
686 (funcall check 'wiki howm-list-prefer-wiki wiki-reg)
687 (funcall check 'related-keyword t howm-keyword-regexp)
688 (funcall check 'keyword t key-reg 'keyword-multi-hits)
689 (funcall check 'file file-reg file-reg))))
690 (when (and (howm-list-title-p)
692 (not (and (member 'file matched)
693 howm-inhibit-title-file-match)))
694 (howm-list-title-internal))
697 (defun howm-make-keyword-string (keyword)
698 (format howm-keyword-format keyword))
699 (defun howm-make-wiki-string (keyword)
700 (format howm-wiki-format keyword))
703 (defvar howm-keyword-regexp-format "%s$"
704 "Format to make entire-match regexp from keyword string.
705 Default is \"%s$\" because we want to make regexp \"<<< foo$\"
706 from keyword string \"<<< foo\",
707 so that we can accept \"<<< foo\" and reject \"<<< foobar\".
708 We need entire-match in order to
709 (1) place \"<<< foo\" on the top when \"foo\" is searched, and
710 (2) judge existence of \"<<< foo\" when [[foo]] is hit.")
711 (defun howm-make-keyword-regexp1 (keyword)
712 (howm-make-keyword-regexp-general keyword #'howm-make-keyword-regexp1-sub))
713 (defun howm-make-keyword-regexp2 (keyword)
714 (howm-make-keyword-regexp-general keyword #'howm-make-keyword-regexp2-sub))
715 (defun howm-make-keyword-regexp1-sub (keyword)
716 (format howm-keyword-regexp-format
717 (regexp-quote (howm-make-keyword-string keyword))))
718 (defun howm-make-keyword-regexp2-sub (keyword)
719 (format howm-keyword-regexp-format
720 (howm-make-keyword-string (regexp-quote keyword))))
721 (defun howm-make-keyword-regexp-general (keyword regexp-generator)
722 (cond ((stringp keyword)
723 (funcall regexp-generator keyword))
725 (mapconcat (lambda (s)
727 (funcall regexp-generator s)
731 (t (error "Wrong type: %s" keyword))))
733 (defun howm-list-related (str)
734 (howm-set-command 'howm-list-related)
735 (let* ((keys (mapcar (lambda (k)
736 (if howm-keyword-case-fold-search
739 (howm-subkeyword str)))
740 (filter `(lambda (items)
741 (howm-filter-items-by-summary items ,(regexp-opt keys)))))
742 ;; Note that regexp-opt returns a regexp for emacs (not for grep).
743 (howm-search (howm-make-keyword-string ".*") nil nil filter)))
745 (defun howm-subkeyword (str)
748 (howm-keyword-for-goto)))
750 (defun howm-list-around ()
752 (howm-set-command 'howm-list-around)
753 (let ((f (buffer-file-name))
754 (item-list (howm-view-sort-by-reverse-date-internal
756 (let ((howm-normalizer #'identity))
757 (howm-normalize-show "" item-list))
758 (let ((pos (cl-position-if (lambda (item)
759 (string= (howm-item-name item) f))
760 (howm-view-item-list))))
761 (goto-char (point-min))
764 (howm-view-summary-check t)))
766 (defun howm-history ()
768 (unless (file-exists-p howm-history-file)
769 (error "No history."))
770 ;; disable expansion of %schedule etc.
771 (let ((howm-menu-display-rules nil)) ;; dirty
772 (howm-menu-open howm-history-file)))
774 ;; (defvar howm-history-exclude
775 ;; (let ((strings '("[0-9][0-9][0-9][0-9]" "^[*=] [^ ]")))
777 ;; ,(mapconcat 'regexp-quote strings "\\|"))))
778 ;; (defun howm-history ()
780 ;; (howm-menu-open howm-history-file)
781 ;; (howm-edit-read-only-buffer
782 ;; (mapc #'flush-lines
783 ;; howm-history-exclude)))
785 (defvar *howm-command* nil
787 (defun howm-set-command (com)
788 (setq *howm-command* com))
789 (defun howm-command ()
792 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
795 (defun howm-create (&optional which-template here)
797 (let* ((t-c (howm-create-default-title-content))
800 (howm-create-file-with-title title which-template nil here content)))
802 (howm-dont-warn-free-variable transient-mark-mode)
803 (howm-dont-warn-free-variable mark-active)
804 (defun howm-create-default-title-content ()
806 (m (or (mark t) -777))
809 (search-str (howm-view-name)))
810 (let* ((transient-mark-p (and (boundp 'transient-mark-mode)
811 transient-mark-mode))
812 (mark-active-p (and (boundp 'mark-active) mark-active))
813 (active-p (if transient-mark-p
816 (strictly-active-p (and transient-mark-p mark-active-p))
817 (title-p (let* ((b (line-beginning-position))
818 (e (line-end-position)))
820 (< 0 beg) (<= b beg) (<= end e) (not (= beg end)))))
821 (content-p (and strictly-active-p
822 howm-content-from-region))
823 (search-p (and howm-title-from-search
824 (stringp search-str)))
825 (s (cond ((or title-p content-p) (buffer-substring-no-properties beg
827 (search-p search-str))))
828 (cond ((null s) (cons "" ""))
829 ((eq content-p t) (cons "" s))
830 ((or title-p search-p) (cons s ""))
831 (content-p (cons "" s))
834 (defun howm-create-here (&optional which-template)
836 (howm-create which-template t))
838 (defun howm-create-file-with-title (title &optional
839 which-template not-use-file here content)
840 (let ((b (current-buffer)))
843 (cond ((howm-buffer-empty-p) nil)
844 ((and here howm-create-here-just) (beginning-of-line))
845 (t (howm-create-newline)))
847 (insert-f (lambda (switch)
848 (howm-insert-template (if switch title "")
849 b which-template (not switch))))
850 (use-file (not not-use-file)))
851 ;; second candidate which appears when undo is called
852 (let ((end (funcall insert-f not-use-file)))
855 (insert (or content "")))
857 (delete-region p end))
858 (funcall insert-f use-file))
859 (howm-create-finish)))
861 (defun howm-create-finish ()
863 (run-hooks 'howm-create-hook))
865 (defun howm-create-newline ()
868 (howm-create-newline-prepend)
869 (howm-create-newline-append)))
870 (defun howm-create-newline-prepend ()
871 (goto-char (point-min)))
872 (defun howm-create-newline-append ()
873 (goto-char (point-max))
875 (when (not (= (line-beginning-position) (point))) ;; not empty line
879 (defun howm-insert-template (title &optional
880 previous-buffer which-template not-use-file)
882 (f (buffer-file-name previous-buffer))
883 (af (and f (howm-abbreviate-file-name f))))
884 (insert (howm-template-string which-template previous-buffer))
885 (let* ((date (format-time-string howm-template-date-format))
886 (use-file (not not-use-file))
887 (file (cond ((not use-file) "")
889 ((string= f (buffer-file-name)) "")
890 (t (format howm-template-file-format af)))))
891 (let ((arg `((title . ,title) (date . ,date) (file . ,file)))
892 (end (point-marker)))
893 (howm-replace howm-template-rules arg beg end)
896 (defvar howm-template-receive-buffer t
897 "Non nil if howm-template should receive previous-buffer
898 when howm-template is a function.
899 Set this option to nil if backward compatibility with howm-1.2.4 or earlier
902 (defun howm-template-string (which-template previous-buffer)
903 ;; which-template should be 1, 2, 3, ...
904 (setq which-template (or which-template 1))
905 (cond ((stringp howm-template) howm-template)
906 ((functionp howm-template) (let ((args (if howm-template-receive-buffer
909 (list which-template))))
910 (apply howm-template args)))
911 ((listp howm-template) (nth (- which-template 1) howm-template))))
913 (defun howm-replace (rules arg &optional beg end)
915 (let ((spell (car pair))
917 (goto-char (or beg (point-min)))
918 (while (re-search-forward spell end t)
919 (delete-region (match-beginning 0) (match-end 0))
920 (funcall disp-f arg))))
923 ;; Use dynamic bindings dirtily!
924 (defun howm-template-title (arg)
925 (insert (cdr (assoc 'title arg))))
926 (defun howm-template-date (arg)
927 (insert (cdr (assoc 'date arg))))
928 (defun howm-template-previous-file (arg)
929 (insert (cdr (assoc 'file arg))))
930 (defun howm-template-cursor (arg)) ;; do nothing
934 (let* ((r (howm-view-paragraph-region))
935 (s (buffer-substring-no-properties (car r) (cadr r))))
940 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
943 (defun howm-completing-read-keyword ()
944 (message "Scanning...")
945 (let* ((kl (howm-keyword-list))
946 (table (mapcar #'list kl))
947 (completion-ignore-case howm-keyword-case-fold-search))
948 (completing-read "Keyword: " table)))
950 (defun howm-insert-keyword ()
952 (insert (howm-completing-read-keyword)))
954 (defun howm-keyword-to-kill-ring (&optional filename-p)
956 (let ((title (howm-title-at-current-point filename-p)))
958 (howm-string-to-kill-ring title)
959 (error "No keyword."))))
961 (defun howm-title-at-current-point (&optional filename-p
962 title-regexp title-regexp-pos)
963 (let ((reg (or title-regexp howm-view-title-regexp))
964 (pos (or title-regexp-pos howm-view-title-regexp-pos)))
967 (cond ((and (not filename-p)
968 (re-search-backward reg nil t))
969 (match-string-no-properties pos))
971 (howm-abbreviate-file-name (buffer-file-name)))
974 (defun howm-string-to-kill-ring (str)
981 (defun howm-keyword-for-comefrom ()
983 (goto-char (point-min))
984 (let ((keyword-list nil))
985 (while (re-search-forward howm-keyword-regexp nil t)
987 (cons (match-string-no-properties howm-keyword-regexp-pos)
989 (reverse keyword-list))))
991 (defun howm-keyword-list ()
992 (let ((sep (format "[\n%s]" (or howm-keyword-list-alias-sep ""))))
993 (with-current-buffer (howm-keyword-buffer)
995 (split-string (buffer-substring (point-min) (point-max)) sep)))))
997 (defun howm-keyword-add (keyword-list)
998 (interactive "sKeyword: ")
999 (setq keyword-list (if (stringp keyword-list)
1002 (with-current-buffer (howm-keyword-buffer)
1004 (goto-char (point-max))
1006 (when (howm-keyword-new-p k)
1009 (when (buffer-file-name)
1010 (howm-basic-save-buffer)))))
1012 (defun howm-keyword-new-p (str)
1014 (let ((r (format "^%s$" (regexp-quote str)))
1015 (case-fold-search howm-keyword-case-fold-search))
1016 (goto-char (point-min))
1017 (not (re-search-forward r nil t)))))
1019 (defun howm-support-aliases-p ()
1020 howm-keyword-list-alias-sep)
1021 (defun howm-aliases ()
1022 (if (howm-support-aliases-p)
1025 (defun howm-read-aliases ()
1026 (with-current-buffer (howm-keyword-buffer)
1029 (goto-char (point-min))
1030 (while (search-forward howm-keyword-list-alias-sep nil t)
1031 (let* ((line (buffer-substring-no-properties (line-beginning-position)
1032 (line-end-position)))
1033 (keys (split-string line howm-keyword-list-alias-sep))
1034 (ks (if howm-keyword-case-fold-search
1035 (mapcar #'downcase keys)
1037 (setq ans (cons ks ans))
1041 (defun howm-expand-aliases-recursively (keyword aliases)
1042 (let ((keys (list keyword))
1044 (cl-labels ((expand (keys)
1045 (sort (cl-remove-duplicates
1046 (cl-mapcan (lambda (k)
1048 (lambda (a) (if (member k a)
1052 keys) :test #'string=)
1054 (while (not (equal prev keys))
1056 (setq keys (expand keys))))
1058 (cl-assert (equal (howm-expand-aliases-recursively "a"
1059 '(("d" "e" "f") ("a" "b" "c")))
1061 (cl-assert (equal (howm-expand-aliases-recursively "a"
1062 '(("d" "e" "b") ("a" "b" "c")))
1063 '("a" "b" "c" "d" "e")))
1065 (defun howm-keyword-aliases (keyword)
1066 "List of strings which are equivalent to KEYWORD.
1067 KEYWORD itself is always at the head of the returneded list.
1069 ;; Return the original keyword (not downcased) for backward compatibility.
1070 ;; I'm not sure whether this behavior is really needed.
1071 (let* ((key (if howm-keyword-case-fold-search
1074 (aliases (howm-aliases))
1075 (equiv (if howm-keyword-aliases-recursive
1076 (howm-expand-aliases-recursively key aliases)
1077 (cl-remove-duplicates
1079 (cl-remove-if-not (lambda (a) (member key a))
1083 (cons keyword (remove key equiv)))))
1085 (defun howm-keyword-search (keyword &optional create-p open-unique-p)
1086 (howm-message-time "key-search"
1087 (howm-set-command 'howm-keyword-search)
1088 (howm-with-normalizer
1089 (howm-keyword-search-subr keyword create-p open-unique-p))))
1091 (defun howm-keyword-search-subr (keyword create-p open-unique-p)
1092 (let* ((aliases (if (howm-support-aliases-p)
1093 (howm-keyword-aliases keyword)
1095 (menu-p (howm-menu-keyword-p keyword))
1096 (comefrom-regexp (if menu-p ;; clean me
1098 (howm-make-keyword-regexp2 aliases)))
1099 (trio (let ((howm-search-other-dir (if menu-p ;; clean me
1101 howm-search-other-dir))
1102 (*howm-view-force-case-fold-search*
1103 howm-keyword-case-fold-search)) ;; dirty!
1104 (howm-call-view-search-internal aliases t)))
1105 ;; code for <http://pc8.2ch.net/test/read.cgi/unix/1077881095/823>.
1106 ;; but this change is canceled; I'll try more fundamental fix. [2005-11-04]
1107 ;; (if open-unique-p
1108 ;; (let ((r (concat "^" (regexp-quote keyword) "$")))
1109 ;; (howm-call-view-search r nil))
1110 ;; (howm-call-view-search aliases t))))
1113 (items (cl-caddr trio))
1115 (found (if items t nil)) ;; want to forget items as soon as possible
1117 (let* ((howm-keyword-format
1118 (if menu-p ;; clean me
1119 (default-value 'howm-keyword-format)
1120 howm-keyword-format))
1121 (r (howm-normalize items aliases
1123 (setq items-pair (cdr r))
1125 (keyword-matched (member 'keyword matched))
1126 (keyword-matched-multi (member 'keyword-multi-hits matched))
1127 (file-matched (member 'file matched))
1128 (title (howm-make-keyword-string keyword)))
1129 ;; main processing (clean me!) [2003-12-01]
1132 ((and menu-p keyword-matched)
1133 (howm-keyword-search-open-menu keyword (car items-pair)
1134 keyword-matched-multi))
1136 ((and create-p (not keyword-matched))
1137 (howm-keyword-search-create title))
1138 ;; open if unique match
1139 ((and open-unique-p (howm-single-element-p items))
1140 (howm-keyword-search-open-unique items))
1142 (howm-call-view-summary name items-pair kw)
1143 (when (howm-normalize-oldp)
1144 ;; sorry for redundancy & inefficiency
1145 (howm-list-normalize-old aliases comefrom-regexp t))))
1148 (howm-write-history keyword))
1149 ;; return information
1150 `((menu-p . ,menu-p)
1152 (matched . ,matched)
1153 (keyword-matched . ,keyword-matched)
1154 (create-p . ,create-p))
1157 (defun howm-keyword-search-open-menu (keyword item-list multi-hits-p)
1158 "Open KEYWORD as menu."
1159 ;; dirty. peeking howm-view.el
1160 (let* ((item (car item-list))
1161 (fname (howm-view-item-filename item))
1162 (place (howm-view-item-place item)))
1163 (let ((howm-search-other-dir nil))
1164 (howm-menu-open fname place (howm-menu-name keyword))))
1166 (message "Warning: found two or more %s." keyword)))
1168 (defun howm-keyword-search-create (title)
1169 "create new memo <<< TITLE."
1170 (howm-create-file-with-title title)
1171 (message "New keyword."))
1173 (defun howm-keyword-search-open-unique (items)
1174 "Open unique match."
1175 (howm-view-open-item (car items)))
1177 ;; (defvar *howm-keyword-buffer* nil) ;; for internal use
1178 (defun howm-keyword-for-goto (&optional keyword-list)
1180 (let ((case-fold-search howm-keyword-case-fold-search))
1181 (sort (cl-mapcan (lambda (k)
1182 (goto-char (point-min))
1183 ;; when howm-check-word-break is non-nil,
1184 ;; checking word breaks is desired for efficiency.
1185 ;; it is not implemented yet.
1186 (if (search-forward k nil 'noerr)
1189 (or keyword-list (howm-keyword-list)))
1191 (> (length x) (length y)))))))
1193 (defun howm-keyword-add-current-buffer ()
1195 (goto-char (point-min))
1196 (let ((m (current-message))
1198 (while (re-search-forward howm-keyword-regexp nil t)
1199 (let ((key-str (if howm-keyword-list-alias-sep
1200 (mapconcat #'identity
1202 howm-keyword-list-alias-sep)
1203 (match-string-no-properties howm-keyword-regexp-pos))))
1204 (setq keyword-list (cons key-str keyword-list))))
1205 (howm-keyword-add keyword-list)
1207 (defun howm-keyword-add-items (items)
1208 (let ((files (mapcar #'howm-view-item-filename items)))
1212 (insert-file-contents f)
1213 (howm-set-configuration-for-file-name f)
1214 (howm-keyword-add-current-buffer))
1217 (defun howm-keyword-read ()
1219 (beg (line-beginning-position)))
1221 (skip-chars-backward " ")
1222 (while (re-search-backward howm-keyword-regexp beg t)
1223 (setq ks (cons (match-string-no-properties howm-keyword-regexp-pos) ks))
1224 (skip-chars-backward " "))
1228 ;;; howm-mode.el ends here