1 ;;; howm-mode.el --- Wiki-like note-taking tool
2 ;;; Copyright (C) 2002, 2003, 2004, 2005-2021
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 (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
283 :init-value nil ;; default = off
284 :lighter howm-lighter ;; mode-line
285 :keymap (mapcar (lambda (entry)
286 (let ((k (car entry))
288 (cons (concat howm-prefix k) f)))
289 howm-default-key-table)
291 (howm-initialize-buffer)
292 (howm-restore-buffer)))
294 (defun howm-set-keymap ()
295 (mapc (lambda (entry)
296 (let* ((k (car entry))
298 (list-mode-p (cl-caddr entry))
299 (global-p (cl-cadddr entry))
300 (pk (concat howm-prefix k)))
301 (define-key howm-mode-map pk f)
306 (list howm-view-summary-mode-map
307 howm-view-contents-mode-map)))
309 (define-key global-map pk f))))
310 howm-default-key-table)
311 (define-key howm-mode-map "\C-x\C-s" 'howm-save-buffer))
314 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
317 (defun howm-refresh ()
321 (howm-initialize-buffer)))
323 (defun howm-initialize-buffer ()
325 (when (not howm-mode)
326 (error "Not howm-mode"))
327 (howm-message-time "init-buf"
330 (howm-set-configuration-for-major-mode major-mode)
331 (howm-action-lock-setup)
332 (howm-mode-add-font-lock)
333 (howm-reminder-add-font-lock)
334 (cheat-font-lock-fontify)
335 ;; make-local-hook is obsolete for emacs >= 21.1.
336 (howm-funcall-if-defined (make-local-hook 'after-save-hook))
337 (add-hook 'after-save-hook 'howm-after-save t t))))
339 (defun howm-after-save ()
341 (howm-keyword-add-current-buffer)
342 (when howm-refresh-after-save
343 (howm-initialize-buffer))
344 (when (and howm-menu-refresh-after-save
345 (> howm-menu-expiry-hours 0))
346 (howm-menu-refresh-background))
347 (run-hooks 'howm-after-save-hook)))
349 (defun howm-restore-buffer ()
350 (action-lock-mode 0))
352 (defun howm-list-all ()
354 (howm-set-command 'howm-list-all)
355 (howm-normalize-show "" (howm-all-items))
356 ;; for backward compatibility
357 (cond ((howm-list-title-p) t) ;; already done in howm-normalize-show
358 (howm-list-all-title (howm-list-title-internal))))
360 (defun howm-all-items ()
361 "Returns list of all items in the first search path."
362 (howm-folder-items (car (howm-search-path)) t))
364 (defun howm-list-recent (&optional days)
366 (howm-set-command 'howm-list-recent)
367 (let* ((d (or days howm-list-recent-days))
369 (from (howm-days-before now d))
370 (item-list (howm-folder-items howm-directory t)))
371 (howm-normalize-show "" (howm-filter-items-by-mtime item-list from now))
372 ;; clean me [2003-11-30]
373 (cond ((howm-list-title-p) t) ;; already done in howm-normalize-show
374 (howm-list-recent-title (howm-list-title-internal))
375 ((not days) (howm-view-summary-to-contents)))))
377 ;; clean me: direct access to howm-view-* is undesirable.
379 (defvar howm-list-title-previous nil
381 (make-variable-buffer-local 'howm-list-title-previous)
382 (defun howm-list-title-put-previous (&optional item-list)
383 (when howm-list-title-undo
384 (setq howm-list-title-previous (or item-list (howm-view-item-list)))))
385 (defun howm-list-title-clear-previous ()
386 (setq howm-list-title-previous nil))
387 (defun howm-list-title-get-previous ()
388 (if howm-list-title-undo
389 (let ((prev howm-list-title-previous))
390 (setq howm-list-title-previous nil)
391 (howm-view-summary-rebuild prev))
392 (error "Undo is not enabled.")))
393 (defun howm-list-title-regexp ()
394 (or howm-list-title-regexp (howm-view-title-regexp-grep)))
395 (defalias 'howm-list-title 'howm-list-toggle-title) ;; backward compatibility
396 (defun howm-list-toggle-title (&optional undo)
398 (if (or undo howm-list-title-previous)
399 (howm-list-title-get-previous)
400 (howm-list-title-internal)))
401 (defun howm-list-title-internal ()
402 (let ((b (current-buffer)))
403 (howm-list-title-put-previous)
404 (howm-view-list-title (howm-list-title-regexp))
405 ;; (howm-view-filter-by-contents (howm-list-title-regexp))
406 (let ((c (current-buffer)))
409 (howm-view-kill-buffer)
411 (howm-view-summary-check t)))))
413 (defun howm-list-title-p ()
414 (let ((a (howm-get-value howm-list-title)))
415 (cond ((null a) nil) ;; I know this is redundant.
416 ((listp a) (member (howm-command) a))
419 (defun howm-days-after (ti days &optional hours)
420 (let* ((ne (howm-decode-time ti))
423 (nh (nth hour-pos ne))
424 (nd (nth day-pos ne)))
425 (setf (nth hour-pos ne) (+ nh (or hours 0)))
426 (setf (nth day-pos ne) (+ nd days))
427 (apply #'encode-time ne)))
429 (defun howm-days-before (ti days)
430 (howm-days-after ti (- days)))
432 (defun howm-list-grep (&optional completion-p)
434 (howm-set-command 'howm-list-grep)
435 (howm-list-grep-general completion-p))
437 (defun howm-list-grep-fixed ()
439 (howm-set-command 'howm-list-grep-fixed)
440 (howm-list-grep-general t))
442 (defun howm-list-grep-general (&optional completion-p)
443 (let* ((regexp (if completion-p
444 (howm-completing-read-keyword)
445 (read-from-minibuffer "Search all (grep): "))))
446 (when completion-p ;; Goto link works only for fixed string at now.
447 (howm-write-history regexp))
448 (howm-search regexp completion-p)))
450 (defun howm-search (regexp fixed-p &optional emacs-regexp filter)
451 (if (string= regexp "")
453 (howm-message-time "search"
454 (let* ((trio (howm-call-view-search-internal regexp fixed-p emacs-regexp))
457 (items (cl-caddr trio)))
459 (setq items (funcall filter items)))
460 (howm-normalize-show name items (or emacs-regexp regexp) nil nil kw)
461 (howm-record-view-window-configuration)))))
463 (defvar *howm-view-window-configuration* nil
465 (defun howm-view-window-configuration ()
466 *howm-view-window-configuration*)
467 (defun howm-set-view-window-configuration (conf)
468 (setq *howm-view-window-configuration* conf))
469 (defun howm-record-view-window-configuration ()
470 (howm-set-view-window-configuration (current-window-configuration)))
471 (defun howm-restore-view-window-configuration ()
472 (set-window-configuration (howm-view-window-configuration)))
473 (defun howm-return-to-list ()
475 (howm-restore-view-window-configuration))
477 (defun howm-call-view-search-internal (regexp fixed-p &optional emacs-regexp)
478 (let ((hilit (if emacs-regexp
479 `((,emacs-regexp . howm-view-hilit-face))
481 (howm-view-search-folder-internal regexp (howm-search-path-folder)
482 nil nil fixed-p hilit)))
484 (defun howm-list-migemo (&optional completion-p)
486 (howm-set-command 'howm-list-migemo)
489 (let* ((roma (read-from-minibuffer "Search all (migemo): "))
490 (e-reg (howm-migemo-get-pattern roma "emacs"))
491 (g-reg (if howm-view-use-grep
492 (howm-migemo-get-pattern roma "egrep")
494 (if (and e-reg g-reg)
495 (howm-search g-reg nil e-reg)
496 (message "No response from migemo-client.")))))
498 (defun howm-migemo-get-pattern (roma type)
499 (when (and (null howm-migemo-client) (not howm-view-use-grep))
501 (cl-labels ((ref (key) (cdr (assoc key howm-migemo-client))))
502 (cond ((and (featurep 'migemo) (string= type "emacs"))
503 (howm-funcall-if-defined (migemo-get-pattern roma)))
504 ((or (null howm-migemo-client) (stringp howm-migemo-client))
505 (car (howm-call-process (or howm-migemo-client "migemo-client")
506 `(,@howm-migemo-client-option "-t" ,type ,roma)
508 ((eq (ref 'type) 'cmigemo)
509 (car (howm-call-process (ref 'command)
510 `(,@howm-migemo-client-option
511 ,@(and (string= type "emacs") '("-e"))
513 (t (error "Invalid howm-migemo-client: %s" howm-migemo-client)))))
515 (defun howm-normalize-oldp ()
516 howm-list-normalizer)
518 ;; ;; generate conv in howm-normalizer-pair
519 ;; (let ((methods '("random" "name" "numerical-name" "date" "reverse-date"
520 ;; "summary" "reminder" "mtime" "reverse")))
521 ;; (mapcar (lambda (m)
523 ;; (howm-get-symbol nil "howm-view-sort-by-" m))
525 ;; (howm-get-symbol nil "howm-sort-items-by-" m)))
526 ;; (cons command internal)))
529 (defun howm-normalizer-pair ()
530 (let* ((old howm-list-normalizer)
531 (new howm-normalizer)
532 (conv '((howm-view-sort-by-random . howm-sort-items-by-random)
533 (howm-view-sort-by-name . howm-sort-items-by-name)
534 (howm-view-sort-by-numerical-name
535 . howm-sort-items-by-numerical-name)
536 (howm-view-sort-by-date . howm-sort-items-by-date)
537 (howm-view-sort-by-reverse-date
538 . howm-sort-items-by-reverse-date)
539 (howm-view-sort-by-summary . howm-sort-items-by-summary)
540 (howm-view-sort-by-reminder . howm-sort-items-by-reminder)
541 (howm-view-sort-by-mtime . howm-sort-items-by-mtime)
542 (howm-view-sort-by-reverse . howm-sort-items-by-reverse)))
544 (q (assoc new conv)))
546 (message "Warning: %s is wrong for howm-normalizer. Use %s." (car q) (cdr q))
548 (cond ((null old) (cons old new))
549 (p (cons nil (cdr p)))
550 (t (cons old #'identity)))))
552 (defmacro howm-with-normalizer (&rest body)
554 (let ((g (cl-gensym)))
556 (when (howm-normalize-oldp)
558 "Warning: howm-list-normalizer is obsolete. Use howm-normalizer."))
559 (let* ((,g (howm-normalizer-pair))
560 (howm-list-normalizer (car ,g))
561 (howm-normalizer (cdr ,g)))
564 (defun howm-normalize-show (name item-list
565 &optional keyword comefrom-regexp no-list-title
567 ;; comefrom-regexp and no-list-title are never used now. [2009-07-23]
568 (howm-with-normalizer
569 (if (howm-normalize-oldp)
570 ;; for backward compatibility.
572 (howm-view-summary name item-list fl-keywords)
573 (howm-list-normalize-old keyword comefrom-regexp no-list-title))
574 (let* ((r (howm-normalize item-list keyword
575 comefrom-regexp no-list-title)))
576 (howm-call-view-summary name (cdr r) fl-keywords)
579 (defun howm-call-view-summary (name item-list-pair fl-keywords)
580 (let ((orig (car item-list-pair))
581 (entitled (cdr item-list-pair)))
582 (howm-view-summary name (or entitled orig) fl-keywords)
585 (howm-list-title-put-previous orig)
586 (howm-list-title-clear-previous))))
588 (defun howm-normalize (item-list
589 &optional keyword comefrom-regexp no-list-title)
590 ;; no-list-title is never used now. [2009-07-23]
591 "Sort ITEM-LIST in the standard order."
593 (entitled-item-list nil))
594 (setq item-list (funcall howm-normalizer item-list))
596 (let ((key-reg (or comefrom-regexp
597 (howm-make-keyword-regexp1 keyword)))
598 (word-reg (format "\\<%s\\>"
599 (if (stringp keyword)
600 (regexp-quote keyword)
601 (regexp-opt keyword t))))
602 (wiki-reg (regexp-quote (howm-make-wiki-string keyword)))
606 (regexp-quote (expand-file-name keyword)))))
607 (case-fold-search howm-keyword-case-fold-search))
608 (cl-labels ((check (tag flag reg &optional tag-when-multi-hits)
610 (let ((r (howm-normalize-check item-list tag reg
611 tag-when-multi-hits)))
612 (setq matched (append (car r) matched))
613 (setq item-list (cdr r))))))
614 ;; not efficient. should I do them at once?
615 (check 'word howm-list-prefer-word word-reg)
616 (check 'wiki howm-list-prefer-wiki wiki-reg)
617 (check 'related-keyword t howm-keyword-regexp)
618 (check 'keyword t key-reg 'keyword-multi-hits)
619 (check 'file file-reg file-reg))))
620 (when (and (howm-list-title-p)
622 (not (and (member 'file matched)
623 howm-inhibit-title-file-match)))
624 (setq entitled-item-list
625 (howm-entitle-items (howm-list-title-regexp) item-list)))
626 (cons matched (cons item-list entitled-item-list))))
628 (defun howm-normalize-check (item-list tag reg tag-when-multi-hits)
629 (let* ((r (if (eq tag 'file)
630 (howm-view-lift-by-path-internal item-list reg)
631 (howm-view-lift-by-summary-internal item-list reg)))
634 (matched (cond ((and tag-when-multi-hits (eq m 'multi))
635 (list tag-when-multi-hits tag))
638 (cons matched item-list)))
640 (defun howm-list-normalize-old (&optional keyword comefrom-regexp no-list-title)
641 "Sort displayed items in the standard order.
642 This function is obsolete. Use `howm-normalize' insteadly.
643 --- Sorry, below documentation is incomplete. ---
644 When KEYWORD is given, matched items are placed on the top.
645 KEYWORD can be a string or a list of strings.
648 (howm-view-in-background
649 (howm-list-normalize-subr keyword comefrom-regexp no-list-title))
650 (howm-view-summary)))
652 (defun howm-list-normalize-subr (keyword comefrom-regexp no-list-title)
653 "Obsolete. Do not use this any more."
655 (funcall howm-list-normalizer)
657 (let ((key-reg (or comefrom-regexp
658 (howm-make-keyword-regexp1 keyword)))
659 (word-reg (format "\\<%s\\>"
660 (if (stringp keyword)
661 (regexp-quote keyword)
662 (regexp-opt keyword t))))
663 (wiki-reg (regexp-quote (howm-make-wiki-string keyword)))
667 (regexp-quote (expand-file-name keyword)))))
668 (case-fold-search howm-keyword-case-fold-search))
670 (let ((check (lambda (tag flag reg &optional tag-when-multi-hits)
672 (let ((m (if (eq tag 'file)
673 (howm-view-lift-by-name nil reg t)
674 (howm-view-lift-by-summary nil reg))))
676 (setq matched (cons tag matched)))
677 (when (and tag-when-multi-hits (eq m 'multi))
679 (cons tag-when-multi-hits matched))))))))
680 (funcall check 'word howm-list-prefer-word word-reg)
681 (funcall check 'wiki howm-list-prefer-wiki wiki-reg)
682 (funcall check 'related-keyword t howm-keyword-regexp)
683 (funcall check 'keyword t key-reg 'keyword-multi-hits)
684 (funcall check 'file file-reg file-reg))))
685 (when (and (howm-list-title-p)
687 (not (and (member 'file matched)
688 howm-inhibit-title-file-match)))
689 (howm-list-title-internal))
692 (defun howm-make-keyword-string (keyword)
693 (format howm-keyword-format keyword))
694 (defun howm-make-wiki-string (keyword)
695 (format howm-wiki-format keyword))
698 (defvar howm-keyword-regexp-format "%s$"
699 "Format to make entire-match regexp from keyword string.
700 Default is \"%s$\" because we want to make regexp \"<<< foo$\"
701 from keyword string \"<<< foo\",
702 so that we can accept \"<<< foo\" and reject \"<<< foobar\".
703 We need entire-match in order to
704 (1) place \"<<< foo\" on the top when \"foo\" is searched, and
705 (2) judge existence of \"<<< foo\" when [[foo]] is hit.")
706 (defun howm-make-keyword-regexp1 (keyword)
707 (howm-make-keyword-regexp-general keyword #'howm-make-keyword-regexp1-sub))
708 (defun howm-make-keyword-regexp2 (keyword)
709 (howm-make-keyword-regexp-general keyword #'howm-make-keyword-regexp2-sub))
710 (defun howm-make-keyword-regexp1-sub (keyword)
711 (format howm-keyword-regexp-format
712 (regexp-quote (howm-make-keyword-string keyword))))
713 (defun howm-make-keyword-regexp2-sub (keyword)
714 (format howm-keyword-regexp-format
715 (howm-make-keyword-string (regexp-quote keyword))))
716 (defun howm-make-keyword-regexp-general (keyword regexp-generator)
717 (cond ((stringp keyword)
718 (funcall regexp-generator keyword))
720 (mapconcat (lambda (s)
722 (funcall regexp-generator s)
726 (t (error "Wrong type: %s" keyword))))
728 (defun howm-list-related (str)
729 (howm-set-command 'howm-list-related)
730 (let* ((keys (mapcar (lambda (k)
731 (if howm-keyword-case-fold-search
734 (howm-subkeyword str)))
735 (filter `(lambda (items)
736 (howm-filter-items-by-summary items ,(regexp-opt keys)))))
737 ;; Note that regexp-opt returns a regexp for emacs (not for grep).
738 (howm-search (howm-make-keyword-string ".*") nil nil filter)))
740 (defun howm-subkeyword (str)
743 (howm-keyword-for-goto)))
745 (defun howm-list-around ()
747 (howm-set-command 'howm-list-around)
748 (let ((f (buffer-file-name))
749 (item-list (howm-view-sort-by-reverse-date-internal
751 (let ((howm-normalizer #'identity))
752 (howm-normalize-show "" item-list))
753 (let ((pos (cl-position-if (lambda (item)
754 (string= (howm-item-name item) f))
755 (howm-view-item-list))))
756 (goto-char (point-min))
759 (howm-view-summary-check t)))
761 (defun howm-history ()
763 (unless (file-exists-p howm-history-file)
764 (error "No history."))
765 ;; disable expansion of %schedule etc.
766 (let ((howm-menu-display-rules nil)) ;; dirty
767 (howm-menu-open howm-history-file)))
769 ;; (defvar howm-history-exclude
770 ;; (let ((strings '("[0-9][0-9][0-9][0-9]" "^[*=] [^ ]")))
772 ;; ,(mapconcat 'regexp-quote strings "\\|"))))
773 ;; (defun howm-history ()
775 ;; (howm-menu-open howm-history-file)
776 ;; (howm-edit-read-only-buffer
777 ;; (mapc #'flush-lines
778 ;; howm-history-exclude)))
780 (defvar *howm-command* nil
782 (defun howm-set-command (com)
783 (setq *howm-command* com))
784 (defun howm-command ()
787 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
790 (defun howm-create (&optional which-template here)
792 (let* ((t-c (howm-create-default-title-content))
795 (howm-create-file-with-title title which-template nil here content)))
797 (howm-dont-warn-free-variable transient-mark-mode)
798 (howm-dont-warn-free-variable mark-active)
799 (defun howm-create-default-title-content ()
801 (m (or (mark t) -777))
804 (search-str (howm-view-name)))
805 (let* ((transient-mark-p (and (boundp 'transient-mark-mode)
806 transient-mark-mode))
807 (mark-active-p (and (boundp 'mark-active) mark-active))
808 (active-p (if transient-mark-p
811 (strictly-active-p (and transient-mark-p mark-active-p))
812 (title-p (let* ((b (line-beginning-position))
813 (e (line-end-position)))
815 (< 0 beg) (<= b beg) (<= end e) (not (= beg end)))))
816 (content-p (and strictly-active-p
817 howm-content-from-region))
818 (search-p (and howm-title-from-search
819 (stringp search-str)))
820 (s (cond ((or title-p content-p) (buffer-substring-no-properties beg
822 (search-p search-str))))
823 (cond ((null s) (cons "" ""))
824 ((eq content-p t) (cons "" s))
825 ((or title-p search-p) (cons s ""))
826 (content-p (cons "" s))
829 (defun howm-create-here (&optional which-template)
831 (howm-create which-template t))
833 (defun howm-create-file-with-title (title &optional
834 which-template not-use-file here content)
835 (let ((b (current-buffer)))
838 (cond ((howm-buffer-empty-p) nil)
839 ((and here howm-create-here-just) (beginning-of-line))
840 (t (howm-create-newline)))
842 (insert-f (lambda (switch)
843 (howm-insert-template (if switch title "")
844 b which-template (not switch))))
845 (use-file (not not-use-file)))
846 ;; second candidate which appears when undo is called
847 (let ((end (funcall insert-f not-use-file)))
850 (insert (or content "")))
852 (delete-region p end))
853 (funcall insert-f use-file))
854 (howm-create-finish)))
856 (defun howm-create-finish ()
858 (run-hooks 'howm-create-hook))
860 (defun howm-create-newline ()
863 (howm-create-newline-prepend)
864 (howm-create-newline-append)))
865 (defun howm-create-newline-prepend ()
866 (goto-char (point-min)))
867 (defun howm-create-newline-append ()
868 (goto-char (point-max))
870 (when (not (= (line-beginning-position) (point))) ;; not empty line
874 (defun howm-insert-template (title &optional
875 previous-buffer which-template not-use-file)
877 (f (buffer-file-name previous-buffer))
878 (af (and f (howm-abbreviate-file-name f))))
879 (insert (howm-template-string which-template previous-buffer))
880 (let* ((date (format-time-string howm-template-date-format))
881 (use-file (not not-use-file))
882 (file (cond ((not use-file) "")
884 ((string= f (buffer-file-name)) "")
885 (t (format howm-template-file-format af)))))
886 (let ((arg `((title . ,title) (date . ,date) (file . ,file)))
887 (end (point-marker)))
888 (howm-replace howm-template-rules arg beg end)
891 (defvar howm-template-receive-buffer t
892 "Non nil if howm-template should receive previous-buffer
893 when howm-template is a function.
894 Set this option to nil if backward compatibility with howm-1.2.4 or earlier
897 (defun howm-template-string (which-template previous-buffer)
898 ;; which-template should be 1, 2, 3, ...
899 (setq which-template (or which-template 1))
900 (cond ((stringp howm-template) howm-template)
901 ((functionp howm-template) (let ((args (if howm-template-receive-buffer
904 (list which-template))))
905 (apply howm-template args)))
906 ((listp howm-template) (nth (- which-template 1) howm-template))))
908 (defun howm-replace (rules arg &optional beg end)
910 (let ((spell (car pair))
912 (goto-char (or beg (point-min)))
913 (while (re-search-forward spell end t)
914 (delete-region (match-beginning 0) (match-end 0))
915 (funcall disp-f arg))))
918 (defun howm-template-title (arg)
919 (insert (cdr (assoc 'title arg))))
920 (defun howm-template-date (arg)
921 (insert (cdr (assoc 'date arg))))
922 (defun howm-template-previous-file (arg)
923 (insert (cdr (assoc 'file arg))))
924 (defun howm-template-cursor (arg)) ;; do nothing
928 (let* ((r (howm-view-paragraph-region))
929 (s (buffer-substring-no-properties (car r) (cadr r))))
934 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
937 (defun howm-completing-read-keyword ()
938 (message "Scanning...")
939 (let* ((kl (howm-keyword-list))
940 (table (mapcar #'list kl))
941 (completion-ignore-case howm-keyword-case-fold-search))
942 (completing-read "Keyword: " table)))
944 (defun howm-insert-keyword ()
946 (insert (howm-completing-read-keyword)))
948 (defun howm-keyword-to-kill-ring (&optional filename-p)
950 (let ((title (howm-title-at-current-point filename-p)))
952 (howm-string-to-kill-ring title)
953 (error "No keyword."))))
955 (defun howm-title-at-current-point (&optional filename-p
956 title-regexp title-regexp-pos)
957 (let ((reg (or title-regexp howm-view-title-regexp))
958 (pos (or title-regexp-pos howm-view-title-regexp-pos)))
961 (cond ((and (not filename-p)
962 (re-search-backward reg nil t))
963 (match-string-no-properties pos))
965 (howm-abbreviate-file-name (buffer-file-name)))
968 (defun howm-string-to-kill-ring (str)
975 (defun howm-keyword-for-comefrom ()
977 (goto-char (point-min))
978 (let ((keyword-list nil))
979 (while (re-search-forward howm-keyword-regexp nil t)
981 (cons (match-string-no-properties howm-keyword-regexp-pos)
983 (reverse keyword-list))))
985 (defun howm-keyword-list ()
986 (let ((sep (format "[\n%s]" (or howm-keyword-list-alias-sep ""))))
987 (with-current-buffer (howm-keyword-buffer)
989 (split-string (buffer-substring (point-min) (point-max)) sep)))))
991 (defun howm-keyword-add (keyword-list)
992 (interactive "sKeyword: ")
993 (setq keyword-list (if (stringp keyword-list)
996 (with-current-buffer (howm-keyword-buffer)
998 (goto-char (point-max))
1000 (when (howm-keyword-new-p k)
1003 (when (buffer-file-name)
1004 (howm-basic-save-buffer)))))
1006 (defun howm-keyword-new-p (str)
1008 (let ((r (format "^%s$" (regexp-quote str)))
1009 (case-fold-search howm-keyword-case-fold-search))
1010 (goto-char (point-min))
1011 (not (re-search-forward r nil t)))))
1013 (defun howm-support-aliases-p ()
1014 howm-keyword-list-alias-sep)
1015 (defun howm-aliases ()
1016 (if (howm-support-aliases-p)
1019 (defun howm-read-aliases ()
1020 (with-current-buffer (howm-keyword-buffer)
1023 (goto-char (point-min))
1024 (while (search-forward howm-keyword-list-alias-sep nil t)
1025 (let* ((line (buffer-substring-no-properties (line-beginning-position)
1026 (line-end-position)))
1027 (keys (split-string line howm-keyword-list-alias-sep))
1028 (ks (if howm-keyword-case-fold-search
1029 (mapcar #'downcase keys)
1031 (setq ans (cons ks ans))
1035 (defun howm-expand-aliases-recursively (keyword aliases)
1036 (let ((keys (list keyword))
1038 (cl-labels ((expand (keys)
1039 (sort (cl-remove-duplicates
1040 (cl-mapcan (lambda (k)
1042 (lambda (a) (if (member k a)
1046 keys) :test #'string=)
1048 (while (not (equal prev keys))
1050 (setq keys (expand keys))))
1052 (cl-assert (equal (howm-expand-aliases-recursively "a"
1053 '(("d" "e" "f") ("a" "b" "c")))
1055 (cl-assert (equal (howm-expand-aliases-recursively "a"
1056 '(("d" "e" "b") ("a" "b" "c")))
1057 '("a" "b" "c" "d" "e")))
1059 (defun howm-keyword-aliases (keyword)
1060 "List of strings which are equivalent to KEYWORD.
1061 KEYWORD itself is always at the head of the returneded list.
1063 ;; Return the original keyword (not downcased) for backward compatibility.
1064 ;; I'm not sure whether this behavior is really needed.
1065 (let* ((key (if howm-keyword-case-fold-search
1068 (aliases (howm-aliases))
1069 (equiv (if howm-keyword-aliases-recursive
1070 (howm-expand-aliases-recursively key aliases)
1071 (cl-remove-duplicates
1073 (cl-remove-if-not (lambda (a) (member key a))
1077 (cons keyword (remove key equiv)))))
1079 (defun howm-keyword-search (keyword &optional create-p open-unique-p)
1080 (howm-message-time "key-search"
1081 (howm-set-command 'howm-keyword-search)
1082 (howm-with-normalizer
1083 (howm-keyword-search-subr keyword create-p open-unique-p))))
1085 (defun howm-keyword-search-subr (keyword create-p open-unique-p)
1086 (let* ((aliases (if (howm-support-aliases-p)
1087 (howm-keyword-aliases keyword)
1089 (menu-p (howm-menu-keyword-p keyword))
1090 (comefrom-regexp (if menu-p ;; clean me
1092 (howm-make-keyword-regexp2 aliases)))
1093 (trio (let ((howm-search-other-dir (if menu-p ;; clean me
1095 howm-search-other-dir))
1096 (*howm-view-force-case-fold-search*
1097 howm-keyword-case-fold-search)) ;; dirty!
1098 (howm-call-view-search-internal aliases t)))
1099 ;; code for <http://pc8.2ch.net/test/read.cgi/unix/1077881095/823>.
1100 ;; but this change is canceled; I'll try more fundamental fix. [2005-11-04]
1101 ;; (if open-unique-p
1102 ;; (let ((r (concat "^" (regexp-quote keyword) "$")))
1103 ;; (howm-call-view-search r nil))
1104 ;; (howm-call-view-search aliases t))))
1107 (items (cl-caddr trio))
1109 (found (if items t nil)) ;; want to forget items as soon as possible
1111 (let* ((howm-keyword-format
1112 (if menu-p ;; clean me
1113 (default-value 'howm-keyword-format)
1114 howm-keyword-format))
1115 (r (howm-normalize items aliases
1117 (setq items-pair (cdr r))
1119 (keyword-matched (member 'keyword matched))
1120 (keyword-matched-multi (member 'keyword-multi-hits matched))
1121 (file-matched (member 'file matched))
1122 (title (howm-make-keyword-string keyword)))
1123 ;; main processing (clean me!) [2003-12-01]
1126 ((and menu-p keyword-matched)
1127 (howm-keyword-search-open-menu keyword (car items-pair)
1128 keyword-matched-multi))
1130 ((and create-p (not keyword-matched))
1131 (howm-keyword-search-create title))
1132 ;; open if unique match
1133 ((and open-unique-p (howm-single-element-p items))
1134 (howm-keyword-search-open-unique items))
1136 (howm-call-view-summary name items-pair kw)
1137 (when (howm-normalize-oldp)
1138 ;; sorry for redundancy & inefficiency
1139 (howm-list-normalize-old aliases comefrom-regexp t))))
1142 (howm-write-history keyword))
1143 ;; return information
1144 `((menu-p . ,menu-p)
1146 (matched . ,matched)
1147 (keyword-matched . ,keyword-matched)
1148 (create-p . ,create-p))
1151 (defun howm-keyword-search-open-menu (keyword item-list multi-hits-p)
1152 "Open KEYWORD as menu."
1153 ;; dirty. peeking howm-view.el
1154 (let* ((item (car item-list))
1155 (fname (howm-view-item-filename item))
1156 (place (howm-view-item-place item)))
1157 (let ((howm-search-other-dir nil))
1158 (howm-menu-open fname place (howm-menu-name keyword))))
1160 (message "Warning: found two or more %s." keyword)))
1162 (defun howm-keyword-search-create (title)
1163 "create new memo <<< TITLE."
1164 (howm-create-file-with-title title)
1165 (message "New keyword."))
1167 (defun howm-keyword-search-open-unique (items)
1168 "Open unique match."
1169 (howm-view-open-item (car items)))
1171 ;; (defvar *howm-keyword-buffer* nil) ;; for internal use
1172 (defun howm-keyword-for-goto (&optional keyword-list)
1174 (let ((case-fold-search howm-keyword-case-fold-search))
1175 (sort (cl-mapcan (lambda (k)
1176 (goto-char (point-min))
1177 ;; when howm-check-word-break is non-nil,
1178 ;; checking word breaks is desired for efficiency.
1179 ;; it is not implemented yet.
1180 (if (search-forward k nil 'noerr)
1183 (or keyword-list (howm-keyword-list)))
1185 (> (length x) (length y)))))))
1187 (defun howm-keyword-add-current-buffer ()
1189 (goto-char (point-min))
1190 (let ((m (current-message))
1192 (while (re-search-forward howm-keyword-regexp nil t)
1193 (let ((key-str (if howm-keyword-list-alias-sep
1194 (mapconcat #'identity
1196 howm-keyword-list-alias-sep)
1197 (match-string-no-properties howm-keyword-regexp-pos))))
1198 (setq keyword-list (cons key-str keyword-list))))
1199 (howm-keyword-add keyword-list)
1201 (defun howm-keyword-add-items (items)
1202 (let ((files (mapcar #'howm-view-item-filename items)))
1206 (insert-file-contents f)
1207 (howm-set-configuration-for-file-name f)
1208 (howm-keyword-add-current-buffer))
1211 (defun howm-keyword-read ()
1213 (beg (line-beginning-position)))
1215 (skip-chars-backward " ")
1216 (while (re-search-backward howm-keyword-regexp beg t)
1217 (setq ks (cons (match-string-no-properties howm-keyword-regexp-pos) ks))
1218 (skip-chars-backward " "))
1222 ;;; howm-mode.el ends here