├── META.in ├── Makefile ├── LICENSE ├── README.md └── tophide.ml /META.in: -------------------------------------------------------------------------------- 1 | # specifications for "tophide": 2 | description = "Hiding toplevel _values" 3 | requires = "" 4 | archive(byte,toploop) = "tophide.cmo" 5 | archive(byte,create_toploop) = "tophide.cmo" 6 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Copyright 2008 Martin Jambon. All rights reserved. 2 | # This file is distributed under the terms stated in file LICENSE. 3 | 4 | VERSION = 1.0.4 5 | export VERSION 6 | 7 | .PHONY: all install clean 8 | 9 | all: META tophide.cmo 10 | tophide.cmo: tophide.ml 11 | ocamlc -I +compiler-libs -c tophide.ml 12 | 13 | install: 14 | ocamlfind install tophide META tophide.cmi tophide.cmo 15 | 16 | uninstall: 17 | ocamlfind remove tophide 18 | 19 | clean: 20 | rm -f *.cmo *.cmi 21 | 22 | META: META.in Makefile 23 | cat META.in > META 24 | echo 'version = "$(VERSION)"' >> META 25 | 26 | archive: 27 | rm -rf /tmp/tophide /tmp/tophide-$(VERSION) && \ 28 | cp -r . /tmp/tophide && \ 29 | cd /tmp/tophide && \ 30 | $(MAKE) clean && \ 31 | $(MAKE) META && \ 32 | rm -f *~ tophide*.tar* && \ 33 | cd /tmp && cp -r tophide tophide-$(VERSION) && \ 34 | tar czf tophide.tar.gz tophide && \ 35 | tar cjf tophide.tar.bz2 tophide && \ 36 | tar czf tophide-$(VERSION).tar.gz tophide-$(VERSION) && \ 37 | tar cjf tophide-$(VERSION).tar.bz2 tophide-$(VERSION) 38 | mv /tmp/tophide.tar.gz /tmp/tophide.tar.bz2 .. 39 | mv /tmp/tophide-$(VERSION).tar.gz /tmp/tophide-$(VERSION).tar.bz2 .. 40 | cp ../tophide.tar.gz ../tophide.tar.bz2 $$WWW/ 41 | cp ../tophide-$(VERSION).tar.gz ../tophide-$(VERSION).tar.bz2 $$WWW/ 42 | cp README $$WWW/tophide-readme.txt 43 | cp LICENSE $$WWW/tophide-license.txt 44 | echo 'let tophide_version = "$(VERSION)"' > $$WWW/tophide-version.ml 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2008 Martin Jambon 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions 6 | are met: 7 | 1. Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | 2. Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | 3. The name of the author may not be used to endorse or promote products 13 | derived from this software without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Tophide 2 | ======= 3 | 4 | Tophide is meant to be loaded in an ocaml toplevel. 5 | Definitions of values whose name starts with an underscore do not result 6 | in the typical 'val NAME : TYPE' output but are simply not shown. 7 | 8 | This behavior is particularly useful for hiding preprocessor-generated 9 | values that are not meant to be reviewed by the user/programmer. 10 | 11 | 12 | Installation 13 | ------------ 14 | 15 | make 16 | make install # requires ocamlfind 17 | 18 | Uninstallation can be performed with 19 | 20 | make uninstall # requires ocamlfind 21 | 22 | 23 | Usage 24 | ----- 25 | 26 | There are two modes: "hide" and "show". 27 | 28 | The "hide" mode is automatically triggered when tophide is loaded. 29 | It is the mode that hides the values whose name starts with "_". 30 | 31 | The "show" mode is the regular mode. This mode can be entered using the 32 | "#hide" directive. It restores the output functions as they were just before 33 | tophide was loaded. 34 | The "#hide" directive allows to switch back to the "hide" mode. 35 | 36 | 37 | Example 38 | ------- 39 | 40 | The best way of using this is with findlib. 41 | Findlib provides the "#require" directive for finding and loading a given 42 | package. 43 | 44 | ``` 45 | #use "topfind";; (* just once *) 46 | - : unit = () 47 | Findlib has been successfully loaded. Additional directives: 48 | #require "package";; to load a package 49 | #list;; to list the available packages 50 | #camlp4o;; to load camlp4 (standard syntax) 51 | #camlp4r;; to load camlp4 (revised syntax) 52 | #predicates "p,q,...";; to set these predicates 53 | Topfind.reset();; to force that packages will be reloaded 54 | #thread;; to enable threads 55 | 56 | - : unit = () 57 | # #require "tophide";; (* loads tophide *) 58 | /home/martin/godi/lib/ocaml/site-lib/tophide: added to search path 59 | /home/martin/godi/lib/ocaml/site-lib/tophide/tophide.cmo: loaded 60 | ``` 61 | 62 | 63 | 64 | It is recommended to put these directives into a .ocamlinit file. 65 | Then they would be executed at the beginning of each ocaml session. 66 | (see chapter "The toplevel system (ocaml)" of the Objective Caml reference 67 | manual) 68 | 69 | Let's see what we get: 70 | 71 | ```ocaml 72 | # let a = 1;; 73 | val a : int = 1 74 | # let _a = 2;; (* good, no output for _a! *) 75 | # 76 | ``` 77 | 78 | Now you can load your favorite camlp4 extensions. 79 | 80 | 81 | META files 82 | ---------- 83 | 84 | Packages depending on tophide should need it only in the toplevel loop. 85 | This is expressed as follows in the `META` file for ocamlfind: 86 | 87 | requires(toploop) += "tophide" 88 | 89 | This comes in addition to other `requires` directives, e.g.: 90 | 91 | requires = "unix pcre" 92 | requires(toploop) += "tophide" 93 | -------------------------------------------------------------------------------- /tophide.ml: -------------------------------------------------------------------------------- 1 | (* $Id$ *) 2 | 3 | (* 4 | Copyright 2008 Martin Jambon. All rights reserved. 5 | This file is distributed under the terms stated in file LICENSE. 6 | *) 7 | 8 | open Toploop 9 | open Outcometree 10 | 11 | type env = { 12 | print_out_class_type : 13 | Format_doc.formatter -> out_class_type -> unit; 14 | print_out_module_type : 15 | Format_doc.formatter -> out_module_type -> unit; 16 | print_out_phrase : 17 | Format.formatter -> out_phrase -> unit; 18 | print_out_sig_item : 19 | Format_doc.formatter -> out_sig_item -> unit; 20 | print_out_signature : 21 | Format_doc.formatter -> out_sig_item list -> unit; 22 | print_out_type : 23 | Format_doc.formatter -> out_type -> unit; 24 | print_out_value : 25 | Format.formatter -> out_value -> unit; 26 | } 27 | 28 | let save_env () = { 29 | print_out_class_type = !print_out_class_type; 30 | print_out_module_type = !print_out_module_type; 31 | print_out_phrase = !print_out_phrase; 32 | print_out_sig_item = !print_out_sig_item; 33 | print_out_signature = !print_out_signature; 34 | print_out_type = !print_out_type; 35 | print_out_value = !print_out_value; 36 | } 37 | 38 | let load_env env = 39 | print_out_class_type := env.print_out_class_type; 40 | print_out_module_type := env.print_out_module_type; 41 | print_out_phrase := env.print_out_phrase; 42 | print_out_sig_item := env.print_out_sig_item; 43 | print_out_signature := env.print_out_signature; 44 | print_out_type := env.print_out_type; 45 | print_out_value := env.print_out_value 46 | 47 | 48 | let default_env = save_env () 49 | 50 | let filter_sig_item = function 51 | Osig_class (_, _, _, _, _) 52 | | Osig_class_type (_, _, _, _, _) 53 | | Osig_typext _ 54 | | Osig_modtype _ 55 | | Osig_module _ 56 | | Osig_ellipsis 57 | | Osig_type _ as x -> Some x 58 | | Osig_value {oval_name = name; _} as x -> 59 | if name <> "" && name.[0] = '_' then None 60 | else Some x 61 | 62 | 63 | let rec select f = function 64 | [] -> [] 65 | | hd :: tl -> 66 | match f hd with 67 | None -> select f tl 68 | | Some x -> x :: select f tl 69 | 70 | 71 | let special_print_out_phrase fmt x0 = 72 | let x = 73 | match x0 with 74 | Ophr_signature l0 -> 75 | let l = 76 | select ( 77 | fun (si0, ov) -> 78 | match filter_sig_item si0 with 79 | None -> None 80 | | Some si -> Some (si, ov) 81 | ) l0 82 | in 83 | Ophr_signature l 84 | 85 | | Ophr_eval _ 86 | | Ophr_exception _ -> x0 87 | in 88 | default_env.print_out_phrase fmt x 89 | 90 | 91 | let special_env = 92 | { default_env with print_out_phrase = special_print_out_phrase } 93 | 94 | 95 | let hide () = load_env special_env 96 | let show () = load_env default_env 97 | 98 | let _ = 99 | (* Add "#hide" directive: *) 100 | add_directive "hide" (Directive_none hide) { section = ""; doc = "" }; 101 | 102 | (* Add "#show" directive: *) 103 | add_directive "show" (Directive_none show) { section = ""; doc = "" }; 104 | 105 | (* Enter "hide" mode upon loading *) 106 | hide () 107 | --------------------------------------------------------------------------------