1 ;;; hactypes.el --- Default action types for Hyperbole.
3 ;; Copyright (C) 1991-1995, 2006 Free Software Foundation, Inc.
4 ;; Developed with support from Motorola Inc.
6 ;; Author: Bob Weiner, Brown U.
7 ;; Maintainer: Mats Lidell <matsl@contactor.se>
8 ;; Keywords: extensions, hypermedia
10 ;; This file is part of GNU Hyperbole.
12 ;; GNU Hyperbole is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 3, or (at
15 ;; your option) any later version.
17 ;; GNU Hyperbole is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
32 ;;; Other required Elisp libraries
35 (mapcar 'require '(hbut hpath hargs hact hmail))
38 ;;; Standard Hyperbole action types
41 (defact annot-bib (key)
42 "Follows internal ref KEY within an annotated bibliography, delimiters=[]."
43 (interactive "sReference key (no []): ")
44 (let ((opoint (point)))
46 (if (re-search-forward
47 (concat "^[*]*[ \t]*\\\[" (ebut:key-to-label key) "\\\]") nil t)
50 (delete-other-windows)
51 (split-window-vertically nil)
57 "Inserts completion at point into minibuffer or other window.
58 Unless at end of buffer or if completion has already been inserted, then
59 deletes completions window."
62 (progn (bury-buffer nil)
66 (defact eval-elisp (lisp-expr)
67 "Evaluates a Lisp expression LISP-EXPR."
68 (interactive "xLisp to eval: ")
71 (defact exec-kbd-macro (kbd-macro &optional repeat-count)
72 "Executes KBD-MACRO REPEAT-COUNT times.
73 KBD-MACRO may be a string of editor command characters, a function symbol or
74 nil to use the last defined keyboard macro.
75 Optional REPEAT-COUNT nil means execute once, zero means repeat until
79 (setq macro (intern-soft
81 "Unquoted macro name or nil for last one defined: "
85 (stringp (hypb:indirect-function sym)))))
87 (cond ((fboundp macro))
88 ((null last-kbd-macro)
90 "(exec-kbd-macro): Define a keyboard macro first."))
91 (t (fset 'zzz last-kbd-macro)
94 (let ((standard-output (get-buffer-create "*macro-def*")))
96 (progn (set-buffer standard-output)
97 (setq buffer-read-only nil)
99 (insert-kbd-macro macro)
100 (goto-char (point-min))
101 (setq macro (car (cdr (cdr (read (current-buffer)))))))
102 (kill-buffer standard-output))))
104 (setq repeat (hargs:read "Repeat count: "
108 (and (integerp repeat) (>= repeat 0)))))
110 (list macro repeat)))
113 (or (and kbd-macro (or (stringp kbd-macro)
114 (and (symbolp kbd-macro) (fboundp kbd-macro))))
115 (hypb:error "(exec-kbd-macro): Bad macro: %s" kbd-macro))
116 (or (null repeat-count) (and (integerp repeat-count) (<= 0 repeat-count))
117 (hypb:error "(exec-kbd-macro): Bad repeat count: %s" repeat-count)))
118 (execute-kbd-macro kbd-macro repeat-count))
120 ;;; Support next two actypes on systems which use the 'comint' shell package
121 ;;; rather than Emacs V18 shell.el.
123 (if (or hyperb:xemacs-p hyperb:emacs19-p)
125 (and (fboundp 'comint-send-input) (not (fboundp 'shell-send-input))
126 (fset 'shell-send-input 'comint-send-input))
127 (and (fboundp 'comint-kill-output) (not (fboundp 'kill-output-from-shell))
128 (fset 'kill-output-from-shell 'comint-kill-output))
129 (and (fboundp 'comint-show-output) (not (fboundp 'show-output-from-shell))
130 (fset 'show-output-from-shell 'comint-show-output))
132 (defact exec-shell-cmd (shell-cmd &optional internal-cmd kill-prev)
133 "Executes a SHELL-CMD string asynchronously.
134 Optional non-nil second argument INTERNAL-CMD means do not display the shell
135 command line executed. Optional non-nil third argument KILL-PREV means
136 kill last output to shell buffer before executing SHELL-CMD."
138 (let ((default (car defaults))
139 (default1 (nth 1 defaults))
140 (default2 (nth 2 defaults)))
141 (list (hargs:read "Shell cmd: "
143 (lambda (cmd) (not (string= cmd ""))))
144 default "Enter a shell command." 'string)
145 (y-or-n-p (format "Omit cmd from output (default = %s): "
147 (y-or-n-p (format "Kill prior cmd's output (default = %s): "
149 (let ((buf-name "*Hypb Shell*")
150 (owind (selected-window)))
153 (if (not (hpath:ange-ftp-p default-directory))
155 (concat "cd " default-directory "; " shell-cmd)))
156 (if (not (get-buffer buf-name))
158 ;; Ensure shell displays in other window unless in the
159 ;; OO-Browser, then use selected window.
162 (if (= (length (hypb:window-list)) 1)
163 (split-window-vertically))
165 (if (eq (minibuffer-window) (selected-window))
167 (shell) (rename-buffer buf-name)
168 (setq last-input-start (point-marker)
169 last-input-end (point-marker))
170 (if (fboundp 'comint-kill-output)
171 (setq comint-last-input-start last-input-start
172 comint-last-input-end last-input-end)
174 (or (equal (buffer-name (current-buffer)) buf-name)
175 (if (br-in-browser) (switch-to-buffer buf-name)
176 (pop-to-buffer buf-name)))
177 (goto-char (point-max))
178 (and kill-prev last-input-end
179 (not (equal last-input-start last-input-end))
180 (kill-output-from-shell))
183 (show-output-from-shell)
184 (or internal-cmd (scroll-down 1)))
185 (select-window owind))))
187 (defact exec-window-cmd (shell-cmd)
188 "Executes an external window-based SHELL-CMD string asynchronously."
190 (let ((default (car defaults)))
191 (list (hargs:read "Shell cmd: "
193 (lambda (cmd) (not (string= cmd ""))))
194 default "Enter a shell command." 'string))))
195 (let ((buf-name "*Hypb Shell*")
196 (cmd (if (hpath:ange-ftp-p default-directory)
197 (concat "(" shell-cmd ") &")
198 (concat "(cd " default-directory "; " shell-cmd ") &")))
199 (msg (format "Executing: %s" shell-cmd))
203 (save-window-excursion
204 (if (not (get-buffer buf-name))
205 (progn (save-excursion
206 (save-window-excursion
208 (setq shell-buf (current-buffer))))
210 ;; Wait for shell to startup before sending it input.
212 (set-buffer shell-buf)
213 (rename-buffer buf-name)
214 (setq last-input-start (point-marker)
215 last-input-end (point-marker))
216 (if (fboundp 'comint-kill-output)
217 (setq comint-last-input-start last-input-start
218 comint-last-input-end last-input-end)
220 (or (equal (buffer-name (current-buffer)) buf-name)
221 (set-buffer buf-name))
222 (goto-char (point-max))
227 (defact hyp-config (&optional out-buf)
228 "Inserts Hyperbole configuration info at end of optional OUT-BUF or current."
229 (hypb:configuration out-buf))
231 (defact hyp-request (&optional out-buf)
232 "Inserts Hyperbole mail list request help into optional OUT-BUF or current."
234 (and out-buf (set-buffer out-buf))
235 (goto-char (point-max))
236 (delete-blank-lines) (delete-blank-lines)
237 (insert "Use one of the following formats on your subject line:\n
238 Subject: Subscribe <joe@any.com> (Joe Williams).
239 Subject: Unsubscribe <joe@any.com>.
241 To change your address, first unsubscribe by sending an unsubscribe
242 request from your old address. Then subscribe by sending a subscribe
243 request from your new address.
245 Possible mail lists are:
246 hyperbole - discussion of Hyperbole
247 hyperbole-announce - Hyperbole announcements only\n")))
249 (defact hyp-source (buf-str-or-file)
250 "Displays a buffer or file from a line beginning with 'hbut:source-prefix'."
252 (list (prin1-to-string (get-buffer-create
253 (read-buffer "Buffer to link to: ")))))
254 (if (stringp buf-str-or-file)
255 (cond ((string-match "\\`#<buffer \"?\\([^ \n\"]+\\)\"?>" buf-str-or-file)
256 (pop-to-buffer (substring buf-str-or-file
257 (match-beginning 1) (match-end 1))))
258 (t (hpath:find-other-window buf-str-or-file)))
259 (hypb:error "(hyp-source): Non-string argument: %s" buf-str-or-file)))
261 (defact link-to-buffer-tmp (buffer)
262 "Displays a BUFFER in another window.
263 Link is generally only good for current Emacs session.
264 Use 'link-to-file' instead for a permanent link."
265 (interactive "bBuffer to link to: ")
266 (if (or (stringp buffer) (bufferp buffer))
267 (pop-to-buffer buffer)
268 (hypb:error "(link-to-buffer-tmp): Not a current buffer: %s" buffer)))
270 (defact link-to-directory (directory)
271 "Displays a DIRECTORY in Dired mode in another window."
272 (interactive "DDirectory to link to: ")
273 (hpath:find-other-window directory))
275 (defact link-to-ebut (key-file key)
276 "Performs action given by another button, specified by KEY-FILE and KEY."
278 (let (but-file but-lbl)
279 (while (cond ((setq but-file
281 "File of button to link to: " nil nil t))
282 (if (string= but-file "")
284 ((not (file-readable-p but-file))
285 (message "(link-to-ebut): You cannot read '%s'."
287 (beep) (sit-for 3))))
290 (find-file-noselect but-file)
291 (while (string= "" (setq but-lbl
293 "Button to link to: "
294 (ebut:alist but-file)
297 (ebut:label-to-key but-lbl)))))
299 (setq key-file (hpath:validate (hpath:substitute-value key-file))))
300 (let ((but (ebut:get key (find-file-noselect key-file))))
301 (if but (hbut:act but)
302 (hypb:error "(link-to-ebut): No button '%s' in '%s'." (ebut:key-to-label key)
305 (defact link-to-elisp-doc (func-symbol)
306 "Displays documentation for FUNC-SYMBOL."
307 (interactive "aFunction to display doc for: ")
308 (cond ((not (symbolp func-symbol))
309 (hypb:error "(link-to-elisp-doc): '%s' not a symbol."
311 ((not (fboundp func-symbol))
312 (hypb:error "(link-to-elisp-doc): '%s' not defined as a function."
314 ((not (documentation func-symbol))
315 (hypb:error "(link-to-elisp-doc): '%s' has no documentation."
317 ((describe-function func-symbol))))
319 (defact link-to-file (path &optional point)
320 "Displays a PATH in another window scrolled to optional POINT.
321 With POINT, buffer is displayed with POINT at the top of the window."
323 (let ((prev-reading-p hargs:reading-p))
325 (let* ((default (car defaults))
326 (hargs:reading-p 'file)
327 (path (read-file-name "Path to link to: " default default))
328 (path-buf (get-file-buffer path)))
331 (set-buffer path-buf)
332 (setq hargs:reading-p 'character)
334 (format "y = Display at present position (line %d); n = no position: "
335 (count-lines 1 (point))))
339 (setq hargs:reading-p prev-reading-p))))
340 (and (hpath:find-other-window path)
342 (progn (goto-char (min (point-max) point))
345 (defact link-to-file-line (path line-num)
346 "Displays a PATH in another window scrolled to LINE-NUM."
347 (interactive "fPath to link to: \nnDisplay at line number: ")
348 (and (setq path (smart-tags-file-path path))
349 (hpath:find-other-window path)
352 (goto-line line-num))))
354 (defact link-to-Info-node (node)
355 "Displays an Info NODE in another window.
356 NODE must be a string of the form `(file)nodename'."
357 (interactive "+IInfo (file)nodename to link to: ")
359 (if (and (stringp node) (string-match "^(\\([^\)]+\\))\\(.*\\)" node))
360 (let ((nodename (substring node (match-beginning 2) (match-end 2)))
361 (file (hpath:absolute-to
362 (substring node (match-beginning 1) (match-end 1))
363 (if (boundp 'Info-directory-list)
366 (if (and file (setq file (hpath:substitute-value file)))
367 (let ((wind (get-buffer-window "*info*")))
368 (if wind (select-window wind)
369 (pop-to-buffer (other-buffer)))
370 (info) (Info-goto-node (concat "(" file ")" nodename)))
371 (hypb:error "(link-to-Info-node): Bad node spec: '%s'" node)))))
373 (defact link-to-kcell (file cell-ref)
374 "Displays FILE with kcell given by CELL-REF at the top of the window.
375 See documentation for 'kcell:ref-to-id' for valid cell-ref formats.
377 If FILE is nil, the current buffer is used.
378 If CELL-REF is nil, the first cell in the view is shown."
379 (interactive "fKotl file to link to: \n+KKcell to link to: ")
381 (cond ((and (stringp cell-ref) (= ?| (aref cell-ref 0)))
382 ;; Activate view spec in current window.
383 (kotl-mode:goto-cell cell-ref))
385 (hpath:find-other-window file)
386 (pop-to-buffer (current-buffer) t))
388 (kotl-mode:goto-cell cell-ref)
389 (kotl-mode:beginning-of-buffer))
392 (defact link-to-mail (mail-msg-id &optional mail-file)
393 "Displays mail msg with MAIL-MSG-ID from optional MAIL-FILE in other window.
394 See documentation for the variable 'hmail:init-function' for information on
395 how to specify a mail reader to use."
396 (interactive "+MMail Msg: ")
397 (if (not (fboundp 'rmail:msg-to-p))
398 (hypb:error "(link-to-mail): Invoke mail reader before trying to follow a mail link.")
399 (if (and (listp mail-msg-id) (null mail-file))
400 (setq mail-file (car (cdr mail-msg-id))
401 mail-msg-id (car mail-msg-id)))
402 (let ((wind (selected-window))
403 (wconfig (current-window-configuration)))
405 (if (eq wind (selected-window))
406 (progn (split-window-vertically nil) (other-window 1)))
407 ;; Initialize user-specified mail reader if need be.
408 (if (and (symbolp hmail:init-function)
409 (fboundp hmail:init-function)
410 (listp (symbol-function hmail:init-function))
411 (eq 'autoload (car (symbol-function hmail:init-function))))
412 (funcall hmail:init-function))
413 (if (rmail:msg-to-p mail-msg-id mail-file)
415 ;; Couldn't find message, restore old window config, report error
416 (set-window-configuration wconfig)
417 (hypb:error "(link-to-mail): No msg '%s' in file \"%s\"."
418 mail-msg-id mail-file)))))
420 (defact link-to-regexp-match (regexp n source &optional buffer-p)
421 "Finds REGEXP's Nth occurrence in SOURCE and displays location at window top.
422 SOURCE is a pathname unless optional BUFFER-P is non-nil, then SOURCE must be
423 a buffer name or buffer.
424 Returns t if found, signals an error if not."
425 (interactive "sRegexp to match: \nnOccurrence number: \nfFile to search: ")
426 (let ((orig-src source))
429 (setq source (get-buffer source)))
430 ;; Source is a pathname.
431 (if (not (stringp source))
433 "(link-to-regexp-match): Source parameter is not a filename: '%s'"
435 (setq source (find-file-noselect (hpath:substitute-value source)))))
436 (if (not (bufferp source))
438 "(link-to-regexp-match): Invalid source parameter: '%s'" orig-src)
439 (switch-to-buffer-other-window source)
441 (goto-char (point-min))
442 (if (re-search-forward regexp nil t n)
443 (progn (beginning-of-line) (recenter 0) t)
445 "(link-to-regexp-match): Pattern not found: '%s'" regexp)))))
447 (defact link-to-rfc (rfc-num)
448 "Retrieves and displays an Internet rfc given by RFC-NUM.
449 RFC-NUM may be a string or an integer. Requires ange-ftp or efs for
451 (interactive "nRFC number to retrieve: ")
452 (if (or (stringp rfc-num) (integerp rfc-num))
453 (hpath:find-other-window (hpath:rfc rfc-num))))
455 (defact link-to-string-match (string n source &optional buffer-p)
456 "Finds STRING's Nth occurrence in SOURCE and displays location at window top.
457 SOURCE is a pathname unless optional BUFFER-P is non-nil, then SOURCE must be
458 a buffer name or buffer.
459 Returns t if found, nil if not."
460 (interactive "sString to match: \nnOccurrence number: \nfFile to search: ")
461 (funcall (actype:action 'link-to-regexp-match)
462 (regexp-quote string) n source buffer-p))
464 (defact man-show (topic)
465 "Displays man page on TOPIC, which may be of the form <command>(<section>)."
466 (interactive "sManual topic: ")
467 (manual-entry topic))
469 (defact rfc-toc (&optional buf-name opoint)
470 "Computes and displays summary of an Internet rfc in BUF-NAME.
471 Assumes point has already been moved to start of region to summarize.
472 Optional OPOINT is point to return to in BUF-NAME after displaying summary."
475 (cond ((get-buffer buf-name)
476 (switch-to-buffer buf-name))
477 ((let ((buf (get-file-buffer buf-name)))
479 (progn (switch-to-buffer (setq buf-name buf))
481 (t (if opoint (goto-char opoint))
482 (hypb:error "(rfc-toc): Invalid buffer name: %s" buf-name))))
483 (let ((sect-regexp "^[ \t]*[1-9][0-9]*\\.[0-9.]*[ \t]+[^ \t\n]"))
485 (set-buffer "*Occur*")
486 (rename-buffer (format "*%s toc*" buf-name))
487 (re-search-forward "^[ ]*[0-9]+:" nil t)
489 (delete-region (point-min) (point))
490 (insert "Contents of " (buffer-name occur-buffer) ":\n")
491 (set-buffer-modified-p nil)
492 (set-buffer buf-name)
493 (if opoint (goto-char opoint))))
495 (defact text-toc (section)
496 "Jumps to the text file SECTION referenced by a table of contents entry at point."
497 (interactive "sGo to section named: ")
498 (if (stringp section)
500 (actypes::link-to-regexp-match
501 (concat "^\\*+[ \t]*" (regexp-quote section))
502 1 (current-buffer) t)
503 (while (and (= (forward-line -1) 0)
504 (looking-at "[ \t]*[-=][-=]")))
510 ;;; hactypes.el ends here